mirror of
https://github.com/linuxserver/Heimdall.git
synced 2025-12-03 05:29:53 +09:00
Update to laravel 7
This commit is contained in:
@@ -15,13 +15,14 @@ use Doctrine\Common\Annotations\Reader;
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\Config\Loader\LoaderResolverInterface;
|
||||
use Symfony\Component\Config\Resource\FileResource;
|
||||
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
/**
|
||||
* AnnotationClassLoader loads routing information from a PHP class and its methods.
|
||||
*
|
||||
* You need to define an implementation for the getRouteDefaults() method. Most of the
|
||||
* You need to define an implementation for the configureRoute() method. Most of the
|
||||
* time, this method should define some PHP callable to be called for the route
|
||||
* (a controller in MVC speak).
|
||||
*
|
||||
@@ -32,7 +33,6 @@ use Symfony\Component\Routing\RouteCollection;
|
||||
* recognizes several parameters: requirements, options, defaults, schemes,
|
||||
* methods, host, and name. The name parameter is mandatory.
|
||||
* Here is an example of how you should be able to use it:
|
||||
*
|
||||
* /**
|
||||
* * @Route("/Blog")
|
||||
* * /
|
||||
@@ -44,7 +44,6 @@ use Symfony\Component\Routing\RouteCollection;
|
||||
* public function index()
|
||||
* {
|
||||
* }
|
||||
*
|
||||
* /**
|
||||
* * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"})
|
||||
* * /
|
||||
@@ -52,34 +51,50 @@ use Symfony\Component\Routing\RouteCollection;
|
||||
* {
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* On PHP 8, the annotation class can be used as an attribute as well:
|
||||
* #[Route('/Blog')]
|
||||
* class Blog
|
||||
* {
|
||||
* #[Route('/', name: 'blog_index')]
|
||||
* public function index()
|
||||
* {
|
||||
* }
|
||||
* #[Route('/{id}', name: 'blog_post', requirements: ["id" => '\d+'])]
|
||||
* public function show()
|
||||
* {
|
||||
* }
|
||||
* }
|
||||
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Alexander M. Turek <me@derrabus.de>
|
||||
*/
|
||||
abstract class AnnotationClassLoader implements LoaderInterface
|
||||
{
|
||||
protected $reader;
|
||||
protected $env;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
|
||||
protected $routeAnnotationClass = RouteAnnotation::class;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $defaultRouteIndex = 0;
|
||||
|
||||
public function __construct(Reader $reader)
|
||||
public function __construct(Reader $reader = null, string $env = null)
|
||||
{
|
||||
$this->reader = $reader;
|
||||
$this->env = $env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the annotation class to read route properties from.
|
||||
*
|
||||
* @param string $class A fully-qualified class name
|
||||
*/
|
||||
public function setRouteAnnotationClass($class)
|
||||
public function setRouteAnnotationClass(string $class)
|
||||
{
|
||||
$this->routeAnnotationClass = $class;
|
||||
}
|
||||
@@ -87,14 +102,13 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
/**
|
||||
* Loads from annotations from a class.
|
||||
*
|
||||
* @param string $class A class name
|
||||
* @param string|null $type The resource type
|
||||
* @param string $class A class name
|
||||
*
|
||||
* @return RouteCollection A RouteCollection instance
|
||||
* @return RouteCollection
|
||||
*
|
||||
* @throws \InvalidArgumentException When route can't be parsed
|
||||
*/
|
||||
public function load($class, $type = null)
|
||||
public function load($class, string $type = null)
|
||||
{
|
||||
if (!class_exists($class)) {
|
||||
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
|
||||
@@ -110,29 +124,36 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
$collection = new RouteCollection();
|
||||
$collection->addResource(new FileResource($class->getFileName()));
|
||||
|
||||
if ($globals['env'] && $this->env !== $globals['env']) {
|
||||
return $collection;
|
||||
}
|
||||
|
||||
foreach ($class->getMethods() as $method) {
|
||||
$this->defaultRouteIndex = 0;
|
||||
foreach ($this->reader->getMethodAnnotations($method) as $annot) {
|
||||
if ($annot instanceof $this->routeAnnotationClass) {
|
||||
$this->addRoute($collection, $annot, $globals, $class, $method);
|
||||
}
|
||||
foreach ($this->getAnnotations($method) as $annot) {
|
||||
$this->addRoute($collection, $annot, $globals, $class, $method);
|
||||
}
|
||||
}
|
||||
|
||||
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
|
||||
$globals = $this->resetGlobals();
|
||||
foreach ($this->reader->getClassAnnotations($class) as $annot) {
|
||||
if ($annot instanceof $this->routeAnnotationClass) {
|
||||
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
|
||||
}
|
||||
foreach ($this->getAnnotations($class) as $annot) {
|
||||
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
|
||||
}
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
|
||||
/**
|
||||
* @param RouteAnnotation $annot or an object that exposes a similar interface
|
||||
*/
|
||||
protected function addRoute(RouteCollection $collection, object $annot, array $globals, \ReflectionClass $class, \ReflectionMethod $method)
|
||||
{
|
||||
if ($annot->getEnv() && $annot->getEnv() !== $this->env) {
|
||||
return;
|
||||
}
|
||||
|
||||
$name = $annot->getName();
|
||||
if (null === $name) {
|
||||
$name = $this->getDefaultRouteName($class, $method);
|
||||
@@ -143,7 +164,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
|
||||
foreach ($requirements as $placeholder => $requirement) {
|
||||
if (\is_int($placeholder)) {
|
||||
@trigger_error(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s::%s()"?', $placeholder, $requirement, $name, $class->getName(), $method->getName()), E_USER_DEPRECATED);
|
||||
throw new \InvalidArgumentException(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s::%s()"?', $placeholder, $requirement, $name, $class->getName(), $method->getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,10 +179,8 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
$host = $globals['host'];
|
||||
}
|
||||
|
||||
$condition = $annot->getCondition();
|
||||
if (null === $condition) {
|
||||
$condition = $globals['condition'];
|
||||
}
|
||||
$condition = $annot->getCondition() ?? $globals['condition'];
|
||||
$priority = $annot->getPriority() ?? $globals['priority'];
|
||||
|
||||
$path = $annot->getLocalizedPaths() ?: $annot->getPath();
|
||||
$prefix = $globals['localized_paths'] ?: $globals['path'];
|
||||
@@ -208,10 +227,11 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
$this->configureRoute($route, $class, $method, $annot);
|
||||
if (0 !== $locale) {
|
||||
$route->setDefault('_locale', $locale);
|
||||
$route->setRequirement('_locale', preg_quote($locale));
|
||||
$route->setDefault('_canonical_route', $name);
|
||||
$collection->add($name.'.'.$locale, $route);
|
||||
$collection->add($name.'.'.$locale, $route, $priority);
|
||||
} else {
|
||||
$collection->add($name, $route);
|
||||
$collection->add($name, $route, $priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,7 +239,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports($resource, $type = null)
|
||||
public function supports($resource, string $type = null)
|
||||
{
|
||||
return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
|
||||
}
|
||||
@@ -241,9 +261,6 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
/**
|
||||
* Gets the default route name for a class method.
|
||||
*
|
||||
* @param \ReflectionClass $class
|
||||
* @param \ReflectionMethod $method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
|
||||
@@ -262,7 +279,15 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
{
|
||||
$globals = $this->resetGlobals();
|
||||
|
||||
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
|
||||
$annot = null;
|
||||
if (\PHP_VERSION_ID >= 80000 && ($attribute = $class->getAttributes($this->routeAnnotationClass, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null)) {
|
||||
$annot = $attribute->newInstance();
|
||||
}
|
||||
if (!$annot && $this->reader) {
|
||||
$annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass);
|
||||
}
|
||||
|
||||
if ($annot) {
|
||||
if (null !== $annot->getName()) {
|
||||
$globals['name'] = $annot->getName();
|
||||
}
|
||||
@@ -301,9 +326,12 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
$globals['condition'] = $annot->getCondition();
|
||||
}
|
||||
|
||||
$globals['priority'] = $annot->getPriority() ?? 0;
|
||||
$globals['env'] = $annot->getEnv();
|
||||
|
||||
foreach ($globals['requirements'] as $placeholder => $requirement) {
|
||||
if (\is_int($placeholder)) {
|
||||
@trigger_error(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" in "%s"?', $placeholder, $requirement, $class->getName()), E_USER_DEPRECATED);
|
||||
throw new \InvalidArgumentException(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" in "%s"?', $placeholder, $requirement, $class->getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -311,7 +339,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
return $globals;
|
||||
}
|
||||
|
||||
private function resetGlobals()
|
||||
private function resetGlobals(): array
|
||||
{
|
||||
return [
|
||||
'path' => null,
|
||||
@@ -324,13 +352,43 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
||||
'host' => '',
|
||||
'condition' => '',
|
||||
'name' => '',
|
||||
'priority' => 0,
|
||||
'env' => null,
|
||||
];
|
||||
}
|
||||
|
||||
protected function createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition)
|
||||
protected function createRoute(string $path, array $defaults, array $requirements, array $options, ?string $host, array $schemes, array $methods, ?string $condition)
|
||||
{
|
||||
return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
|
||||
}
|
||||
|
||||
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
|
||||
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot);
|
||||
|
||||
/**
|
||||
* @param \ReflectionClass|\ReflectionMethod $reflection
|
||||
*
|
||||
* @return iterable<int, RouteAnnotation>
|
||||
*/
|
||||
private function getAnnotations(object $reflection): iterable
|
||||
{
|
||||
if (\PHP_VERSION_ID >= 80000) {
|
||||
foreach ($reflection->getAttributes($this->routeAnnotationClass, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
|
||||
yield $attribute->newInstance();
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->reader) {
|
||||
return;
|
||||
}
|
||||
|
||||
$anntotations = $reflection instanceof \ReflectionClass
|
||||
? $this->reader->getClassAnnotations($reflection)
|
||||
: $this->reader->getMethodAnnotations($reflection);
|
||||
|
||||
foreach ($anntotations as $annotation) {
|
||||
if ($annotation instanceof $this->routeAnnotationClass) {
|
||||
yield $annotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user