[Routing] Add more parameter types.

This commit is contained in:
Alexander M. Turek 2019-08-16 01:08:34 +02:00
parent 90e3da4edd
commit daf402e3f3
13 changed files with 42 additions and 73 deletions

View File

@ -74,10 +74,8 @@ abstract class AnnotationClassLoader implements LoaderInterface
/**
* 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;
}
@ -85,8 +83,7 @@ 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
*
@ -129,7 +126,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
return $collection;
}
protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
protected function addRoute(RouteCollection $collection, $annot, array $globals, \ReflectionClass $class, \ReflectionMethod $method)
{
$name = $annot->getName();
if (null === $name) {
@ -217,7 +214,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);
}
@ -306,7 +303,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
return $globals;
}
private function resetGlobals()
private function resetGlobals(): array
{
return [
'path' => null,
@ -322,7 +319,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
];
}
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);
}

View File

@ -81,11 +81,9 @@ class AnnotationFileLoader extends FileLoader
/**
* Returns the full class name for the first class in the file.
*
* @param string $file A PHP file path
*
* @return string|false Full class name if found, false otherwise
*/
protected function findClass($file)
protected function findClass(string $file)
{
$class = false;
$namespace = false;

View File

@ -33,7 +33,7 @@ class RoutingConfigurator
$this->file = $file;
}
final public function import($resource, $type = null, $ignoreErrors = false): ImportConfigurator
final public function import($resource, string $type = null, bool $ignoreErrors = false): ImportConfigurator
{
$this->loader->setCurrentDir(\dirname($this->path));
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
@ -49,7 +49,7 @@ class RoutingConfigurator
return new ImportConfigurator($this->collection, $mergedCollection);
}
final public function collection($name = ''): CollectionConfigurator
final public function collection(string $name = ''): CollectionConfigurator
{
return new CollectionConfigurator($this->collection, $name);
}

View File

@ -69,7 +69,7 @@ class XmlFileLoader extends FileLoader
*
* @throws \InvalidArgumentException When the XML is invalid
*/
protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file)
protected function parseNode(RouteCollection $collection, \DOMElement $node, string $path, string $file)
{
if (self::NAMESPACE_URI !== $node->namespaceURI) {
return;
@ -103,7 +103,7 @@ class XmlFileLoader extends FileLoader
*
* @throws \InvalidArgumentException When the XML is invalid
*/
protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
protected function parseRoute(RouteCollection $collection, \DOMElement $node, string $path)
{
if ('' === $id = $node->getAttribute('id')) {
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" attribute.', $path));
@ -144,7 +144,7 @@ class XmlFileLoader extends FileLoader
*
* @throws \InvalidArgumentException When the XML is invalid
*/
protected function parseImport(RouteCollection $collection, \DOMElement $node, $path, $file)
protected function parseImport(RouteCollection $collection, \DOMElement $node, string $path, string $file)
{
if ('' === $resource = $node->getAttribute('resource')) {
throw new \InvalidArgumentException(sprintf('The <import> element in file "%s" must have a "resource" attribute.', $path));
@ -242,7 +242,7 @@ class XmlFileLoader extends FileLoader
* or when the XML structure is not as expected by the scheme -
* see validate()
*/
protected function loadFile($file)
protected function loadFile(string $file)
{
return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH);
}

View File

@ -93,7 +93,7 @@ class YamlFileLoader extends FileLoader
/**
* {@inheritdoc}
*/
public function supports($resource, $type = null)
public function supports($resource, string $type = null)
{
return \is_string($resource) && \in_array(pathinfo($resource, PATHINFO_EXTENSION), ['yml', 'yaml'], true) && (!$type || 'yaml' === $type);
}
@ -105,7 +105,7 @@ class YamlFileLoader extends FileLoader
* @param array $config Route definition
* @param string $path Full path of the YAML file being processed
*/
protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
protected function parseRoute(RouteCollection $collection, string $name, array $config, string $path)
{
$defaults = isset($config['defaults']) ? $config['defaults'] : [];
$requirements = isset($config['requirements']) ? $config['requirements'] : [];
@ -157,7 +157,7 @@ class YamlFileLoader extends FileLoader
* @param string $path Full path of the YAML file being processed
* @param string $file Loaded file name
*/
protected function parseImport(RouteCollection $collection, array $config, $path, $file)
protected function parseImport(RouteCollection $collection, array $config, string $path, string $file)
{
$type = isset($config['type']) ? $config['type'] : null;
$prefix = isset($config['prefix']) ? $config['prefix'] : '';
@ -260,7 +260,7 @@ class YamlFileLoader extends FileLoader
* @throws \InvalidArgumentException If one of the provided config keys is not supported,
* something is missing or the combination is nonsense
*/
protected function validate($config, $name, $path)
protected function validate($config, string $name, string $path)
{
if (!\is_array($config)) {
throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));

View File

@ -32,7 +32,7 @@ trait CompiledUrlMatcherTrait
private $dynamicRoutes = [];
private $checkCondition;
public function match($pathinfo)
public function match(string $pathinfo)
{
$allow = $allowSchemes = [];
if ($ret = $this->doMatch($pathinfo, $allow, $allowSchemes)) {

View File

@ -29,7 +29,7 @@ class TraceableUrlMatcher extends UrlMatcher
protected $traces;
public function getTraces($pathinfo)
public function getTraces(string $pathinfo)
{
$this->traces = [];
@ -50,7 +50,7 @@ class TraceableUrlMatcher extends UrlMatcher
return $traces;
}
protected function matchCollection($pathinfo, RouteCollection $routes)
protected function matchCollection(string $pathinfo, RouteCollection $routes)
{
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();

View File

@ -128,7 +128,7 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
* @throws ResourceNotFoundException If the resource could not be found
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
*/
protected function matchCollection($pathinfo, RouteCollection $routes)
protected function matchCollection(string $pathinfo, RouteCollection $routes)
{
// HEAD and GET are equivalent as per RFC
if ('HEAD' === $method = $this->context->getMethod()) {
@ -207,12 +207,9 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
* in matchers that do not have access to the matched Route instance
* (like the PHP and Apache matcher dumpers).
*
* @param string $name The name of the route
* @param array $attributes An array of attributes from the matcher
*
* @return array An array of parameters
*/
protected function getAttributes(Route $route, $name, array $attributes)
protected function getAttributes(Route $route, string $name, array $attributes)
{
$defaults = $route->getDefaults();
if (isset($defaults['_canonical_route'])) {
@ -227,12 +224,9 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
/**
* Handles specific route requirements.
*
* @param string $pathinfo The path
* @param string $name The route name
*
* @return array The first element represents the status, the second contains additional information
*/
protected function handleRouteRequirements($pathinfo, $name, Route $route)
protected function handleRouteRequirements(string $pathinfo, string $name, Route $route)
{
// expression condition
if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), ['context' => $this->context, 'request' => $this->request ?: $this->createRequest($pathinfo)])) {
@ -245,12 +239,9 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
/**
* Get merged default parameters.
*
* @param array $params The parameters
* @param array $defaults The defaults
*
* @return array Merged default parameters
*/
protected function mergeDefaults($params, $defaults)
protected function mergeDefaults(array $params, array $defaults)
{
foreach ($params as $key => $value) {
if (!\is_int($key) && null !== $value) {
@ -276,7 +267,7 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
/**
* @internal
*/
protected function createRequest($pathinfo)
protected function createRequest(string $pathinfo)
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
return null;

View File

@ -48,15 +48,13 @@ class RouteCollectionBuilder
*
* $routes->import('blog.yml', '/blog');
*
* @param mixed $resource
* @param string|null $prefix
* @param string $type
* @param mixed $resource
*
* @return self
*
* @throws LoaderLoadException
*/
public function import($resource, $prefix = '/', $type = null)
public function import($resource, string $prefix = '/', string $type = null)
{
/** @var RouteCollection[] $collections */
$collections = $this->load($resource, $type);
@ -87,13 +85,9 @@ class RouteCollectionBuilder
/**
* Adds a route and returns it for future modification.
*
* @param string $path The route path
* @param string $controller The route's controller
* @param string|null $name The name to give this route
*
* @return Route
*/
public function add($path, $controller, $name = null)
public function add(string $path, string $controller, string $name = null)
{
$route = new Route($path);
$route->setDefault('_controller', $controller);
@ -114,10 +108,8 @@ class RouteCollectionBuilder
/**
* Add a RouteCollectionBuilder.
*
* @param string $prefix
*/
public function mount($prefix, self $builder)
public function mount(string $prefix, self $builder)
{
$builder->prefix = trim(trim($prefix), '/');
$this->routes[] = $builder;
@ -126,11 +118,9 @@ class RouteCollectionBuilder
/**
* Adds a Route object to the builder.
*
* @param string|null $name
*
* @return $this
*/
public function addRoute(Route $route, $name = null)
public function addRoute(Route $route, string $name = null)
{
if (null === $name) {
// used as a flag to know which routes will need a name later
@ -145,11 +135,9 @@ class RouteCollectionBuilder
/**
* Sets the host on all embedded routes (unless already set).
*
* @param string $pattern
*
* @return $this
*/
public function setHost($pattern)
public function setHost(?string $pattern)
{
$this->host = $pattern;
@ -159,11 +147,9 @@ class RouteCollectionBuilder
/**
* Sets a condition on all embedded routes (unless already set).
*
* @param string $condition
*
* @return $this
*/
public function setCondition($condition)
public function setCondition(?string $condition)
{
$this->condition = $condition;
@ -174,12 +160,11 @@ class RouteCollectionBuilder
* Sets a default value that will be added to all embedded routes (unless that
* default value is already set).
*
* @param string $key
* @param mixed $value
* @param mixed $value
*
* @return $this
*/
public function setDefault($key, $value)
public function setDefault(string $key, $value)
{
$this->defaults[$key] = $value;
@ -190,12 +175,11 @@ class RouteCollectionBuilder
* Sets a requirement that will be added to all embedded routes (unless that
* requirement is already set).
*
* @param string $key
* @param mixed $regex
* @param mixed $regex
*
* @return $this
*/
public function setRequirement($key, $regex)
public function setRequirement(string $key, $regex)
{
$this->requirements[$key] = $regex;
@ -206,12 +190,11 @@ class RouteCollectionBuilder
* Sets an option that will be added to all embedded routes (unless that
* option is already set).
*
* @param string $key
* @param mixed $value
* @param mixed $value
*
* @return $this
*/
public function setOption($key, $value)
public function setOption(string $key, $value)
{
$this->options[$key] = $value;

View File

@ -19,7 +19,7 @@ use Symfony\Component\Routing\Loader\XmlFileLoader;
*/
class CustomXmlFileLoader extends XmlFileLoader
{
protected function loadFile($file)
protected function loadFile(string $file)
{
return XmlUtils::loadFile($file, function () { return true; });
}

View File

@ -6,7 +6,7 @@ use Symfony\Component\Config\FileLocatorInterface;
class FileLocatorStub implements FileLocatorInterface
{
public function locate($name, $currentPath = null, $first = true)
public function locate(string $name, string $currentPath = null, bool $first = true)
{
if (0 === strpos($name, 'http')) {
return $name;

View File

@ -38,7 +38,7 @@ class GlobFileLoaderTest extends TestCase
class GlobFileLoaderWithoutImport extends GlobFileLoader
{
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
public function import($resource, string $type = null, bool $ignoreErrors = false, string $sourceResource = null)
{
return new RouteCollection();
}

View File

@ -98,7 +98,7 @@ class TestObjectLoader extends ObjectLoader
{
public $loaderMap = [];
public function supports($resource, $type = null)
public function supports($resource, string $type = null)
{
return 'service';
}