diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 4ac933c..c081f80 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -91,7 +91,7 @@ private function addPanels(ArrayNodeDefinition $rootNode) ->fixXmlConfig('panel') ->children() ->arrayNode(static::PANELS) - ->prototype('scalar') + ->prototype('variable') ->end() ->end(); } diff --git a/src/DependencyInjection/InvalidPanelException.php b/src/DependencyInjection/InvalidPanelException.php new file mode 100644 index 0000000..1541541 --- /dev/null +++ b/src/DependencyInjection/InvalidPanelException.php @@ -0,0 +1,29 @@ +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]); } } diff --git a/tests/DependencyInjection/MonologTracyExtensionTest.php b/tests/DependencyInjection/MonologTracyExtensionTest.php index 6af03a7..64043fc 100644 --- a/tests/DependencyInjection/MonologTracyExtensionTest.php +++ b/tests/DependencyInjection/MonologTracyExtensionTest.php @@ -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 { @@ -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 { diff --git a/tests/DependencyInjection/fixtures/sectionPanelsArray.yml b/tests/DependencyInjection/fixtures/sectionPanelsArray.yml new file mode 100644 index 0000000..73491ad --- /dev/null +++ b/tests/DependencyInjection/fixtures/sectionPanelsArray.yml @@ -0,0 +1,3 @@ +monolog_tracy: + panels: + - [Nella\MonologTracyBundle\Panel\TestPanel, invoke] diff --git a/tests/DependencyInjection/fixtures/sectionPanelsArrayWithService.yml b/tests/DependencyInjection/fixtures/sectionPanelsArrayWithService.yml new file mode 100644 index 0000000..ea6d6a1 --- /dev/null +++ b/tests/DependencyInjection/fixtures/sectionPanelsArrayWithService.yml @@ -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] diff --git a/tests/DependencyInjection/fixtures/sectionPanelsInvalid.yml b/tests/DependencyInjection/fixtures/sectionPanelsInvalid.yml new file mode 100644 index 0000000..2bd28d4 --- /dev/null +++ b/tests/DependencyInjection/fixtures/sectionPanelsInvalid.yml @@ -0,0 +1,3 @@ +monolog_tracy: + panels: + - invalid diff --git a/tests/DependencyInjection/fixtures/sectionPanelsString.yml b/tests/DependencyInjection/fixtures/sectionPanelsString.yml new file mode 100644 index 0000000..10d7e9f --- /dev/null +++ b/tests/DependencyInjection/fixtures/sectionPanelsString.yml @@ -0,0 +1,3 @@ +monolog_tracy: + panels: + - Nella\MonologTracyBundle\Panel\TestPanel::invoke diff --git a/tests/Panel/TestPanel.php b/tests/Panel/TestPanel.php index 976ce6a..6fb917a 100644 --- a/tests/Panel/TestPanel.php +++ b/tests/Panel/TestPanel.php @@ -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); + } + }