Skip to content

Commit

Permalink
feat(admin): add download logs action
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 16, 2024
1 parent c2f3ae2 commit 596a694
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
23 changes: 0 additions & 23 deletions src/Hooks/Concern/UsesPdkRequestConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace MyParcelNL\WooCommerce\Hooks\Concern;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use WP_REST_Request;
use WP_REST_Response;

trait UsesPdkRequestConverter
{
Expand Down Expand Up @@ -35,26 +33,5 @@ protected function convertRequest(WP_REST_Request $wpRestRequest): Request

return $request;
}

/**
* Convert a WP_REST_Response to a Symfony Response.
*
* @param \Symfony\Component\HttpFoundation\Response $response
*
* @return \WP_REST_Response
*/
protected function convertResponse(Response $response): WP_REST_Response
{
if ($response->headers->has('Content-Type') && $response->headers->get('Content-Type') === 'application/json') {
$content = json_decode($response->getContent(), true);
} else {
$content = $response->getContent();
}

$wpResponse = new WP_REST_Response($content, $response->getStatusCode());
$wpResponse->header('Content-Type', $response->headers->get('Content-Type'));

return $wpResponse;
}
}

43 changes: 41 additions & 2 deletions src/Logger/WcLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@

namespace MyParcelNL\WooCommerce\Logger;

use Automattic\WooCommerce\Utilities\LoggingUtil;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Logger\AbstractLogger;
use WC_Logger_Interface;

class WcLogger extends AbstractLogger
{
/**
* @return array
*/
public function getLogFiles(): array
{
$logger = $this->getWcLogger();

if (! $logger) {
return parent::getLogFiles();
}

$logDirectory = trailingslashit(LoggingUtil::get_log_directory());
$foundFiles = glob(sprintf('%s%s-*.log', $logDirectory, $this->getLogPrefix()));

if (! $foundFiles) {
return parent::getLogFiles();
}

return $foundFiles;
}

/**
* @param $level
* @param $message
Expand All @@ -18,14 +41,30 @@ class WcLogger extends AbstractLogger
*/
public function log($level, $message, array $context = []): void
{
$logger = function_exists('wc_get_logger') ? wc_get_logger() : null;
$logger = $this->getWcLogger();

if (! $logger) {
return;
}

$json = count($context) ? PHP_EOL . json_encode($context, JSON_PRETTY_PRINT) : '';

$logger->log($level, $message . $json, ['source' => Pdk::getAppInfo()->name]);
$logger->log($level, $message . $json, ['source' => $this->getLogPrefix()]);
}

/**
* @return string
*/
private function getLogPrefix(): string
{
return Pdk::getAppInfo()->name;
}

/**
* @return null|\WC_Logger_Interface
*/
private function getWcLogger(): ?WC_Logger_Interface
{
return function_exists('wc_get_logger') ? wc_get_logger() : null;
}
}
13 changes: 6 additions & 7 deletions src/Pdk/Hooks/AbstractPdkEndpointHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use MyParcelNL\WooCommerce\Hooks\Concern\UsesPdkRequestConverter;
use MyParcelNL\WooCommerce\Hooks\Contract\WordPressHooksInterface;
use WP_REST_Request;
use WP_REST_Response;

abstract class AbstractPdkEndpointHooks implements WordPressHooksInterface
{
Expand All @@ -28,23 +27,23 @@ public function __construct(PdkEndpoint $endpoint)
}

/**
* Convert the request to a format that the PDK understands, calls the endpoint and converts the response back to
* a format that WordPress understands.
* Convert the request to a format that the PDK understands, calls the endpoint and sends the response.
* Disables notices to prevent random plugin notices from breaking the response.
*
* @param string $context
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response
* @return void
*/
protected function processRequest(string $context, WP_REST_Request $request): WP_REST_Response
protected function processRequest(string $context, WP_REST_Request $request): void
{
error_reporting(error_reporting() & ~E_NOTICE);

$convertedRequest = $this->convertRequest($request);
$response = $this->endpoint->call($convertedRequest, $context);

$response = $this->endpoint->call($convertedRequest, $context);
$response->send();

return $this->convertResponse($response);
exit;

Check warning on line 47 in src/Pdk/Hooks/AbstractPdkEndpointHooks.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Pdk/Hooks/AbstractPdkEndpointHooks.php#L47

The method processRequest() contains an exit expression.
}
}
7 changes: 3 additions & 4 deletions src/Pdk/Hooks/PdkAdminEndpointHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use MyParcelNL\Pdk\App\Api\PdkEndpoint;
use MyParcelNL\Pdk\Facade\Pdk;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;

final class PdkAdminEndpointHooks extends AbstractPdkEndpointHooks
Expand All @@ -20,11 +19,11 @@ public function apply(): void
/**
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response
* @return void
*/
public function processBackendRequest(WP_REST_Request $request): WP_REST_Response
public function processBackendRequest(WP_REST_Request $request): void
{
return $this->processRequest(PdkEndpoint::CONTEXT_BACKEND, $request);
$this->processRequest(PdkEndpoint::CONTEXT_BACKEND, $request);
}

/**
Expand Down

0 comments on commit 596a694

Please sign in to comment.