-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmergency.php
More file actions
74 lines (65 loc) · 2.44 KB
/
Copy pathEmergency.php
File metadata and controls
74 lines (65 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
/*
* This file is part of ezkoding
*
* (c) 2025 Oliver Glowa, coding.glowa.com
*
* This source file is subject to the Apache-2.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace ollily\Tools;
/**
* Utility class to stop an application.
*/
class Emergency
{
/** Default message */
public const string MSG_DEFAULT = 'Undefined reason to stop now!';
/** Default code */
public const int ERR_CODE_DEFAULT = 1;
/** Maximum code which is allowed. */
private const int ERR_CODE_MAX = 254;
/**
* Immediately stopping the application caused by an exception. As a hmomage to "Knight Rider".
*
* @param \Throwable $throwable The exception which is thrown to end the application
* @param bool $unitTest TRUE=don't call exit(), it's an unit test, FALSE= Standard termination (Default: FALSE)
*
* @return int The error code for ending the application
*
* @SuppressWarnings("PHPMD.ExitExpression")
*/
public static function exceptionStop(\Throwable $throwable, bool $unitTest = false): int
{
$errMsg = sprintf('\%s - %s', $throwable::class, $throwable->getMessage());
/** @psalm-suppress PossiblyInvalidArgument */
return static::breakSystem($throwable->getCode(), $errMsg, $unitTest);
}
/**
* Immediately stopping the application. As a hmomage to "Knight Rider"'s "Emergency Break System".
*
* @param int $errorCode The error code for ending the application
* @param string $errorMessage The error message for ending the application
* @param bool $unitTest TRUE=don't call exit(), it's an unit test, FALSE= Standard termination (Default: FALSE)
*
* @return int The error code for ending the application
*
* @SuppressWarnings("PHPMD.ExitExpression")
*/
public static function breakSystem(int $errorCode = self::ERR_CODE_DEFAULT, string $errorMessage = '', bool $unitTest = false): int
{
if ($errorCode < self::ERR_CODE_DEFAULT || $errorCode > self::ERR_CODE_MAX) {
$errorCode = self::ERR_CODE_DEFAULT;
}
if (empty($errorMessage)) {
$errorMessage = self::MSG_DEFAULT;
}
echo sprintf("\nEMERGENCY: %s [%s]\n", $errorMessage, $errorCode);
if (!$unitTest) {
die($errorCode); // NOSONAR:php:S1799
} else {
return $errorCode;
}
}
}