Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ jobs:
symfony-require: "5.0.*"

- php-version: "8.0"
symfony-require: "*"
symfony-require: "5.0.*"

- php-version: "8.0"
symfony-require: "6.0.*"

steps:
- name: "Checkout project"
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
],
"require": {
"php": "^7.2 || ^8.0",
"symfony/routing": "^4.4 || ^5.0",
"symfony/http-kernel": "^4.4 || ^5.0",
"symfony/routing": "^4.4 || ^5.0 || ^6.0",
"symfony/http-kernel": "^4.4 || ^5.0 || ^6.0",
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"symfony/config": "^4.4 || ^5.0",
"symfony/event-dispatcher": "^4.4 || ^5.0",
"symfony/phpunit-bridge": "^5.4 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.0 || ^6.0",
"symfony/config": "^4.4 || ^5.0 || ^6.0",
"symfony/event-dispatcher": "^4.4 || ^5.0 || ^6.0",
"symfony-cmf/testing": "^3@dev"
},
"suggest": {
Expand Down
16 changes: 7 additions & 9 deletions src/ChainRouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ public function __clone()
*
* @return \ArrayIterator An \ArrayIterator object for iterating over routes
*/
public function getIterator()
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->all());
}

/**
* Gets the number of Routes in this collection.
*
* @return int The number of routes
*/
public function count()
public function count(): int
{
$count = 0;
foreach ($this->routeCollections as $routeCollection) {
Expand Down Expand Up @@ -81,7 +79,7 @@ public function add($name, Route $route, int $priority = 0)
*
* @return Route[] An array of routes
*/
public function all()
public function all(): array
{
$routeCollectionAll = new RouteCollection();
foreach ($this->routeCollections as $routeCollection) {
Expand All @@ -95,17 +93,17 @@ public function all()
* Gets a route by name.
*
* @param string $name The route name
*
* @return Route|null A Route instance or null when not found
*/
public function get($name)
public function get($name): ?Route
{
foreach ($this->routeCollections as $routeCollection) {
$route = $routeCollection->get($name);
if (null !== $route) {
return $route;
}
}

return null;
}

/**
Expand Down Expand Up @@ -240,7 +238,7 @@ public function setMethods($methods)
*
* @return ResourceInterface[] An array of resources
*/
public function getResources()
public function getResources(): array
{
if (0 === count($this->routeCollections)) {
return [];
Expand Down
23 changes: 7 additions & 16 deletions src/ChainRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public function __construct(LoggerInterface $logger = null)
$this->logger = $logger;
}

/**
* @return RequestContext
*/
public function getContext()
public function getContext(): RequestContext
{
if (!$this->context) {
$this->context = new RequestContext();
Expand Down Expand Up @@ -143,7 +140,7 @@ protected function sortRouters()
*
* Note: You should use matchRequest if you can.
*/
public function match($pathinfo)
public function match($pathinfo): array
{
return $this->doMatch($pathinfo);
}
Expand All @@ -153,7 +150,7 @@ public function match($pathinfo)
*
* Loops through all routes and tries to match the passed request.
*/
public function matchRequest(Request $request)
public function matchRequest(Request $request): array
{
return $this->doMatch($request->getPathInfo(), $request);
}
Expand Down Expand Up @@ -213,21 +210,15 @@ private function doMatch($pathinfo, Request $request = null)
/**
* {@inheritdoc}
*
* @param mixed $name
*
* The CMF routing system used to allow to pass route objects as $name to generate the route.
* Since Symfony 5.0, the UrlGeneratorInterface declares $name as string. We widen the contract
* for BC but deprecate passing non-strings.
* Instead, Pass the RouteObjectInterface::OBJECT_BASED_ROUTE_NAME as route name and the object
* in the parameters with key RouteObjectInterface::ROUTE_OBJECT.
* @param string $name
*
* Loops through all registered routers and returns a router if one is found.
* It will always return the first route generated.
*/
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
if (is_object($name)) {
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.', E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check $name with is_string and error if it is not a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've applied the check!

if (!is_string($name)) {
throw new \InvalidArgumentException('The "$name" parameter should of type string.');
}

$debug = [];
Expand Down
41 changes: 13 additions & 28 deletions src/ContentAwareGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,28 @@ public function setContentRepository(ContentRepositoryInterface $contentReposito
/**
* {@inheritdoc}
*
* The CMF routing system used to allow to pass route objects as $name to generate the route.
* Since Symfony 5.0, the UrlGeneratorInterface declares $name as string. We widen the contract
* for BC but deprecate passing non-strings.
* Instead, Pass the RouteObjectInterface::OBJECT_BASED_ROUTE_NAME as route name and the object
* in the parameters with key RouteObjectInterface::ROUTE_OBJECT or the ID of a
* RouteReferrersReadInterface in 'content_id.
*
* @param mixed $name ignored
* @param array $parameters must either contain the field 'route' with a
* RouteObjectInterface or the field 'content_id'
* with the id of a document implementing
* RouteReferrersReadInterface
* @param string $name ignored
* @param array $parameters must either contain the field 'route' with a
* RouteObjectInterface or the field 'content_id'
* with the id of a document implementing RouteReferrersReadInterface
*
* @throws RouteNotFoundException If there is no such route in the database
*/
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
if ($name instanceof SymfonyRoute) {
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT` resp the content id with content_id.', E_USER_DEPRECATED);
if (!is_string($name)) {
throw new \InvalidArgumentException('The "$name" parameter should of type string.');
}

$route = $this->getBestLocaleRoute($name, $parameters);
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) {
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) {
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute
) {
$route = $this->getBestLocaleRoute($parameters[RouteObjectInterface::ROUTE_OBJECT], $parameters);
} else {
$route = $this->getRouteByContent($name, $parameters);
}
} elseif (is_string($name) && $name) {
} elseif (!empty($name)) {
$route = $this->getRouteByName($name, $parameters);
} else {
$route = $this->getRouteByContent($name, $parameters);
Expand Down Expand Up @@ -167,21 +159,16 @@ protected function getBestLocaleRoute(SymfonyRoute $route, $parameters)
* If no route with matching locale is found, falls back to just return the
* first route.
*
* @param mixed $name
* @param array $parameters which should contain a content field containing
* a RouteReferrersReadInterface object
*
* @return SymfonyRoute the route instance
*
* @throws RouteNotFoundException if no route can be determined
*/
protected function getRouteByContent($name, &$parameters)
protected function getRouteByContent(string $name, &$parameters)
{
if ($name instanceof RouteReferrersReadInterface) {
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.', E_USER_DEPRECATED);

$content = $name;
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface
) {
Expand All @@ -197,9 +184,7 @@ protected function getRouteByContent($name, &$parameters)
throw new RouteNotFoundException('Content repository did not return a RouteReferrersReadInterface instance for id '.$parameters['content_id']);
}
} else {
$hint = is_object($name) ? get_class($name) : gettype($name);

throw new RouteNotFoundException("The route name argument '$hint' is not a RouteReferrersReadInterface instance and there is no 'content_id' parameter");
throw new RouteNotFoundException(sprintf("The route name argument '%s' is not a RouteReferrersReadInterface instance and there is no 'content_id' parameter", gettype($name)));
}

$routes = $content->getRoutes();
Expand Down
14 changes: 7 additions & 7 deletions src/DynamicRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ public function getGenerator()
* Instead, Pass the RouteObjectInterface::OBJECT_BASED_ROUTE_NAME as route name and the object
* in the parameters with key RouteObjectInterface::ROUTE_OBJECT.
*
* @param string|Route $name The name of the route or the Route instance
* @param string $name The name of the route
*
* @throws RouteNotFoundException if route doesn't exist
*/
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
if (is_object($name)) {
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT', E_USER_DEPRECATED);
if (!is_string($name)) {
throw new \InvalidArgumentException('The "$name" parameter should of type string.');
}

if ($this->eventDispatcher) {
Expand Down Expand Up @@ -213,7 +213,7 @@ public function supports($name)
*
* @api
*/
public function match($pathinfo)
public function match($pathinfo): array
{
@trigger_error(__METHOD__.'() is deprecated since version 1.3 and will be removed in 2.0. Use matchRequest() instead.', E_USER_DEPRECATED);

Expand Down Expand Up @@ -252,7 +252,7 @@ public function match($pathinfo)
* @throws MethodNotAllowedException If a matching resource was found but
* the request method is not allowed
*/
public function matchRequest(Request $request)
public function matchRequest(Request $request): array
{
if ($this->eventDispatcher) {
$event = new RouterMatchEvent($request);
Expand Down Expand Up @@ -294,7 +294,7 @@ public function setContext(RequestContext $context)
*
* @api
*/
public function getContext()
public function getContext(): RequestContext
{
return $this->context;
}
Expand Down
14 changes: 5 additions & 9 deletions src/LazyRouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ public function __construct(RouteProviderInterface $provider)
/**
* {@inheritdoc}
*/
public function getIterator()
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->all());
}

/**
* Gets the number of Routes in this collection.
*
* @return int The number of routes
*/
public function count()
public function count(): int
{
return count($this->all());
}
Expand All @@ -52,7 +50,7 @@ public function count()
*
* @return Route[] An array of routes
*/
public function all()
public function all(): array
{
return $this->provider->getRoutesByNames(null);
}
Expand All @@ -61,15 +59,13 @@ public function all()
* Gets a route by name.
*
* @param string $name The route name
*
* @return Route|null A Route instance or null when not found
*/
public function get($name)
public function get($name): ?Route
{
try {
return $this->provider->getRouteByName($name);
} catch (RouteNotFoundException $e) {
return;
return null;
}
}
}
2 changes: 1 addition & 1 deletion src/NestedMatcher/NestedMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function setFinalMatcher(FinalMatcherInterface $final)
/**
* {@inheritdoc}
*/
public function matchRequest(Request $request)
public function matchRequest(Request $request): array
{
$collection = $this->routeProvider->getRouteCollectionForRequest($request);
if (!count($collection)) {
Expand Down
2 changes: 1 addition & 1 deletion src/NestedMatcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function finalMatch(RouteCollection $collection, Request $request)
/**
* {@inheritdoc}
*/
protected function getAttributes(Route $route, $name, array $attributes)
protected function getAttributes(Route $route, $name, array $attributes): array
{
if ($route instanceof RouteObjectInterface && is_string($route->getRouteKey())) {
$name = $route->getRouteKey();
Expand Down
14 changes: 8 additions & 6 deletions src/PagedRouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ protected function loadNextElements($offset)
}

/**
* {@inheritdoc}
* @return mixed
*/
#[\ReturnTypeWillChange]
public function current()
{
return current($this->currentRoutes);
Expand All @@ -79,7 +80,7 @@ public function current()
/**
* {@inheritdoc}
*/
public function next()
public function next(): void
{
$result = next($this->currentRoutes);
if (false === $result) {
Expand All @@ -89,8 +90,9 @@ public function next()
}

/**
* {@inheritdoc}
* @return mixed
*/
#[\ReturnTypeWillChange]
public function key()
{
return key($this->currentRoutes);
Expand All @@ -99,15 +101,15 @@ public function key()
/**
* {@inheritdoc}
*/
public function valid()
public function valid(): bool
{
return key($this->currentRoutes);
return null !== key($this->currentRoutes);
}

/**
* {@inheritdoc}
*/
public function rewind()
public function rewind(): void
{
$this->current = 0;
$this->currentRoutes = null;
Expand Down
Loading