mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-30 22:18:46 +02:00
migration symfony 5 4 (#300)
* symfony 5.4 (diff dev) * symfony 5.4 (working) * symfony 5.4 (update autoload) * symfony 5.4 (remove swiftmailer mailer implementation) * symfony 5.4 (php doc and split Global accessor class) ### Impacted packages: composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
@@ -18,9 +18,13 @@ use Symfony\Component\Config\ConfigCacheInterface;
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Generator\CompiledUrlGenerator;
|
||||
use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface;
|
||||
use Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper;
|
||||
use Symfony\Component\Routing\Generator\Dumper\GeneratorDumperInterface;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
|
||||
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
|
||||
use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
|
||||
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
|
||||
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
|
||||
@@ -73,6 +77,11 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $defaultLocale;
|
||||
|
||||
/**
|
||||
* @var ConfigCacheFactoryInterface|null
|
||||
*/
|
||||
@@ -83,20 +92,19 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
*/
|
||||
private $expressionLanguageProviders = [];
|
||||
|
||||
private static $cache = [];
|
||||
|
||||
/**
|
||||
* @param LoaderInterface $loader A LoaderInterface instance
|
||||
* @param mixed $resource The main resource to load
|
||||
* @param array $options An array of options
|
||||
* @param RequestContext $context The context
|
||||
* @param LoggerInterface $logger A logger instance
|
||||
* @param mixed $resource The main resource to load
|
||||
*/
|
||||
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null)
|
||||
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
|
||||
{
|
||||
$this->loader = $loader;
|
||||
$this->resource = $resource;
|
||||
$this->logger = $logger;
|
||||
$this->context = $context ?: new RequestContext();
|
||||
$this->context = $context ?? new RequestContext();
|
||||
$this->setOptions($options);
|
||||
$this->defaultLocale = $defaultLocale;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,19 +115,13 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
* * cache_dir: The cache directory (or null to disable caching)
|
||||
* * debug: Whether to enable debugging or not (false by default)
|
||||
* * generator_class: The name of a UrlGeneratorInterface implementation
|
||||
* * generator_base_class: The base class for the dumped generator class
|
||||
* * generator_cache_class: The class name for the dumped generator class
|
||||
* * generator_dumper_class: The name of a GeneratorDumperInterface implementation
|
||||
* * matcher_class: The name of a UrlMatcherInterface implementation
|
||||
* * matcher_base_class: The base class for the dumped matcher class
|
||||
* * matcher_dumper_class: The class name for the dumped matcher class
|
||||
* * matcher_cache_class: The name of a MatcherDumperInterface implementation
|
||||
* * matcher_dumper_class: The name of a MatcherDumperInterface implementation
|
||||
* * resource_type: Type hint for the main resource (optional)
|
||||
* * strict_requirements: Configure strict requirement checking for generators
|
||||
* implementing ConfigurableRequirementsInterface (default is true)
|
||||
*
|
||||
* @param array $options An array of options
|
||||
*
|
||||
* @throws \InvalidArgumentException When unsupported option is provided
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
@@ -127,14 +129,10 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
$this->options = [
|
||||
'cache_dir' => null,
|
||||
'debug' => false,
|
||||
'generator_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
|
||||
'generator_base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
|
||||
'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
|
||||
'generator_cache_class' => 'ProjectUrlGenerator',
|
||||
'matcher_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
|
||||
'matcher_base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
|
||||
'matcher_dumper_class' => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
|
||||
'matcher_cache_class' => 'ProjectUrlMatcher',
|
||||
'generator_class' => CompiledUrlGenerator::class,
|
||||
'generator_dumper_class' => CompiledUrlGeneratorDumper::class,
|
||||
'matcher_class' => CompiledUrlMatcher::class,
|
||||
'matcher_dumper_class' => CompiledUrlMatcherDumper::class,
|
||||
'resource_type' => null,
|
||||
'strict_requirements' => true,
|
||||
];
|
||||
@@ -157,12 +155,11 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
/**
|
||||
* Sets an option.
|
||||
*
|
||||
* @param string $key The key
|
||||
* @param mixed $value The value
|
||||
* @param mixed $value The value
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function setOption($key, $value)
|
||||
public function setOption(string $key, $value)
|
||||
{
|
||||
if (!\array_key_exists($key, $this->options)) {
|
||||
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
|
||||
@@ -174,13 +171,11 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
/**
|
||||
* Gets an option value.
|
||||
*
|
||||
* @param string $key The key
|
||||
*
|
||||
* @return mixed The value
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function getOption($key)
|
||||
public function getOption(string $key)
|
||||
{
|
||||
if (!\array_key_exists($key, $this->options)) {
|
||||
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
|
||||
@@ -235,7 +230,7 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
|
||||
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH)
|
||||
{
|
||||
return $this->getGenerator()->generate($name, $parameters, $referenceType);
|
||||
}
|
||||
@@ -243,7 +238,7 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function match($pathinfo)
|
||||
public function match(string $pathinfo)
|
||||
{
|
||||
return $this->getMatcher()->match($pathinfo);
|
||||
}
|
||||
@@ -273,8 +268,13 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
return $this->matcher;
|
||||
}
|
||||
|
||||
if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
|
||||
$this->matcher = new $this->options['matcher_class']($this->getRouteCollection(), $this->context);
|
||||
if (null === $this->options['cache_dir']) {
|
||||
$routes = $this->getRouteCollection();
|
||||
$compiled = is_a($this->options['matcher_class'], CompiledUrlMatcher::class, true);
|
||||
if ($compiled) {
|
||||
$routes = (new CompiledUrlMatcherDumper($routes))->getCompiledRoutes();
|
||||
}
|
||||
$this->matcher = new $this->options['matcher_class']($routes, $this->context);
|
||||
if (method_exists($this->matcher, 'addExpressionLanguageProvider')) {
|
||||
foreach ($this->expressionLanguageProviders as $provider) {
|
||||
$this->matcher->addExpressionLanguageProvider($provider);
|
||||
@@ -284,7 +284,7 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
return $this->matcher;
|
||||
}
|
||||
|
||||
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['matcher_cache_class'].'.php',
|
||||
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/url_matching_routes.php',
|
||||
function (ConfigCacheInterface $cache) {
|
||||
$dumper = $this->getMatcherDumperInstance();
|
||||
if (method_exists($dumper, 'addExpressionLanguageProvider')) {
|
||||
@@ -293,26 +293,17 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
}
|
||||
}
|
||||
|
||||
$options = [
|
||||
'class' => $this->options['matcher_cache_class'],
|
||||
'base_class' => $this->options['matcher_base_class'],
|
||||
];
|
||||
|
||||
$cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
|
||||
$cache->write($dumper->dump(), $this->getRouteCollection()->getResources());
|
||||
}
|
||||
);
|
||||
|
||||
if (!class_exists($this->options['matcher_cache_class'], false)) {
|
||||
require_once $cache->getPath();
|
||||
}
|
||||
|
||||
return $this->matcher = new $this->options['matcher_cache_class']($this->context);
|
||||
return $this->matcher = new $this->options['matcher_class'](self::getCompiledRoutes($cache->getPath()), $this->context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the UrlGenerator instance associated with this Router.
|
||||
*
|
||||
* @return UrlGeneratorInterface A UrlGeneratorInterface instance
|
||||
* @return UrlGeneratorInterface
|
||||
*/
|
||||
public function getGenerator()
|
||||
{
|
||||
@@ -320,27 +311,26 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
return $this->generator;
|
||||
}
|
||||
|
||||
if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
|
||||
$this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
|
||||
if (null === $this->options['cache_dir']) {
|
||||
$routes = $this->getRouteCollection();
|
||||
$aliases = [];
|
||||
$compiled = is_a($this->options['generator_class'], CompiledUrlGenerator::class, true);
|
||||
if ($compiled) {
|
||||
$generatorDumper = new CompiledUrlGeneratorDumper($routes);
|
||||
$routes = $generatorDumper->getCompiledRoutes();
|
||||
$aliases = $generatorDumper->getCompiledAliases();
|
||||
}
|
||||
$this->generator = new $this->options['generator_class'](array_merge($routes, $aliases), $this->context, $this->logger, $this->defaultLocale);
|
||||
} else {
|
||||
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['generator_cache_class'].'.php',
|
||||
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/url_generating_routes.php',
|
||||
function (ConfigCacheInterface $cache) {
|
||||
$dumper = $this->getGeneratorDumperInstance();
|
||||
|
||||
$options = [
|
||||
'class' => $this->options['generator_cache_class'],
|
||||
'base_class' => $this->options['generator_base_class'],
|
||||
];
|
||||
|
||||
$cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
|
||||
$cache->write($dumper->dump(), $this->getRouteCollection()->getResources());
|
||||
}
|
||||
);
|
||||
|
||||
if (!class_exists($this->options['generator_cache_class'], false)) {
|
||||
require_once $cache->getPath();
|
||||
}
|
||||
|
||||
$this->generator = new $this->options['generator_cache_class']($this->context, $this->logger);
|
||||
$this->generator = new $this->options['generator_class'](self::getCompiledRoutes($cache->getPath()), $this->context, $this->logger, $this->defaultLocale);
|
||||
}
|
||||
|
||||
if ($this->generator instanceof ConfigurableRequirementsInterface) {
|
||||
@@ -374,10 +364,8 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
/**
|
||||
* Provides the ConfigCache factory implementation, falling back to a
|
||||
* default implementation if necessary.
|
||||
*
|
||||
* @return ConfigCacheFactoryInterface
|
||||
*/
|
||||
private function getConfigCacheFactory()
|
||||
private function getConfigCacheFactory(): ConfigCacheFactoryInterface
|
||||
{
|
||||
if (null === $this->configCacheFactory) {
|
||||
$this->configCacheFactory = new ConfigCacheFactory($this->options['debug']);
|
||||
@@ -385,4 +373,21 @@ class Router implements RouterInterface, RequestMatcherInterface
|
||||
|
||||
return $this->configCacheFactory;
|
||||
}
|
||||
|
||||
private static function getCompiledRoutes(string $path): array
|
||||
{
|
||||
if ([] === self::$cache && \function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN) && (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) || filter_var(ini_get('opcache.enable_cli'), \FILTER_VALIDATE_BOOLEAN))) {
|
||||
self::$cache = null;
|
||||
}
|
||||
|
||||
if (null === self::$cache) {
|
||||
return require $path;
|
||||
}
|
||||
|
||||
if (isset(self::$cache[$path])) {
|
||||
return self::$cache[$path];
|
||||
}
|
||||
|
||||
return self::$cache[$path] = require $path;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user