This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SymfonyEventDispatcher] interoperability experiments
- Loading branch information
1 parent
2d78b12
commit 686897f
Showing
13 changed files
with
146 additions
and
58 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
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
14 changes: 14 additions & 0 deletions
14
packages/SymfonyEventDispatcher/packages/dependency-injection-utils/composer.json
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,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" | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...entDispatcher/packages/dependency-injection-utils/src/Adapter/Nette/DI/CollectorTrait.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,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]); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...her/packages/dependency-injection-utils/src/Adapter/Nette/DI/GetDefinitionByTypeTrait.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 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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ges/dependency-injection-utils/src/Adapter/Symfony/DependencyInjection/CollectorTrait.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,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)]); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ency-injection-utils/src/Adapter/Symfony/DependencyInjection/GetDefinitionByTypeTrait.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,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 | ||
)); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
packages/SymfonyEventDispatcher/src/Adapter/Nette/DI/test.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 declare(strict_types = 1); | ||
|
||
|
||
namespace Symplify\SymfonyEventDispatcher\Adapter\Nette\DI; | ||
|
||
class test | ||
{ | ||
|
||
} |
2 changes: 0 additions & 2 deletions
2
packages/SymfonyEventDispatcher/src/Adapter/Nette/config/services.neon
This file was deleted.
Oops, something went wrong.
38 changes: 10 additions & 28 deletions
38
...entDispatcher/src/Adapter/Symfony/DependencyInjection/Compiler/CollectSubscribersPass.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 |
---|---|---|
@@ -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' | ||
); | ||
} | ||
} |
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
File renamed without changes.