generated from rich-id/bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from rich-id/v1.1.0
Prepare v1.1.0
- Loading branch information
Showing
29 changed files
with
569 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
# Changelog | ||
|
||
## Version 1.1.0 | ||
|
||
- Add service tag injection | ||
- Add method annotation to add MethodCall to the service definition | ||
|
||
## Version 1.0.1 | ||
|
||
- Fix annotation dependency where it was |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RichId\AutoconfigureBundle\Annotation; | ||
|
||
abstract class AbstractServiceInjectionAnnotation implements AutoconfigureAnnotation | ||
{ | ||
public const SERVICE_TYPE = 'service'; | ||
public const PARAMETER_TYPE = 'parameter'; | ||
public const SERVICES_BY_TAG = 'services_by_tag'; | ||
|
||
/** @var string */ | ||
public $value; | ||
|
||
/** @var string */ | ||
public $type; | ||
|
||
public function __construct(string $value, string $type = self::SERVICE_TYPE) | ||
{ | ||
$this->value = $value; | ||
$this->type = $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
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RichId\AutoconfigureBundle\Annotation; | ||
|
||
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; | ||
|
||
/** | ||
* Class Method. | ||
* | ||
* Bind a service or a parameter to the designated method. | ||
* | ||
* @author Nicolas Guilloux <[email protected]> | ||
* @copyright 2014 - 2021 Rich ID (https://www.rich-id.fr) | ||
* | ||
* @Annotation({"CLASS"}) | ||
* @NamedArgumentConstructor() | ||
*/ | ||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS)] | ||
final class Method extends AbstractServiceInjectionAnnotation | ||
{ | ||
/** @var string */ | ||
public $method; | ||
|
||
/** @var int */ | ||
public $position = 0; | ||
|
||
public function __construct(string $method, string $value, string $type = self::SERVICE_TYPE, int $position = 0) | ||
{ | ||
parent::__construct($value, $type); | ||
|
||
$this->method = $method; | ||
$this->position = $position; | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/Configurators/Partials/AbstractServiceInjectionAutoConfigurator.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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RichId\AutoconfigureBundle\Configurators\Partials; | ||
|
||
use RichId\AutoconfigureBundle\Annotation\AbstractServiceInjectionAnnotation; | ||
use RichId\AutoconfigureBundle\Configurators\Basics\ServiceAutoConfiguratorInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
abstract class AbstractServiceInjectionAutoConfigurator implements ServiceAutoConfiguratorInterface | ||
{ | ||
protected function resolveObject(ContainerBuilder $container, array $options) | ||
{ | ||
$type = $options['type'] ?? null; | ||
$value = $options['value'] ?? null; | ||
|
||
switch ($type) { | ||
case AbstractServiceInjectionAnnotation::SERVICE_TYPE: | ||
return new Reference($value); | ||
|
||
case AbstractServiceInjectionAnnotation::PARAMETER_TYPE: | ||
return $container->getParameter($value); | ||
|
||
case AbstractServiceInjectionAnnotation::SERVICES_BY_TAG: | ||
$serviceTags = $container->findTaggedServiceIds($value); | ||
|
||
return \array_map( | ||
static function (string $serviceId): Reference { | ||
return new Reference($serviceId); | ||
}, | ||
\array_keys($serviceTags) | ||
); | ||
|
||
default: | ||
throw new \UnexpectedValueException('The type used a service configuration is wrong.'); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,23 +4,20 @@ | |
|
||
namespace RichId\AutoconfigureBundle\Configurators\Partials; | ||
|
||
use RichId\AutoconfigureBundle\Annotation\Argument; | ||
use RichId\AutoconfigureBundle\Configurators\Basics\ServiceAutoConfiguratorInterface; | ||
use RichId\AutoconfigureBundle\Model\ServiceConfiguration; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Class ArgumentAutoConfigurator. | ||
* | ||
* @author Nicolas Guilloux <[email protected]> | ||
* @copyright 2014 - 2021 Rich ID (https://www.rich-id.fr) | ||
*/ | ||
final class ArgumentAutoConfigurator implements ServiceAutoConfiguratorInterface | ||
final class ArgumentAutoConfigurator extends AbstractServiceInjectionAutoConfigurator | ||
{ | ||
public function autoconfigure( | ||
Container $container, | ||
ContainerBuilder $container, | ||
Definition $definition, | ||
ServiceConfiguration $configuration | ||
): void { | ||
|
@@ -29,25 +26,8 @@ public function autoconfigure( | |
|
||
$definition->setArgument( | ||
$sanitizedArgument, | ||
$this->getObject($container, $options) | ||
$this->resolveObject($container, $options) | ||
); | ||
} | ||
} | ||
|
||
public function getObject(Container $container, array $options) | ||
{ | ||
$type = $options['type'] ?? null; | ||
$value = $options['value'] ?? null; | ||
|
||
switch ($type) { | ||
case Argument::SERVICE_TYPE: | ||
return new Reference($value); | ||
|
||
case Argument::PARAMETER_TYPE: | ||
return $container->getParameter($value); | ||
|
||
default: | ||
throw new \UnexpectedValueException('The type used a service configuration is wrong.'); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,9 +4,8 @@ | |
|
||
namespace RichId\AutoconfigureBundle\Configurators\Partials; | ||
|
||
use RichId\AutoconfigureBundle\Configurators\Basics\ServiceAutoConfiguratorInterface; | ||
use RichId\AutoconfigureBundle\Model\ServiceConfiguration; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
|
@@ -15,15 +14,48 @@ | |
* @author Nicolas Guilloux <[email protected]> | ||
* @copyright 2014 - 2021 Rich ID (https://www.rich-id.fr) | ||
*/ | ||
final class MethodCallAutoConfigurator implements ServiceAutoConfiguratorInterface | ||
final class MethodCallAutoConfigurator extends AbstractServiceInjectionAutoConfigurator | ||
{ | ||
public function autoconfigure( | ||
Container $container, | ||
ContainerBuilder $container, | ||
Definition $definition, | ||
ServiceConfiguration $configuration | ||
): void { | ||
foreach ($configuration->getMethodCalls() as $method => $arguments) { | ||
$definition->addMethodCall($method, $arguments); | ||
$resolvedArguments = $this->findMethodCallExistingArguments($definition, $method); | ||
|
||
foreach ($arguments as $key => $value) { | ||
$resolvedArguments[$key] = $this->resolveObject($container, $value); | ||
} | ||
|
||
$this->setMethodCall($definition, $method, $resolvedArguments); | ||
} | ||
} | ||
|
||
private function findMethodCallExistingArguments(Definition $definition, string $method): array | ||
{ | ||
foreach ($definition->getMethodCalls() as $data) { | ||
if ($data[0] === $method) { | ||
return $data[1] ?? []; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private function setMethodCall(Definition $definition, string $method, array $arguments): void | ||
{ | ||
$methodCalls = $definition->getMethodCalls(); | ||
|
||
foreach ($methodCalls as $key => $data) { | ||
if ($data[0] === $method) { | ||
$methodCalls[$key] = [$method, $arguments]; | ||
$definition->setMethodCalls($methodCalls); | ||
|
||
return; | ||
} | ||
} | ||
|
||
$definition->addMethodCall($method, $arguments); | ||
} | ||
} |
Oops, something went wrong.