Skip to content

Commit

Permalink
Changes class constant visibilities, requires php >= 7.1, bumping v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodotme committed Jan 3, 2017
1 parent 64ab256 commit a50840f
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 115 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 7.0
- 7.1

branches:
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepachangelog.com).

## [2.0.0] - 2017-01-03

### Changed

* Class constant visibility to private in class `Client`
* Class constant visibility to privare in class `Encoders\PacketEncoder`
* Class constant visibility to public in class `SocketConnections\Defaults`
* Composer requires php >= 7.1


## [1.0.0] - 2017-01-03

Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](https://github.com/adoy/PHP-FastCGI-Client/):
Expand All @@ -27,4 +37,5 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http
* Getters/Setters for connect timeout, read/write timeout, keep alive, socket persistence from `Client` (now part of the socket connection)
* Method `Client->getValues()`

[2.0.0]: https://github.com/hollodotme/fast-cgi-client/compare/v1.0.0...v2.0.0
[1.0.0]: https://github.com/hollodotme/fast-cgi-client/tree/v1.0.0
4 changes: 2 additions & 2 deletions bin/fcgiget
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ if ( file_exists( __DIR__ . '/../vendor/autoload.php' ) )
{
require(__DIR__ . '/../vendor/autoload.php');
}
elseif ( file_exists( __DIR__ . '/../autoload.php' ) )
elseif ( file_exists( __DIR__ . '/../../../autoload.php' ) )
{
require(__DIR__ . '/../autoload.php');
require(__DIR__ . '/../../../autoload.php');
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"hollodotme\\FastCGI\\Tests\\": "tests/"
}
},
"require": {
"php": ">=7.1"
},
"require-dev": {
"tm/tooly-composer-script": "^1.0"
},
Expand Down
6 changes: 4 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

208 changes: 105 additions & 103 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,35 @@
*/
class Client
{
const BEGIN_REQUEST = 1;
private const BEGIN_REQUEST = 1;

const END_REQUEST = 3;
private const END_REQUEST = 3;

const PARAMS = 4;
private const PARAMS = 4;

const STDIN = 5;
private const STDIN = 5;

const STDOUT = 6;
private const STDOUT = 6;

const STDERR = 7;
private const STDERR = 7;

const RESPONDER = 1;
private const RESPONDER = 1;

const REQUEST_COMPLETE = 0;
private const REQUEST_COMPLETE = 0;

const CANT_MPX_CONN = 1;
private const CANT_MPX_CONN = 1;

const OVERLOADED = 2;
private const OVERLOADED = 2;

const UNKNOWN_ROLE = 3;
private const UNKNOWN_ROLE = 3;

const HEADER_LEN = 8;
private const HEADER_LEN = 8;

const REQ_STATE_WRITTEN = 1;
private const REQ_STATE_WRITTEN = 1;

const REQ_STATE_OK = 2;
private const REQ_STATE_OK = 2;

const REQ_STATE_ERR = 3;
private const REQ_STATE_ERR = 3;

/** @var ConfiguresSocketConnection */
private $connection;
Expand Down Expand Up @@ -99,7 +99,95 @@ public function __construct( ConfiguresSocketConnection $connection )
$this->nameValuePairEncoder = new NameValuePairEncoder();
}

private function connect()
/**
* Execute a request to the FastCGI application
*
* @param array $params Array of parameters
* @param string $content Content
*
* @return string
*/
public function sendRequest( array $params, string $content ) : string
{
$requestId = $this->sendAsyncRequest( $params, $content );

return $this->waitForResponse( $requestId );
}

/**
* Execute a request to the FastCGI application asyncronously
* This sends request to application and returns the assigned ID for that request.
* You should keep this id for later use with wait_for_response(). Ids are chosen randomly
* rather than seqentially to guard against false-positives when using persistent sockets.
* In that case it is possible that a delayed response to a request made by a previous script
* invocation comes back on this socket and is mistaken for response to request made with same ID
* during this request.
*
* @param array $params Array of parameters
* @param string $content Content
*
* @throws TimedoutException
* @throws WriteFailedException
* @return int
*/
public function sendAsyncRequest( array $params, string $content ) : int
{
$this->connect();

// Pick random number between 1 and max 16 bit unsigned int 65535
$requestId = mt_rand( 1, (1 << 16) - 1 );

// Using persistent sockets implies you want them kept alive by server
$keepAlive = intval( $this->connection->keepAlive() || $this->connection->isPersistent() );

$request = $this->packetEncoder->encodePacket(
self::BEGIN_REQUEST,
chr( 0 ) . chr( self::RESPONDER ) . chr( $keepAlive ) . str_repeat( chr( 0 ), 5 ),
$requestId
);

$paramsRequest = $this->nameValuePairEncoder->encodePairs( $params );

if ( $paramsRequest )
{
$request .= $this->packetEncoder->encodePacket( self::PARAMS, $paramsRequest, $requestId );
}

$request .= $this->packetEncoder->encodePacket( self::PARAMS, '', $requestId );

if ( $content )
{
$request .= $this->packetEncoder->encodePacket( self::STDIN, $content, $requestId );
}

$request .= $this->packetEncoder->encodePacket( self::STDIN, '', $requestId );

if ( fwrite( $this->socket, $request ) === false || fflush( $this->socket ) === false )
{
$info = stream_get_meta_data( $this->socket );

if ( $info['timed_out'] )
{
throw new TimedoutException( 'Write timed out' );
}

// Broken pipe, tear down so future requests might succeed
fclose( $this->socket );

throw new WriteFailedException( 'Failed to write request to socket [broken pipe]' );
}

$this->requests[ $requestId ] = [
'state' => self::REQ_STATE_WRITTEN,
'response' => null,
];

return $requestId;
}

private function connect() : void
{
if ( $this->socket === null )
{
Expand Down Expand Up @@ -157,7 +245,7 @@ private function setStreamTimeout( int $timeoutMs ) : bool
* Read a FastCGI PacketEncoder
* @return array|null
*/
private function readPacket()
private function readPacket() : ?array
{
if ( $packet = fread( $this->socket, self::HEADER_LEN ) )
{
Expand Down Expand Up @@ -188,92 +276,6 @@ private function readPacket()
}
}

/**
* Execute a request to the FastCGI application
*
* @param array $params Array of parameters
* @param string $content Content
*
* @return string
*/
public function sendRequest( array $params, string $content ) : string
{
$requestId = $this->sendAsyncRequest( $params, $content );

return $this->waitForResponse( $requestId );
}

/**
* Execute a request to the FastCGI application asyncronously
* This sends request to application and returns the assigned ID for that request.
* You should keep this id for later use with wait_for_response(). Ids are chosen randomly
* rather than seqentially to guard against false-positives when using persistent sockets.
* In that case it is possible that a delayed response to a request made by a previous script
* invocation comes back on this socket and is mistaken for response to request made with same ID
* during this request.
*
* @param array $params Array of parameters
* @param string $content Content
*
*@throws TimedoutException
* @throws WriteFailedException
* @return int
*/
public function sendAsyncRequest( array $params, string $content ) : int
{
$this->connect();

// Pick random number between 1 and max 16 bit unsigned int 65535
$requestId = mt_rand( 1, (1 << 16) - 1 );

// Using persistent sockets implies you want them kept alive by server
$keepAlive = intval( $this->connection->keepAlive() || $this->connection->isPersistent() );

$request = $this->packetEncoder->encodePacket(
self::BEGIN_REQUEST,
chr( 0 ) . chr( self::RESPONDER ) . chr( $keepAlive ) . str_repeat( chr( 0 ), 5 ),
$requestId
);

$paramsRequest = $this->nameValuePairEncoder->encodePairs( $params );

if ( $paramsRequest )
{
$request .= $this->packetEncoder->encodePacket( self::PARAMS, $paramsRequest, $requestId );
}

$request .= $this->packetEncoder->encodePacket( self::PARAMS, '', $requestId );

if ( $content )
{
$request .= $this->packetEncoder->encodePacket( self::STDIN, $content, $requestId );
}

$request .= $this->packetEncoder->encodePacket( self::STDIN, '', $requestId );

if ( fwrite( $this->socket, $request ) === false || fflush( $this->socket ) === false )
{
$info = stream_get_meta_data( $this->socket );

if ( $info['timed_out'] )
{
throw new TimedoutException( 'Write timed out' );
}

// Broken pipe, tear down so future requests might succeed
fclose( $this->socket );

throw new WriteFailedException( 'Failed to write request to socket [broken pipe]' );
}

$this->requests[ $requestId ] = [
'state' => self::REQ_STATE_WRITTEN,
'response' => null,
];

return $requestId;
}

/**
* Blocking call that waits for response to specific request
*
Expand Down
2 changes: 1 addition & 1 deletion src/Encoders/PacketEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
final class PacketEncoder
{
const VERSION = 1;
private const VERSION = 1;

public function encodePacket( int $type, string $content, int $requestId ) : string
{
Expand Down
8 changes: 4 additions & 4 deletions src/SocketConnections/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
*/
abstract class Defaults
{
const CONNECT_TIMEOUT = 5000;
public const CONNECT_TIMEOUT = 5000;

const READ_WRITE_TIMEOUT = 5000;
public const READ_WRITE_TIMEOUT = 5000;

const KEEP_ALIVE = false;
public const KEEP_ALIVE = false;

const PERSISTENT = false;
public const PERSISTENT = false;
}
4 changes: 2 additions & 2 deletions src/Timing/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct( int $timeoutMs )
$this->timeoutMs = $timeoutMs;
}

public function start()
public function start() : void
{
$this->startTime = microtime( true );
}
Expand All @@ -57,7 +57,7 @@ public function timedOut() : bool
return ((microtime( true ) - $this->startTime) > ($this->timeoutMs / 1000));
}

public function reset()
public function reset() : void
{
$this->startTime = null;
}
Expand Down

0 comments on commit a50840f

Please sign in to comment.