Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

wip #70

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
Expand Down Expand Up @@ -81,6 +82,20 @@ public function getCommands()
return $commands;
}

public function registerCompilers()
{
$configuration = new Configuration();
$configuration->setEvaluateExpressions(true);

return [
[new ValidateServiceDefinitionsPass($configuration), PassConfig::TYPE_AFTER_REMOVING, true],
[new FixValidatorDefinitionPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, true],
[new RegisterListenersPass(Dispatchers::READ, Dispatchers::READ_LISTENER, Dispatchers::READ_SUBSCRIBER), PassConfig::TYPE_BEFORE_REMOVING, false],
[new RegisterListenersPass(Dispatchers::WRITE, Dispatchers::WRITE_LISTENER, Dispatchers::WRITE_SUBSCRIBER), PassConfig::TYPE_BEFORE_REMOVING, false],
[new DecorateRegisterListenerPass(), PassConfig::TYPE_BEFORE_REMOVING, false],
];
}

protected function prepareContainer(ContainerBuilder $container)
{
$extensions = array();
Expand All @@ -98,23 +113,21 @@ protected function prepareContainer(ContainerBuilder $container)
$bundle->build($container);
}

$this->buildBundleless($container);
$this->hookCompilers($container);

// ensure these extensions are implicitly loaded
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
}

private function buildBundleless(ContainerBuilder $container)
private function hookCompilers(ContainerBuilder $container)
{
if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new FixValidatorDefinitionPass());

$configuration = new Configuration();
$configuration->setEvaluateExpressions(true);
$container->addCompilerPass(
new ValidateServiceDefinitionsPass($configuration),
PassConfig::TYPE_AFTER_REMOVING
);
foreach ($this->registerCompilers() as $compilerRow) {
$instance = $compilerRow[0];
$type = $compilerRow[1];
$debug = $compilerRow[2];
if (!$debug || $container->getParameter('kernel.debug')) {
$container->addCompilerPass($instance, $type);
}
}
}
}
14 changes: 14 additions & 0 deletions src/Dispatchers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Grace;

class Dispatchers
{
const READ = 'read_event_dispatcher';
const READ_LISTENER = 'read_event_dispatcher_listener';
const READ_SUBSCRIBER = 'read_event_dispatcher_subscriber';

const WRITE = 'write_event_dispatcher';
const WRITE_LISTENER = 'write_event_dispatcher_listener';
const WRITE_SUBSCRIBER = 'write_event_dispatcher_subscriber';
}
15 changes: 15 additions & 0 deletions src/ExampleSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Grace;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ExampleSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [

];
}
}
5 changes: 5 additions & 0 deletions src/Resources/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ services:
- @grace.container
- %grace.from%
- @swiftmailer.mailer
grace.some_domain_event_subscriber:
class: Grace\ExampleSubscriber
tags:
-
name: Grace\Dispatchers::READ_SUBSCRIBER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know we could use constants in YAML configuration, is this new?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does not give any error though but will change to be an xml so it can work, i think @stof is better suited for these types of questions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no such thing in YAML. this creates a tag named Grace\Dispatchers::READ_SUBSCRIBER, which will not be processed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and switching to XML will not allow you to use PHP constants in tag names either. Constants can be used only in parameter and argument values

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnugat see? he was more suited, i did not even know that even xml was like this. In any case just switching to the string name is good enough. the compiler pass can be adapted to deal with this nuance.