1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 19:42:38 +02:00
Daniel Neto 2024-03-05 19:22:51 -03:00
parent 7673eda07e
commit 330cdbe615
9054 changed files with 480487 additions and 41800 deletions

View file

@ -82,22 +82,16 @@ class Router implements RouterInterface, RequestMatcherInterface
*/
protected $defaultLocale;
/**
* @var ConfigCacheFactoryInterface|null
*/
private $configCacheFactory;
private ConfigCacheFactoryInterface $configCacheFactory;
/**
* @var ExpressionFunctionProviderInterface[]
*/
private $expressionLanguageProviders = [];
private array $expressionLanguageProviders = [];
private static $cache = [];
private static ?array $cache = [];
/**
* @param mixed $resource The main resource to load
*/
public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
public function __construct(LoaderInterface $loader, mixed $resource, array $options = [], ?RequestContext $context = null, ?LoggerInterface $logger = null, ?string $defaultLocale = null)
{
$this->loader = $loader;
$this->resource = $resource;
@ -122,6 +116,8 @@ class Router implements RouterInterface, RequestMatcherInterface
* * strict_requirements: Configure strict requirement checking for generators
* implementing ConfigurableRequirementsInterface (default is true)
*
* @return void
*
* @throws \InvalidArgumentException When unsupported option is provided
*/
public function setOptions(array $options)
@ -155,11 +151,11 @@ class Router implements RouterInterface, RequestMatcherInterface
/**
* Sets an option.
*
* @param mixed $value The value
* @return void
*
* @throws \InvalidArgumentException
*/
public function setOption(string $key, $value)
public function setOption(string $key, mixed $value)
{
if (!\array_key_exists($key, $this->options)) {
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
@ -171,11 +167,9 @@ class Router implements RouterInterface, RequestMatcherInterface
/**
* Gets an option value.
*
* @return mixed
*
* @throws \InvalidArgumentException
*/
public function getOption(string $key)
public function getOption(string $key): mixed
{
if (!\array_key_exists($key, $this->options)) {
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
@ -185,68 +179,54 @@ class Router implements RouterInterface, RequestMatcherInterface
}
/**
* {@inheritdoc}
* @return RouteCollection
*/
public function getRouteCollection()
{
if (null === $this->collection) {
$this->collection = $this->loader->load($this->resource, $this->options['resource_type']);
}
return $this->collection;
return $this->collection ??= $this->loader->load($this->resource, $this->options['resource_type']);
}
/**
* {@inheritdoc}
* @return void
*/
public function setContext(RequestContext $context)
{
$this->context = $context;
if (null !== $this->matcher) {
if (isset($this->matcher)) {
$this->getMatcher()->setContext($context);
}
if (null !== $this->generator) {
if (isset($this->generator)) {
$this->getGenerator()->setContext($context);
}
}
/**
* {@inheritdoc}
*/
public function getContext()
public function getContext(): RequestContext
{
return $this->context;
}
/**
* Sets the ConfigCache factory to use.
*
* @return void
*/
public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
{
$this->configCacheFactory = $configCacheFactory;
}
/**
* {@inheritdoc}
*/
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH)
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
{
return $this->getGenerator()->generate($name, $parameters, $referenceType);
}
/**
* {@inheritdoc}
*/
public function match(string $pathinfo)
public function match(string $pathinfo): array
{
return $this->getMatcher()->match($pathinfo);
}
/**
* {@inheritdoc}
*/
public function matchRequest(Request $request)
public function matchRequest(Request $request): array
{
$matcher = $this->getMatcher();
if (!$matcher instanceof RequestMatcherInterface) {
@ -259,12 +239,10 @@ class Router implements RouterInterface, RequestMatcherInterface
/**
* Gets the UrlMatcher or RequestMatcher instance associated with this Router.
*
* @return UrlMatcherInterface|RequestMatcherInterface
*/
public function getMatcher()
public function getMatcher(): UrlMatcherInterface|RequestMatcherInterface
{
if (null !== $this->matcher) {
if (isset($this->matcher)) {
return $this->matcher;
}
@ -302,12 +280,10 @@ class Router implements RouterInterface, RequestMatcherInterface
/**
* Gets the UrlGenerator instance associated with this Router.
*
* @return UrlGeneratorInterface
*/
public function getGenerator()
public function getGenerator(): UrlGeneratorInterface
{
if (null !== $this->generator) {
if (isset($this->generator)) {
return $this->generator;
}
@ -338,23 +314,20 @@ class Router implements RouterInterface, RequestMatcherInterface
return $this->generator;
}
/**
* @return void
*/
public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
{
$this->expressionLanguageProviders[] = $provider;
}
/**
* @return GeneratorDumperInterface
*/
protected function getGeneratorDumperInstance()
protected function getGeneratorDumperInstance(): GeneratorDumperInterface
{
return new $this->options['generator_dumper_class']($this->getRouteCollection());
}
/**
* @return MatcherDumperInterface
*/
protected function getMatcherDumperInstance()
protected function getMatcherDumperInstance(): MatcherDumperInterface
{
return new $this->options['matcher_dumper_class']($this->getRouteCollection());
}
@ -365,16 +338,12 @@ class Router implements RouterInterface, RequestMatcherInterface
*/
private function getConfigCacheFactory(): ConfigCacheFactoryInterface
{
if (null === $this->configCacheFactory) {
$this->configCacheFactory = new ConfigCacheFactory($this->options['debug']);
}
return $this->configCacheFactory;
return $this->configCacheFactory ??= new ConfigCacheFactory($this->options['debug']);
}
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))) {
if ([] === self::$cache && \function_exists('opcache_invalidate') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOL) && (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) || filter_var(\ini_get('opcache.enable_cli'), \FILTER_VALIDATE_BOOL))) {
self::$cache = null;
}
@ -382,10 +351,6 @@ class Router implements RouterInterface, RequestMatcherInterface
return require $path;
}
if (isset(self::$cache[$path])) {
return self::$cache[$path];
}
return self::$cache[$path] = require $path;
return self::$cache[$path] ??= require $path;
}
}