-
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 #16 from pfilsx/feature/DPC-11
DPC-11 argument resolver
- Loading branch information
Showing
28 changed files
with
976 additions
and
193 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
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\DtoParamConverter\Annotation; | ||
|
||
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; | ||
|
||
/** | ||
* @Annotation | ||
* @NamedArgumentConstructor | ||
*/ | ||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)] | ||
final class DtoResolver | ||
{ | ||
private array $options; | ||
|
||
public function __construct(array $options = []) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/DependencyInjection/Compiler/AddExpressionLanguageProvidersPass.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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\DtoParamConverter\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class AddExpressionLanguageProvidersPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if ($container->hasDefinition('pfilsx.dto_converter.expression_language.default')) { | ||
$definition = $container->findDefinition('pfilsx.dto_converter.expression_language.default'); | ||
foreach ($container->findTaggedServiceIds('security.expression_language_provider') as $id => $attributes) { | ||
$definition->addMethodCall('registerProvider', [new Reference($id)]); | ||
} | ||
} | ||
} | ||
} |
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
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\DtoParamConverter\EventSubscriber; | ||
|
||
use Pfilsx\DtoParamConverter\Provider\RouteMetadataProvider; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\KernelEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
final class ControllerEventSubscriber implements EventSubscriberInterface | ||
{ | ||
private RouteMetadataProvider $routeMetadataProvider; | ||
|
||
public function __construct(RouteMetadataProvider $routeMetadataProvider) | ||
{ | ||
$this->routeMetadataProvider = $routeMetadataProvider; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::CONTROLLER => 'onKernelController', | ||
]; | ||
} | ||
|
||
public function onKernelController(KernelEvent $event): void | ||
{ | ||
$controller = $event->getController(); | ||
|
||
if (!\is_array($controller) && method_exists($controller, '__invoke')) { | ||
$controller = [$controller, '__invoke']; | ||
} | ||
|
||
if (!\is_array($controller)) { | ||
return; | ||
} | ||
|
||
$route = $event->getRequest()->attributes->get('_route'); | ||
|
||
if (empty($route)) { | ||
return; | ||
} | ||
|
||
$this->routeMetadataProvider->createMetadata($route, $controller); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfilsx\DtoParamConverter\Provider; | ||
|
||
use Doctrine\Common\Annotations\Reader; | ||
use Pfilsx\DtoParamConverter\Annotation\Dto; | ||
use ReflectionClass; | ||
|
||
final class DtoMetadataProvider | ||
{ | ||
private Reader $reader; | ||
|
||
/** | ||
* @var array<string, null|Dto> | ||
*/ | ||
private array $localCollection = []; | ||
|
||
public function __construct(Reader $reader) | ||
{ | ||
$this->reader = $reader; | ||
} | ||
|
||
public function getDtoMetadata(string $className): ?Dto | ||
{ | ||
if (array_key_exists($className, $this->localCollection)) { | ||
return $this->localCollection[$className]; | ||
} | ||
|
||
try { | ||
$refClass = new ReflectionClass($className); | ||
} catch (\ReflectionException $e) { | ||
$this->localCollection[$className] = null; | ||
|
||
return null; | ||
} | ||
|
||
$metadata = $this->reader->getClassAnnotation($refClass, Dto::class); | ||
|
||
if ($metadata !== null) { | ||
$this->localCollection[$className] = $metadata; | ||
|
||
return $metadata; | ||
} | ||
|
||
if (\PHP_VERSION_ID >= 80000) { | ||
$metadata = array_map( | ||
function (\ReflectionAttribute $attribute) { | ||
return $attribute->newInstance(); | ||
}, | ||
$refClass->getAttributes(Dto::class, \ReflectionAttribute::IS_INSTANCEOF) | ||
)[0] ?? null; | ||
|
||
if ($metadata !== null) { | ||
$this->localCollection[$className] = $metadata; | ||
|
||
return $metadata; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.