Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marc J. Schmidt
committed
Feb 25, 2014
0 parents
commit 1e63ad1
Showing
10 changed files
with
613 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace PHPPM\Bridges; | ||
|
||
interface BridgeInterface | ||
{ | ||
public function bootstrap(); | ||
public function onRequest(\React\Http\Request $request, \React\Http\Response $response); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace PHPPM\Bridges; | ||
|
||
use PHPPM\Bridges\BridgeInterface; | ||
|
||
class Symfony implements BridgeInterface | ||
{ | ||
|
||
/** | ||
* @var \AppKernel | ||
*/ | ||
protected $kernel; | ||
|
||
public function bootstrap() | ||
{ | ||
require_once './vendor/autoload.php'; | ||
require_once './app/AppKernel.php'; | ||
$this->kernel = new \AppKernel('prod', false); | ||
$this->kernel->loadClassCache(); | ||
} | ||
|
||
public function onRequest(\React\Http\Request $request, \React\Http\Response $response) | ||
{ | ||
$syRequest = new \Symfony\Component\HttpFoundation\Request(); | ||
$syRequest->headers->replace($request->getHeaders()); | ||
$syRequest->setMethod($request->getMethod()); | ||
$syRequest->server->set('REQUEST_URI', $request->getPath()); | ||
$syRequest->server->set('SERVER_NAME', explode(':', $request->getHeaders()['Host'])[0]); | ||
|
||
$syResponse = $this->kernel->handle($syRequest); | ||
$headers = array_map('current', $syResponse->headers->all()); | ||
$response->writeHead($syResponse->getStatusCode(), $headers); | ||
$response->end($syResponse->getContent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace PHPPM; | ||
|
||
class Client | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
protected $controllerPort = 5100; | ||
|
||
/** | ||
* @var \React\EventLoop\ExtEventLoop|\React\EventLoop\LibEventLoop|\React\EventLoop\LibEvLoop|\React\EventLoop\StreamSelectLoop | ||
*/ | ||
protected $loop; | ||
|
||
/** | ||
* @var \React\Socket\Connection | ||
*/ | ||
protected $connection; | ||
|
||
function __construct($controllerPort = 8080) | ||
{ | ||
$this->controllerPort = $controllerPort; | ||
$this->loop = \React\EventLoop\Factory::create(); | ||
} | ||
|
||
/** | ||
* @return \React\Socket\Connection | ||
*/ | ||
protected function getConnection() | ||
{ | ||
if ($this->connection) { | ||
$this->connection->close(); | ||
unset($this->connection); | ||
} | ||
$client = stream_socket_client('tcp://127.0.0.1:5500'); | ||
$this->connection = new \React\Socket\Connection($client, $this->loop); | ||
return $this->connection; | ||
} | ||
|
||
protected function request($command, $options, $callback) | ||
{ | ||
$data['cmd'] = $command; | ||
$data['options'] = $options; | ||
$connection = $this->getConnection(); | ||
|
||
$result = ''; | ||
$connection->on('data', function($data) use ($result) { | ||
$result .= $data; | ||
}); | ||
|
||
$connection->on('close', function() use ($callback, $result) { | ||
$callback($result); | ||
}); | ||
|
||
$connection->write(json_encode($data)); | ||
} | ||
|
||
public function getStatus(callable $callback) | ||
{ | ||
$this->request('status', [], function($result) use ($callback) { | ||
$callback(json_decode($result, true)); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace PHPPM\Commands; | ||
|
||
use PHPPM\ProcessManager; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class StartCommand extends Command | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
parent::configure(); | ||
|
||
$this | ||
->setName('start') | ||
->addArgument('working-directory', null, 'working directory', './') | ||
->addOption('bridge', null, InputOption::VALUE_REQUIRED) | ||
->setDescription('Starts the server') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
if ($workingDir = $input->getArgument('working-directory')) { | ||
chdir($workingDir); | ||
} | ||
|
||
$bridge = $input->getOption('bridge'); | ||
|
||
$handler = new ProcessManager(); | ||
$handler->setBridge($bridge); | ||
$handler->run(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace PHPPM\Commands; | ||
|
||
use PHPPM\Client; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class StatusCommand extends Command | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
parent::configure(); | ||
|
||
$this | ||
->setName('status') | ||
->addArgument('working-directory', null, 'working directory', './') | ||
->setDescription('Status of all processes') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$handler = new Client(); | ||
$handler->getStatus(function($status) { | ||
var_dump($status); | ||
}); | ||
} | ||
|
||
} |
Oops, something went wrong.