Skip to content

Commit

Permalink
configurable additional collapse paths for default BlueScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
VasekPurchart committed Aug 5, 2016
1 parent 582f8b6 commit 73a84e7
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 3 deletions.
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ tracy_blue_screen:

# Priority with which the listener will be registered.
listener_priority: 0

blue_screen:
# Add paths which should be collapsed (for external/compiled code) so that actual error is expanded.
collapse_paths:
# Defaults:
- %kernel.root_dir%/bootstrap.php.cache
- %kernel.cache_dir%
# plus paths set in BlueScreen instance used (/vendor)

```

You can also override services used internally, for example if you need to specify options for the BlueScreen instance, you can provide custom instance with an [alias](http://symfony.com/doc/current/components/dependency_injection/advanced.html#aliasing):
Expand Down
22 changes: 22 additions & 0 deletions src/BlueScreen/BlueScreenFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace VasekPurchart\TracyBlueScreenBundle\BlueScreen;

use Tracy\Debugger;

class BlueScreenFactory
{

/**
* @param string[] $collapsePaths
* @return \Tracy\BlueScreen
*/
public static function create(array $collapsePaths)
{
$blueScreen = Debugger::getBlueScreen();
$blueScreen->collapsePaths = array_merge($blueScreen->collapsePaths, $collapsePaths);

return $blueScreen;
}

}
33 changes: 32 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,47 @@
class Configuration implements \Symfony\Component\Config\Definition\ConfigurationInterface
{

const PARAMETER_COLLAPSE_PATHS = 'collapse_paths';
const PARAMETER_CONSOLE_BROWSER = 'browser';
const PARAMETER_CONSOLE_ENABLED = 'enabled';
const PARAMETER_CONSOLE_LISTENER_PRIORITY = 'listener_priority';
const PARAMETER_CONSOLE_LOG_DIRECTORY = 'log_directory';
const PARAMETER_CONTROLLER_ENABLED = 'enabled';
const PARAMETER_CONTROLLER_LISTENER_PRIORITY = 'listener_priority';

const SECTION_BLUE_SCREEN = 'blue_screen';
const SECTION_CONSOLE = 'console';
const SECTION_CONTROLLER = 'controller';

/** @var string */
private $rootNode;

/** @var string */
private $kernelRootDir;

/** @var string */
private $kernelLogsDir;

/** @var string */
private $kernelCacheDir;

/**
* @param string $rootNode
* @param string $kernelRootDir
* @param string $kernelLogsDir
* @param string $kernelCacheDir
*/
public function __construct($rootNode, $kernelLogsDir)
public function __construct(
$rootNode,
$kernelRootDir,
$kernelLogsDir,
$kernelCacheDir
)
{
$this->rootNode = $rootNode;
$this->kernelRootDir = $kernelRootDir;
$this->kernelLogsDir = $kernelLogsDir;
$this->kernelCacheDir = $kernelCacheDir;
}

/**
Expand Down Expand Up @@ -87,6 +104,20 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode(self::SECTION_BLUE_SCREEN)
->addDefaultsIfNotSet()
->children()
->arrayNode(self::PARAMETER_COLLAPSE_PATHS)
->info('Add paths which should be collapsed (for external/compiled code) so that actual error is expanded.')
->prototype('scalar')
->end()
->defaultValue([
$this->kernelRootDir . '/bootstrap.php.cache',
$this->kernelCacheDir,
])
->end()
->end()
->end()
->end()
->end();

Expand Down
9 changes: 8 additions & 1 deletion src/DependencyInjection/TracyBlueScreenExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class TracyBlueScreenExtension extends \Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension
{

const CONTAINER_PARAMETER_BLUE_SCREEN_COLLAPSE_PATHS = 'vasek_purchart.tracy_blue_screen.blue_screen.collapse_paths';
const CONTAINER_PARAMETER_CONSOLE_BROWSER = 'vasek_purchart.tracy_blue_screen.console.browser';
const CONTAINER_PARAMETER_CONSOLE_LISTENER_PRIORITY = 'vasek_purchart.tracy_blue_screen.console.listener_priority';
const CONTAINER_PARAMETER_CONSOLE_LOG_DIRECTORY = 'vasek_purchart.tracy_blue_screen.console.log_directory';
Expand All @@ -20,6 +21,10 @@ class TracyBlueScreenExtension extends \Symfony\Component\HttpKernel\DependencyI
*/
public function loadInternal(array $mergedConfig, ContainerBuilder $container)
{
$container->setParameter(
self::CONTAINER_PARAMETER_BLUE_SCREEN_COLLAPSE_PATHS,
$mergedConfig[Configuration::SECTION_BLUE_SCREEN][Configuration::PARAMETER_COLLAPSE_PATHS]
);
$container->setParameter(
self::CONTAINER_PARAMETER_CONSOLE_BROWSER,
$mergedConfig[Configuration::SECTION_CONSOLE][Configuration::PARAMETER_CONSOLE_BROWSER]
Expand Down Expand Up @@ -68,7 +73,9 @@ public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration(
$this->getAlias(),
$container->getParameter('kernel.logs_dir')
$container->getParameter('kernel.root_dir'),
$container->getParameter('kernel.logs_dir'),
$container->getParameter('kernel.cache_dir')
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/DependencyInjection/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ services:

vasek_purchart.tracy_blue_screen.tracy.blue_screen.default:
class: Tracy\BlueScreen
factory: [Tracy\Debugger, getBlueScreen]
factory: [VasekPurchart\TracyBlueScreenBundle\BlueScreen\BlueScreenFactory, create]
arguments:
- %vasek_purchart.tracy_blue_screen.blue_screen.collapse_paths%
public: false

vasek_purchart.tracy_blue_screen.tracy.logger: '@vasek_purchart.tracy_blue_screen.tracy.logger.default'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class TracyBlueScreenExtensionConsoleTest extends \Matthias\SymfonyDependencyInj
public function setUp()
{
parent::setUp();
$this->setParameter('kernel.root_dir', __DIR__);
$this->setParameter('kernel.logs_dir', __DIR__ . '/tests-logs-dir');
$this->setParameter('kernel.cache_dir', __DIR__ . '/tests-cache-dir');
$this->setParameter('kernel.environment', 'dev');
$this->setParameter('kernel.debug', true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class TracyBlueScreenExtensionControllerTest extends \Matthias\SymfonyDependency
public function setUp()
{
parent::setUp();
$this->setParameter('kernel.root_dir', __DIR__);
$this->setParameter('kernel.logs_dir', __DIR__);
$this->setParameter('kernel.cache_dir', __DIR__ . '/tests-cache-dir');
$this->setParameter('kernel.environment', 'dev');
$this->setParameter('kernel.debug', true);
}
Expand Down
111 changes: 111 additions & 0 deletions tests/DependencyInjection/TracyBlueScreenExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace VasekPurchart\TracyBlueScreenBundle\DependencyInjection;

use Tracy\BlueScreen;

class TracyBlueScreenExtensionTest extends \Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase
{

public function setUp()
{
parent::setUp();
$this->setParameter('kernel.root_dir', __DIR__);
$this->setParameter('kernel.logs_dir', __DIR__);
$this->setParameter('kernel.cache_dir', __DIR__ . '/tests-cache-dir');
$this->setParameter('kernel.environment', 'dev');
$this->setParameter('kernel.debug', true);
}

/**
* @return \Symfony\Component\DependencyInjection\Extension\ExtensionInterface[]
*/
protected function getContainerExtensions()
{
return [
new TracyBlueScreenExtension(),
];
}

public function testOnlyAddCollapsePaths()
{
$this->load();

$this->assertContainerBuilderHasService('vasek_purchart.tracy_blue_screen.tracy.blue_screen.default', BlueScreen::class);

$blueScreen = $this->container->get('vasek_purchart.tracy_blue_screen.tracy.blue_screen.default');
$collapsePaths = $blueScreen->collapsePaths;

$this->assertArrayContainsStringPart('/bootstrap.php.cache', $collapsePaths);
$this->assertArrayContainsStringPart('/tests-cache-dir', $collapsePaths);
$this->assertArrayContainsStringPart('/vendor', $collapsePaths);

$this->compile();
}

public function testCollapseCacheDirsByDefault()
{
$this->load();

$this->assertContainerBuilderHasParameter('vasek_purchart.tracy_blue_screen.blue_screen.collapse_paths');
$collapsePaths = $this->container->getParameter('vasek_purchart.tracy_blue_screen.blue_screen.collapse_paths');

$this->assertArrayContainsStringPart('/bootstrap.php.cache', $collapsePaths);
$this->assertArrayContainsStringPart('/tests-cache-dir', $collapsePaths);

$this->compile();
}

public function testSetCollapseDirs()
{
$paths = [
__DIR__ . '/foobar',
];

$this->load([
'blue_screen' => [
'collapse_paths' => $paths,
],
]);

$this->assertContainerBuilderHasParameter('vasek_purchart.tracy_blue_screen.blue_screen.collapse_paths');
$collapsePaths = $this->container->getParameter('vasek_purchart.tracy_blue_screen.blue_screen.collapse_paths');

$this->assertEquals($paths, $collapsePaths);

$this->compile();
}

public function testEmptyCollapseDirs()
{
$this->load([
'blue_screen' => [
'collapse_paths' => [],
],
]);

$this->assertContainerBuilderHasParameter('vasek_purchart.tracy_blue_screen.blue_screen.collapse_paths');
$collapsePaths = $this->container->getParameter('vasek_purchart.tracy_blue_screen.blue_screen.collapse_paths');

$this->assertEmpty($collapsePaths);

$this->compile();
}

/**
* @param string $string
* @param string[] $array
*/
private function assertArrayContainsStringPart($string, array $array)
{
$found = false;
foreach ($array as $item) {
if (strpos($item, $string) !== false) {
$found = true;
break;
}
}
$this->assertTrue($found, sprintf('%s not found in any elements of the given %s', $string, var_export($array, true)));
}

}

0 comments on commit 73a84e7

Please sign in to comment.