Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
14 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ vendor | |
cache | ||
__BlankSculpinProject__/ | ||
__SculpinTestProject__/ | ||
output_test/ | ||
clover.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...Sculpin/Bundle/SculpinBundle/DependencyInjection/Compiler/EventSubscriberOverridePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is a part of Sculpin. | ||
* | ||
* (c) Dragonfly Development Inc. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Override default visibility of services tagged with kernel.event_subscriber | ||
*/ | ||
class EventSubscriberOverridePass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$services = $container->findTaggedServiceIds('kernel.event_subscriber'); | ||
foreach ($services as $id => $tags) { | ||
$definition = $container->getDefinition($id); | ||
$definition->setPublic(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Sculpin/Tests/Functional/EventListenerExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sculpin\Tests\Functional; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
|
||
class EventListenerExtensionTest extends FunctionalTestCase | ||
{ | ||
protected const PROJECT_DIR = '/__EventListenerFixture__'; | ||
|
||
public function setUp(): void | ||
{ | ||
$outputDir = $this->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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
.../EventListenerTestFixtureBundle/DependencyInjection/EventListenerTestFixtureExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Sculpin\Tests\Functional\EventListenerTestFixtureBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
class EventListenerTestFixtureExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.xml'); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...culpin/Tests/Functional/EventListenerTestFixtureBundle/EventListenerTestFixtureBundle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Sculpin\Tests\Functional\EventListenerTestFixtureBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class EventListenerTestFixtureBundle extends Bundle | ||
{ | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/Listener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Sculpin\Tests\Functional\EventListenerTestFixtureBundle; | ||
|
||
use Sculpin\Core\Event\SourceSetEvent; | ||
use Sculpin\Core\Sculpin; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
class Listener implements EventSubscriberInterface | ||
{ | ||
protected $outputDir; | ||
|
||
public function __construct($outputDir) | ||
{ | ||
$this->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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Sculpin/Tests/Functional/EventListenerTestFixtureBundle/Resources/config/services.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="sculpin.tests.event_listener_test_fixture" class="\Sculpin\Tests\Functional\EventListenerTestFixtureBundle\Listener"> | ||
<argument>%sculpin.output_dir%</argument> | ||
<tag name="kernel.event_subscriber" /> | ||
</service> | ||
|
||
</services> | ||
|
||
</container> |
11 changes: 11 additions & 0 deletions
11
src/Sculpin/Tests/Functional/__EventListenerFixture__/app/SculpinKernel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
class SculpinKernel extends \Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel | ||
{ | ||
protected function getAdditionalSculpinBundles(): array | ||
{ | ||
return [ | ||
\Sculpin\Tests\Functional\EventListenerTestFixtureBundle\EventListenerTestFixtureBundle::class, | ||
]; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Sculpin/Tests/Functional/__EventListenerFixture__/app/config/sculpin_kernel.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
sculpin_content_types: | ||
posts: | ||
enabled: false |
4 changes: 4 additions & 0 deletions
4
src/Sculpin/Tests/Functional/__EventListenerFixture__/app/config/sculpin_site.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
title: "My Custom Title" | ||
subtitle: "Custom Subtitle" | ||
google_analytics_tracking_id: '' | ||
url: '' |
6 changes: 6 additions & 0 deletions
6
src/Sculpin/Tests/Functional/__EventListenerFixture__/source/_views/default.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<head><title>{{site.title}}</title></head> | ||
<body> | ||
{% block content_wrapper %}{% block content '' %}{% endblock content_wrapper %} | ||
</body> | ||
</html> |
5 changes: 5 additions & 0 deletions
5
src/Sculpin/Tests/Functional/__EventListenerFixture__/source/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
layout: default | ||
--- | ||
|
||
<h1>Welcome to {{site.title}}</h1> |