diff --git a/framework/core/src/Extend/Route.php b/framework/core/src/Extend/Route.php deleted file mode 100644 index 260bb37a5..000000000 --- a/framework/core/src/Extend/Route.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * 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) - ); - } -} diff --git a/framework/core/src/Extend/Routes.php b/framework/core/src/Extend/Routes.php new file mode 100644 index 000000000..c8dda4255 --- /dev/null +++ b/framework/core/src/Extend/Routes.php @@ -0,0 +1,86 @@ + + * + * 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']) + ); + } + } +}