Skip to content

Commit

Permalink
Add a transport capable of logging to get debug data
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Babker committed Oct 15, 2016
1 parent 7708880 commit 509c2d1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 13 deletions.
29 changes: 27 additions & 2 deletions src/JTracker/Github/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Joomla\Github\Github as JGitHub;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

Expand All @@ -25,7 +24,13 @@
*/
class Github extends JGitHub implements LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* Logger
*
* @var LoggerInterface
* @since 1.0
*/
protected $logger;

/**
* Magic method to lazily create API objects
Expand Down Expand Up @@ -74,4 +79,24 @@ public function getLogger()

return $this->logger;
}

/**
* Sets a logger.
*
* @param LoggerInterface $logger The logger object.
*
* @return void
*
* @since 1.0
*/
public function setLogger(LoggerInterface $logger)
{
// Also inject the logger into the transport if it supports logging
if ($this->client instanceof LoggerAwareInterface)
{
$this->client->setLogger($logger);
}

$this->logger = $logger;
}
}
13 changes: 2 additions & 11 deletions src/JTracker/Github/GithubFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
namespace JTracker\Github;

use Joomla\Github\Http;
use Joomla\Http\HttpFactory;
use Joomla\Registry\Registry;

use JTracker\Application;
use JTracker\Http\CurlTransport;

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
Expand Down Expand Up @@ -99,16 +99,7 @@ public static function getInstance($app, $useBot = false, $botUser = '', $botPas
}

// The cURL extension is required to properly work.
$transport = HttpFactory::getAvailableDriver($options, ['curl']);

// Check if we *really* got a cURL transport...
if (false === $transport)
{
throw new \RuntimeException('No transports available (please install php-curl)');
}

// Instantiate the object
$github = new Github($options, new Http($options, $transport));
$github = new Github($options, new Http($options, new CurlTransport($options)));

// If debugging is enabled, inject a logger
if ($app->get('debug.github', false))
Expand Down
101 changes: 101 additions & 0 deletions src/JTracker/Http/CurlTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Part of the Joomla Tracker
*
* @copyright Copyright (C) 2012 - 2014 Open Source Matters, Inc. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/

namespace JTracker\Http;

use Joomla\Http\Exception\InvalidResponseCodeException;
use Joomla\Http\Response;
use Joomla\Http\Transport\Curl;
use Joomla\Uri\UriInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
* Logger aware curl transport
*
* @since 1.0
*/
class CurlTransport extends Curl implements LoggerAwareInterface
{
use LoggerAwareTrait;

/**
* Get the logger.
*
* @return LoggerInterface
*
* @since 1.0
*/
public function getLogger()
{
// If a logger hasn't been set, use NullLogger
if (!($this->logger instanceof LoggerInterface))
{
$this->logger = new NullLogger();
}

return $this->logger;
}

/**
* Method to get a response object from a server response.
*
* @param string $content The complete server response, including headers
* as a string if the response has no errors.
* @param array $info The cURL request information.
*
* @return Response
*
* @since 1.0
* @throws InvalidResponseCodeException
*/
protected function getResponse($content, $info)
{
$this->logger->debug(
'Building response for curl request',
[
'response' => $content,
'transport_info' => $info,
]
);

return parent::getResponse($content, $info);
}

/**
* Send a request to the server and return a Response object with the response.
*
* @param string $method The HTTP method for sending the request.
* @param UriInterface $uri The URI to the resource to request.
* @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of request headers to send with the request.
* @param integer $timeout Read timeout in seconds.
* @param string $userAgent The optional user agent string to send with the request.
*
* @return Response
*
* @since 1.0
* @throws \RuntimeException
*/
public function request($method, UriInterface $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
{
$this->logger->debug(
'Request started for curl transport',
[
'method' => $method,
'uri' => (string) $uri,
'data' => $data,
'headers' => $headers,
]
);

return parent::request($method, $uri, $data, $headers, $timeout, $userAgent);
}
}

0 comments on commit 509c2d1

Please sign in to comment.