Skip to content

Commit

Permalink
Merge branch 'release/v0.14.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Oct 1, 2019
2 parents f600fd0 + 6f55c45 commit 29d113b
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.14.1 (2019-10-02)
* Improved HTTP header handling
* Added Packet class for Transport passthrough

## v0.14.0 (2019-10-01)
* Simplified dump interface
* Added header buffer sender mechanism
Expand Down
12 changes: 8 additions & 4 deletions src/Glitch/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Context implements LoggerAwareInterface, FacadeTarget
use FacadeTargetTrait;

const FACADE = 'Glitch';
const VERSION = 'v0.14.0';
const VERSION = 'v0.14.1';

protected $startTime;
protected $runMode = 'development';
Expand Down Expand Up @@ -185,7 +185,7 @@ public function dumpValues(array $values, int $rewind=0, bool $exit=true): void
unset($inspector);

$packet = $this->getRenderer()->renderDump($dump);
$this->getTransport()->sendDump($packet, $this->headerBufferSender);
$this->getTransport()->sendDump($packet, $exit);

if ($exit) {
exit(1);
Expand Down Expand Up @@ -230,7 +230,7 @@ public function dumpException(\Throwable $exception, bool $exit=true): void
unset($inspector);

$packet = $this->getRenderer()->renderException($exception, $entity, $dump);
$this->getTransport()->sendException($packet, $this->headerBufferSender);
$this->getTransport()->sendException($packet, $exit);

if ($exit) {
exit(1);
Expand Down Expand Up @@ -680,7 +680,11 @@ public function setTransport(Transport $transport): Context
public function getTransport(): Transport
{
if (!$this->transport) {
$this->transport = new Transport\Stdout($this);
if (in_array(\PHP_SAPI, ['cli', 'phpdbg'])) {
$this->transport = new Transport\Stdout($this);
} else {
$this->transport = new Transport\Http($this);
}
}

return $this->transport;
Expand Down
38 changes: 38 additions & 0 deletions src/Glitch/Packet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* This file is part of the Glitch package
* @license http://opensource.org/licenses/MIT
*/
declare(strict_types=1);
namespace DecodeLabs\Glitch;

class Packet
{
protected $body;
protected $contentType;

/**
* Init with body and content type
*/
public function __construct(string $body, string $contentType)
{
$this->body = $body;
$this->contentType = $contentType;
}

/**
* Get body
*/
public function getBody(): string
{
return $this->body;
}

/**
* Get content type
*/
public function getContentType(): string
{
return $this->contentType;
}
}
4 changes: 2 additions & 2 deletions src/Glitch/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

interface Renderer
{
public function renderDump(Dump $dump): string;
public function renderException(\Throwable $exception, Entity $entity, Dump $dataDump): string;
public function renderDump(Dump $dump): Packet;
public function renderException(\Throwable $exception, Entity $entity, Dump $dataDump): Packet;
}
14 changes: 8 additions & 6 deletions src/Glitch/Renderer/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use DecodeLabs\Glitch\Context;
use DecodeLabs\Glitch\Renderer;
use DecodeLabs\Glitch\Packet;
use DecodeLabs\Glitch\Stack\Trace;
use DecodeLabs\Glitch\Stack\Frame;
use DecodeLabs\Glitch\Dumper\Dump;
Expand Down Expand Up @@ -62,7 +63,7 @@ protected function shouldRender(): bool
/**
* Convert Dump object to HTML string
*/
public function renderDump(Dump $dump): string
public function renderDump(Dump $dump): Packet
{
if (!$this->shouldRender()) {
return '';
Expand Down Expand Up @@ -92,7 +93,7 @@ public function renderDump(Dump $dump): string
/**
* Inspect handled exception
*/
public function renderException(\Throwable $exception, Entity $entity, Dump $dataDump): string
public function renderException(\Throwable $exception, Entity $entity, Dump $dataDump): Packet
{
$output = [];

Expand Down Expand Up @@ -247,23 +248,24 @@ protected function renderFooter(): string
/**
* Flatten buffer for final render
*/
protected function exportBuffer(array $buffer): string
protected function exportBuffer(array $buffer): Packet
{
return implode("\n\n", $buffer);
$output = implode("\n\n", $buffer);
return new Packet($output, 'text/plain');
}

/**
* Flatten dump buffer for final render
*/
protected function exportDumpBuffer(array $buffer): string
protected function exportDumpBuffer(array $buffer): Packet
{
return $this->exportBuffer($buffer);
}

/**
* Flatten dump buffer for final render
*/
protected function exportExceptionBuffer(array $buffer): string
protected function exportExceptionBuffer(array $buffer): Packet
{
return $this->exportBuffer($buffer);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Glitch/Renderer/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace DecodeLabs\Glitch\Renderer;

use DecodeLabs\Glitch\Context;
use DecodeLabs\Glitch\Packet;
use DecodeLabs\Glitch\Stack\Trace;
use DecodeLabs\Glitch\Stack\Frame;
use DecodeLabs\Glitch\Renderer;
Expand Down Expand Up @@ -122,9 +123,10 @@ protected function renderExceptionMessage(\Throwable $exception): string
/**
* Flatten buffer for final render
*/
protected function exportBuffer(array $buffer): string
protected function exportBuffer(array $buffer): Packet
{
return "\n".implode("\n\n", $buffer)."\n\n";
$output = "\n".implode("\n\n", $buffer)."\n\n";
return new Packet($output, 'text/plain');
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Glitch/Renderer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace DecodeLabs\Glitch\Renderer;

use DecodeLabs\Glitch\Context;
use DecodeLabs\Glitch\Packet;
use DecodeLabs\Glitch\Stack\Trace;
use DecodeLabs\Glitch\Stack\Frame;
use DecodeLabs\Glitch\Renderer;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function getCustomCssFile(): ?string
/**
* Convert Dump object to HTML string
*/
public function renderDump(Dump $dump): string
public function renderDump(Dump $dump): Packet
{
if (!$this->shouldRender()) {
return '';
Expand Down Expand Up @@ -137,7 +138,7 @@ public function renderDump(Dump $dump): string
/**
* Inspect handled exception
*/
public function renderException(\Throwable $exception, Entity $entity, Dump $dataDump): string
public function renderException(\Throwable $exception, Entity $entity, Dump $dataDump): Packet
{
$output = [];

Expand Down Expand Up @@ -534,7 +535,7 @@ protected function renderFooter(): string
/**
* Implode buffer and wrap it in JS iframe injector
*/
protected function exportBuffer(array $buffer): string
protected function exportBuffer(array $buffer): Packet
{
$html = implode("\n", $buffer);
$id = uniqid('glitch-dump');
Expand All @@ -554,7 +555,8 @@ protected function exportBuffer(array $buffer): string
$output[] = '</script>';
$output[] = '</div>';

return implode("\n", $output);
$output = implode("\n", $output);
return new Packet($output, 'text/html');
}


Expand Down
4 changes: 2 additions & 2 deletions src/Glitch/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

interface Transport
{
public function sendDump(string $packet, ?callable $headerBufferSender): void;
public function sendException(string $packet, ?callable $headerBufferSender): void;
public function sendDump(Packet $packet, bool $final): void;
public function sendException(Packet $packet, bool $final): void;
}
48 changes: 48 additions & 0 deletions src/Glitch/Transport/Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* This file is part of the Glitch package
* @license http://opensource.org/licenses/MIT
*/
declare(strict_types=1);
namespace DecodeLabs\Glitch\Transport;

use DecodeLabs\Glitch\Transport;
use DecodeLabs\Glitch\Packet;

class Http implements Transport
{
/**
* Send dump straight to output
*/
public function sendDump(Packet $packet, bool $final): void
{
$this->sendPacket($packet, $final);
}

/**
* Send exception dump straight to output
*/
public function sendException(Packet $packet, bool $final): void
{
$this->sendPacket($packet, $final);
}

/**
* Send packet
*/
protected function sendPacket(Packet $packet, bool $final): void
{
if ($final && !headers_sent()) {
header('HTTP/1.1 501');
header('Content-Type: '.$packet->getContentType().'; charset=UTF-8');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');

if ($headerBufferSender = Glitch::getHeaderBufferSender()) {
$headerBufferSender();
}
}

echo $packet->getBody();
}
}
20 changes: 10 additions & 10 deletions src/Glitch/Transport/Stdout.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@
namespace DecodeLabs\Glitch\Transport;

use DecodeLabs\Glitch\Transport;
use DecodeLabs\Glitch\Packet;

class Stdout implements Transport
{
/**
* Send dump straight to output
*/
public function sendDump(string $packet, ?callable $headerBufferSender): void
public function sendDump(Packet $packet, bool $final): void
{
$this->sendPacket($packet, $headerBufferSender);
$this->sendPacket($packet, $final);
}

/**
* Send exception dump straight to output
*/
public function sendException(string $packet, ?callable $headerBufferSender): void
public function sendException(Packet $packet, bool $final): void
{
$this->sendPacket($packet, $headerBufferSender);
$this->sendPacket($packet, $final);
}

protected function sendPacket(string $packet, ?callable $headerBufferSender): void
/**
* Send packet
*/
protected function sendPacket(Packet $packet, bool $final): void
{
if (!in_array(\PHP_SAPI, ['cli', 'phpdbg']) && !headers_sent() && $headerBufferSender) {
$headerBufferSender();
}

echo $packet;
echo $packet->getBody();
}
}

0 comments on commit 29d113b

Please sign in to comment.