1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
<?php
namespace ICanBoogie\Routing;
use ICanBoogie\HTTP\Request;
class RouteMaker
{
const ACTION_INDEX = 'index';
const ACTION_NEW = 'new';
const ACTION_CREATE = 'create';
const ACTION_SHOW = 'show';
const ACTION_EDIT = 'edit';
const ACTION_UPDATE = 'update';
const ACTION_DELETE = 'delete';
const OPTION_ID_NAME = 'id_name';
const OPTION_ID_REGEX = 'id_regex';
const OPTION_ONLY = 'only';
const OPTION_EXCEPT = 'except';
const OPTION_AS = 'as';
const OPTION_ACTIONS = 'actions';
const SEPARATOR = ':';
const CONTROLLER_ACTION_SEPARATOR = '#';
static public function actions($name, $controller, $actions, array $options = [])
{
$options = self::normalize_options($options);
$actions = static::filter_actions($actions, $options);
$actions = static::resolve_patterns($name, $actions, $options);
$options_as = $options[self::OPTION_AS];
$routes = [];
foreach ($actions as $action => list($pattern, $via))
{
$as = empty($options_as[$action]) ? $name . self::SEPARATOR . $action : $options_as[$action];
$routes[$as] = [
RouteDefinition::PATTERN => $pattern,
RouteDefinition::CONTROLLER => $controller,
RouteDefinition::ACTION => $action,
RouteDefinition::VIA => $via,
RouteDefinition::ID => $as
];
}
return $routes;
}
static public function resource($name, $controller, array $options = [])
{
$options = static::normalize_options($options);
$actions = array_merge(static::get_resource_actions(), $options[self::OPTION_ACTIONS]);
return static::actions($name, $controller, $actions, $options);
}
static protected function normalize_options(array $options)
{
return $options + [
self::OPTION_ID_NAME => 'id',
self::OPTION_ID_REGEX => '\d+',
self::OPTION_ONLY => [ ],
self::OPTION_EXCEPT => [ ],
self::OPTION_AS => [ ],
self::OPTION_ACTIONS => [ ]
];
}
static protected function get_resource_actions()
{
return [
self::ACTION_INDEX => [ '/{name}', Request::METHOD_GET ],
self::ACTION_NEW => [ '/{name}/new', Request::METHOD_GET ],
self::ACTION_CREATE => [ '/{name}', Request::METHOD_POST ],
self::ACTION_SHOW => [ '/{name}/{id}', Request::METHOD_GET ],
self::ACTION_EDIT => [ '/{name}/{id}/edit', Request::METHOD_GET ],
self::ACTION_UPDATE => [ '/{name}/{id}', [ Request::METHOD_PUT, Request::METHOD_PATCH ] ],
self::ACTION_DELETE => [ '/{name}/{id}', Request::METHOD_DELETE ]
];
}
static protected function filter_actions(array $actions, array $options = [])
{
if ($options[self::OPTION_ONLY])
{
$actions = array_intersect_key($actions, array_flip((array) $options[self::OPTION_ONLY]));
}
if ($options[self::OPTION_EXCEPT])
{
$actions = array_diff_key($actions, array_flip((array) $options[self::OPTION_EXCEPT]));
}
return $actions;
}
static protected function resolve_patterns($name, array $actions, $options)
{
$id = "<{$options[self::OPTION_ID_NAME]}:{$options[self::OPTION_ID_REGEX]}>";
$replace = [ '{name}' => $name, '{id}' => $id ];
foreach ($actions as $action => &$template)
{
$template[0] = strtr($template[0], $replace);
}
return $actions;
}
}