mirror of
https://github.com/flarum/core.git
synced 2025-07-19 07:41:22 +02:00
Tweak route registration extender: Use plural
This makes it more consistent with other existing extenders, while also making registration of multiple routes more comfortable for extension developers, and likely slightly more performant. :-)
This commit is contained in:
@@ -1,49 +0,0 @@
|
|||||||
<?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\Extend;
|
|
||||||
|
|
||||||
use Flarum\Http\RouteCollection;
|
|
||||||
use Flarum\Http\RouteHandlerFactory;
|
|
||||||
use Illuminate\Contracts\Container\Container;
|
|
||||||
|
|
||||||
class Route implements Extender
|
|
||||||
{
|
|
||||||
protected $appName;
|
|
||||||
protected $name;
|
|
||||||
protected $httpMethod;
|
|
||||||
protected $path;
|
|
||||||
protected $handler;
|
|
||||||
|
|
||||||
public function __construct($appName, $name, $httpMethod, $path, $handler)
|
|
||||||
{
|
|
||||||
$this->appName = $appName;
|
|
||||||
$this->name = $name;
|
|
||||||
$this->httpMethod = $httpMethod;
|
|
||||||
$this->path = $path;
|
|
||||||
$this->handler = $handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function apply(Container $container)
|
|
||||||
{
|
|
||||||
/** @var RouteCollection $routes */
|
|
||||||
$collection = $container->make("flarum.{$this->appName}.routes");
|
|
||||||
|
|
||||||
/** @var RouteHandlerFactory $factory */
|
|
||||||
$factory = $container->make(RouteHandlerFactory::class);
|
|
||||||
|
|
||||||
$collection->{$this->httpMethod}(
|
|
||||||
$this->path,
|
|
||||||
$this->name,
|
|
||||||
$factory->toController($this->handler)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
86
framework/core/src/Extend/Routes.php
Normal file
86
framework/core/src/Extend/Routes.php
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
<?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\Extend;
|
||||||
|
|
||||||
|
use Flarum\Http\RouteHandlerFactory;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
|
class Routes implements Extender
|
||||||
|
{
|
||||||
|
protected $appName;
|
||||||
|
|
||||||
|
protected $routes = [];
|
||||||
|
|
||||||
|
public function __construct($appName)
|
||||||
|
{
|
||||||
|
$this->appName = $appName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($path, $name, $handler)
|
||||||
|
{
|
||||||
|
return $this->route('GET', $path, $name, $handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function post($path, $name, $handler)
|
||||||
|
{
|
||||||
|
return $this->route('POST', $path, $name, $handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function put($path, $name, $handler)
|
||||||
|
{
|
||||||
|
return $this->route('PUT', $path, $name, $handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function patch($path, $name, $handler)
|
||||||
|
{
|
||||||
|
return $this->route('PATCH', $path, $name, $handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete($path, $name, $handler)
|
||||||
|
{
|
||||||
|
return $this->route('DELETE', $path, $name, $handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function route($httpMethod, $path, $name, $handler)
|
||||||
|
{
|
||||||
|
$this->routes[] = [
|
||||||
|
'method' => $httpMethod,
|
||||||
|
'path' => $path,
|
||||||
|
'name' => $name,
|
||||||
|
'handler' => $handler
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function apply(Container $container)
|
||||||
|
{
|
||||||
|
if (empty($this->routes)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var \Flarum\Http\RouteCollection $collection */
|
||||||
|
$collection = $container->make("flarum.{$this->appName}.routes");
|
||||||
|
|
||||||
|
/** @var RouteHandlerFactory $factory */
|
||||||
|
$factory = $container->make(RouteHandlerFactory::class);
|
||||||
|
|
||||||
|
foreach ($this->routes as $route) {
|
||||||
|
$collection->addRoute(
|
||||||
|
$route['method'],
|
||||||
|
$route['path'],
|
||||||
|
$route['name'],
|
||||||
|
$factory->toController($route['handler'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user