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: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Router;
trait RouteMethods {
/**
* @inheritDocs
* @param \MvcCore\Route[]|array $routes
* Keyed array with routes, keys are route names or route
* `Controller::Action` definitions.
* @param string|NULL $groupName
* Group name is first matched/parsed word in requested path to
* group routes by to try to match only routes you really need,
* not all of them. If `NULL` by default, routes are inserted
* into default group.
* @param bool $autoInitialize
* If `TRUE`, locale routes array is cleaned and then all
* routes (or configuration arrays) are sent into method
* `$router->AddRoutes();`, where are routes auto initialized
* for missing route names or route controller or route action
* record, completed always from array keys. You can you `FALSE`
* to set routes without any change or auto-initialization, it
* could be useful to restore cached routes etc.
* @return \MvcCore\Router
*/
public function SetRoutes ($routes = [], $groupName = NULL, $autoInitialize = TRUE) {
/** @var $this \MvcCore\Router */
if ($autoInitialize) {
$this->routes = [];
$this->AddRoutes($routes, $groupName);
} else {
$routesAreEmpty = count($routes) === 0;
$noGroupNameDefined = $groupName === NULL;
// complete URL routes and routes with name keys
$newRoutes = [];
$this->urlRoutes = [];
foreach ($routes as $route) {
$routeName = $route->GetName();
$newRoutes[$routeName] = $route;
$this->urlRoutes[$routeName] = $route;
$controllerAction = $route->GetControllerAction();
if ($controllerAction !== ':')
$this->urlRoutes[$controllerAction] = $route;
if ($noGroupNameDefined) {
$routeGroupName = $route->GetGroupName();
if ($routeGroupName === NULL) $routeGroupName = '';
if (!array_key_exists($routeGroupName, $this->routesGroups))
$this->routesGroups[$routeGroupName] = [];
$this->routesGroups[$routeGroupName][] = $route;
}
}
$this->routes = $newRoutes;
if ($noGroupNameDefined) {
if ($routesAreEmpty) {
$this->routesGroups = [];
$this->noUrlRoutes = [];
}
$this->routesGroups[''] = $newRoutes;
} else {
$this->routesGroups[$groupName] = $newRoutes;
}
$this->anyRoutesConfigured = (!$routesAreEmpty) || $this->preRouteMatchingHandler !== NULL;
}
return $this;
}
/**
* @inheritDocs
* @param \MvcCore\Route[]|array $routes
* Keyed array with routes, keys are route names or route
* `Controller::Action` definitions.
* @param string|NULL $groupName
* Group name is first matched/parsed word in requested path to
* group routes by to try to match only routes you really need,
* not all of them. If `NULL` by default, routes are inserted
* into default group.
* @param bool $prepend
* Optional, if `TRUE`, all given routes will be prepended from
* the last to the first in given list, not appended.
* @param bool $throwExceptionForDuplication
* `TRUE` by default. Throw an exception, if route `name` or
* route `Controller:Action` has been defined already. If
* `FALSE` old route is over-written by new one.
* @return \MvcCore\Router
*/
public function AddRoutes (array $routes = [], $groupName = NULL, $prepend = FALSE, $throwExceptionForDuplication = TRUE) {
/** @var $this \MvcCore\Router */
if ($prepend) $routes = array_reverse($routes);
$routeClass = self::$routeClass;
foreach ($routes as $routeName => & $route) {
$numericKey = is_numeric($routeName);
$ctrlActionName = !$numericKey && mb_strpos($routeName, ':') !== FALSE;
if ($route instanceof \MvcCore\IRoute) {
if ($numericKey) {
if (!$route->GetName()) {
$routeAutoName = $route->GetControllerAction();
if ($routeAutoName === ':') $routeAutoName = 'Route_' . $routeName;
$route->SetName($routeAutoName);
}
} else {
if ($ctrlActionName) {
$route->SetControllerAction($routeName);
} else if ($route->GetName() !== $routeName && $route->GetName() === $route->GetControllerAction()) {
$route->SetName($routeName);
}
if ($route->GetName() === NULL)
$route->SetName($routeName);
}
$this->AddRoute(
$route, $groupName, $prepend, $throwExceptionForDuplication
);
} else if (is_array($route)) {
if (!$numericKey)
$route[$ctrlActionName ? 'controllerAction' : 'name'] = $routeName;
$this->AddRoute(
$this->getRouteInstance($route),
$groupName, $prepend, $throwExceptionForDuplication
);
} else if (is_string($route)) {
// route name is always Controller:Action
$routeCfgData = ['pattern' => $route];
$routeCfgData[$ctrlActionName ? 'controllerAction' : 'name'] = $routeName;
$this->AddRoute(
$routeClass::CreateInstance($routeCfgData),
$groupName, $prepend, $throwExceptionForDuplication
);
} else {
throw new \InvalidArgumentException (
"[".get_class()."] Route is not possible to assign"
." (key: `{$routeName}`, value: `" . serialize($route) . "`)."
);
}
}
$this->anyRoutesConfigured = count($routes) > 0 || $this->preRouteMatchingHandler !== NULL;
return $this;
}
/**
* @inheritDocs
* @param \MvcCore\Route|array $routeCfgOrRoute
* Route instance or route config array.
* @param string|NULL $groupName
* Group name is first matched/parsed word in requested path to
* group routes by to try to match only routes you really need,
* not all of them. If `NULL` by default, routes are inserted
* into default group.
* @param bool $prepend
* Optional, if `TRUE`, given route will be prepended,
* not appended.
* @param bool $throwExceptionForDuplication
* `TRUE` by default. Throw an exception, if route `name` or
* route `Controller:Action` has been defined already. If
* `FALSE` old route is over-written by new one.
* @return \MvcCore\Router
*/
public function AddRoute ($routeCfgOrRoute, $groupName = NULL, $prepend = FALSE, $throwExceptionForDuplication = TRUE) {
/** @var $this \MvcCore\Router */
$instance = $this->getRouteInstance($routeCfgOrRoute);
$routeName = $instance->GetName();
$controllerAction = $instance->GetControllerAction();
if ($throwExceptionForDuplication) {
$errorMsgs = [];
if (isset($this->routes[$routeName]))
$errorMsgs[] = 'Route with name `'.$routeName.'` has already been defined between router routes.';
if (isset($this->urlRoutes[$controllerAction]))
$errorMsgs[] = 'Route with `Controller:Action` combination: `'.$controllerAction.'` has already been defined between router routes.';
if ($errorMsgs) {
//var_dump($this->routes);
$debBack = debug_backtrace();
$debBackLength = count($debBack);
if ($debBackLength > 1) {
$debBackSemiFinalRec = $debBack[$debBackLength - 2];
$file = str_replace('\\', '/', $debBackSemiFinalRec['file']);
$bootstrapFilePath = '/App/Bootstrap.php';
if (mb_strpos($file, $bootstrapFilePath) === mb_strlen($file) - mb_strlen($bootstrapFilePath))
die('['.get_class().'] '.implode(' ',$errorMsgs));
}
throw new \InvalidArgumentException('['.get_class().'] '.implode(' ',$errorMsgs));
}
}
$this->urlRoutes[$routeName] = $instance;
if ($controllerAction !== ':') $this->urlRoutes[$controllerAction] = $instance;
$this->addRouteToGroup ($instance, $routeName, $groupName, $prepend);
if ($prepend) {
$newItem = [$routeName => $instance];
$this->routes = $newItem + $this->routes;
} else {
$this->routes[$routeName] = $instance;
}
$this->anyRoutesConfigured = TRUE;
return $this;
}
/**
* Add route instance into named routes group. Every routes group is chosen
* in routing moment by first parsed word from requested URL.
* @param \MvcCore\Route $route A route instance reference.
* @param string $routeName Route name.
* @param string|NULL $groupName Group name, first parsed word from requested URL.
* @param bool $prepend IF `TRUE`, prepend route instance, `FALSE` otherwise.
* @return void
*/
protected function addRouteToGroup (\MvcCore\IRoute $route, $routeName, $groupName, $prepend) {
/** @var $this \MvcCore\Router */
if ($groupName === NULL) {
$routesGroupsKey = '';
} else {
$routesGroupsKey = $groupName;
$route->SetGroupName($groupName);
}
if (isset($this->routesGroups[$routesGroupsKey])) {
$groupRoutes = & $this->routesGroups[$routesGroupsKey];
} else {
$groupRoutes = [];
$this->routesGroups[$routesGroupsKey] = & $groupRoutes;
}
if ($prepend) {
$newItem = [$routeName => $route];
$groupRoutes = $newItem + $groupRoutes;
} else {
$groupRoutes[$routeName] = $route;
}
}
/**
* @inheritDocs
* @param string|\MvcCore\Route $routeOrRouteName
* @return bool
*/
public function HasRoute ($routeOrRouteName) {
/** @var $this \MvcCore\Router */
if (is_string($routeOrRouteName)) {
return isset($this->routes[$routeOrRouteName]);
} else /*if ($routeOrRouteName instance of \MvcCore\IRoute)*/ {
return (
isset($this->routes[$routeOrRouteName->GetName()]) ||
isset($this->routes[$routeOrRouteName->GetControllerAction()])
);
}
//return FALSE;
}
/**
* @inheritDocs
* @param string $routeName
* @return \MvcCore\Route|NULL
*/
public function RemoveRoute ($routeName) {
/** @var $this \MvcCore\Router */
$result = NULL;
if (isset($this->routes[$routeName])) {
$result = $this->routes[$routeName];
unset($this->routes[$routeName]);
$this->removeRouteFromGroup($result, $routeName);
$controllerAction = $result->GetControllerAction();
if (isset($this->urlRoutes[$routeName]))
unset($this->urlRoutes[$routeName]);
if (isset($this->urlRoutes[$controllerAction]))
unset($this->urlRoutes[$controllerAction]);
/** @var $currentRoute \MvcCore\Route */
$currentRoute = $this->currentRoute;
if ($currentRoute->GetName() === $result->GetName())
$this->currentRoute = NULL;
}
if (!$this->routes && $this->preRouteMatchingHandler === NULL)
$this->anyRoutesConfigured = FALSE;
return $result;
}
/**
* Unset route from defined group. This method doesn't unset the route
* from router object to not be possible to create URL by given route anymore.
* This does route method: `\MvcCore\Route::RemoveRoute($routeName);`.
* @param \MvcCore\IRoute $route
* @param string $routeName
* @return void
*/
protected function removeRouteFromGroup (\MvcCore\IRoute $route, $routeName) {
/** @var $this \MvcCore\Router */
$routeGroup = $route->GetGroupName();
$groupRoutesKey = $routeGroup ?: '';
if (isset($this->routesGroups[$groupRoutesKey]))
unset($this->routesGroups[$groupRoutesKey][$routeName]);
}
/**
* @inheritDocs
* @param string|NULL $groupName
* Group name is first matched/parsed word in requested path to
* group routes by to try to match only routes you really need,
* not all of them. If `NULL` by default, there are returned
* all routes from all groups.
* @return \MvcCore\Route[]
*/
public function GetRoutes ($groupName = NULL) {
/** @var $this \MvcCore\Router */
if ($groupName !== NULL)
return $this->routesGroups[$groupName];
return $this->routes;
}
/**
* @inheritDocs
* @return \MvcCore\Route|NULL
*/
public function GetRoute ($routeName) {
/** @var $this \MvcCore\Router */
if (isset($this->routes[$routeName]))
return $this->routes[$routeName];
return NULL;
}
/**
* @inheritDocs
* @param \MvcCore\Route $currentRoute
* @return \MvcCore\Router
*/
public function SetCurrentRoute (\MvcCore\IRoute $currentRoute) {
/** @var $this \MvcCore\Router */
/** @var $currentRoute \MvcCore\Route */
$this->currentRoute = $currentRoute;
return $this;
}
/**
* @inheritDocs
* @return \MvcCore\Route
*/
public function GetCurrentRoute () {
/** @var $this \MvcCore\Router */
return $this->currentRoute;
}
/**
* Get always route instance from given route configuration data or return
* already created given instance.
* @param \MvcCore\Route|array $routeCfgOrRoute Route instance or route config array.
* @return \MvcCore\Route
*/
protected function getRouteInstance (& $routeCfgOrRoute) {
/** @var $this \MvcCore\Router */
if ($routeCfgOrRoute instanceof \MvcCore\IRoute)
return $routeCfgOrRoute->SetRouter($this);
$routeClass = self::$routeClass;
return $routeClass::CreateInstance($routeCfgOrRoute)->SetRouter($this);
}
}