Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Separate RouteConfiguration from Router #2970

Merged
merged 34 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
86595f3
Use RouterInterface for injection of router
sorenmalling Feb 6, 2023
9d7073f
POC: RouteConfiguration and Routes DTO
sorenmalling Feb 28, 2023
f11459c
Tweak configuration manager and add signal
sorenmalling Mar 2, 2023
a26300f
styleci
sorenmalling Mar 2, 2023
10c740f
styleci
sorenmalling Mar 2, 2023
980290f
Correct commandcontroller
sorenmalling Mar 3, 2023
c31c97e
correct unpacking of arguments
sorenmalling Mar 3, 2023
367c4c0
Adjust tests to new reality
sorenmalling Mar 3, 2023
0080bf8
Set default empty routes collection
sorenmalling Mar 3, 2023
fb76934
use array for mockbuilder
sorenmalling Mar 3, 2023
02973f2
Further test adjustments
sorenmalling Mar 3, 2023
e6998fb
Adjust tests
sorenmalling Mar 3, 2023
c0fed35
Adjust tests
sorenmalling Mar 3, 2023
ef171fb
Adjust to feedback
sorenmalling Mar 3, 2023
73e0806
remove objects.yaml injection
sorenmalling Mar 3, 2023
41cf8ce
Assign routes to variable
sorenmalling Mar 3, 2023
6aec057
FEATURE: Introduce RoutesProvider concept for extension point
sorenmalling Mar 24, 2023
180fe40
TASK: Remove reference to Routes
sorenmalling Mar 24, 2023
818568a
Merge branch '8.3' into 2948/router-configuration
sorenmalling Mar 24, 2023
f2088c3
Merge branch '8.3' into 2948/router-configuration
mficzel Nov 30, 2023
28b7009
TASK: Readd `cache.lifetime` and `cache.tags` from configuration that…
mficzel Nov 30, 2023
b325530
Apply fixes from StyleCi
mficzel Nov 30, 2023
cea96b1
BUGFIX: Add return to createRoutes from Configuration, fix add and pr…
mficzel Nov 30, 2023
5bef616
TASK: Fix tests and ensure PHP 8.0 compatibility
mficzel Dec 22, 2023
6a32e94
TASK: Prevent duplicate array unpacking during `withPrependedRoute`
mficzel Dec 22, 2023
ee86457
Merge remote-tracking branch 'origin/9.0' into 2948/router-configuration
mhsdesign Jan 13, 2024
7326720
WIP introduce TestingRoutesProvider
mhsdesign Jan 13, 2024
c8678b5
TASK cleanup usage of RoutesProviderInterface in tests
mhsdesign Jan 13, 2024
286ac11
TASK inline routes variable
mhsdesign Jan 13, 2024
c9d0bcd
TASK validate routes in constructor
mhsdesign Jan 13, 2024
3aa3fc1
TASK document RoutesProviderInterface
mhsdesign Jan 13, 2024
5256af2
TASK cleanup Routes collection object
mhsdesign Jan 13, 2024
c69ce6e
Merge remote-tracking branch 'origin/9.0' into 2948/router-configuration
mhsdesign Feb 6, 2024
1f9edf1
TASK: Re-add missing Routing Unit tests
mhsdesign Feb 6, 2024
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
8 changes: 6 additions & 2 deletions Neos.Flow/Classes/Mvc/Routing/ConfigurationRoutesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
*/
final class ConfigurationRoutesProvider implements RoutesProviderInterface
{
private ConfigurationManager $configurationManager;

public function __construct(
private readonly ConfigurationManager $configurationManager
) {}
ConfigurationManager $configurationManager
) {
$this->configurationManager = $configurationManager;
}

public function getRoutes(): Routes
{
Expand Down
11 changes: 10 additions & 1 deletion Neos.Flow/Classes/Mvc/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ class Route
*/
protected $persistenceManager;

public static function fromConfiguration(array $configuration): self
public static function fromConfiguration(array $configuration): static
{
/** @phpstan-ignore-next-line */
$route = new static();
if (isset($configuration['name'])) {
$route->setName($configuration['name']);
Expand All @@ -192,6 +193,14 @@ public static function fromConfiguration(array $configuration): self
if (isset($configuration['appendExceedingArguments'])) {
$route->setAppendExceedingArguments($configuration['appendExceedingArguments']);
}
if (isset($configuration['cache'])) {
if (isset($configuration['cache']['lifetime']) && !is_null($configuration['cache']['lifetime'])) {
$route->setCacheLifetime(RouteLifetime::fromInt($configuration['cache']['lifetime']));
}
if (isset($configuration['cache']['tags']) && !empty($configuration['cache']['lifetime'])) {
$route->setCacheTags(RouteTags::createFromArray($configuration['cache']['tags']));
}
}

return $route;
}
Expand Down
2 changes: 0 additions & 2 deletions Neos.Flow/Classes/Mvc/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
use Neos\Flow\Mvc\Exception\NoMatchingRouteException;
use Neos\Flow\Mvc\Routing\Dto\ResolveContext;
use Neos\Flow\Mvc\Routing\Dto\RouteContext;
use Neos\Flow\Mvc\Routing\Dto\RouteLifetime;
use Neos\Flow\Mvc\Routing\Dto\RouteTags;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;

Expand Down
22 changes: 13 additions & 9 deletions Neos.Flow/Classes/Mvc/Routing/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
namespace Neos\Flow\Mvc\Routing;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Mvc\Exception\InvalidRouteSetupException;
use Traversable;

/**
* @Flow\Proxy(false)
* @implements \IteratorAggregate<int, Route>
*/
final class Routes implements \IteratorAggregate
{
/**
* @var array<int, Route>
*/
private readonly array $routes;
private array $routes;

private function __construct(
public function __construct(
Route ...$routes
) {
$this->routes = $routes;
}

public static function fromConfiguration(array $configuration): self
{
$routes = self::empty();
$routes = [];
$routesWithHttpMethodConstraints = [];
foreach ($configuration as $routeConfiguration) {
$route = Route::fromConfiguration($routeConfiguration);
Expand All @@ -44,23 +44,27 @@ public static function fromConfiguration(array $configuration): self
}
$routesWithHttpMethodConstraints[$uriPattern] = false;
}
$routes = $routes->append($route);
$routes[] = $route;
}
return new self(...$routes);
}

public static function empty(): self
{
return new self();
}
mhsdesign marked this conversation as resolved.
Show resolved Hide resolved
public function prepend(Route $route): self
public function withPrependedRoute(Route $route): self
{
return new self($route, ...$this->routes);
return new self(...[$route, ...$this->routes]);
}
mhsdesign marked this conversation as resolved.
Show resolved Hide resolved
public function append(Route $route): self
public function withAppendedRoute(Route $route): self
{
return new self(...$this->routes, $route);
return new self(...[...$this->routes, $route]);
}

/**
* @return \ArrayIterator<int, Route>
*/
public function getIterator(): Traversable
mhsdesign marked this conversation as resolved.
Show resolved Hide resolved
{
return new \ArrayIterator($this->routes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* source code.
*/

use Neos\Flow\Mvc\Routing\Route;
use Neos\Flow\Tests\FunctionalTestCase;

/**
Expand All @@ -31,17 +30,17 @@ protected function setUp(): void
{
parent::setUp();

$route = new Route();
$route->setName('Functional Test - Http::Client::InternalRequestEngine');
$route->setUriPattern('test/security/restricted');
$route->setDefaults([
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'Restricted',
'@action' => 'admin',
'@format' => 'html'
]);
$this->router->addRoute($route);
$this->registerRoute(
'Functional Test - Http::Client::InternalRequestEngine',
'test/security/restricted',
[
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'Restricted',
'@action' => 'admin',
'@format' => 'html'
]
);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions Neos.Flow/Tests/Functional/Http/RequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class RequestHandlerTest extends FunctionalTestCase
public function httpRequestIsConvertedToAnActionRequestAndDispatchedToTheRespectiveController(): void
{
$foundRoute = false;
foreach ($this->router->getRoutes() as $route) {
if ($route->getName() === 'Neos.Flow :: Functional Test: HTTP - FooController') {
$foundRoute = true;
if ($this->routes !== null) {
foreach ($this->routes as $route) {
if ($route->getName() === 'Neos.Flow :: Functional Test: HTTP - FooController') {
$foundRoute = true;
}
}
}
if (!$foundRoute) {
Expand Down
24 changes: 11 additions & 13 deletions Neos.Flow/Tests/Functional/Mvc/AbstractControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* source code.
*/

use Neos\Flow\Mvc\Routing\Route;
use Neos\Flow\Tests\FunctionalTestCase;

class AbstractControllerTest extends FunctionalTestCase
Expand All @@ -27,18 +26,17 @@ class AbstractControllerTest extends FunctionalTestCase
protected function setUp(): void
{
parent::setUp();

$route = new Route();
$route->setName('AbstractControllerTest Route 1');
$route->setUriPattern('test/mvc/abstractcontrollertesta/{@action}');
$route->setDefaults([
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Mvc\Fixtures',
'@controller' => 'AbstractControllerTestA',
'@format' =>'html'
]);
$route->setAppendExceedingArguments(true);
$this->router->addRoute($route);
$this->registerRoute(
'AbstractControllerTest Route 1',
'test/mvc/abstractcontrollertesta/{@action}',
[
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Mvc\Fixtures',
'@controller' => 'AbstractControllerTestA',
'@format' =>'html'
],
true
);
}

/**
Expand Down
43 changes: 6 additions & 37 deletions Neos.Flow/Tests/Functional/Mvc/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ protected function setUp(): void

$foundRoute = false;
/** @var $route Route */
foreach ($this->router->getRoutes() as $route) {
if ($route->getName() === 'Neos.Flow :: Functional Test: HTTP - FooController') {
$foundRoute = true;
break;
if ($this->routes !== null) {
foreach ($this->routes as $route) {
if ($route->getName() === 'Neos.Flow :: Functional Test: HTTP - FooController') {
$foundRoute = true;
break;
}
}
}

Expand Down Expand Up @@ -382,37 +384,4 @@ public function uriPathPrefixIsRespectedInRoute()

self::assertSame('/index.php/neos/flow/test/http/foo', (string)$actualResult);
}

/**
* @test
*/
public function explicitlySpecifiedRoutesOverruleConfiguredRoutes()
{
$routeValues = [
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Http\Fixtures',
'@controller' => 'Foo',
'@action' => 'index',
'@format' => 'html'
];
$routesConfiguration = [
[
'uriPattern' => 'custom/uri/pattern',
'defaults' => [
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Http\Fixtures',
'@controller' => 'Foo',
'@action' => 'index',
'@format' => 'html'
],
]
];
$this->router->setRoutesConfiguration($routesConfiguration);
$baseUri = new Uri('http://localhost');
$actualResult = $this->router->resolve(new ResolveContext($baseUri, $routeValues, false, '', RouteParameters::createEmpty()));
self::assertSame('/custom/uri/pattern', (string)$actualResult);

// reset router configuration for following tests
$this->router->setRoutesConfiguration(null);
}
mhsdesign marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion Neos.Flow/Tests/Functional/Mvc/UriBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function whenLinkingToSameHostTheUrlIsAsExpectedNotContainingDoubleSlashe
*/
public function whenLinkingToRootOfSameHostTheUrlContainsASingleSlash()
{
// NOTE: the route part handler here does not really match; as we link to the the route
// NOTE: the route part handler here does not really match; as we link to the route
// registered in "registerAbsoluteRoute()".
$this->registerSingleRoute(UriBuilderSetDomainRoutePartHandler::class);
// NOTE: the registered route is PREPENDED to the existing list; so we need to register the absolute route LAST as it should match FIRST.
Expand Down
103 changes: 51 additions & 52 deletions Neos.Flow/Tests/Functional/Security/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

use GuzzleHttp\Psr7\Uri;
use Neos\Flow\Mvc\Routing\Route;
use Neos\Flow\Security\AccountFactory;
use Neos\Flow\Security\AccountRepository;
use Neos\Flow\Tests\FunctionalTestCase;
Expand Down Expand Up @@ -56,57 +55,57 @@ protected function setUp(): void
$accountRepository->add($account3);
$this->persistenceManager->persistAll();

$route = new Route();
$route->setName('Functional Test - Security::Restricted');
$route->setUriPattern('test/security/restricted(/{@action})');
$route->setDefaults([
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'Restricted',
'@action' => 'public',
'@format' =>'html'
]);
$route->setAppendExceedingArguments(true);
$this->router->addRoute($route);

$route2 = new Route();
$route2->setName('Functional Test - Security::Authentication');
$route2->setUriPattern('test/security/authentication(/{@action})');
$route2->setDefaults([
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'Authentication',
'@action' => 'authenticate',
'@format' => 'html'
]);
$route2->setAppendExceedingArguments(true);
$this->router->addRoute($route2);

$route3 = new Route();
$route3->setName('Functional Test - Security::HttpBasicAuthentication');
$route3->setUriPattern('test/security/authentication/httpbasic(/{@action})');
$route3->setDefaults([
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'HttpBasicTest',
'@action' => 'authenticate',
'@format' => 'html'
]);
$route3->setAppendExceedingArguments(true);
$this->router->addRoute($route3);

$route4 = new Route();
$route4->setName('Functional Test - Security::UsernamePasswordAuthentication');
$route4->setUriPattern('test/security/authentication/usernamepassword(/{@action})');
$route4->setDefaults([
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'UsernamePasswordTest',
'@action' => 'authenticate',
'@format' => 'html'
]);
$route4->setAppendExceedingArguments(true);
$this->router->addRoute($route4);
$this->registerRoute(
'Functional Test - Security::Restricted',
'test/security/restricted(/{@action})',
[
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'Restricted',
'@action' => 'public',
'@format' =>'html'
],
true
);

$this->registerRoute(
'Functional Test - Security::Authentication',
'test/security/authentication(/{@action})',
[
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'Authentication',
'@action' => 'authenticate',
'@format' => 'html'
],
true
);

$this->registerRoute(
'Functional Test - Security::HttpBasicAuthentication',
'test/security/authentication/httpbasic(/{@action})',
[
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'HttpBasicTest',
'@action' => 'authenticate',
'@format' => 'html'
],
true
);

$this->registerRoute(
'Functional Test - Security::UsernamePasswordAuthentication',
'test/security/authentication/usernamepassword(/{@action})',
[
'@package' => 'Neos.Flow',
'@subpackage' => 'Tests\Functional\Security\Fixtures',
'@controller' => 'UsernamePasswordTest',
'@action' => 'authenticate',
'@format' => 'html'
],
true
);

$this->serverRequestFactory = $this->objectManager->get(ServerRequestFactoryInterface::class);
}
Expand Down
Loading
Loading