Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

Commit

Permalink
Fix panels
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtak-CZ committed Jan 18, 2016
1 parent a1e45d1 commit 6bc7944
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function addPanels(ArrayNodeDefinition $rootNode)
->fixXmlConfig('panel')
->children()
->arrayNode(static::PANELS)
->prototype('scalar')
->prototype('variable')
->end()
->end();
}
Expand Down
29 changes: 29 additions & 0 deletions src/DependencyInjection/InvalidPanelException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* This file is part of the Nella Project (https://monolog-tracy.nella.io).
*
* Copyright (c) 2014 Pavel Kučera (http://github.com/pavelkucera)
* Copyright (c) Patrik Votoček (https://patrik.votocek.cz)
*
* For the full copyright and license information,
* please view the file LICENSE.md that was distributed with this source code.
*/

namespace Nella\MonologTracyBundle\DependencyInjection;

class InvalidPanelException extends \RuntimeException implements \Nella\MonologTracyBundle\DependencyInjection\Exception
{

/**
* @param string|mixed[] $panel
* @param \Exception|NULL $previous
*/
public function __construct($panel, \Exception $previous = NULL)
{
if (!is_string($panel)) {
$panel = var_export($panel, TRUE);
}
parent::__construct(sprintf('This `%s` is not valid panel.', $panel), 0, $previous);
}

}
26 changes: 25 additions & 1 deletion src/DependencyInjection/MonologTracyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;

class MonologTracyExtension extends \Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension implements \Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface
{
Expand Down Expand Up @@ -237,7 +238,30 @@ private function processInfoItems(Definition $definition, array $infoItems)
private function processPanels(Definition $definition, array $panels)
{
foreach ($panels as $panel) {
$definition->addMethodCall('registerPanel', [$panel]);
if (is_string($panel) && strncmp($panel, '@', 1) === 0) {
$callback = new Reference(substr($panel, 1));
} elseif (is_string($panel) && strpos($panel, '::') !== FALSE) {
$callback = $panel;
} elseif (is_array($panel) && count($panel) === 2) {
list($resource, $method) = $panel;
if (is_string($resource) && strncmp($resource, '@', 1) === 0) {
$callback = [
new Reference(substr($resource, 1)),
$method,
];
} elseif (is_string($resource)) {
$callback = [
$resource,
$method,
];
}
}

if (!isset($callback)) {
throw new \Nella\MonologTracyBundle\DependencyInjection\InvalidPanelException($panel);
}

$definition->addMethodCall('registerPanel', [$callback]);
}
}

Expand Down
64 changes: 63 additions & 1 deletion tests/DependencyInjection/MonologTracyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;

class MonologTracyExtensionTest extends \Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase
{
Expand Down Expand Up @@ -174,13 +175,74 @@ public function testPanels()
MonologTracyExtension::BLUESCREEN_FACTORY_SERVICE_ID,
'registerPanel',
[
'@nella.monolog_tracy.panel.test_panel',
new Reference('nella.monolog_tracy.panel.test_panel'),
]
);

$this->compile();
}

public function testPanelsArrayWithService()
{
$this->load([], [
'sectionPanelsArrayWithService.yml',
]);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
MonologTracyExtension::BLUESCREEN_FACTORY_SERVICE_ID,
'registerPanel',
[
[new Reference('nella.monolog_tracy.panel.test_panel'), '__invoke']
]
);

$this->compile();
}

public function testPanelsArray()
{
$this->load([], [
'sectionPanelsArray.yml',
]);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
MonologTracyExtension::BLUESCREEN_FACTORY_SERVICE_ID,
'registerPanel',
[
['Nella\MonologTracyBundle\Panel\TestPanel', 'invoke']
]
);

$this->compile();
}

public function testPanelsString()
{
$this->load([], [
'sectionPanelsString.yml',
]);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
MonologTracyExtension::BLUESCREEN_FACTORY_SERVICE_ID,
'registerPanel',
[
'Nella\MonologTracyBundle\Panel\TestPanel::invoke'
]
);

$this->compile();
}

/**
* @expectedException \Nella\MonologTracyBundle\DependencyInjection\InvalidPanelException
*/
public function testPanelsInvalid()
{
$this->load([], [
'sectionPanelsInvalid.yml',
]);
}

public function testCollapsePaths()
{
try {
Expand Down
3 changes: 3 additions & 0 deletions tests/DependencyInjection/fixtures/sectionPanelsArray.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
monolog_tracy:
panels:
- [Nella\MonologTracyBundle\Panel\TestPanel, invoke]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
nella.monolog_tracy.panel.test_panel:
class: Nella\MonologTracyBundle\Panel\TestPanel

monolog_tracy:
panels:
- ['@nella.monolog_tracy.panel.test_panel', __invoke]
3 changes: 3 additions & 0 deletions tests/DependencyInjection/fixtures/sectionPanelsInvalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
monolog_tracy:
panels:
- invalid
3 changes: 3 additions & 0 deletions tests/DependencyInjection/fixtures/sectionPanelsString.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
monolog_tracy:
panels:
- Nella\MonologTracyBundle\Panel\TestPanel::invoke
10 changes: 10 additions & 0 deletions tests/Panel/TestPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ public function getPanel($exception)
return Dumper::toHtml($exception);
}

/**
* @param \Exception|\Throwable $exception
* @return string[]
*/
public static function invoke($exception)
{
$instance = new static();
return $instance->__invoke($exception);
}

}

0 comments on commit 6bc7944

Please sign in to comment.