<?php
namespace Koselig\Routing;
use Illuminate\Container\Container;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
class Routing
{
public function template($slug, $action)
{
$action = $this->formatAction($action);
$route = (new TemplateRoute($action['method'], $slug, $action))
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
public function category($categories = [], $action = [])
{
if (empty($action)) {
$action = $categories;
$categories = [];
}
if (!is_array($categories)) {
$categories = [$categories];
}
$action = $this->formatAction($action);
$route = (new CategoryRoute($action['method'], $categories, $action))
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
public function page($slug, $action)
{
$action = $this->formatAction($action);
$route = (new PageRoute($action['method'], $slug, $action))
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
public function archive($postTypes = [], $action = [])
{
if (empty($action)) {
$action = $postTypes;
$postTypes = [];
}
if (!is_array($postTypes)) {
$postTypes = [$postTypes];
}
$action = $this->formatAction($action);
$route = (new ArchiveRoute($action['method'], $postTypes, $action))
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
public function singular($types, $action)
{
if (!is_array($types)) {
$types = [$types];
}
$action = $this->formatAction($action);
$route = (new SingularRoute($action['method'], $types, $action))
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
public function author($users, $action = [])
{
if (empty($action)) {
$action = $users;
$users = [];
}
if (!is_array($users)) {
$users = [$users];
}
$action = $this->formatAction($action);
$route = (new AuthorRoute($action['method'], $users, $action))
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
public function hasGroupStack()
{
return !empty($this->groupStack);
}
protected function actionReferencesController($action)
{
if (!$action instanceof \Closure) {
return is_string($action) || (isset($action['uses']) && is_string($action['uses']));
}
return false;
}
protected function prependGroupNamespace($class)
{
$group = end($this->groupStack);
return isset($group['namespace']) && strpos($class, '\\') !== 0
? $group['namespace'] . '\\' . $class : $class;
}
protected function convertToControllerAction($action)
{
if (is_string($action)) {
$action = ['uses' => $action];
}
if (!empty($this->groupStack)) {
$action['uses'] = $this->prependGroupNamespace($action['uses']);
}
$action['controller'] = $action['uses'];
return $action;
}
protected function formatAction($action)
{
if ($this->actionReferencesController($action)) {
$action = $this->convertToControllerAction($action);
}
if (!is_array($action)) {
$action = ['uses' => $action];
}
if (!isset($action['method'])) {
$action['method'] = ['GET'];
}
return $action;
}
protected function addWhereClausesToRoute($route)
{
$route->where(array_merge(
Route::getPatterns(), $route->getAction()['where'] ?? []
));
return $route;
}
protected function mergeGroupAttributesIntoRoute($route)
{
$route->setAction($this->mergeWithLastGroup($route->getAction()));
}
protected function applyStack($route)
{
if ($this->hasGroupStack()) {
$this->mergeGroupAttributesIntoRoute($route);
}
$this->addWhereClausesToRoute($route);
return $route;
}
}