More development

This commit is contained in:
Simon Sessingø
2016-11-19 05:22:51 +01:00
parent 4e12cb8bc3
commit d4a04920b8
5 changed files with 57 additions and 45 deletions
+11 -5
View File
@@ -77,15 +77,21 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
return $this; return $this;
} }
public function setData(array $settings) /**
* Merge with information from another route.
*
* @param array $values
* @return static
*/
public function merge(array $values)
{ {
// Change as to alias // Change as to alias
if (isset($settings['as'])) { if (isset($values['as'])) {
$this->setAlias($settings['as']); $this->setAlias($values['as']);
} }
return parent::setData($settings); parent::merge($values);
return $this;
} }
} }
+2 -4
View File
@@ -142,7 +142,7 @@ class RouterBase
/* @var $route RouterEntry */ /* @var $route RouterEntry */
foreach ($routes as $route) { foreach ($routes as $route) {
$route->setData($settings); $route->merge($settings);
if ($parent !== null) { if ($parent !== null) {
$route->setParent($parent); $route->setParent($parent);
@@ -170,7 +170,7 @@ class RouterBase
if ($route->matchRoute($this->request)) { if ($route->matchRoute($this->request)) {
$mergedSettings = array_merge($mergedSettings, $route->getMergeableData()); $mergedSettings = array_merge($mergedSettings, $route->toArray());
// Add ExceptionHandler // Add ExceptionHandler
if (count($route->getExceptionHandlers()) > 0) { if (count($route->getExceptionHandlers()) > 0) {
@@ -188,8 +188,6 @@ class RouterBase
// Route any routes added to the backstack // Route any routes added to the backstack
$this->processRoutes($backStack, $mergedSettings, $prefixes, $route); $this->processRoutes($backStack, $mergedSettings, $prefixes, $route);
} }
} }
} }
+23 -23
View File
@@ -360,64 +360,64 @@ abstract class RouterEntry
} }
/** /**
* Get arguments that can be inherited by child routes. * Export route settings to array so they can be merged with another route.
* *
* @return array * @return array
*/ */
public function getMergeableData() public function toArray()
{ {
$output = array(); $values = array();
if ($this->namespace !== null) { if ($this->namespace !== null) {
$output['namespace'] = $this->namespace; $values['namespace'] = $this->namespace;
} }
if (count($this->middlewares) > 0) { if (count($this->middlewares) > 0) {
$output['middleware'] = $this->middlewares; $values['middleware'] = $this->middlewares;
} }
/*if (count($this->where) > 0) { /*if (count($this->where) > 0) {
$output['where'] = $this->where; $values['where'] = $this->where;
}*/ }*/
if (count($this->requestMethods) > 0) { if (count($this->requestMethods) > 0) {
$output['method'] = $this->requestMethods; $values['method'] = $this->requestMethods;
} }
if (count($this->parameters) > 0) { if (count($this->parameters) > 0) {
$output['parameters'] = $this->parameters; $values['parameters'] = $this->parameters;
} }
return $output; return $values;
} }
/** /**
* Set arguments/data by array * Merge with information from another route.
* *
* @param array $settings * @param array $values
* @return static * @return static $this
*/ */
public function setData(array $settings) public function merge(array $values)
{ {
if (isset($settings['namespace'])) { if (isset($values['namespace'])) {
$this->setNamespace($settings['namespace']); $this->setNamespace($values['namespace']);
} }
// Push middleware if multiple // Push middleware if multiple
if (isset($settings['middleware'])) { if (isset($values['middleware'])) {
$this->middlewares = array_merge((array)$settings['middleware'], $this->middlewares); $this->middlewares = array_merge((array)$values['middleware'], $this->middlewares);
} }
if (isset($settings['method'])) { if (isset($values['method'])) {
$this->setRequestMethods((array)$settings['method']); $this->setRequestMethods((array)$values['method']);
} }
if (isset($settings['where'])) { if (isset($values['where'])) {
$this->where($settings['where']); $this->where($values['where']);
} }
if (isset($settings['parameters'])) { if (isset($values['parameters'])) {
$this->setParameters($settings['parameters']); $this->setParameters($values['parameters']);
} }
return $this; return $this;
+16 -8
View File
@@ -78,21 +78,29 @@ class RouterGroup extends RouterEntry
return $this->prefix; return $this->prefix;
} }
public function setData(array $settings) /**
* Merge with information from another route.
*
* @param array $values
* @return static
*/
public function merge(array $values)
{ {
if (isset($settings['prefix'])) { if (isset($values['prefix'])) {
$this->setPrefix($settings['prefix']); $this->setPrefix($values['prefix']);
} }
if (isset($settings['exceptionHandler'])) { if (isset($values['exceptionHandler'])) {
$this->setExceptionHandlers((array)$settings['exceptionHandler']); $this->setExceptionHandlers((array)$values['exceptionHandler']);
} }
if (isset($settings['domain'])) { if (isset($values['domain'])) {
$this->setDomains((array)$settings['domain']); $this->setDomains((array)$values['domain']);
} }
return parent::setData($settings); parent::merge($values);
return $this;
} }
} }
+5 -5
View File
@@ -147,7 +147,7 @@ class SimpleRouter
$group->setCallback($callback); $group->setCallback($callback);
if ($settings !== null && is_array($settings) === true) { if ($settings !== null && is_array($settings) === true) {
$group->setData($settings); $group->merge($settings);
} }
if (is_callable($callback) === false) { if (is_callable($callback) === false) {
@@ -204,7 +204,7 @@ class SimpleRouter
$route = static::addDefaultNamespace($route); $route = static::addDefaultNamespace($route);
if ($settings !== null) { if ($settings !== null) {
$route->setData($settings); $route->merge($settings);
} }
static::router()->addRoute($route); static::router()->addRoute($route);
@@ -227,7 +227,7 @@ class SimpleRouter
$route = static::addDefaultNamespace($route); $route = static::addDefaultNamespace($route);
if ($settings !== null) { if ($settings !== null) {
$route->setData($settings); $route->merge($settings);
} }
static::router()->addRoute($route); static::router()->addRoute($route);
@@ -250,7 +250,7 @@ class SimpleRouter
$route = static::addDefaultNamespace($route); $route = static::addDefaultNamespace($route);
if ($settings !== null) { if ($settings !== null) {
$route->setData($settings); $route->merge($settings);
} }
static::router()->addRoute($route); static::router()->addRoute($route);
@@ -271,7 +271,7 @@ class SimpleRouter
$route = new RouterResource($url, $controller); $route = new RouterResource($url, $controller);
if ($settings !== null) { if ($settings !== null) {
$route->setData($settings); $route->merge($settings);
} }
static::router()->addRoute($route); static::router()->addRoute($route);