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:
<?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\Route;
trait Instancing {
/**
* @inheritDocs
* @param string|array $patternOrConfig
* Required, configuration array or route pattern value
* to parse into match and reverse patterns.
* @param string $controllerAction
* Optional, controller and action name in pascal case
* like: `"Products:List"`.
* @param array $defaults
* Optional, default param values like:
* `["name" => "default-name", "page" => 1]`.
* @param array $constraints
* Optional, params regular expression constraints for
* regular expression match function if no `"match"`
* property in config array as first argument defined.
* @param array $advancedConfiguration
* Optional, http method to only match requests by this
* method. If `NULL` (by default), request with any http
* method could be matched by this route. Given value is
* automatically converted to upper case.
* @return \MvcCore\Route
*/
public static function CreateInstance (
$patternOrConfig = NULL,
$controllerAction = NULL,
$defaults = [],
$constraints = [],
$advancedConfiguration = []
) {
/** @var $this \MvcCore\Route */
return (new \ReflectionClass(get_called_class()))
->newInstanceArgs(func_get_args());
}
/**
* Create new route instance. First argument could be configuration array
* with all necessary constructor values or all separated arguments - first
* is route pattern value to parse into match and reverse values, then
* controller with action, params default values and constraints.
* Example:
* `new Route([
* "pattern" => "/products-list/<name>/<color>",
* "controllerAction" => "Products:List",
* "defaults" => ["name" => "default-name", "color" => "red"],
* "constraints" => ["name" => "[^/]*", "color" => "[a-z]*"]
* ]);`
* or:
* `new Route(
* "/products-list/<name>/<color>",
* "Products:List",
* ["name" => "default-name", "color" => "red"],
* ["name" => "[^/]*", "color" => "[a-z]*"]
* );`
* or:
* `new Route([
* "name" => "products_list",
* "match" => "#^/products\-list/(?<name>[^/]*)/(?<color>[a-z]*)(?=/$|$)#",
* "reverse" => "/products-list/<name>/<color>",
* "controller" => "Products",
* "action" => "List",
* "defaults" => ["name" => "default-name", "color" => "red"],
* ]);`
* @param string|array $patternOrConfig
* Required, configuration array or route pattern value
* to parse into match and reverse patterns.
* @param string $controllerAction
* Optional, controller and action name in pascal case
* like: `"Products:List"`.
* @param array $defaults
* Optional, default param values like:
* `["name" => "default-name", "page" => 1]`.
* @param array $constraints
* Optional, params regular expression constraints for
* regular expression match function no `"match"` record
* in configuration array as first argument defined.
* @param array $advancedConfiguration
* Optional, http method to only match requests by this
* method. If `NULL` (by default), request with any http
* method could be matched by this route. Given value is
* automatically converted to upper case.
* @return void
*/
public function __construct (
$patternOrConfig = NULL,
$controllerAction = NULL,
$defaults = [],
$constraints = [],
$advancedConfiguration = []
) {
/** @var $this \MvcCore\Route */
if (count(func_get_args()) === 0) return;
if (is_array($patternOrConfig)) {
$data = (object) $patternOrConfig;
$this->constructDataPatternsDefaultsConstraintsFilters($data);
$this->constructDataCtrlActionName($data);
$this->constructDataAdvConf($data);
$this->config = & $patternOrConfig;
} else {
$this->constructVarsPatternDefaultsConstraintsFilters(
$patternOrConfig, $defaults, $constraints, $advancedConfiguration
);
$this->constructVarCtrlActionNameByData($controllerAction);
$this->constructVarAdvConf($advancedConfiguration);
$this->config = & $advancedConfiguration;
}
$this->constructCtrlOrActionByName();
}
/**
* If route is initialized by single array argument with all data,
* initialize following properties if those exist in given object:
* `pattern`, `match` and `reverse`. If properties `defaults`, `constraints`
* and `filters` exist in given object, initialize them by setter methods.
* @param \stdClass $data Object containing properties `pattern`,
* `match`, `reverse`, `filters` and `defaults`.
* @return void
*/
protected function constructDataPatternsDefaultsConstraintsFilters (& $data) {
/** @var $this \MvcCore\Route */
if (isset($data->pattern))
$this->pattern = $data->pattern;
if (isset($data->match))
$this->match = $data->match;
if (isset($data->reverse))
$this->reverse = $data->reverse;
if (isset($data->defaults))
$this->SetDefaults($data->defaults);
if (isset($data->constraints))
$this->SetConstraints($data->constraints);
if (isset($data->filters) && is_array($data->filters))
$this->SetFilters($data->filters);
}
/**
* If route is initialized by single array argument with all data,
* initialize following properties if those exist in given object:
* `controller`, `action` (or `controllerAction`) and `name`.
* @param \stdClass $data Object containing properties `controller`,
* `action` (or `controllerAction`) and `name`.
* @return void
*/
protected function constructDataCtrlActionName (& $data) {
/** @var $this \MvcCore\Route */
if (isset($data->controllerAction)) {
list($ctrl, $action) = explode(':', $data->controllerAction);
if ($ctrl) $this->controller = $ctrl;
if ($action) $this->action = $action;
if (isset($data->name)) {
$this->name = $data->name;
} else {
$this->name = $data->controllerAction;
}
} else {
$this->controller = isset($data->controller) ? $data->controller : NULL;
$this->action = isset($data->action) ? $data->action : NULL;
if (isset($data->name)) {
$this->name = $data->name;
} else if ($this->controller !== NULL && $this->action !== NULL) {
$this->name = $this->controller . ':' . $this->action;
} else {
$this->name = NULL;
}
}
}
/**
* If route is initialized by single array argument with all data,
* initialize following properties if those exist in given object:
* `method`, `redirect` and `absolute`.
* @param \stdClass $data Object containing properties `method`,
* `redirect` and `absolute`.
* @return void
*/
protected function constructDataAdvConf (& $data) {
/** @var $this \MvcCore\Route */
$methodParam = static::CONFIG_METHOD;
if (isset($data->{$methodParam}))
$this->method = strtoupper((string) $data->{$methodParam});
$redirectParam = static::CONFIG_REDIRECT;
if (isset($data->{$redirectParam}))
$this->redirect = (string) $data->{$redirectParam};
$absoluteParam = static::CONFIG_ABSOLUTE;
if (isset($data->{$absoluteParam}))
$this->absolute = (bool) $data->{$absoluteParam};
}
/**
* If route is initialized by each constructor function arguments,
* initialize `pattern` and `defaults`, if those are not `NULL` and
* initialize constraints by setter if not `NULL` and initialize filter in
* and filter out by filter setter from `$advCfg` array if there are those
* filter keys found.
* @param string|NULL $pattern Route pattern string.
* @param array|NULL $defaults Route defaults array, keys are param
* names, values are default values.
* @param array|NULL $constraints Route params regular expression
* constraints array, keys are param
* names, values are allowed regular
* expression rules.
* @param array $advCfg An array with possible keys `in` and
* `out` to define route filter in and
* filter out callable.
* @return void
*/
protected function constructVarsPatternDefaultsConstraintsFilters (& $pattern, & $defaults, & $constraints, & $advCfg) {
/** @var $this \MvcCore\Route */
if ($pattern !== NULL)
$this->pattern = $pattern;
if ($defaults !== NULL)
$this->defaults = $defaults;
if ($constraints !== NULL)
$this->SetConstraints($constraints);
$filterInParam = static::CONFIG_FILTER_IN;
if (isset($advCfg[$filterInParam]))
$this->SetFilter($advCfg[$filterInParam], $filterInParam);
$filterOutParam = static::CONFIG_FILTER_OUT;
if (isset($advCfg[$filterOutParam]))
$this->SetFilter($advCfg[$filterOutParam], $filterOutParam);
}
/**
* If route is initialized by each constructor function arguments,
* initialize `controller` and `action`, if any of them is defined in given
* argument `$ctrlAction`.
* @param string|NULL $ctrlAction Controller and action combination
* definition, it could be `"Products:List"`
* or only `"Products:"` etc.
* @return void
*/
protected function constructVarCtrlActionNameByData (& $ctrlAction) {
/** @var $this \MvcCore\Route */
if ($ctrlAction !== NULL) {
list($ctrl, $action) = explode(':', $ctrlAction);
if ($ctrl) $this->controller = $ctrl;
if ($action) $this->action = $action;
}
}
/**
* If route is initialized by each constructor function arguments,
* initialize `method`, `redirect` and `absolute`.
* @param array $advCfg An array with possible keys `method`,
* `redirect` and `absolute`.
* @return void
*/
protected function constructVarAdvConf (& $advCfg) {
/** @var $this \MvcCore\Route */
$methodParam = static::CONFIG_METHOD;
if (isset($advCfg[$methodParam]))
$this->method = strtoupper((string) $advCfg[$methodParam]);
$redirectParam = static::CONFIG_REDIRECT;
if (isset($advCfg[$redirectParam]))
$this->redirect = (string) $advCfg[$redirectParam];
$absoluteParam = static::CONFIG_ABSOLUTE;
if (isset($advCfg[$absoluteParam]))
$this->absolute = (bool) $advCfg[$absoluteParam];
}
/**
* If route is initialized by each constructor function arguments or also
* if route is initialized by single array argument with all data, this
* function is called to initialize `controller` and `action` properties if
* those are still `NULL`. Function tries to initialize those properties
* from route `action` property`, if it contains colon char `:`.
* @param array $advCfg An array with possible keys `method`,
* `redirect` and `absolute`.
* @return void
*/
protected function constructCtrlOrActionByName () {
/** @var $this \MvcCore\Route */
if (!$this->controller && !$this->action && strpos($this->name, ':') !== FALSE && strlen($this->name) > 1) {
list($ctrl, $action) = explode(':', $this->name);
if ($ctrl) $this->controller = $ctrl;
if ($action) $this->action = $action;
}
}
}