Skip to content

Commit

Permalink
Refactoring ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 5, 2024
1 parent 92f91c5 commit d8e3ba9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/Dto/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
Expand Down
5 changes: 1 addition & 4 deletions src/Normalizer/UrlNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}

Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/definition/path_type_reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
55 changes: 38 additions & 17 deletions src/Resources/config/definition/utils/url_node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> $examples
Expand All @@ -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;
}
1 change: 0 additions & 1 deletion src/SpomkyLabsPwaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d8e3ba9

Please sign in to comment.