Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PSR-3 log for frame logging #85

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Features

* modern PHP standards
* [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/) & [PSR-4](http://www.php-fig.org/psr/psr-4/)
* [PSR-3](http://www.php-fig.org/psr/psr-3/) for logging command and response frames
* notice and warning free (find them, and I'll fix it!)
* high-level usage (Plug & Play)
* simplified client for socket and http(s) connections (auto login/logout, auto inject clTRID)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"require": {
"php": ">=5.5.0",
"ext-intl": "*",
"ext-openssl": "*"
"ext-openssl": "*",
"psr/log": "^1.1"
},
"require-dev": {
"php-coveralls/php-coveralls": "^1.0|^2.0"
Expand Down
39 changes: 36 additions & 3 deletions src/AfriCC/EPP/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use AfriCC\EPP\Frame\Response as ResponseFrame;
use AfriCC\EPP\Frame\ResponseFactory;
use Exception;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
* An abstract client client for the Extensible Provisioning Protocol (EPP)
Expand All @@ -18,8 +20,10 @@
* As this class is abstract and relies on subclass implementation details it's untestable
* @codeCoverageIgnore
*/
abstract class AbstractClient implements ClientInterface
abstract class AbstractClient implements ClientInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

protected $host;
protected $port;
protected $username;
Expand All @@ -45,7 +49,33 @@ abstract public function connect($newPassword = false);

abstract public function close();

abstract protected function log($message);
/**
* Prints out debugging data (eg raw frame bytes)
*
* @param string $message
*/
abstract protected function debugLog($message);

protected function logCommand(FrameInterface $frame)
{
if (isset($this->logger)) {
$command = \get_class($frame);
$frame_xml = (string) $frame;
$this->logger->info("Sending EPP Command '$command'. Full frame: $frame_xml");
}
}

/**
* @param ResponseFrame|string $frame
*/
protected function logResponse($frame)
{
if (isset($this->logger)) {
$type = $frame instanceof ResponseFrame ? \get_class($frame) : 'INCORRECT';
$frame_xml = (string) $frame;
$this->logger->info("Received EPP '$type' Response. Full frame: $frame_xml");
}
}

/**
* Send frame to EPP server
Expand Down Expand Up @@ -73,11 +103,14 @@ public function request(FrameInterface $frame)
);
}

$this->logCommand($frame);
$this->sendFrame($frame);

$return = $this->getFrame();
$response = ResponseFactory::build($return, $this->objectSpec);
$this->logResponse($response);

return ResponseFactory::build($return, $this->objectSpec);
return $response;
}

public function __construct(array $config, ObjectSpec $objectSpec = null)
Expand Down
12 changes: 8 additions & 4 deletions src/AfriCC/EPP/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use AfriCC\EPP\Frame\Command\Logout as LogoutCommand;
use Exception;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
* A high level TCP (SSL) based client for the Extensible Provisioning Protocol (EPP)
Expand All @@ -22,8 +24,10 @@
* As this class deals directly with sockets it's untestable
* @codeCoverageIgnore
*/
class Client extends AbstractClient implements ClientInterface
class Client extends AbstractClient implements ClientInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

protected $socket;
protected $chunk_size;
protected $verify_peer_name;
Expand Down Expand Up @@ -196,7 +200,7 @@ public function sendFrame(FrameInterface $frame)
return $this->send($header . $buffer);
}

protected function log($message, $color = '0;32')
protected function debugLog($message, $color = '0;32')
{
if ($message === '' || !$this->debug) {
return;
Expand Down Expand Up @@ -236,7 +240,7 @@ private function recv($length)

// If the buffer actually contains something then add it to the result
if ($buffer !== false) {
$this->log($buffer);
$this->debugLog($buffer);
$result .= $buffer;

// break if all data received
Expand Down Expand Up @@ -289,7 +293,7 @@ private function send($buffer)
// If we read something, bump up the position
if ($written) {
if ($this->debug) {
$this->log(mb_substr($buffer, $pos, $wlen, 'ASCII'), '1;31');
$this->debugLog(mb_substr($buffer, $pos, $wlen, 'ASCII'), '1;31');
}
$pos += $written;

Expand Down
24 changes: 22 additions & 2 deletions src/AfriCC/EPP/HTTPClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace AfriCC\EPP;

use AfriCC\EPP\Frame\Command\Logout as LogoutCommand;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
* A high level HTTP(S) based client for the Extensible Provisioning Protocol (EPP)
Expand All @@ -12,10 +14,13 @@
* As this class deals directly with cURL it's untestable
* @codeCoverageIgnore
*/
class HTTPClient extends AbstractClient implements ClientInterface
class HTTPClient extends AbstractClient implements ClientInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

protected $curl;
protected $cookiejar;
protected $curlDebugStream;

public function __construct(array $config, ObjectSpec $objectSpec = null)
{
Expand Down Expand Up @@ -68,6 +73,10 @@ private function setupCurl()
if ($this->ssl) {
$this->setupCurlSSL();
}

if ($this->debug) {
$this->setupCurlDebug();
}
}

private function setupCurlOpts()
Expand Down Expand Up @@ -109,6 +118,13 @@ private function setupCurlSSL()
}
}

private function setupCurlDebug()
{
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
$this->curlDebugStream = fopen('php://temp', 'w+');
curl_setopt($this->curl, CURLOPT_STDERR, $this->curlDebugStream);
}

/**
* Open a new connection to the EPP server
*
Expand Down Expand Up @@ -156,16 +172,20 @@ public function getFrame()
if ($return === false) {
$code = curl_errno($this->curl);
$msg = curl_error($this->curl);
$this->debugLog("cURL error: $msg");
throw new \Exception($msg, $code);
}

return $return;
}

protected function log($message)
protected function debugLog($message)
{
if ($this->debug) {
\error_log($message);
rewind($this->curlDebugStream);
$curlDebug = stream_get_contents($this->curlDebugStream);
\error_log("Full info:\n$curlDebug");
}
}

Expand Down