Skip to content

Commit

Permalink
Allow adding routes after applying others
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Feb 19, 2021
1 parent 902afdb commit 9785d63
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Http/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class RouteCollection
*/
protected $routes = [];

/**
* @var array
*/
protected $pendingRoutes = [];

public function __construct()
{
$this->dataGenerator = new DataGenerator\GroupCountBased;
Expand Down Expand Up @@ -69,24 +74,24 @@ public function delete($path, $name, $handler)
public function addRoute($method, $path, $name, $handler)
{
if (isset($this->routes[$method][$name])) {
throw new \RuntimeException("Route $name already exists");
throw new \RuntimeException("Route $name on method $method already exists");
}

$this->routes[$method][$name] = compact('path', 'handler');
$this->routes[$method][$name] = $this->pendingRoutes[$method][$name] = compact('path', 'handler');

return $this;
}

public function removeRoute(string $method, string $name): self
{
unset($this->routes[$method][$name]);
unset($this->routes[$method][$name], $this->pendingRoutes[$method][$name]);

return $this;
}

protected function applyRoutes(): void
{
foreach ($this->routes as $method => $routes) {
foreach ($this->pendingRoutes as $method => $routes) {
foreach ($routes as $name => $route) {
$routeDatas = $this->routeParser->parse($route['path']);

Expand All @@ -97,6 +102,8 @@ protected function applyRoutes(): void
$this->reverse[$name] = $routeDatas;
}
}

$this->pendingRoutes = [];
}

public function getRoutes(): array
Expand All @@ -106,7 +113,7 @@ public function getRoutes(): array

public function getRouteData()
{
if (empty($this->reverse)) {
if (! empty($this->pendingRoutes)) {
$this->applyRoutes();
}

Expand All @@ -122,7 +129,7 @@ protected function fixPathPart(&$part, $key, array $parameters)

public function getPath($name, array $parameters = [])
{
if (empty($this->reverse)) {
if (! empty($this->pendingRoutes)) {
$this->applyRoutes();
}

Expand Down
47 changes: 47 additions & 0 deletions tests/unit/Http/RouteCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Tests\unit\Http;

use Flarum\Http\RouteCollection;
use Flarum\Tests\unit\TestCase;

class RouteCollectionTest extends TestCase
{
/** @test */
public function can_add_routes()
{
$routeCollection = (new RouteCollection)
->addRoute('GET', '/index', 'index', function () {
echo 'index';
})
->addRoute('DELETE', '/posts', 'forum.posts.delete', function () {
echo 'delete posts';
});

$this->assertEquals('/index', $routeCollection->getPath('index'));
$this->assertEquals('/posts', $routeCollection->getPath('forum.posts.delete'));
}

/** @test */
public function can_add_routes_late()
{
$routeCollection = (new RouteCollection)->addRoute('GET', '/index', 'index', function () {
echo 'index';
});

$this->assertEquals('/index', $routeCollection->getPath('index'));

$routeCollection->addRoute('DELETE', '/posts', 'forum.posts.delete', function () {
echo 'delete posts';
});

$this->assertEquals('/posts', $routeCollection->getPath('forum.posts.delete'));
}
}

0 comments on commit 9785d63

Please sign in to comment.