From d8e3ba94a834214d0c913e7ec8cffafa547f30a0 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Tue, 5 Mar 2024 13:00:50 +0100 Subject: [PATCH] Refactoring ideas --- src/Dto/Url.php | 6 ++ src/Normalizer/UrlNormalizer.php | 5 +- .../config/definition/path_type_reference.php | 5 ++ .../config/definition/utils/url_node.php | 55 +++++++++++++------ src/SpomkyLabsPwaBundle.php | 1 - 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/Dto/Url.php b/src/Dto/Url.php index 341cdf5..0e9110e 100644 --- a/src/Dto/Url.php +++ b/src/Dto/Url.php @@ -4,10 +4,16 @@ namespace SpomkyLabs\PwaBundle\Dto; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Serializer\Attribute\SerializedName; + final class Url { public string $path; + #[SerializedName('path_type_reference')] + public int $pathTypeReference = UrlGeneratorInterface::ABSOLUTE_PATH; + /** * @var array */ diff --git a/src/Normalizer/UrlNormalizer.php b/src/Normalizer/UrlNormalizer.php index cfb54b9..ea35626 100644 --- a/src/Normalizer/UrlNormalizer.php +++ b/src/Normalizer/UrlNormalizer.php @@ -5,7 +5,6 @@ namespace SpomkyLabs\PwaBundle\Normalizer; use SpomkyLabs\PwaBundle\Dto\Url; -use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; @@ -19,8 +18,6 @@ final class UrlNormalizer implements NormalizerInterface, NormalizerAwareInterfa public function __construct( private readonly RouterInterface $router, - #[Autowire('%spomky_labs_pwa.routes.reference_type%')] - private readonly int $referenceType, ) { } @@ -29,7 +26,7 @@ public function normalize(mixed $object, string $format = null, array $context = assert($object instanceof Url); if (! str_starts_with($object->path, '/') && filter_var($object->path, FILTER_VALIDATE_URL) === false) { - return $this->router->generate($object->path, $object->params, $this->referenceType); + return $this->router->generate($object->path, $object->params, $object->pathTypeReference); } return $object->path; diff --git a/src/Resources/config/definition/path_type_reference.php b/src/Resources/config/definition/path_type_reference.php index 317d7f9..8b66d7e 100644 --- a/src/Resources/config/definition/path_type_reference.php +++ b/src/Resources/config/definition/path_type_reference.php @@ -9,6 +9,11 @@ $definition->rootNode() ->children() ->integerNode('path_type_reference') + ->setDeprecated( + 'spomky-labs/phpwa', + '1.1.0', + 'The "%node%" configuration key is deprecated. Use the "path_type_reference" of URL nodes instead.' + ) ->defaultValue(UrlGeneratorInterface::ABSOLUTE_PATH) ->info( 'The path type reference to generate paths/URLs. See https://symfony.com/doc/current/routing.html#generating-urls-in-controllers for more information.' diff --git a/src/Resources/config/definition/utils/url_node.php b/src/Resources/config/definition/utils/url_node.php index 0ba2464..9ebacc0 100644 --- a/src/Resources/config/definition/utils/url_node.php +++ b/src/Resources/config/definition/utils/url_node.php @@ -4,6 +4,7 @@ use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** * @param array $examples @@ -17,26 +18,46 @@ function getUrlNode(string $name, string $info, null|array $examples = null): Ar ->info($info) ->beforeNormalization() ->ifString() - ->then(static fn (string $v): array => [ - 'path' => $v, - ]) + ->then(static fn (string $v): array => [ + 'path' => $v, + ]) ->end() ->children() - ->scalarNode('path') - ->isRequired() - ->info('The URL or route name.') - ->example($examples ?? ['https://example.com', 'app_action_route', '/do/action']) + ->scalarNode('path') + ->isRequired() + ->info('The URL or route name.') + ->example($examples ?? ['https://example.com', 'app_action_route', '/do/action']) + ->end() + ->integerNode('path_type_reference') + ->defaultValue(UrlGeneratorInterface::ABSOLUTE_PATH) + ->info( + 'The path type reference to generate paths/URLs. See https://symfony.com/doc/current/routing.html#generating-urls-in-controllers for more information.' + ) + ->example([ + UrlGeneratorInterface::ABSOLUTE_PATH, + UrlGeneratorInterface::ABSOLUTE_URL, + UrlGeneratorInterface::NETWORK_PATH, + UrlGeneratorInterface::RELATIVE_PATH, + ]) + ->validate() + ->ifNotInArray([ + UrlGeneratorInterface::ABSOLUTE_PATH, + UrlGeneratorInterface::ABSOLUTE_URL, + UrlGeneratorInterface::NETWORK_PATH, + UrlGeneratorInterface::RELATIVE_PATH, + ]) + ->thenInvalid('Invalid path type reference "%s".') + ->end() + ->end() + ->arrayNode('params') + ->treatFalseLike([]) + ->treatTrueLike([]) + ->treatNullLike([]) + ->prototype('variable')->end() + ->info('The parameters of the action. Only used if the action is a route to a controller.') + ->end() ->end() - ->arrayNode('params') - ->treatFalseLike([]) - ->treatTrueLike([]) - ->treatNullLike([]) - ->prototype('variable') - ->end() - ->info('The parameters of the action. Only used if the action is a route to a controller.') - ->end() - ->end() - ->end(); + ->end(); return $node; } diff --git a/src/SpomkyLabsPwaBundle.php b/src/SpomkyLabsPwaBundle.php index 55cbf6d..7e1bb91 100644 --- a/src/SpomkyLabsPwaBundle.php +++ b/src/SpomkyLabsPwaBundle.php @@ -31,7 +31,6 @@ public function loadExtension(array $config, ContainerConfigurator $container, C if ($config['web_client'] !== null) { $builder->setAlias('pwa.web_client', $config['web_client']); } - $builder->setParameter('spomky_labs_pwa.routes.reference_type', $config['path_type_reference']); $serviceWorkerConfig = $config['serviceworker']; $manifestConfig = $config['manifest']; if ($serviceWorkerConfig['enabled'] === true && $manifestConfig['enabled'] === true) {