Skip to content

Commit

Permalink
Merge pull request #241 from podio-community/240-fix-kint-debugging
Browse files Browse the repository at this point in the history
fix: kint debugging for web setups + making kint an optional dependency
  • Loading branch information
daniel-sc committed Aug 14, 2023
2 parents 5369410 + 9b172af commit 51d68f2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* BREAKING: Replace `save` (and `completed`/`incompleted`/`destroy` on `PodioTask`) methods on instances with static methods #234
* BREAKING: Remove obsolete `PodioClient::secret` and `PodioClient::headers` properties.
* BREAKING: `Podio::debug` changed from public to protected: use `PodioClient::set_debug(..)`
* BREAKING: Kint is now an optional dependency of the package. Use `composer require kint-php/kint` to install it, if you need it.
* Bugfix: Debug output via Kint is now working again. #240
* See [migration guide](https://github.com/podio-community/podio-php/blob/master/MIGRATION_GUIDE_v7.md) for details.

[6.1.1](#v6.1.1) / 2023-06-12
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
],
"require": {
"php": "^7.3 || ^8.0",
"kint-php/kint": "^4.2.0",
"guzzlehttp/guzzle": ">=6.2.0",
"ext-json": "*"
},
"suggest": {
"composer/ca-bundle": "Improve security through providing current CA ROOT certificates bundle"
"composer/ca-bundle": "Improve security through providing current CA ROOT certificates bundle",
"kint-php/kint": "Debugging helper for web setups"
},
"autoload": {
"classmap": ["lib/", "lib/error/", "models/", "models/field/"]
Expand Down
31 changes: 15 additions & 16 deletions lib/PodioClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
class PodioClient
{
public $oauth;
/** @var bool|string */
protected $debug = false;
/**
* Only created/used if debug is enabled and set to 'file'.
*
* @var ?PodioLogger
*/
public $logger;
public $session_manager;
/** @var ?PodioResponse */
Expand Down Expand Up @@ -186,10 +192,6 @@ public function is_authenticated(): bool
*/
public function request($method, $url, $attributes = [], $options = [])
{
if (!$this->http_client) {
throw new Exception('Client has not been setup with client id and client secret.');
}

$original_url = $url;
$encoded_attributes = null;

Expand Down Expand Up @@ -425,26 +427,26 @@ public function set_debug(bool $toggle, string $output = "stdout")
}
}

public function log_request($method, $url, $encoded_attributes, $response, $transferTime): void
protected function log_request($method, $url, $encoded_attributes, $response, $transferTime): void
{
if ($this->debug) {
if (!$this->logger) {
$this->logger = new PodioLogger();
}
$timestamp = gmdate('Y-m-d H:i:s');
$text = "{$timestamp} {$response->status} {$method} {$url}\n";
if (!empty($encoded_attributes)) {
$text .= "{$timestamp} Request body: " . $encoded_attributes . "\n";
}
$text .= "{$timestamp} Reponse: {$response->body}\n\n";
$text .= "{$timestamp} Response: {$response->body}\n\n";

if ($this->debug === 'file') {
if (!$this->logger) {
$this->logger = new PodioLogger();
}
$this->logger->log($text);
} elseif ($this->debug === 'stdout' && php_sapi_name() === 'cli') {
} elseif ($this->debug === 'stdout' && php_sapi_name() !== 'cli' && class_exists('\\Kint\\Kint')) {
/** @noinspection PhpFullyQualifiedNameUsageInspection */
\Kint\Kint::dump("{$method} {$url}", $encoded_attributes, $response);
} else {
print $text;
} elseif ($this->debug === 'stdout' && php_sapi_name() === 'cli') {
require_once 'vendor/kint/Kint.class.php';
Kint::dump("{$method} {$url}", $encoded_attributes, $response);
}

$this->logger->call_log[] = $transferTime;
Expand All @@ -471,9 +473,6 @@ public function shutdown()

$text = "\n{$timestamp} Performed {$count} request(s) in {$duration} seconds\n";
if ($this->debug === 'file') {
if (!$this->logger) {
$this->logger = new PodioLogger();
}
$this->logger->log($text);
} elseif ($this->debug === 'stdout' && php_sapi_name() === 'cli') {
print $text;
Expand Down
11 changes: 0 additions & 11 deletions lib/PodioLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
class PodioLogger
{
protected $enabled = true;
public $call_log = array();
public $file;
public $maxsize;
Expand All @@ -16,16 +15,6 @@ public function __construct()
$this->maxsize = 1024*1024;
}

public function disable()
{
$this->enabled = false;
}

public function enable()
{
$this->enabled = true;
}

public function log($text)
{
if (!is_dir(dirname($this->file))) {
Expand Down
24 changes: 24 additions & 0 deletions tests/PodioClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Podio\Tests;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use PodioClient;
use PHPUnit\Framework\TestCase;

class PodioClientTest extends TestCase
{
public function testDebugToStdOut()
{
$client = new PodioClient('test-client-id', 'test-client-secret');
$client->set_debug(true);
$httpClientMock = $this->createMock(Client::class);
$httpClientMock->method('send')->willReturn(new Response(200, [], '{"message": "OK"}'));
$client->http_client = $httpClientMock;

$client->get('/test');

$this->expectOutputRegex('/200 GET \/test.*{"message": "OK"}/ms');
}
}

0 comments on commit 51d68f2

Please sign in to comment.