Skip to content

Commit

Permalink
minor #33191 [Routing] Add more parameter types (derrabus)
Browse files Browse the repository at this point in the history
This PR was merged into the 5.0-dev branch.

Discussion
----------

[Routing] Add more parameter types

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32179
| License       | MIT
| Doc PR        | N/A

Commits
-------

daf402e [Routing] Add more parameter types.
  • Loading branch information
nicolas-grekas committed Aug 19, 2019
2 parents 0b6f45e + daf402e commit bbea1e6
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 73 deletions.
15 changes: 6 additions & 9 deletions src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,16 @@ public function __construct(Reader $reader)

/**
* 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;
}

/**
* 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
*
Expand Down Expand Up @@ -129,7 +126,7 @@ public function load($class, string $type = null)
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) {
Expand Down Expand Up @@ -217,7 +214,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
/**
* {@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);
}
Expand Down Expand Up @@ -306,7 +303,7 @@ protected function getGlobals(\ReflectionClass $class)
return $globals;
}

private function resetGlobals()
private function resetGlobals(): array
{
return [
'path' => null,
Expand All @@ -322,7 +319,7 @@ private function resetGlobals()
];
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ public function supports($resource, string $type = null)
/**
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(RouteCollection $collection, PhpFileLoader $loader,
$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);
Expand All @@ -49,7 +49,7 @@ final public function import($resource, $type = null, $ignoreErrors = false): Im
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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function load($file, string $type = null)
*
* @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;
Expand Down Expand Up @@ -103,7 +103,7 @@ public function supports($resource, string $type = null)
*
* @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));
Expand Down Expand Up @@ -144,7 +144,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p
*
* @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));
Expand Down Expand Up @@ -242,7 +242,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
* 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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function load($file, string $type = null)
/**
* {@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);
}
Expand All @@ -105,7 +105,7 @@ public function supports($resource, $type = null)
* @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'] : [];
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
* @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'] : '';
Expand Down Expand Up @@ -260,7 +260,7 @@ protected function parseImport(RouteCollection $collection, array $config, $path
* @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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TraceableUrlMatcher extends UrlMatcher

protected $traces;

public function getTraces($pathinfo)
public function getTraces(string $pathinfo)
{
$this->traces = [];

Expand All @@ -50,7 +50,7 @@ public function getTracesForRequest(Request $request)
return $traces;
}

protected function matchCollection($pathinfo, RouteCollection $routes)
protected function matchCollection(string $pathinfo, RouteCollection $routes)
{
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();
Expand Down
19 changes: 5 additions & 14 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
* @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()) {
Expand Down Expand Up @@ -207,12 +207,9 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
* 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'])) {
Expand All @@ -227,12 +224,9 @@ protected function getAttributes(Route $route, $name, array $attributes)
/**
* 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)])) {
Expand All @@ -245,12 +239,9 @@ protected function handleRouteRequirements($pathinfo, $name, Route $route)
/**
* 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) {
Expand All @@ -276,7 +267,7 @@ protected function getExpressionLanguage()
/**
* @internal
*/
protected function createRequest($pathinfo)
protected function createRequest(string $pathinfo)
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
return null;
Expand Down
43 changes: 13 additions & 30 deletions src/Symfony/Component/Routing/RouteCollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ public function __construct(LoaderInterface $loader = null)
*
* $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);
Expand Down Expand Up @@ -87,13 +85,9 @@ public function import($resource, $prefix = '/', $type = null)
/**
* 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);
Expand All @@ -114,10 +108,8 @@ public function createBuilder()

/**
* 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;
Expand All @@ -126,11 +118,9 @@ public function mount($prefix, self $builder)
/**
* 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
Expand All @@ -145,11 +135,9 @@ public function addRoute(Route $route, $name = null)
/**
* 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;

Expand All @@ -159,11 +147,9 @@ public function setHost($pattern)
/**
* 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;

Expand All @@ -174,12 +160,11 @@ public function setCondition($condition)
* 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;

Expand All @@ -190,12 +175,11 @@ public function setDefault($key, $value)
* 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;

Expand All @@ -206,12 +190,11 @@ public function setRequirement($key, $regex)
* 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class CustomXmlFileLoader extends XmlFileLoader
{
protected function loadFile($file)
protected function loadFile(string $file)
{
return XmlUtils::loadFile($file, function () { return true; });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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;
Expand Down

0 comments on commit bbea1e6

Please sign in to comment.