This commit is contained in:
2025-05-12 14:25:25 +02:00
parent ab2db755ef
commit 9e378ca2b7
2719 changed files with 46505 additions and 60181 deletions

View File

@@ -111,7 +111,7 @@ abstract class AttributeClassLoader implements LoaderInterface
/**
* @throws \InvalidArgumentException When route can't be parsed
*/
public function load(mixed $class, string $type = null): RouteCollection
public function load(mixed $class, ?string $type = null): RouteCollection
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
@@ -267,7 +267,7 @@ abstract class AttributeClassLoader implements LoaderInterface
}
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
if ('annotation' === $type) {
trigger_deprecation('symfony/routing', '6.4', 'The "annotation" route type is deprecated, use the "attribute" route type instead.');

View File

@@ -26,7 +26,7 @@ class AttributeDirectoryLoader extends AttributeFileLoader
/**
* @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed
*/
public function load(mixed $path, string $type = null): ?RouteCollection
public function load(mixed $path, ?string $type = null): ?RouteCollection
{
if (!is_dir($dir = $this->locator->locate($path))) {
return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();
@@ -61,7 +61,7 @@ class AttributeDirectoryLoader extends AttributeFileLoader
return $collection;
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
if (!\is_string($resource)) {
return false;

View File

@@ -43,7 +43,7 @@ class AttributeFileLoader extends FileLoader
*
* @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed
*/
public function load(mixed $file, string $type = null): ?RouteCollection
public function load(mixed $file, ?string $type = null): ?RouteCollection
{
$path = $this->locator->locate($file);
@@ -63,7 +63,7 @@ class AttributeFileLoader extends FileLoader
return $collection;
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
if ('annotation' === $type) {
trigger_deprecation('symfony/routing', '6.4', 'The "annotation" route type is deprecated, use the "attribute" route type instead.');
@@ -82,7 +82,7 @@ class AttributeFileLoader extends FileLoader
$tokens = token_get_all(file_get_contents($file));
if (1 === \count($tokens) && \T_INLINE_HTML === $tokens[0][0]) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forgot to add the "<?php" start tag at the beginning of the file?', $file));
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forget to add the "<?php" start tag at the beginning of the file?', $file));
}
$nsTokens = [\T_NS_SEPARATOR => true, \T_STRING => true];

View File

@@ -26,12 +26,12 @@ class ClosureLoader extends Loader
/**
* Loads a Closure.
*/
public function load(mixed $closure, string $type = null): RouteCollection
public function load(mixed $closure, ?string $type = null): RouteCollection
{
return $closure($this->env);
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
return $resource instanceof \Closure && (!$type || 'closure' === $type);
}

View File

@@ -28,7 +28,7 @@ class CollectionConfigurator
private ?array $parentPrefixes;
private string|array|null $host = null;
public function __construct(RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)
public function __construct(RouteCollection $parent, string $name, ?self $parentConfigurator = null, ?array $parentPrefixes = null)
{
$this->parent = $parent;
$this->name = $name;

View File

@@ -24,7 +24,7 @@ class RouteConfigurator
protected $parentConfigurator;
public function __construct(RouteCollection $collection, RouteCollection $route, string $name = '', CollectionConfigurator $parentConfigurator = null, array $prefixes = null)
public function __construct(RouteCollection $collection, RouteCollection $route, string $name = '', ?CollectionConfigurator $parentConfigurator = null, ?array $prefixes = null)
{
$this->collection = $collection;
$this->route = $route;
@@ -42,7 +42,14 @@ class RouteConfigurator
*/
final public function host(string|array $host): static
{
$previousRoutes = clone $this->route;
$this->addHost($this->route, $host);
foreach ($previousRoutes as $name => $route) {
if (!$this->route->get($name)) {
$this->collection->remove($name);
}
}
$this->collection->addCollection($this->route);
return $this;
}

View File

@@ -26,7 +26,7 @@ class RoutingConfigurator
private string $file;
private ?string $env;
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file, string $env = null)
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file, ?string $env = null)
{
$this->collection = $collection;
$this->loader = $loader;
@@ -38,7 +38,7 @@ class RoutingConfigurator
/**
* @param string|string[]|null $exclude Glob patterns to exclude from the import
*/
final public function import(string|array $resource, string $type = null, bool $ignoreErrors = false, string|array $exclude = null): ImportConfigurator
final public function import(string|array $resource, ?string $type = null, bool $ignoreErrors = false, string|array|null $exclude = null): ImportConfigurator
{
$this->loader->setCurrentDir(\dirname($this->path));

View File

@@ -28,6 +28,7 @@ trait HostTrait
foreach ($routes->all() as $name => $route) {
if (null === $locale = $route->getDefault('_locale')) {
$priority = $routes->getPriority($name) ?? 0;
$routes->remove($name);
foreach ($hosts as $locale => $host) {
$localizedRoute = clone $route;
@@ -35,14 +36,14 @@ trait HostTrait
$localizedRoute->setRequirement('_locale', preg_quote($locale));
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setHost($host);
$routes->add($name.'.'.$locale, $localizedRoute);
$routes->add($name.'.'.$locale, $localizedRoute, $priority);
}
} elseif (!isset($hosts[$locale])) {
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding host in its parent collection.', $name, $locale));
} else {
$route->setHost($hosts[$locale]);
$route->setRequirement('_locale', preg_quote($locale));
$routes->add($name, $route);
$routes->add($name, $route, $routes->getPriority($name) ?? 0);
}
}
}

View File

@@ -27,7 +27,7 @@ trait LocalizedRouteTrait
*
* @param string|array $path the path, or the localized paths of the route
*/
final protected function createLocalizedRoute(RouteCollection $collection, string $name, string|array $path, string $namePrefix = '', array $prefixes = null): RouteCollection
final protected function createLocalizedRoute(RouteCollection $collection, string $name, string|array $path, string $namePrefix = '', ?array $prefixes = null): RouteCollection
{
$paths = [];

View File

@@ -29,6 +29,7 @@ trait PrefixTrait
}
foreach ($routes->all() as $name => $route) {
if (null === $locale = $route->getDefault('_locale')) {
$priority = $routes->getPriority($name) ?? 0;
$routes->remove($name);
foreach ($prefix as $locale => $localePrefix) {
$localizedRoute = clone $route;
@@ -36,13 +37,13 @@ trait PrefixTrait
$localizedRoute->setRequirement('_locale', preg_quote($locale));
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
$routes->add($name.'.'.$locale, $localizedRoute);
$routes->add($name.'.'.$locale, $localizedRoute, $priority);
}
} elseif (!isset($prefix[$locale])) {
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
} else {
$route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
$routes->add($name, $route);
$routes->add($name, $route, $routes->getPriority($name) ?? 0);
}
}

View File

@@ -22,13 +22,13 @@ class ContainerLoader extends ObjectLoader
{
private ContainerInterface $container;
public function __construct(ContainerInterface $container, string $env = null)
public function __construct(ContainerInterface $container, ?string $env = null)
{
$this->container = $container;
parent::__construct($env);
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
return 'service' === $type && \is_string($resource);
}

View File

@@ -17,7 +17,7 @@ use Symfony\Component\Routing\RouteCollection;
class DirectoryLoader extends FileLoader
{
public function load(mixed $file, string $type = null): mixed
public function load(mixed $file, ?string $type = null): mixed
{
$path = $this->locator->locate($file);
@@ -43,7 +43,7 @@ class DirectoryLoader extends FileLoader
return $collection;
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
// only when type is forced to directory, not to conflict with AttributeLoader

View File

@@ -21,7 +21,7 @@ use Symfony\Component\Routing\RouteCollection;
*/
class GlobFileLoader extends FileLoader
{
public function load(mixed $resource, string $type = null): mixed
public function load(mixed $resource, ?string $type = null): mixed
{
$collection = new RouteCollection();
@@ -34,7 +34,7 @@ class GlobFileLoader extends FileLoader
return $collection;
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
return 'glob' === $type;
}

View File

@@ -33,7 +33,7 @@ abstract class ObjectLoader extends Loader
/**
* Calls the object method that will load the routes.
*/
public function load(mixed $resource, string $type = null): RouteCollection
public function load(mixed $resource, ?string $type = null): RouteCollection
{
if (!preg_match('/^[^\:]+(?:::(?:[^\:]+))?$/', $resource)) {
throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the %s route loader: use the format "object_id::method" or "object_id" if your object class has an "__invoke" method.', $resource, \is_string($type) ? '"'.$type.'"' : 'object'));

View File

@@ -30,7 +30,7 @@ class PhpFileLoader extends FileLoader
/**
* Loads a PHP file.
*/
public function load(mixed $file, string $type = null): RouteCollection
public function load(mixed $file, ?string $type = null): RouteCollection
{
$path = $this->locator->locate($file);
$this->setCurrentDir(\dirname($path));
@@ -54,7 +54,7 @@ class PhpFileLoader extends FileLoader
return $collection;
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'php' === $type);
}

View File

@@ -36,7 +36,7 @@ final class Psr4DirectoryLoader extends Loader implements DirectoryAwareLoaderIn
/**
* @param array{path: string, namespace: string} $resource
*/
public function load(mixed $resource, string $type = null): ?RouteCollection
public function load(mixed $resource, ?string $type = null): ?RouteCollection
{
$path = $this->locator->locate($resource['path'], $this->currentDirectory);
if (!is_dir($path)) {
@@ -46,7 +46,7 @@ final class Psr4DirectoryLoader extends Loader implements DirectoryAwareLoaderIn
return $this->loadFromDirectory($path, trim($resource['namespace'], '\\'));
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
return ('attribute' === $type || 'annotation' === $type) && \is_array($resource) && isset($resource['path'], $resource['namespace']);
}

View File

@@ -38,7 +38,7 @@ class XmlFileLoader extends FileLoader
* @throws \InvalidArgumentException when the file cannot be loaded or when the XML cannot be
* parsed because it does not validate against the scheme
*/
public function load(mixed $file, string $type = null): RouteCollection
public function load(mixed $file, ?string $type = null): RouteCollection
{
$path = $this->locator->locate($file);
@@ -94,7 +94,7 @@ class XmlFileLoader extends FileLoader
}
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
return \is_string($resource) && 'xml' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'xml' === $type);
}
@@ -135,7 +135,7 @@ class XmlFileLoader extends FileLoader
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "path" attribute and <path> child nodes.', $path));
}
$routes = $this->createLocalizedRoute($collection, $id, $paths ?: $node->getAttribute('path'));
$routes = $this->createLocalizedRoute(new RouteCollection(), $id, $paths ?: $node->getAttribute('path'));
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
@@ -146,6 +146,8 @@ class XmlFileLoader extends FileLoader
if (null !== $hosts) {
$this->addHost($routes, $hosts);
}
$collection->addCollection($routes);
}
/**

View File

@@ -41,7 +41,7 @@ class YamlFileLoader extends FileLoader
/**
* @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
*/
public function load(mixed $file, string $type = null): RouteCollection
public function load(mixed $file, ?string $type = null): RouteCollection
{
$path = $this->locator->locate($file);
@@ -105,7 +105,7 @@ class YamlFileLoader extends FileLoader
return $collection;
}
public function supports(mixed $resource, string $type = null): bool
public function supports(mixed $resource, ?string $type = null): bool
{
return \is_string($resource) && \in_array(pathinfo($resource, \PATHINFO_EXTENSION), ['yml', 'yaml'], true) && (!$type || 'yaml' === $type);
}
@@ -157,7 +157,7 @@ class YamlFileLoader extends FileLoader
$defaults['_stateless'] = $config['stateless'];
}
$routes = $this->createLocalizedRoute($collection, $name, $config['path']);
$routes = $this->createLocalizedRoute(new RouteCollection(), $name, $config['path']);
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
@@ -168,6 +168,8 @@ class YamlFileLoader extends FileLoader
if (isset($config['host'])) {
$this->addHost($routes, $config['host']);
}
$collection->addCollection($routes);
}
/**