-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DependencyInjection] New Dependency Injection component
- Loading branch information
Showing
52 changed files
with
383 additions
and
108 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
76 changes: 76 additions & 0 deletions
76
packages/dependency-injection/Integration/ExtendableExtensionTrait.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,76 @@ | ||
<?php | ||
|
||
namespace Draw\Component\DependencyInjection\Integration; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; | ||
|
||
trait ExtendableExtensionTrait | ||
{ | ||
/** | ||
* @var array<IntegrationInterface> | ||
*/ | ||
private array $integrations = []; | ||
|
||
abstract private function provideExtensionClasses(): array; | ||
|
||
private function registerDefaultIntegrations(): void | ||
{ | ||
foreach ($this->provideExtensionClasses() as $extensionClass) { | ||
if (class_exists($extensionClass)) { | ||
$integration = new $extensionClass(); | ||
|
||
if (!$integration instanceof IntegrationInterface) { | ||
throw new \RuntimeException(sprintf('The class "%s" must implement "%s".', $extensionClass, IntegrationInterface::class)); | ||
} | ||
|
||
$this->integrations[] = $integration; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return array Parsed configuration | ||
*/ | ||
private function loadIntegrations(array $configs, ContainerBuilder $container): array | ||
{ | ||
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); | ||
|
||
foreach ($this->integrations as $integration) { | ||
$container->addObjectResource($integration); | ||
} | ||
|
||
$loader = new PhpFileLoader($container, new FileLocator([])); | ||
|
||
foreach ($this->integrations as $integration) { | ||
if ($this->isConfigEnabled($container, $config[$integration->getConfigSectionName()])) { | ||
$integration->load($config[$integration->getConfigSectionName()], $loader, $container); | ||
} | ||
} | ||
|
||
return $config; | ||
} | ||
|
||
private function prependIntegrations(ContainerBuilder $container, string $mainExtension): void | ||
{ | ||
$configs = $container->getExtensionConfig($mainExtension); | ||
|
||
$config = $this->processConfiguration( | ||
$this->getConfiguration($configs, $container), | ||
$container->getParameterBag()->resolveValue($configs) | ||
); | ||
|
||
foreach ($this->integrations as $integration) { | ||
if (!$integration instanceof PrependIntegrationInterface) { | ||
continue; | ||
} | ||
|
||
$integrationConfiguration = $config[$integration->getConfigSectionName()]; | ||
|
||
if ($this->isConfigEnabled($container, $integrationConfiguration)) { | ||
$integration->prepend($container, $integrationConfiguration); | ||
} | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tion/Integration/IntegrationInterface.php → ...tion/Integration/IntegrationInterface.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
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
2 changes: 1 addition & 1 deletion
2
...tegration/PrependIntegrationInterface.php → ...tegration/PrependIntegrationInterface.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Dependency Injection | ||
|
||
This package provides addons to the Symfony Dependency Injection component. | ||
|
||
## Installation | ||
|
||
``` | ||
composer require draw/dependency-injection | ||
``` | ||
|
||
## Integration | ||
|
||
The `Draw\Component\DependencyInjection\Integration` namespace contains classes that can be used to easily integrate | ||
subcomponents into a main bundle. | ||
|
||
An example of this is all the draw components that are integrated into the `DrawFrameworkExtraBundle`. | ||
|
||
When creating the main bundle extension you can use the `IntegrationTrait` to easily integrate all the subcomponents. | ||
|
||
```php | ||
|
||
namespace Example\Bundle\MyBundle\DependencyInjection; | ||
|
||
use Draw\Component\DependencyInjection\IntegrationTrait; | ||
use Example\Component\MyComponent\DependencyInjection\MyCompnentIntegration; | ||
use Example\Component\MyOtherComponent\DependencyInjection\MyOtherComponentIntegration; | ||
|
||
class ExampleMyBundle extends Bundle | ||
{ | ||
use ExtendableExtensionTrait; | ||
|
||
public function __construct() | ||
{ | ||
$this->registerDefaultIntegrations(); | ||
} | ||
|
||
private function provideExtensionClasses(): array | ||
{ | ||
return [ | ||
MyCompnentIntegration::class, | ||
]; | ||
} | ||
|
||
public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface | ||
{ | ||
return new Configuration($this->integrations); | ||
} | ||
|
||
public function load(array $configs, ContainerBuilder $container): void | ||
{ | ||
$config = $this->loadIntegrations($configs, $container); | ||
|
||
// Do your bundle specific configuration here | ||
} | ||
|
||
public function prepend(ContainerBuilder $container): void | ||
{ | ||
$this->prependIntegrations($container, 'example_my_bundle'); | ||
} | ||
} | ||
``` | ||
|
||
**registerDefaultIntegrations** | ||
|
||
The `registerDefaultIntegrations` method will automatically register all the integrations that are in the `provideExtensionClasses` method. | ||
|
||
It will check if the class exists and if it does it will create a new instance of it and add it to the `integrations` property. | ||
|
||
That way you can define the integration classes in the specific component, and it will automatically be integrated into the main bundle | ||
if your component is installed. | ||
|
||
**loadIntegrations** | ||
|
||
The `loadIntegrations` method will call the `load` method on all the integrations that are registered. | ||
|
||
It will automatically pass the configuration to the existing configuration only if they are `enabled`. | ||
|
||
**prependIntegrations** | ||
|
||
The `prependIntegrations` method will call the `prepend` method on all the integrations that are registered. | ||
|
||
It will check if the configuration is `enabled` and if it is it will call the `prepend` method. | ||
|
||
### Configuration | ||
|
||
Here is an example of configuration base on the example above. | ||
|
||
```yaml | ||
example_my_bundle: | ||
my_component: | ||
enabled: true | ||
my_component_configuration: true | ||
my_other_component: | ||
enabled: false | ||
``` | ||
This example will enable the `MyComponentIntegration` and disable the `MyOtherComponentIntegration`. | ||
|
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,32 @@ | ||
{ | ||
"name": "draw/dependency-injection", | ||
"description": "Dependency injection addons to Symfony", | ||
"license": "MIT", | ||
"type": "library", | ||
"keywords": ["draw", "component", "symfony"], | ||
"authors": [ | ||
{ | ||
"name": "Martin Poirier Theoret", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.1", | ||
"symfony/config": "^6.4.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^10.0" | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"autoload": { | ||
"psr-4": { | ||
"Draw\\Component\\DependencyInjection\\": "" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "0.11-dev" | ||
} | ||
} | ||
} |
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 @@ | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"> | ||
<testsuites> | ||
<testsuite name="Main"> | ||
<directory>./Tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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
Oops, something went wrong.