Completed
Pull Request — master (#1105)
by Tim
42:55
created

Ssh::shutdown()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
ccs 0
cts 9
cp 0
crap 20
1
<?php
2
3
/**
4
 * AppserverIo\Appserver\Core\Consoles\SshConsole
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/appserver
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Appserver\Core\Consoles;
22
23
use AppserverIo\Psr\Cli\ConsoleInterface;
24
25
/**
26
 * A SSH based management console implementation using a React PHP socket server.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/appserver
32
 * @link      http://www.appserver.io
33
 */
34
class Ssh extends \Thread implements ConsoleInterface
35
{
36
37
    /**
38
     * appserver.io written in ASCI art.
39
     *
40
     * @var string
41
     */
42
    protected static $logo = '                                                    _
43
  ____ _____  ____  ________  ______   _____  _____(_)___
44
 / __ `/ __ \/ __ \/ ___/ _ \/ ___/ | / / _ \/ ___/ / __ \
45
/ /_/ / /_/ / /_/ (__  )  __/ /   | |/ /  __/ /  / / /_/ /
46
\__,_/ .___/ .___/____/\___/_/    |___/\___/_(_)/_/\____/
47
    /_/   /_/
48
49
';
50
51
    /**
52
     * Initialize and start the management console.
53
     *
54
     * @param \AppserverIo\Psr\ApplicationServer\ApplicationServerInterface $applicationServer The reference to the server
55
     *
56
     * @return void
57
     */
58
    public function __construct($applicationServer)
59
    {
60
        $this->applicationServer = $applicationServer;
61
        $this->start(PTHREADS_INHERIT_ALL);
62
    }
63
64
    /**
65
     * Return's the service name.
66
     *
67
     * @return string The service name
68
     */
69
    public function getName()
70
    {
71
        return 'console';
72
    }
73
74
    /**
75
     * Shutdown handler that checks for fatal/user errors.
76
     *
77
     * @return void
78
     */
79
    public function shutdown()
80
    {
81
        // check if there was a fatal error caused shutdown
82
        if ($lastError = error_get_last()) {
83
            // initialize type + message
84
            $type = 0;
85
            $message = '';
86
            // extract the last error values
87
            extract($lastError);
88
            // query whether we've a fatal/user error
89
            if ($type === E_ERROR || $type === E_USER_ERROR) {
90
                echo $message . PHP_EOL;
91
            }
92
        }
93
    }
94
95
    /**
96
     * Stop the console and closes all connections.
97
     *
98
     * @return void
99
     */
100
    public function stop()
101
    {
102
        $this->kill();
103
    }
104
105
    /**
106
     * The thread's run() method that runs asynchronously.
107
     *
108
     * @return void
109
     * @link http://www.php.net/manual/en/thread.run.php
110
     */
111
    public function run()
112
    {
113
114
        // register a shutdown handler for controlled shutdown
115
        register_shutdown_function(array(&$this, 'shutdown'));
116
117
        // we need the autloader again
118
        require SERVER_AUTOLOADER;
119
120
        require_once 'vendor/fpoirotte/pssht/src/CLI.php';
121
122
        main();
123
    }
124
}
125