Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc J. Schmidt committed Feb 25, 2014
0 parents commit 1e63ad1
Show file tree
Hide file tree
Showing 10 changed files with 613 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Bridges/BridgeInterface.php
@@ -0,0 +1,9 @@
<?php

namespace PHPPM\Bridges;

interface BridgeInterface
{
public function bootstrap();
public function onRequest(\React\Http\Request $request, \React\Http\Response $response);
}
36 changes: 36 additions & 0 deletions Bridges/Symfony.php
@@ -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());
}
}
67 changes: 67 additions & 0 deletions Client.php
@@ -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));
});
}

}
41 changes: 41 additions & 0 deletions Commands/StartCommand.php
@@ -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();
}

}
34 changes: 34 additions & 0 deletions Commands/StatusCommand.php
@@ -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);
});
}

}

0 comments on commit 1e63ad1

Please sign in to comment.