Skip to content

Commit

Permalink
Fix BC Break with services tagged kernel.event_subscriber (#410)
Browse files Browse the repository at this point in the history
* 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
beryllium committed Mar 7, 2019
1 parent d77c590 commit 2ddd521
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,4 +2,5 @@ vendor
cache
__BlankSculpinProject__/
__SculpinTestProject__/
output_test/
clover.xml
1 change: 1 addition & 0 deletions phpcs.xml
Expand Up @@ -8,6 +8,7 @@
<file>./src/</file>

<exclude-pattern>*/__BlankSculpinProject__/*</exclude-pattern>
<exclude-pattern>*/__EventListenerFixture__/*</exclude-pattern>

<rule ref="PSR2"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
Expand Down
@@ -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);
}
}
}
2 changes: 2 additions & 0 deletions src/Sculpin/Bundle/SculpinBundle/SculpinBundle.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
31 changes: 31 additions & 0 deletions src/Sculpin/Tests/Functional/EventListenerExtensionTest.php
@@ -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);
}
}
@@ -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');
}
}
@@ -0,0 +1,9 @@
<?php

namespace Sculpin\Tests\Functional\EventListenerTestFixtureBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class EventListenerTestFixtureBundle extends Bundle
{
}
@@ -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);
}
}
@@ -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>
@@ -0,0 +1,11 @@
<?php

class SculpinKernel extends \Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel
{
protected function getAdditionalSculpinBundles(): array
{
return [
\Sculpin\Tests\Functional\EventListenerTestFixtureBundle\EventListenerTestFixtureBundle::class,
];
}
}
@@ -0,0 +1,3 @@
sculpin_content_types:
posts:
enabled: false
@@ -0,0 +1,4 @@
title: "My Custom Title"
subtitle: "Custom Subtitle"
google_analytics_tracking_id: ''
url: ''
@@ -0,0 +1,6 @@
<html>
<head><title>{{site.title}}</title></head>
<body>
{% block content_wrapper %}{% block content '' %}{% endblock content_wrapper %}
</body>
</html>
@@ -0,0 +1,5 @@
---
layout: default
---

<h1>Welcome to {{site.title}}</h1>

0 comments on commit 2ddd521

Please sign in to comment.