first commit

This commit is contained in:
2025-07-18 16:20:14 +07:00
commit 98af45c018
16382 changed files with 3148096 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing;
trigger_deprecation('symfony/framework-bundle', '6.4', 'The "%s" class is deprecated, use "%s" instead.', AnnotatedRouteControllerLoader::class, AttributeRouteControllerLoader::class);
class_exists(AttributeRouteControllerLoader::class);
if (false) {
/**
* @deprecated since Symfony 6.4, to be removed in 7.0, use {@link AttributeRouteControllerLoader} instead
*/
class AnnotatedRouteControllerLoader extends AttributeRouteControllerLoader
{
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Attribute;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
/**
* Service tag to autoconfigure routing condition services.
*
* You can tag a service:
*
* #[AsRoutingConditionService('foo')]
* class SomeFooService
* {
* public function bar(): bool
* {
* // ...
* }
* }
*
* Then you can use the tagged service in the routing condition:
*
* class PageController
* {
* #[Route('/page', condition: "service('foo').bar()")]
* public function page(): Response
* {
* // ...
* }
* }
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class AsRoutingConditionService extends AutoconfigureTag
{
public function __construct(
?string $alias = null,
int $priority = 0,
) {
parent::__construct('routing.condition_service', ['alias' => $alias, 'priority' => $priority]);
}
}

View File

@@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing;
use Symfony\Component\Routing\Loader\AttributeClassLoader;
use Symfony\Component\Routing\Route;
/**
* AttributeRouteControllerLoader is an implementation of AttributeClassLoader
* that sets the '_controller' default based on the class and method names.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Alexandre Daubois <alex.daubois@gmail.com>
*/
class AttributeRouteControllerLoader extends AttributeClassLoader
{
/**
* Configures the _controller default parameter of a given Route instance.
*
* @return void
*/
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot)
{
if ('__invoke' === $method->getName()) {
$route->setDefault('_controller', $class->getName());
} else {
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
}
}
/**
* Makes the default route name more sane by removing common keywords.
*/
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method): string
{
$name = preg_replace('/(bundle|controller)_/', '_', parent::getDefaultRouteName($class, $method));
if (str_ends_with($method->name, 'Action') || str_ends_with($method->name, '_action')) {
$name = preg_replace('/action(_\d+)?$/', '\\1', $name);
}
return str_replace('__', '_', $name);
}
}
if (!class_exists(AnnotatedRouteControllerLoader::class, false)) {
class_alias(AttributeRouteControllerLoader::class, AnnotatedRouteControllerLoader::class);
}

View File

@@ -0,0 +1,92 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing;
use Symfony\Component\Config\Exception\LoaderLoadException;
use Symfony\Component\Config\Loader\DelegatingLoader as BaseDelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* DelegatingLoader delegates route loading to other loaders using a loader resolver.
*
* This implementation resolves the _controller attribute from the short notation
* to the fully-qualified form (from a:b:c to class::method).
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final
*/
class DelegatingLoader extends BaseDelegatingLoader
{
private bool $loading = false;
private array $defaultOptions;
private array $defaultRequirements;
public function __construct(LoaderResolverInterface $resolver, array $defaultOptions = [], array $defaultRequirements = [])
{
$this->defaultOptions = $defaultOptions;
$this->defaultRequirements = $defaultRequirements;
parent::__construct($resolver);
}
public function load(mixed $resource, ?string $type = null): RouteCollection
{
if ($this->loading) {
// This can happen if a fatal error occurs in parent::load().
// Here is the scenario:
// - while routes are being loaded by parent::load() below, a fatal error
// occurs (e.g. parse error in a controller while loading annotations);
// - PHP abruptly empties the stack trace, bypassing all catch/finally blocks;
// it then calls the registered shutdown functions;
// - the ErrorHandler catches the fatal error and re-injects it for rendering
// thanks to HttpKernel->terminateWithException() (that calls handleException());
// - at this stage, if we try to load the routes again, we must prevent
// the fatal error from occurring a second time,
// otherwise the PHP process would be killed immediately;
// - while rendering the exception page, the router can be required
// (by e.g. the web profiler that needs to generate a URL);
// - this handles the case and prevents the second fatal error
// by triggering an exception beforehand.
throw new LoaderLoadException($resource, null, 0, null, $type);
}
$this->loading = true;
try {
$collection = parent::load($resource, $type);
} finally {
$this->loading = false;
}
foreach ($collection->all() as $route) {
if ($this->defaultOptions) {
$route->setOptions($route->getOptions() + $this->defaultOptions);
}
if ($this->defaultRequirements) {
$route->setRequirements($route->getRequirements() + $this->defaultRequirements);
}
if (!\is_string($controller = $route->getDefault('_controller'))) {
continue;
}
if (str_contains($controller, '::')) {
continue;
}
$route->setDefault('_controller', $controller);
}
return $collection;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing;
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @internal
*/
class RedirectableCompiledUrlMatcher extends CompiledUrlMatcher implements RedirectableUrlMatcherInterface
{
public function redirect(string $path, string $route, ?string $scheme = null): array
{
return [
'_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction',
'path' => $path,
'permanent' => true,
'scheme' => $scheme,
'httpPort' => $this->context->getHttpPort(),
'httpsPort' => $this->context->getHttpsPort(),
'_route' => $route,
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing;
/**
* Marker interface for service route loaders.
*/
interface RouteLoaderInterface
{
}

View File

@@ -0,0 +1,197 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Config\ContainerParametersResource;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Router as BaseRouter;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
/**
* This Router creates the Loader only when the cache is empty.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Router extends BaseRouter implements WarmableInterface, ServiceSubscriberInterface
{
private ContainerInterface $container;
private array $collectedParameters = [];
private \Closure $paramFetcher;
/**
* @param mixed $resource The main resource to load
*/
public function __construct(ContainerInterface $container, mixed $resource, array $options = [], ?RequestContext $context = null, ?ContainerInterface $parameters = null, ?LoggerInterface $logger = null, ?string $defaultLocale = null)
{
$this->container = $container;
$this->resource = $resource;
$this->context = $context ?? new RequestContext();
$this->logger = $logger;
$this->setOptions($options);
if ($parameters) {
$this->paramFetcher = $parameters->get(...);
} elseif ($container instanceof SymfonyContainerInterface) {
$this->paramFetcher = $container->getParameter(...);
} else {
throw new \LogicException(sprintf('You should either pass a "%s" instance or provide the $parameters argument of the "%s" method.', SymfonyContainerInterface::class, __METHOD__));
}
$this->defaultLocale = $defaultLocale;
}
public function getRouteCollection(): RouteCollection
{
if (!isset($this->collection)) {
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
$this->resolveParameters($this->collection);
$this->collection->addResource(new ContainerParametersResource($this->collectedParameters));
try {
$containerFile = ($this->paramFetcher)('kernel.cache_dir').'/'.($this->paramFetcher)('kernel.container_class').'.php';
if (file_exists($containerFile)) {
$this->collection->addResource(new FileResource($containerFile));
} else {
$this->collection->addResource(new FileExistenceResource($containerFile));
}
} catch (ParameterNotFoundException) {
}
}
return $this->collection;
}
/**
* @param string|null $buildDir
*/
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
{
$currentDir = $this->getOption('cache_dir');
// force cache generation
$this->setOption('cache_dir', $cacheDir);
$this->getMatcher();
$this->getGenerator();
$this->setOption('cache_dir', $currentDir);
return [
$this->getOption('generator_class'),
$this->getOption('matcher_class'),
];
}
/**
* Replaces placeholders with service container parameter values in:
* - the route defaults,
* - the route requirements,
* - the route path,
* - the route host,
* - the route schemes,
* - the route methods.
*/
private function resolveParameters(RouteCollection $collection): void
{
foreach ($collection as $route) {
foreach ($route->getDefaults() as $name => $value) {
$route->setDefault($name, $this->resolve($value));
}
foreach ($route->getRequirements() as $name => $value) {
$route->setRequirement($name, $this->resolve($value));
}
$route->setPath($this->resolve($route->getPath()));
$route->setHost($this->resolve($route->getHost()));
$schemes = [];
foreach ($route->getSchemes() as $scheme) {
$schemes[] = explode('|', $this->resolve($scheme));
}
$route->setSchemes(array_merge([], ...$schemes));
$methods = [];
foreach ($route->getMethods() as $method) {
$methods[] = explode('|', $this->resolve($method));
}
$route->setMethods(array_merge([], ...$methods));
$route->setCondition($this->resolve($route->getCondition()));
}
}
/**
* Recursively replaces %placeholders% with the service container parameters.
*
* @throws ParameterNotFoundException When a placeholder does not exist as a container parameter
* @throws RuntimeException When a container value is not a string or a numeric value
*/
private function resolve(mixed $value): mixed
{
if (\is_array($value)) {
foreach ($value as $key => $val) {
$value[$key] = $this->resolve($val);
}
return $value;
}
if (!\is_string($value)) {
return $value;
}
$escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($value) {
// skip %%
if (!isset($match[1])) {
return '%%';
}
if (preg_match('/^env\((?:\w++:)*+\w++\)$/', $match[1])) {
throw new RuntimeException(sprintf('Using "%%%s%%" is not allowed in routing configuration.', $match[1]));
}
$resolved = ($this->paramFetcher)($match[1]);
if (\is_scalar($resolved)) {
$this->collectedParameters[$match[1]] = $resolved;
if (\is_string($resolved)) {
$resolved = $this->resolve($resolved);
}
if (\is_scalar($resolved)) {
return false === $resolved ? '0' : (string) $resolved;
}
}
throw new RuntimeException(sprintf('The container parameter "%s", used in the route configuration value "%s", must be a string or numeric, but it is of type "%s".', $match[1], $value, get_debug_type($resolved)));
}, $value);
return str_replace('%%', '%', $escapedValue);
}
public static function getSubscribedServices(): array
{
return [
'routing.loader' => LoaderInterface::class,
];
}
}