Skip to content

Commit

Permalink
Replaces flsockopen with stream_socket_client, closes #9
Browse files Browse the repository at this point in the history
Changed

* Replaced methods `getHost()` and `getPort()` with `getSocketAddress()` in interface `hollodotme\FastCGI\Interfaces\ConfiguresSocketConnection`
* The transport protocol `unix://` must be omitted for the first parameter of `hollodotme\FastCGI\SocketConnections\UnixDomainSocket`
  Only the socket path must be passed.
* Replaced `fsockopen()` with `stream_socket_client()` for connecting to php-fpm.
  • Loading branch information
hollodotme committed Jun 15, 2017
1 parent 339ef21 commit 81f3d73
Show file tree
Hide file tree
Showing 28 changed files with 157 additions and 162 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
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.3.0] - 2017-06-15

### Changed

* Replaced methods `getHost()` and `getPort()` with `getSocketAddress()` in interface `hollodotme\FastCGI\Interfaces\ConfiguresSocketConnection` - [#9]
* The transport protocol `unix://` must be omitted for the first parameter of `hollodotme\FastCGI\SocketConnections\UnixDomainSocket`
Only the socket path must be passed. - [#9]
* Replaced `fsockopen()` with `stream_socket_client()` for connecting to php-fpm. - [#9]

## [2.2.0] - 2017-04-15

### Added
Expand Down Expand Up @@ -98,6 +107,7 @@ 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.3.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.2.0...v2.3.0
[2.2.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.1.0...v2.2.0
[2.1.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.0.1...v2.1.0
[2.0.1]: https://github.com/hollodotme/fast-cgi-client/compare/v2.0.0...v2.0.1
Expand All @@ -108,3 +118,4 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http
[#2]: https://github.com/hollodotme/fast-cgi-client/issues/2
[#5]: https://github.com/hollodotme/fast-cgi-client/issues/5
[#6]: https://github.com/hollodotme/fast-cgi-client/issues/6
[#9]: https://github.com/hollodotme/fast-cgi-client/issues/9
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ You can find an experimental use-case in my related blog posts:
* [Experimental async PHP vol. 1](http://bit.ly/eapv1)
* [Experimental async PHP vol. 2](http://bit.ly/eapv2)

You can also find slides of my talks about this project on [speakerdeck.com](https://speakerdeck.com/hollodotme).

---

## Installation
Expand All @@ -41,7 +43,7 @@ The following examples assume a that the content of `/path/to/target/script.php`
```php
<?php declare(strict_types=1);

sleep((int)$_REQUEST['sleep'] ?? 0);
sleep((int)($_REQUEST['sleep'] ?? 0));
echo $_REQUEST['key'] ?? '';
```

Expand All @@ -56,14 +58,16 @@ use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;

$connection = new UnixDomainSocket(
'unix:///var/run/php/php7.1-fpm.sock', # Socket path to php-fpm
5000, # Connect timeout in milliseconds (default: 5000)
5000 # Read/write timeout in milliseconds (default: 5000)
'/var/run/php/php7.1-fpm.sock', # Socket path to php-fpm
5000, # Connect timeout in milliseconds (default: 5000)
5000 # Read/write timeout in milliseconds (default: 5000)
);

$client = new Client( $connection );
```

**PLEASE NOTE:** In versions before 2.3.0 you also need to provide the transport protocol `unix://` in the first parameter.

### Init client with a network socket connection

```php
Expand Down Expand Up @@ -95,7 +99,7 @@ use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\Requests\PostRequest;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;

$client = new Client( new UnixDomainSocket( 'unix:///var/run/php/php1.0-fpm.sock' ) );
$client = new Client( new UnixDomainSocket( '/var/run/php/php1.0-fpm.sock' ) );
$content = http_build_query(['key' => 'value']);

$request = new PostRequest('/path/to/target/script.php', $content);
Expand Down Expand Up @@ -652,4 +656,6 @@ Run a call through a network socket:

Run a call through a Unix Domain Socket

bin/fcgiget unix:/var/run/php/php7.0-fpm.sock/status
bin/fcgiget /var/run/php/php7.1-fpm.sock/status

This shows the response of the php-fpm status page, if enabled.
6 changes: 3 additions & 3 deletions bin/fcgiget
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if ( $_SERVER['argc'] < 2 )
{
echo 'Usage: ' . $_SERVER['argv'][0] . " URI\n\n";
echo 'Ex: ' . $_SERVER['argv'][0] . " localhost:9000/status\n";
echo 'Ex: ' . $_SERVER['argv'][0] . " unix:/var/run/php-fpm/web.sock/status\n";
echo 'Ex: ' . $_SERVER['argv'][0] . " /var/run/php-fpm/web.sock/status\n";
exit( 1 );
}

Expand Down Expand Up @@ -92,8 +92,8 @@ else
}
if ( $sock )
{
$client = new Client( new UnixDomainSocket( "unix://{$sock}" ) );
echo "Call: {$uri} on UDS {$sock}\n\n";
$client = new Client( new UnixDomainSocket( (string)$sock ) );
echo "Call: {$uri} on UDS unix://{$sock}\n\n";
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
"extra": {
"tools": {
"phpunit": {
"url": "https://phar.phpunit.de/phpunit-6.1.0.phar",
"url": "https://phar.phpunit.de/phpunit-6.2.2.phar",
"only-dev": true
},
"coveralls": {
"url": "https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar",
"only-dev": true
},
"phpmetrics": {
"url": "https://github.com/phpmetrics/PhpMetrics/releases/download/v2.1.0/phpmetrics.phar",
"url": "https://github.com/phpmetrics/PhpMetrics/releases/download/v2.2.0/phpmetrics.phar",
"only-dev": true
}
}
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

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

2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function getSocketWithId( int $requestId ) : Socket
return $this->sockets[ $requestId ];
}

private function guardSocketExists( int $requestId )
private function guardSocketExists( int $requestId ) : void
{
if ( !isset( $this->sockets[ $requestId ] ) )
{
Expand Down
6 changes: 2 additions & 4 deletions src/Interfaces/ConfiguresSocketConnection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2017 Holger Woltersdorf
Expand Down Expand Up @@ -29,9 +29,7 @@
*/
interface ConfiguresSocketConnection
{
public function getHost() : string;

public function getPort() : int;
public function getSocketAddress() : string;

public function getConnectTimeout() : int;

Expand Down
30 changes: 15 additions & 15 deletions src/Requests/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getServerSoftware() : string
return $this->serverSoftware;
}

public function setServerSoftware( string $serverSoftware )
public function setServerSoftware( string $serverSoftware ) : void
{
$this->serverSoftware = $serverSoftware;
}
Expand All @@ -101,7 +101,7 @@ public function getRemoteAddress() : string
return $this->remoteAddress;
}

public function setRemoteAddress( string $remoteAddress )
public function setRemoteAddress( string $remoteAddress ) : void
{
$this->remoteAddress = $remoteAddress;
}
Expand All @@ -111,7 +111,7 @@ public function getRemotePort() : int
return $this->remotePort;
}

public function setRemotePort( int $remotePort )
public function setRemotePort( int $remotePort ) : void
{
$this->remotePort = $remotePort;
}
Expand All @@ -121,7 +121,7 @@ public function getServerAddress() : string
return $this->serverAddress;
}

public function setServerAddress( string $serverAddress )
public function setServerAddress( string $serverAddress ) : void
{
$this->serverAddress = $serverAddress;
}
Expand All @@ -131,7 +131,7 @@ public function getServerPort() : int
return $this->serverPort;
}

public function setServerPort( int $serverPort )
public function setServerPort( int $serverPort ) : void
{
$this->serverPort = $serverPort;
}
Expand All @@ -141,7 +141,7 @@ public function getServerName() : string
return $this->serverName;
}

public function setServerName( string $serverName )
public function setServerName( string $serverName ) : void
{
$this->serverName = $serverName;
}
Expand All @@ -151,7 +151,7 @@ public function getServerProtocol() : string
return $this->serverProtocol;
}

public function setServerProtocol( string $serverProtocol )
public function setServerProtocol( string $serverProtocol ) : void
{
$this->serverProtocol = $serverProtocol;
}
Expand All @@ -161,7 +161,7 @@ public function getContentType() : string
return $this->contentType;
}

public function setContentType( string $contentType )
public function setContentType( string $contentType ) : void
{
$this->contentType = $contentType;
}
Expand All @@ -171,23 +171,23 @@ public function getContent() : string
return $this->content;
}

public function setContent( string $content )
public function setContent( string $content ) : void
{
$this->content = $content;
$this->contentLength = strlen( $content );
}

public function setCustomVar( string $key, $value )
public function setCustomVar( string $key, $value ) : void
{
$this->customVars[ $key ] = $value;
}

public function addCustomVars( array $vars )
public function addCustomVars( array $vars ) : void
{
$this->customVars = array_merge( $this->customVars, $vars );
}

public function resetCustomVars()
public function resetCustomVars() : void
{
$this->customVars = [];
}
Expand Down Expand Up @@ -239,7 +239,7 @@ public function getRequestUri() : string
return $this->requestUri;
}

public function setRequestUri( string $requestUri )
public function setRequestUri( string $requestUri ) : void
{
$this->requestUri = $requestUri;
}
Expand All @@ -249,7 +249,7 @@ public function getResponseCallbacks() : array
return $this->responseCallbacks;
}

public function addResponseCallbacks( callable ...$callbacks )
public function addResponseCallbacks( callable ...$callbacks ) : void
{
$this->responseCallbacks = array_merge( $this->responseCallbacks, $callbacks );
}
Expand All @@ -259,7 +259,7 @@ public function getFailureCallbacks() : array
return $this->failureCallbacks;
}

public function addFailureCallbacks( callable ...$callbacks )
public function addFailureCallbacks( callable ...$callbacks ) : void
{
$this->failureCallbacks = array_merge( $this->failureCallbacks, $callbacks );
}
Expand Down
2 changes: 1 addition & 1 deletion src/Responses/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct( int $requestId, string $rawResponse, float $duratio
$this->parseHeadersAndBody();
}

private function parseHeadersAndBody()
private function parseHeadersAndBody() : void
{
$lines = explode( PHP_EOL, $this->rawResponse );
$offset = 0;
Expand Down
16 changes: 5 additions & 11 deletions src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,12 @@ public function sendRequest( ProvidesRequestData $request ) : void
$this->startTime = microtime( true );
}

private function connect()
private function connect() : void
{
if ( is_resource( $this->resource ) )
{
return;
}

try
{
$this->resource = @fsockopen(
$this->connection->getHost(),
$this->connection->getPort(),
$this->resource = @stream_socket_client(
$this->connection->getSocketAddress(),
$errorNumber,
$errorString,
$this->connection->getConnectTimeout() / 1000
Expand Down Expand Up @@ -351,7 +345,7 @@ private function readPacket() : ?array
return null;
}

private function handleNullPacket( $packet )
private function handleNullPacket( $packet ) : void
{
if ( $packet === null )
{
Expand All @@ -371,7 +365,7 @@ private function handleNullPacket( $packet )
}
}

private function guardRequestCompleted( int $flag )
private function guardRequestCompleted( int $flag ) : void
{
switch ( $flag )
{
Expand Down
11 changes: 3 additions & 8 deletions src/SocketConnections/NetworkSocket.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2017 Holger Woltersdorf
Expand Down Expand Up @@ -56,14 +56,9 @@ public function __construct(
$this->readWriteTimeout = $readWriteTimeout;
}

public function getHost() : string
public function getSocketAddress() : string
{
return $this->host;
}

public function getPort() : int
{
return $this->port;
return sprintf( 'tcp://%s:%d', $this->host, $this->port );
}

public function getConnectTimeout() : int
Expand Down
19 changes: 5 additions & 14 deletions src/SocketConnections/UnixDomainSocket.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2017 Holger Woltersdorf
Expand Down Expand Up @@ -32,10 +32,7 @@
class UnixDomainSocket implements ConfiguresSocketConnection
{
/** @var string */
private $host;

/** @var int */
private $port;
private $socketPath;

/** @var int */
private $connectTimeout;
Expand All @@ -49,20 +46,14 @@ public function __construct(
int $readWriteTimeout = Defaults::READ_WRITE_TIMEOUT
)
{
$this->host = $socketPath;
$this->port = -1;
$this->socketPath = $socketPath;
$this->connectTimeout = $connectTimeout;
$this->readWriteTimeout = $readWriteTimeout;
}

public function getHost() : string
{
return $this->host;
}

public function getPort() : int
public function getSocketAddress() : string
{
return $this->port;
return 'unix://' . $this->socketPath;
}

public function getConnectTimeout() : int
Expand Down
Loading

0 comments on commit 81f3d73

Please sign in to comment.