Skip to content
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
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ use Awesomite\ErrorDumper\Handlers\ErrorHandler;
use Awesomite\ErrorDumper\Listeners\OnExceptionCallable;
use Awesomite\ErrorDumper\Listeners\OnExceptionDevView;
use Awesomite\ErrorDumper\Views\ViewFactory;
use Awesomite\ErrorDumper\Views\ViewHtml;
use Awesomite\ErrorDumper\Views\ViewCli;

/**
* Create new error handler.
Expand All @@ -56,9 +58,12 @@ $handler = new ErrorHandler(/* optional $mode = null */);
/**
* Create and push new error listener,
* this handler will print programmer-friendly stack trace.
*
* You can use ViewCli for displaying error in cli mode.
* ViewFactory::create() creates ViewCli or ViewHtml depending on php_sapi_name().
*/
$devViewListener = new OnExceptionDevView(ViewFactory::create());
$handler->pushListener($devViewListener);
$htmlView = new OnExceptionDevView(new ViewHtml());
$handler->pushListener($htmlView);

/**
* Create and push new custom error listener.
Expand Down Expand Up @@ -112,8 +117,8 @@ To run example in terminal, execute `bin/test.php`.

## Content Security Policy

This library uses *.js files hosted on `maxcdn.bootstrapcdn.com` and `code.jquery.com`
(`@see \Awesomite\ErrorDumper\Views\ViewHtml::getResources`).
This library uses *.js files hosted on `maxcdn.bootstrapcdn.com`, `code.jquery.com`
and `cdnjs.cloudflare.com` (`@see \Awesomite\ErrorDumper\Views\ViewHtml::getResources`).
Add those domains to your `Content-Security-Policy` header during display errors.

## Symfony integration
Expand Down
13 changes: 12 additions & 1 deletion bin/scripts/exceptionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
* file that was distributed with this source code.
*/

use Awesomite\ErrorDumper\Serializable\ContextVarsFactory;
use Awesomite\ErrorDumper\Serializable\SerializableException;
use Awesomite\ErrorDumper\Views\ViewHtml;
use Awesomite\StackTrace\StackTraceFactory;
use Awesomite\VarDumper\LightVarDumper;

/**
* @internal
Expand Down Expand Up @@ -56,6 +59,14 @@ public function execute($callable, $arguments)
$executor->execute(array(new Calculator(), 'divide'), array(5, 0));
} catch (\Exception $exception) {
$view = new ViewHtml();
$view->display(new SerializableException($exception));

$varDumper = new LightVarDumper();
$varDumper
->setMaxChildren(100)
->setMaxDepth(100);
$stackTraceFactory = new StackTraceFactory($varDumper);
$contextFactory = new ContextVarsFactory($varDumper);

$view->display(new SerializableException($exception, 0, false, true, true, $stackTraceFactory, $contextFactory));
exit;
}
7 changes: 5 additions & 2 deletions bin/scripts/symfonyVarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Awesomite\ErrorDumper\Handlers\ErrorHandler;
use Awesomite\ErrorDumper\Listeners\OnExceptionCallable;
use Awesomite\ErrorDumper\Serializable\ContextVarsFactory;
use Awesomite\ErrorDumper\Serializable\SerializableException;
use Awesomite\ErrorDumper\Views\ViewHtml;
use Awesomite\StackTrace\StackTraceFactory;
Expand All @@ -20,8 +21,10 @@
$handler = new ErrorHandler();

$handler->pushListener(new OnExceptionCallable(function ($exception) {
$stackTraceFactory = new StackTraceFactory(new SymfonyVarDumper(new CliDumper()));
$serializable = new SerializableException($exception, 0, false, true, true, $stackTraceFactory);
$varDumper = new SymfonyVarDumper(new CliDumper());
$stackTraceFactory = new StackTraceFactory($varDumper);
$contextVarsFactory = new ContextVarsFactory($varDumper);
$serializable = new SerializableException($exception, 0, false, true, true, $stackTraceFactory, $contextVarsFactory);

$view = new ViewHtml();
$view->display($serializable);
Expand Down
22 changes: 22 additions & 0 deletions optimizer/OptimizerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ private function optimize($input)
$output = $this->removeWhiteSpacesBetween('}}', '{%', $output);
$output = $this->removeWhiteSpacesBetween('"', '>', $output);
$output = $this->removeWhiteSpacesBetween('"', '/>', $output);
$output = $this->removeWhiteSpacesBetween('}}()', '{%', $output);
$output = $this->removeWhiteSpacesBetween('>', '#{{', $output);

// '<script src' => '<script src'
$output = \preg_replace_callback(
Expand All @@ -71,6 +73,26 @@ function ($match) {
$output
);

$output = \preg_replace_callback(
'/(?<start>\<script.*?\>)(?<script>.*?)\<\/script\>/s',
function ($match) {
if ('' === $match['script']) {
return $match[0];
}

$parts = \array_map(
function ($part) {
return \preg_replace('/\s/', '', $part);
},
\explode('var ', $match['script'])
);
$script = \implode('var ', $parts);

return $match['start'] . $script . '</script>';
},
$output
);

// "{{ ' ' }}" => ' '
$output = \preg_replace_callback(
'/\{\{\s*\'(?<spaces>\s+)\'\s*\}\}/',
Expand Down
36 changes: 20 additions & 16 deletions src/Views/ViewHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

class ViewHtml implements ViewInterface
{
const TAG_HTML = '<!-- @ErrorDumper -->';
const TAG_UNDER_TITLE = '<!-- @ErrorDumper under title -->';
const TAG_HTML = '<!-- @ErrorDumper -->';
const TAG_UNDER_MENU = '<!-- @ErrorDumper under menu -->';

private static $headers
= array(
Expand All @@ -31,7 +31,7 @@ class ViewHtml implements ViewInterface
*/
private $editor;

private $contentUnderTitle;
private $contentUnderMenu;

private $cacheDirectory;

Expand Down Expand Up @@ -77,19 +77,19 @@ public function display(SerializableExceptionInterface $exception)
'resources' => $this->getResources(),
'editor' => $this->editor,
'hasEditor' => !\is_null($this->editor),
'contentUnderTitle' => $this->contentUnderTitle,
'contentUnderMenu' => $this->contentUnderMenu,
'appendToBody' => $this->appendToBody,
));
}

/**
* @param object|string $string
* @param object|string $stringable
*
* @return $this
*/
public function setContentUnderTitle($string)
public function setContentUnderMenu($stringable)
{
$this->contentUnderTitle = $string;
$this->contentUnderMenu = $stringable;

return $this;
}
Expand All @@ -109,13 +109,13 @@ public function setEditor(EditorInterface $editor = null)
}

/**
* @param object|string $string
* @param object|string $stringable
*
* @return $this
*/
public function appendToBody($string)
public function appendToBody($stringable)
{
$this->appendToBody[] = $string;
$this->appendToBody[] = $stringable;

return $this;
}
Expand Down Expand Up @@ -198,18 +198,22 @@ private function getResources()
return array(
'css' => array(
array(
'link' => '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css',
'integrity' => 'sha384-pdapHxIh7EYuwy6K7iE41uXVxGCXY0sAjBzaElYGJUrzwodck3Lx6IE2lA0rFREo',
'link' => 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css',
'integrity' => 'sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm',
),
),
'js' => array(
array(
'link' => '//code.jquery.com/jquery-2.1.4.min.js',
'integrity' => 'sha384-R4/ztc4ZlRqWjqIuvf6RX5yb/v90qNGx6fS48N0tRxiGkqveZETq72KgDVJCp2TC',
'link' => 'https://code.jquery.com/jquery-3.2.1.slim.min.js',
'integrity' => 'sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN',
),
array(
'link' => '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js',
'integrity' => 'sha384-pPttEvTHTuUJ9L2kCoMnNqCRcaMPMVMsWVO+RLaaaYDmfSP5//dP6eKRusbPcqhZ',
'link' => 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js',
'integrity' => 'sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q',
),
array(
'link' => 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js',
'integrity' => 'sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl',
),
),
);
Expand Down
27 changes: 0 additions & 27 deletions templates/exception-context.twig

This file was deleted.

19 changes: 19 additions & 0 deletions templates/exception-context_vars.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h2>Context variables</h2>

<div>
{% for var in exception.getContext %}
{% set showFull = loop.index0 < 10 %}
{% set varId = 'context-variable-' ~ loop.index %}

<div class="card">
<div class="card-header">
<a data-toggle="collapse" class="var-header" data-target="#{{ varId }}">{{ var.getName }}</a>
</div>
<div id="{{ varId }}" class="collapse max-height{{ showFull ? ' show' }}">
<div class="card-body">
<pre>{{ var.dumpAsString }}</pre>
</div>
</div>
</div>
{% endfor %}
</div>
Loading