More optimisations

This commit is contained in:
Simon Sessingø
2016-11-19 19:28:10 +01:00
parent 7e63197252
commit 87d619ca24
6 changed files with 36 additions and 10 deletions
+7 -3
View File
@@ -8,7 +8,7 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
const PARAMETER_OPTIONAL_SYMBOL = '?'; const PARAMETER_OPTIONAL_SYMBOL = '?';
protected $url; protected $url;
protected $names = array(); protected $names = [];
public function getUrl() public function getUrl()
{ {
@@ -60,7 +60,8 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
* Get route names * Get route names
* @return array * @return array
*/ */
public function getNames() { public function getNames()
{
return $this->names; return $this->names;
} }
@@ -109,6 +110,7 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
public function setName($name) public function setName($name)
{ {
array_push($this->names, $name); array_push($this->names, $name);
return $this; return $this;
} }
@@ -118,8 +120,10 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
* @param array $names * @param array $names
* @return static $this * @return static $this
*/ */
public function setNames(array $names) { public function setNames(array $names)
{
$this->names = $names; $this->names = $names;
return $this; return $this;
} }
+3 -2
View File
@@ -162,7 +162,7 @@ class RouterBase
} }
} }
if($group !== null) { if ($group !== null) {
/* Add the parent group */ /* Add the parent group */
$route->setGroup($group); $route->setGroup($group);
@@ -437,7 +437,7 @@ class RouterBase
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null'); throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
} }
if($getParams === null) { if ($getParams === null) {
$getParams = $_GET; $getParams = $_GET;
} }
@@ -481,6 +481,7 @@ class RouterBase
} }
/* No result so we assume that someone is using a hardcoded url and join everything together. */ /* No result so we assume that someone is using a hardcoded url and join everything together. */
return '/' . trim(join('/', array_merge((array)$name, (array)$parameters)), '/') . '/' . $this->arrayToParams($getParams); return '/' . trim(join('/', array_merge((array)$name, (array)$parameters)), '/') . '/' . $this->arrayToParams($getParams);
} }
+3 -1
View File
@@ -34,7 +34,7 @@ class RouterController extends LoadableRoute implements IControllerRoute
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404); throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
} }
call_user_func_array(array($class, $method), $this->getParameters()); call_user_func_array([$class, $method], $this->getParameters());
return $class; return $class;
} }
@@ -86,6 +86,7 @@ class RouterController extends LoadableRoute implements IControllerRoute
public function setController($controller) public function setController($controller)
{ {
$this->controller = $controller; $this->controller = $controller;
return $this; return $this;
} }
@@ -104,6 +105,7 @@ class RouterController extends LoadableRoute implements IControllerRoute
public function setMethod($method) public function setMethod($method)
{ {
$this->method = $method; $this->method = $method;
return $this; return $this;
} }
+12
View File
@@ -197,6 +197,7 @@ abstract class RouterEntry
public function setRequestMethods(array $methods) public function setRequestMethods(array $methods)
{ {
$this->requestMethods = $methods; $this->requestMethods = $methods;
return $this; return $this;
} }
@@ -246,6 +247,7 @@ abstract class RouterEntry
public function setParent(RouterEntry $parent) public function setParent(RouterEntry $parent)
{ {
$this->parent = $parent; $this->parent = $parent;
return $this; return $this;
} }
@@ -256,6 +258,7 @@ abstract class RouterEntry
public function setCallback($callback) public function setCallback($callback)
{ {
$this->callback = $callback; $this->callback = $callback;
return $this; return $this;
} }
@@ -292,12 +295,14 @@ abstract class RouterEntry
public function setMethod($method) public function setMethod($method)
{ {
$this->callback = sprintf('%s@%s', $this->getClass(), $method); $this->callback = sprintf('%s@%s', $this->getClass(), $method);
return $this; return $this;
} }
public function setClass($class) public function setClass($class)
{ {
$this->callback = sprintf('%s@%s', $class, $this->getMethod()); $this->callback = sprintf('%s@%s', $class, $this->getMethod());
return $this; return $this;
} }
@@ -308,12 +313,14 @@ abstract class RouterEntry
public function setMiddleware($middleware) public function setMiddleware($middleware)
{ {
$this->middlewares[] = $middleware; $this->middlewares[] = $middleware;
return $this; return $this;
} }
public function setMiddlewares(array $middlewares) public function setMiddlewares(array $middlewares)
{ {
$this->middlewares = $middlewares; $this->middlewares = $middlewares;
return $this; return $this;
} }
@@ -324,6 +331,7 @@ abstract class RouterEntry
public function setNamespace($namespace) public function setNamespace($namespace)
{ {
$this->namespace = $namespace; $this->namespace = $namespace;
return $this; return $this;
} }
@@ -334,6 +342,7 @@ abstract class RouterEntry
public function setDefaultNamespace($namespace) public function setDefaultNamespace($namespace)
{ {
$this->defaultNamespace = $namespace; $this->defaultNamespace = $namespace;
return $this; return $this;
} }
@@ -373,6 +382,7 @@ abstract class RouterEntry
public function setParameters($parameters) public function setParameters($parameters)
{ {
$this->parameters = $parameters; $this->parameters = $parameters;
return $this; return $this;
} }
@@ -385,6 +395,7 @@ abstract class RouterEntry
public function setWhere(array $options) public function setWhere(array $options)
{ {
$this->where = $options; $this->where = $options;
return $this; return $this;
} }
@@ -397,6 +408,7 @@ abstract class RouterEntry
public function setMatch($regex) public function setMatch($regex)
{ {
$this->regex = $regex; $this->regex = $regex;
return $this; return $this;
} }
+7 -3
View File
@@ -6,8 +6,8 @@ use Pecee\Http\Request;
class RouterGroup extends RouterEntry class RouterGroup extends RouterEntry
{ {
protected $prefix; protected $prefix;
protected $domains = array(); protected $domains = [];
protected $exceptionHandlers = array(); protected $exceptionHandlers = [];
public function matchDomain(Request $request) public function matchDomain(Request $request)
{ {
@@ -18,6 +18,7 @@ class RouterGroup extends RouterEntry
if ($parameters !== null) { if ($parameters !== null) {
$this->parameters = $parameters; $this->parameters = $parameters;
return true; return true;
} }
} }
@@ -41,6 +42,7 @@ class RouterGroup extends RouterEntry
public function setExceptionHandlers(array $handlers) public function setExceptionHandlers(array $handlers)
{ {
$this->exceptionHandlers = $handlers; $this->exceptionHandlers = $handlers;
return $this; return $this;
} }
@@ -57,6 +59,7 @@ class RouterGroup extends RouterEntry
public function setDomains(array $domains) public function setDomains(array $domains)
{ {
$this->domains = $domains; $this->domains = $domains;
return $this; return $this;
} }
@@ -67,6 +70,7 @@ class RouterGroup extends RouterEntry
public function setPrefix($prefix) public function setPrefix($prefix)
{ {
$this->prefix = '/' . trim($prefix, '/'); $this->prefix = '/' . trim($prefix, '/');
return $this; return $this;
} }
@@ -110,7 +114,7 @@ class RouterGroup extends RouterEntry
*/ */
public function toArray() public function toArray()
{ {
$values = array(); $values = [];
if ($this->prefix !== null) { if ($this->prefix !== null) {
$values['prefix'] = $this->getPrefix(); $values['prefix'] = $this->getPrefix();
+4 -1
View File
@@ -18,11 +18,13 @@ class RouterRoute extends LoadableRoute
// Match on custom defined regular expression // Match on custom defined regular expression
if ($this->regex !== null) { if ($this->regex !== null) {
$parameters = array(); $parameters = [];
if (preg_match('/(' . $this->regex . ')/is', $request->getHost() . $url, $parameters)) { if (preg_match('/(' . $this->regex . ')/is', $request->getHost() . $url, $parameters)) {
$this->parameters = (array)$parameters[0]; $this->parameters = (array)$parameters[0];
return true; return true;
} }
return null; return null;
} }
@@ -33,6 +35,7 @@ class RouterRoute extends LoadableRoute
if ($parameters !== null) { if ($parameters !== null) {
$this->parameters = array_merge($this->parameters, $parameters); $this->parameters = array_merge($this->parameters, $parameters);
return true; return true;
} }