Skip to content

Commit

Permalink
Merge pull request #12 from PHPFastCGI/encapsulation
Browse files Browse the repository at this point in the history
Switched from protected to private and reordered some methods to improve readability
  • Loading branch information
AndrewCarterUK committed Aug 26, 2015
2 parents 071920b + fd82606 commit 57f337a
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 213 deletions.
2 changes: 1 addition & 1 deletion src/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ApplicationFactory implements ApplicationFactoryInterface
/**
* @var DaemonFactoryInterface
*/
protected $daemonFactory;
private $daemonFactory;

/**
* Constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/CallbackWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CallbackWrapper implements KernelInterface
/**
* @var callable
*/
protected $callback;
private $callback;

/**
* Constructor.
Expand Down
4 changes: 2 additions & 2 deletions src/Command/DaemonRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class DaemonRunCommand extends Command
/**
* @var DaemonFactoryInterface
*/
protected $daemonFactory;
private $daemonFactory;

/**
* @var KernelInterface|callable
*/
protected $kernel;
private $kernel;

/**
* Constructor.
Expand Down
4 changes: 2 additions & 2 deletions src/Connection/StreamSocketConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class StreamSocketConnection implements ConnectionInterface
/**
* @var resource
*/
protected $socket;
private $socket;

/**
* @var bool
*/
protected $closed;
private $closed;

/**
* Constructor.
Expand Down
12 changes: 6 additions & 6 deletions src/Connection/StreamSocketConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ class StreamSocketConnectionPool implements ConnectionPoolInterface
/**
* @var resource
*/
protected $serverSocket;
private $serverSocket;

/**
* @var resource[]
*/
protected $clientSockets;
private $clientSockets;

/**
* @var Connection[]
*/
protected $connections;
private $connections;

/**
* @var ConnectionHandlerInterface[]
*/
protected $connectionHandlers;
private $connectionHandlers;

/**
* Constructor.
Expand Down Expand Up @@ -110,7 +110,7 @@ public function shutdown()
*
* @param ConnectionHandlerFactoryInterface $connectionHandlerFactory The factory used to create connection handlers
*/
protected function acceptConnection(ConnectionHandlerFactoryInterface $connectionHandlerFactory)
private function acceptConnection(ConnectionHandlerFactoryInterface $connectionHandlerFactory)
{
$clientSocket = @stream_socket_accept($this->serverSocket);

Expand All @@ -131,7 +131,7 @@ protected function acceptConnection(ConnectionHandlerFactoryInterface $connectio
/**
* Remove connections.
*/
protected function removeConnections()
private function removeConnections()
{
foreach ($this->connections as $id => $connection) {
if ($connection->isClosed()) {
Expand Down
184 changes: 92 additions & 92 deletions src/ConnectionHandler/ConnectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ class ConnectionHandler implements ConnectionHandlerInterface, LoggerAwareInterf
/**
* @var bool
*/
protected $shutdown;
private $shutdown;

/**
* @var KernelInterface
*/
protected $kernel;
private $kernel;

/**
* @var ConnectionInterface
*/
protected $connection;
private $connection;

/**
* @var array
*/
protected $requests;
private $requests;

/**
* @var string
*/
protected $buffer;
private $buffer;

/**
* @var int
*/
protected $bufferLength;
private $bufferLength;

/**
* Constructor.
Expand Down Expand Up @@ -127,7 +127,7 @@ public function close()
*
* @return array|null The record that has been read
*/
protected function readRecord()
private function readRecord()
{
// Not enough data to read header
if ($this->bufferLength < 8) {
Expand Down Expand Up @@ -156,91 +156,14 @@ protected function readRecord()
return $record;
}

/**
* Write a record to the connection.
*
* @param int $requestId The request id to write to
* @param int $type The type of record
* @param string $content The content of the record
*/
protected function writeRecord($requestId, $type, $content = null)
{
$contentLength = null === $content ? 0 : strlen($content);

$headerData = pack('CCnnxx', DaemonInterface::FCGI_VERSION_1, $type, $requestId, $contentLength);

$this->connection->write($headerData);

if (null !== $content) {
$this->connection->write($content);
}
}

/**
* Write a response to the connection as FCGI_STDOUT records.
*
* @param int $requestId The request id to write to
* @param string $headerData The header data to write (including terminating CRLFCRLF)
* @param StreamInterface $stream The stream to write
*/
protected function writeResponse($requestId, $headerData, StreamInterface $stream)
{
$data = $headerData;
$eof = false;

$stream->rewind();

do {
$dataLength = strlen($data);

if ($dataLength < 65535 && !$eof && !($eof = $stream->eof())) {
$readLength = 65535 - $dataLength;
$data .= $stream->read($readLength);
$dataLength = strlen($data);
}

$writeSize = min($dataLength, 65535);
$writeData = substr($data, 0, $writeSize);
$data = substr($data, $writeSize);

$this->writeRecord($requestId, DaemonInterface::FCGI_STDOUT, $writeData);
} while ($writeSize === 65535);

$this->writeRecord($requestId, DaemonInterface::FCGI_STDOUT);
}

/**
* End the request by writing an FCGI_END_REQUEST record and then removing
* the request from memory and closing the connection if necessary.
*
* @param int $requestId The request id to end
* @param int $appStatus The application status to declare
* @param int $protocolStatus The protocol status to declare
*/
protected function endRequest($requestId, $appStatus = 0, $protocolStatus = DaemonInterface::FCGI_REQUEST_COMPLETE)
{
$content = pack('NCx3', $appStatus, $protocolStatus);
$this->writeRecord($requestId, DaemonInterface::FCGI_END_REQUEST, $content);

$keepAlive = $this->requests[$requestId]['keepAlive'];

fclose($this->requests[$requestId]['stdin']);

unset($this->requests[$requestId]);

if (!$keepAlive) {
$this->close();
}
}

/**
* Process a record.
*
* @param array $record The record that has been read
*
* @throws ProtocolException If the client sends an unexpected record.
*/
protected function processRecord(array $record)
private function processRecord(array $record)
{
$requestId = $record['requestId'];

Expand Down Expand Up @@ -276,7 +199,7 @@ protected function processRecord(array $record)
*
* @throws ProtocolException If the FCGI_BEGIN_REQUEST record is unexpected
*/
protected function processBeginRequestRecord($requestId, $contentData)
private function processBeginRequestRecord($requestId, $contentData)
{
if (isset($this->requests[$requestId])) {
throw new ProtocolException('Unexpected FCGI_BEGIN_REQUEST record');
Expand Down Expand Up @@ -313,7 +236,7 @@ protected function processBeginRequestRecord($requestId, $contentData)
*
* @throws ProtocolException If the FCGI_PARAMS record is unexpected
*/
protected function processParamsRecord($requestId, $contentData)
private function processParamsRecord($requestId, $contentData)
{
if (!isset($this->requests[$requestId])) {
throw new ProtocolException('Unexpected FCGI_PARAMS record');
Expand Down Expand Up @@ -364,7 +287,7 @@ protected function processParamsRecord($requestId, $contentData)
*
* @throws ProtocolException If the FCGI_STDIN record is unexpected
*/
protected function processStdinRecord($requestId, $contentData)
private function processStdinRecord($requestId, $contentData)
{
if (!isset($this->requests[$requestId])) {
throw new ProtocolException('Unexpected FCGI_STDIN record');
Expand All @@ -386,14 +309,91 @@ protected function processStdinRecord($requestId, $contentData)
*
* @throws ProtocolException If the FCGI_ABORT_REQUEST record is unexpected
*/
protected function processAbortRequestRecord($requestId)
private function processAbortRequestRecord($requestId)
{
if (!isset($this->requests[$requestId])) {
throw new ProtocolException('Unexpected FCG_ABORT_REQUEST record');
}

$this->endRequest($requestId);
}

/**
* End the request by writing an FCGI_END_REQUEST record and then removing
* the request from memory and closing the connection if necessary.
*
* @param int $requestId The request id to end
* @param int $appStatus The application status to declare
* @param int $protocolStatus The protocol status to declare
*/
private function endRequest($requestId, $appStatus = 0, $protocolStatus = DaemonInterface::FCGI_REQUEST_COMPLETE)
{
$content = pack('NCx3', $appStatus, $protocolStatus);
$this->writeRecord($requestId, DaemonInterface::FCGI_END_REQUEST, $content);

$keepAlive = $this->requests[$requestId]['keepAlive'];

fclose($this->requests[$requestId]['stdin']);

unset($this->requests[$requestId]);

if (!$keepAlive) {
$this->close();
}
}

/**
* Write a record to the connection.
*
* @param int $requestId The request id to write to
* @param int $type The type of record
* @param string $content The content of the record
*/
private function writeRecord($requestId, $type, $content = null)
{
$contentLength = null === $content ? 0 : strlen($content);

$headerData = pack('CCnnxx', DaemonInterface::FCGI_VERSION_1, $type, $requestId, $contentLength);

$this->connection->write($headerData);

if (null !== $content) {
$this->connection->write($content);
}
}

/**
* Write a response to the connection as FCGI_STDOUT records.
*
* @param int $requestId The request id to write to
* @param string $headerData The header data to write (including terminating CRLFCRLF)
* @param StreamInterface $stream The stream to write
*/
private function writeResponse($requestId, $headerData, StreamInterface $stream)
{
$data = $headerData;
$eof = false;

$stream->rewind();

do {
$dataLength = strlen($data);

if ($dataLength < 65535 && !$eof && !($eof = $stream->eof())) {
$readLength = 65535 - $dataLength;
$data .= $stream->read($readLength);
$dataLength = strlen($data);
}

$writeSize = min($dataLength, 65535);
$writeData = substr($data, 0, $writeSize);
$data = substr($data, $writeSize);

$this->writeRecord($requestId, DaemonInterface::FCGI_STDOUT, $writeData);
} while ($writeSize === 65535);

$this->writeRecord($requestId, DaemonInterface::FCGI_STDOUT);
}

/**
* Dispatches a request to the kernel.
Expand All @@ -402,7 +402,7 @@ protected function processAbortRequestRecord($requestId)
*
* @throws DaemonException If there is an error dispatching the request
*/
protected function dispatchRequest($requestId)
private function dispatchRequest($requestId)
{
$request = new Request(
$this->requests[$requestId]['params'],
Expand Down Expand Up @@ -434,7 +434,7 @@ protected function dispatchRequest($requestId)
* @param int $requestId The request id to respond to
* @param ResponseInterface $response The PSR-7 HTTP response message
*/
protected function sendResponse($requestId, ResponseInterface $response)
private function sendResponse($requestId, ResponseInterface $response)
{
$statusCode = $response->getStatusCode();
$reasonPhrase = $response->getReasonPhrase();
Expand All @@ -456,7 +456,7 @@ protected function sendResponse($requestId, ResponseInterface $response)
* @param int $requestId The request id to respond to
* @param HttpFoundationResponse $response The HTTP foundation response message
*/
protected function sendHttpFoundationResponse($requestId, HttpFoundationResponse $response)
private function sendHttpFoundationResponse($requestId, HttpFoundationResponse $response)
{
$statusCode = $response->getStatusCode();

Expand Down
2 changes: 1 addition & 1 deletion src/ConnectionHandler/ConnectionHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ConnectionHandlerFactory implements ConnectionHandlerFactoryInterface, Log
/**
* @var KernelInterface
*/
protected $kernel;
private $kernel;

/**
* Constructor.
Expand Down
Loading

0 comments on commit 57f337a

Please sign in to comment.