Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[SymfonyEventDispatcher] interoperability experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 8, 2017
1 parent 2d78b12 commit 686897f
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 58 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@

"friendsofphp/php-cs-fixer": "^2.0",
"squizlabs/php_codesniffer": "^2.7",
"zenify/modular-latte-filters": "^4.2"
"zenify/modular-latte-filters": "^4.2",
"symplify/dependency-injection-utils": "@dev"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
Expand Down Expand Up @@ -100,6 +101,9 @@
"packages/Statie/bin/include-autoload.php"
]
},
"repositories": [
{ "type": "path", "url": "packages/SymfonyEventDispatcher/packages/dependency-injection-utils" }
],
"scripts": {
"complete-check": ["vendor/bin/phpunit", "@fix-cs", "@phpstan"],
"check-cs": "packages/CodingStandard/bin/symplify-cs check packages/*/src packages/*/tests",
Expand Down
6 changes: 5 additions & 1 deletion packages/SymfonyEventDispatcher/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"nette/di": "^2.4",
"nette/http": "^2.4",
"symfony/event-dispatcher": "^3.2"
"symfony/depe": "^3.2"
"symfony/depe": "^3.2",
"symplify/dependency-injection-utils": "@dev"
},
"require-dev": {
"kdyby/console": "^2.4",
Expand All @@ -28,6 +29,9 @@
"Symplify\\SymfonyEventDispatcher\\Tests\\": "tests"
}
},
"repositories": [
{ "type": "path", "url": "packages/dependency-injection-utils" }
],
"scripts": {
"check-cs": "vendor/bin/symplify-cs check src tests",
"fix-cs": "vendor/bin/symplify-cs fix src tests"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "symplify/dependency-injection-utils",
"require": {
"php": "^7.1",
"nette/di": "^2.4",
"nette/neon": "^2.4",
"symfony/dependency-injection": "^3.2"
},
"autoload": {
"psr-4": {
"Symplify\\DependencyInjectionUtils\\": "src"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Symplify\DependencyInjectionUtils\Adapter\Nette\DI;

use Nette\DI\ContainerBuilder;
use Nette\DI\ServiceDefinition;

/**
* @method ContainerBuilder getContainerBuilder()
*/
trait CollectorTrait
{
use GetDefinitionByTypeTrait;

public function loadCollectorWithType(string $collectorType, string $collectedType, string $setterMethod) : void
{
$collectorDefinition = $this->getDefinitionByType($collectorType);

$collectedDefinitions = $this->getContainerBuilder()
->findByType($collectedType);

foreach ($collectedDefinitions as $name => $definition) {
$collectorDefinition->addSetup($setterMethod, ['@' . $name]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Symplify\DependencyInjectionUtils\Adapter\Nette\DI;

use Nette\DI\ContainerBuilder;
use Nette\DI\ServiceDefinition;

/**
* @method ContainerBuilder getContainerBuilder()
*/
trait GetDefinitionByTypeTrait
{
public function getDefinitionByType(string $type) : ServiceDefinition
{
$containerBuilder = $this->getContainerBuilder();
$definitionName = $containerBuilder->getByType($type);

return $containerBuilder->getDefinition($definitionName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Symplify\DependencyInjectionUtils\Adapter\Symfony\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

trait CollectorTrait
{
// @todo: consider making class with ContainerBuilder in ctor instead of trait
use GetDefinitionByTypeTrait;

public function loadCollectorWithType(
ContainerBuilder $containerBuilder, string $collectorType, string $collectedType, string $setterMethod
) : void {
$collectorDefinition = $this->getDefinitionByType($containerBuilder, $collectorType);

foreach ($containerBuilder->getDefinitions() as $name => $definition) {
if (! is_subclass_of($definition->getClass(), $collectedType)) {
return;
}

$collectorDefinition->addMethodCall($setterMethod, [new Reference($name)]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Symplify\DependencyInjectionUtils\Adapter\Symfony\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

trait GetDefinitionByTypeTrait
{
public function getDefinitionByType(ContainerBuilder $containerBuilder, string $type) : Definition
{
foreach ($containerBuilder->getDefinitions() as $definition) {
if (is_a($definition->getClass(), $type, true)) {
return $definition;
}
}

throw new \Exception(sprintf(
'Definition for type %s not found', $type
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symplify\DependencyInjectionUtils\Adapter\Nette\DI\CollectorTrait;

final class SymfonyEventDispatcherExtension extends CompilerExtension
{
use CollectorTrait;

public function loadConfiguration() : void
{
if ($this->isKdybyEventsRegistered()) {
Expand All @@ -20,7 +23,7 @@ public function loadConfiguration() : void

Compiler::loadDefinitions(
$this->getContainerBuilder(),
$this->loadFromFile(__DIR__ . '/../config/services.neon')['services']
$this->loadFromFile(__DIR__ . '/../../../config/services.neon')['services']
);
}

Expand All @@ -34,8 +37,8 @@ public function beforeCompile() : void
}

$this->addSubscribersToEventDispatcher();
$this->bindNetteEvents();
$this->bindEventDispatcherToSymfonyConsole();
$this->bindNetteEvents();
}

private function isKdybyEventsRegistered() : bool
Expand All @@ -45,20 +48,7 @@ private function isKdybyEventsRegistered() : bool

private function addSubscribersToEventDispatcher() : void
{
$containerBuilder = $this->getContainerBuilder();
$eventDispatcher = $this->getDefinitionByType(EventDispatcherInterface::class);

foreach ($containerBuilder->findByType(EventSubscriberInterface::class) as $eventSubscriberDefinition) {
$eventDispatcher->addSetup('addSubscriber', ['@' . $eventSubscriberDefinition->getClass()]);
}
}

private function getDefinitionByType(string $type) : ServiceDefinition
{
$containerBuilder = $this->getContainerBuilder();
$definitionName = $containerBuilder->getByType($type);

return $containerBuilder->getDefinition($definitionName);
$this->loadCollectorWithType(EventDispatcherInterface::class, EventSubscriberInterface::class, 'addSubscriber');
}

private function bindNetteEvents() : void
Expand Down
9 changes: 9 additions & 0 deletions packages/SymfonyEventDispatcher/src/Adapter/Nette/DI/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types = 1);


namespace Symplify\SymfonyEventDispatcher\Adapter\Nette\DI;

class test
{

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
<?php

declare(strict_types = 1);

/*
* This file is part of Symplify
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
*/
<?php declare(strict_types = 1);

namespace Symplify\SymfonyEventDispatcher\Adapter\Symfony\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symplify\DependencyInjectionUtils\Adapter\Symfony\DependencyInjection\CollectorTrait;

final class CollectSubscribersPass implements CompilerPassInterface
{
/**
* @var ContainerBuilder
*/
private $containerBuilder;
use CollectorTrait;

public function process(ContainerBuilder $containerBuilder)
{
$this->containerBuilder = $containerBuilder;

$this->loadSubscribersToEventDispatcher();
}

private function loadSubscribersToEventDispatcher()
{
$eventDispatcherDefinition = $this->containerBuilder->findDefinition('symplify.event_dispatcher');
foreach ($this->containerBuilder->getDefinitions() as $name => $definition) {
if (! is_subclass_of($definition->getClass(), EventSubscriberInterface::class)) {
return;
}

$eventDispatcherDefinition->addMethodCall('addSubscriber', [new Reference($name)]);
}
$this->loadCollectorWithType(
$containerBuilder,
EventDispatcherInterface::class,
EventSubscriberInterface::class,
'addSubscriber'
);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php

declare(strict_types = 1);

/*
* This file is part of Symplify.
* Copyright (c) 2017 Tomas Votruba (http://tomasvotruba.cz).
*/
<?php declare(strict_types = 1);

namespace Symplify\SymfonyEventDispatcher\Adapter\Symfony\DependencyInjection\Extension;

Expand All @@ -18,7 +11,7 @@ final class ContainerExtension extends Extension
{
public function load(array $configs, ContainerBuilder $containerBuilder)
{
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../../Resources/config'));
$loader->load('services.yml');
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../../../../config'));
$loader->load('services.neon');
}
}

0 comments on commit 686897f

Please sign in to comment.