1 | <?php |
||
21 | class ProcessTitle |
||
|
|||
22 | { |
||
23 | /** |
||
24 | * @var bool |
||
25 | */ |
||
26 | private static $supported; |
||
27 | |||
28 | /** |
||
29 | * @var string[] |
||
30 | */ |
||
31 | private static $processTitles = array(); |
||
32 | |||
33 | /** |
||
34 | * Returns whether process titles can be set. |
||
35 | * |
||
36 | * @return bool Returns `true` if process titles can be set and `false` |
||
37 | * otherwise. |
||
38 | */ |
||
39 | public static function isSupported() |
||
40 | { |
||
41 | if (null === self::$supported) { |
||
42 | self::$supported = function_exists('cli_set_process_title') || function_exists('setproctitle'); |
||
43 | } |
||
44 | |||
45 | return self::$supported; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Sets the title of the PHP process. |
||
50 | * |
||
51 | * @param string $processTitle The process title. |
||
52 | */ |
||
53 | public static function setProcessTitle($processTitle) |
||
54 | { |
||
55 | self::$processTitles[] = $processTitle; |
||
56 | |||
57 | self::changeProcessTitleTo($processTitle); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Resets the title of the PHP process to the previous value. |
||
62 | */ |
||
63 | public static function resetProcessTitle() |
||
64 | { |
||
65 | $processTitle = self::$processTitles ? array_pop(self::$processTitles) : null; |
||
66 | |||
67 | self::changeProcessTitleTo($processTitle); |
||
68 | } |
||
69 | |||
70 | private static function changeProcessTitleTo($processTitle) |
||
71 | { |
||
72 | if (function_exists('cli_set_process_title')) { |
||
73 | cli_set_process_title($processTitle); |
||
74 | } elseif (function_exists('setproctitle')) { |
||
75 | setproctitle($processTitle); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | private function __construct() |
||
82 | } |
||
83 |