public function run()
{
// register a shutdown handler for controlled shutdown
register_shutdown_function(array(&$this, 'shutdown'));
// we need the autloader again
require SERVER_AUTOLOADER;
// create a reference to the application server instance
$applicationServer = $this->applicationServer;
// initialize the event loop and the socket server
$loop = \React\EventLoop\Factory::create();
$socket = new \React\Socket\Server($loop);
// wait for connections
$socket->on('connection', function ($conn) use($applicationServer) {
// write the appserver.io logo to the console
$conn->write(Telnet::$logo);
$conn->write("\$ ");
// wait for user input => usually a command
$conn->on('data', function ($data) use($conn, $applicationServer) {
try {
// extract command name and parameters
list($commandName, ) = explode(' ', $data);
$params = explode(' ', trim(substr($data, strlen($commandName))));
// initialize and execute the command
$command = CommandFactory::factory(trim($commandName), array($conn, $applicationServer));
$command->execute($params);
} catch (\ReflectionException $re) {
$conn->write("Unknown command {$commandName}");
} catch (\Exception $e) {
$conn->write($e->__toString());
}
// write the command prompt
$conn->write("\$ ");
});
});
// listen to the management socket
$socket->listen($this->getPort(), $this->getAddress());
// start the event loop and the socket server, but disable warnings as some React warnings cannot (or won't) be dealt with.
// Specifically the warning if a client disconnects unexpectedly or does not even connect to begin with ("Interrupted system call") is unevitable
// @see https://github.com/reactphp/react/pull/297
// @see https://github.com/reactphp/react/issues/296
// @see http://php.net/manual/de/function.stream-select.php
$currentReportingLevel = error_reporting();
error_reporting(E_ALL ^ E_WARNING);
$loop->run();
error_reporting($currentReportingLevel);
}