From 2ddd5217d3c84c59c5a2ddcc7f733e34c3d5ef2e Mon Sep 17 00:00:00 2001 From: Kevin Boyd Date: Wed, 6 Mar 2019 21:17:37 -0800 Subject: [PATCH] Fix BC Break with services tagged kernel.event_subscriber (#410) * Add a test to demonstrate a problem with custom bundle event subscribers * Add a new compiler pass to make kernel.event_subscriber services public * Apply code sniffer suggestion and add an exclude pattern. --- .gitignore | 1 + phpcs.xml | 1 + .../Compiler/EventSubscriberOverridePass.php | 35 +++++++++++++++++++ .../Bundle/SculpinBundle/SculpinBundle.php | 2 ++ .../Functional/EventListenerExtensionTest.php | 31 ++++++++++++++++ .../EventListenerTestFixtureExtension.php | 20 +++++++++++ .../EventListenerTestFixtureBundle.php | 9 +++++ .../Listener.php | 29 +++++++++++++++ .../Resources/config/services.xml | 15 ++++++++ .../app/SculpinKernel.php | 11 ++++++ .../app/config/sculpin_kernel.yml | 3 ++ .../app/config/sculpin_site.yml | 4 +++ .../source/_views/default.html | 6 ++++ .../__EventListenerFixture__/source/index.md | 5 +++ 14 files changed, 172 insertions(+) create mode 100644 src/Sculpin/Bundle/SculpinBundle/DependencyInjection/Compiler/EventSubscriberOverridePass.php create mode 100644 src/Sculpin/Tests/Functional/EventListenerExtensionTest.php create mode 100644 src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/DependencyInjection/EventListenerTestFixtureExtension.php create mode 100644 src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/EventListenerTestFixtureBundle.php create mode 100644 src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/Listener.php create mode 100644 src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/Resources/config/services.xml create mode 100644 src/Sculpin/Tests/Functional/__EventListenerFixture__/app/SculpinKernel.php create mode 100644 src/Sculpin/Tests/Functional/__EventListenerFixture__/app/config/sculpin_kernel.yml create mode 100644 src/Sculpin/Tests/Functional/__EventListenerFixture__/app/config/sculpin_site.yml create mode 100644 src/Sculpin/Tests/Functional/__EventListenerFixture__/source/_views/default.html create mode 100644 src/Sculpin/Tests/Functional/__EventListenerFixture__/source/index.md diff --git a/.gitignore b/.gitignore index 73225476..0afc2aa8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ vendor cache __BlankSculpinProject__/ __SculpinTestProject__/ +output_test/ clover.xml diff --git a/phpcs.xml b/phpcs.xml index f27ab45d..f99c7bcd 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -8,6 +8,7 @@ ./src/ */__BlankSculpinProject__/* + */__EventListenerFixture__/* diff --git a/src/Sculpin/Bundle/SculpinBundle/DependencyInjection/Compiler/EventSubscriberOverridePass.php b/src/Sculpin/Bundle/SculpinBundle/DependencyInjection/Compiler/EventSubscriberOverridePass.php new file mode 100644 index 00000000..40ae1ad2 --- /dev/null +++ b/src/Sculpin/Bundle/SculpinBundle/DependencyInjection/Compiler/EventSubscriberOverridePass.php @@ -0,0 +1,35 @@ +findTaggedServiceIds('kernel.event_subscriber'); + foreach ($services as $id => $tags) { + $definition = $container->getDefinition($id); + $definition->setPublic(true); + } + } +} diff --git a/src/Sculpin/Bundle/SculpinBundle/SculpinBundle.php b/src/Sculpin/Bundle/SculpinBundle/SculpinBundle.php index 04ea0dfb..9e7d5c50 100644 --- a/src/Sculpin/Bundle/SculpinBundle/SculpinBundle.php +++ b/src/Sculpin/Bundle/SculpinBundle/SculpinBundle.php @@ -16,6 +16,7 @@ use Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler\ConverterManagerPass; use Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler\DataSourcePass; use Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler\DataProviderManagerPass; +use Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler\EventSubscriberOverridePass; use Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler\FormatterManagerPass; use Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler\GeneratorManagerPass; use Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler\PathConfiguratorPass; @@ -48,5 +49,6 @@ public function build(ContainerBuilder $container): void $container->addCompilerPass(new WriterPass); $container->addCompilerPass(new AddConsoleCommandPass); $container->addCompilerPass(new DirectoryOverridePass); + $container->addCompilerPass(new EventSubscriberOverridePass); } } diff --git a/src/Sculpin/Tests/Functional/EventListenerExtensionTest.php b/src/Sculpin/Tests/Functional/EventListenerExtensionTest.php new file mode 100644 index 00000000..21ba81d4 --- /dev/null +++ b/src/Sculpin/Tests/Functional/EventListenerExtensionTest.php @@ -0,0 +1,31 @@ +projectDir() . '/output_test'; + if (static::$fs->exists($outputDir)) { + static::$fs->remove($outputDir); + } + } + + public function testEventListenerExtensionBundle(): void + { + $expectedFile = 'sculpin.core.after_run.event'; + + $this->assertProjectLacksFile('/output_test/' . $expectedFile); + + $this->executeSculpin('generate'); + + $this->assertProjectHasGeneratedFile('/' . $expectedFile); + } +} diff --git a/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/DependencyInjection/EventListenerTestFixtureExtension.php b/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/DependencyInjection/EventListenerTestFixtureExtension.php new file mode 100644 index 00000000..d499e6a0 --- /dev/null +++ b/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/DependencyInjection/EventListenerTestFixtureExtension.php @@ -0,0 +1,20 @@ +load('services.xml'); + } +} diff --git a/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/EventListenerTestFixtureBundle.php b/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/EventListenerTestFixtureBundle.php new file mode 100644 index 00000000..4fe0c0f5 --- /dev/null +++ b/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/EventListenerTestFixtureBundle.php @@ -0,0 +1,9 @@ +outputDir = $outputDir; + } + + public static function getSubscribedEvents(): array + { + return [ + Sculpin::EVENT_AFTER_RUN => 'createSuccessFile', + ]; + } + + public function createSuccessFile(SourceSetEvent $event, $eventName): void + { + file_put_contents($this->outputDir . '/' . $eventName . '.event', $eventName); + } +} diff --git a/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/Resources/config/services.xml b/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/Resources/config/services.xml new file mode 100644 index 00000000..5b107dfa --- /dev/null +++ b/src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/Resources/config/services.xml @@ -0,0 +1,15 @@ + + + + + + + %sculpin.output_dir% + + + + + + diff --git a/src/Sculpin/Tests/Functional/__EventListenerFixture__/app/SculpinKernel.php b/src/Sculpin/Tests/Functional/__EventListenerFixture__/app/SculpinKernel.php new file mode 100644 index 00000000..07459e55 --- /dev/null +++ b/src/Sculpin/Tests/Functional/__EventListenerFixture__/app/SculpinKernel.php @@ -0,0 +1,11 @@ + +{{site.title}} + +{% block content_wrapper %}{% block content '' %}{% endblock content_wrapper %} + + diff --git a/src/Sculpin/Tests/Functional/__EventListenerFixture__/source/index.md b/src/Sculpin/Tests/Functional/__EventListenerFixture__/source/index.md new file mode 100644 index 00000000..7662ead3 --- /dev/null +++ b/src/Sculpin/Tests/Functional/__EventListenerFixture__/source/index.md @@ -0,0 +1,5 @@ +--- +layout: default +--- + +

Welcome to {{site.title}}