mirror of
https://github.com/flarum/core.git
synced 2025-10-12 23:44:27 +02:00
Spent quite a while looking into the best solution here and ended up going with three separate classes. Thanks to @Luceos for the PR that got this rolling (#518). My reasoning is: - The task of routing and URL generation is independent for each section of the app. Take Flarum\Api\Users\IndexAction for example. I don't want to generate a URL to a Flarum route... I specifically want to generate a URL to an API route. So there should be a class with that specific responsibility. - In fact, each URL generator is slightly different, because we need to add a certain prefix to the start (e.g. /api) - This also allows us to get rid of the "flarum.api" prefix on each route's name. - It's still DRY, because they all extend a base class. At the same time, I could see no reason this needed to be "interfaced", so all of the classes are concrete. Goes a long way to fixing #123 - still just a few places left remaining with hardcoded URLs.
101 lines
2.3 KiB
PHP
101 lines
2.3 KiB
PHP
<?php
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Http;
|
|
|
|
use FastRoute\DataGenerator;
|
|
use FastRoute\RouteParser;
|
|
|
|
class RouteCollection
|
|
{
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $reverse = [];
|
|
|
|
/**
|
|
* @var DataGenerator
|
|
*/
|
|
protected $dataGenerator;
|
|
|
|
/**
|
|
* @var RouteParser
|
|
*/
|
|
protected $routeParser;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->dataGenerator = new DataGenerator\GroupCountBased;
|
|
$this->routeParser = new RouteParser\Std;
|
|
}
|
|
|
|
public function get($path, $name, $handler)
|
|
{
|
|
return $this->addRoute('GET', $path, $name, $handler);
|
|
}
|
|
|
|
public function post($path, $name, $handler)
|
|
{
|
|
return $this->addRoute('POST', $path, $name, $handler);
|
|
}
|
|
|
|
public function put($path, $name, $handler)
|
|
{
|
|
return $this->addRoute('PUT', $path, $name, $handler);
|
|
}
|
|
|
|
public function patch($path, $name, $handler)
|
|
{
|
|
return $this->addRoute('PATCH', $path, $name, $handler);
|
|
}
|
|
|
|
public function delete($path, $name, $handler)
|
|
{
|
|
return $this->addRoute('DELETE', $path, $name, $handler);
|
|
}
|
|
|
|
public function addRoute($method, $path, $name, $handler)
|
|
{
|
|
$routeDatas = $this->routeParser->parse($path);
|
|
|
|
foreach ($routeDatas as $routeData) {
|
|
$this->dataGenerator->addRoute($method, $routeData, $handler);
|
|
}
|
|
|
|
$this->reverse[$name] = $routeDatas;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getRouteData()
|
|
{
|
|
return $this->dataGenerator->getData();
|
|
}
|
|
|
|
protected function fixPathPart(&$part, $key, array $parameters)
|
|
{
|
|
if (is_array($part) && array_key_exists($part[0], $parameters)) {
|
|
$part = $parameters[$part[0]];
|
|
}
|
|
}
|
|
|
|
public function getPath($name, array $parameters = [])
|
|
{
|
|
if (isset($this->reverse[$name])) {
|
|
$parts = $this->reverse[$name][0];
|
|
array_walk($parts, [$this, 'fixPathPart'], $parameters);
|
|
|
|
return '/' . ltrim(implode('', $parts), '/');
|
|
}
|
|
|
|
throw new \RuntimeException("Route $name not found");
|
|
}
|
|
}
|