ProcessTitle::setProcessTitle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Console\Util;
13
14
/**
15
 * Sets and resets the PHP process title.
16
 *
17
 * @since  1.0
18
 *
19
 * @author Bernhard Schussek <[email protected]>
20
 */
21
class ProcessTitle
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
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()
80
    {
81
    }
82
}
83