mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-10 23:22:13 +00:00
Bugfixes
- Array arguments are now longer automaticially merged. - Added domain-route parameter unit-test.
This commit is contained in:
@@ -2,41 +2,25 @@
|
||||
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
use Pecee\Exception\RouterException;
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class RouterGroup extends RouterEntry {
|
||||
|
||||
public function __construct() {
|
||||
$this->settings = array_merge($this->settings, [
|
||||
'exceptionHandlers' => array()
|
||||
]);
|
||||
}
|
||||
protected $prefix;
|
||||
protected $domains = array();
|
||||
protected $exceptionHandlers = array();
|
||||
|
||||
public function matchDomain(Request $request) {
|
||||
if($this->getSetting('domain') !== null) {
|
||||
if(count($this->domains)) {
|
||||
for($i = 0; $i < count($this->domains); $i++) {
|
||||
$domain = $this->domains[$i];
|
||||
|
||||
if(is_array($this->getSetting('domain'))) {
|
||||
$parameters = $this->parseParameters($domain, $request->getHost(), '.*');
|
||||
|
||||
for($i = 0; $i < count($this->getSetting('domain')); $i++) {
|
||||
$domain = $this->settings['domain'][$i];
|
||||
|
||||
$parameters = $this->parseParameters($domain, $request->getHost(), '[^.]*');
|
||||
|
||||
if($parameters !== null) {
|
||||
$this->settings['parameters'] = $parameters;
|
||||
return true;
|
||||
}
|
||||
if($parameters !== null) {
|
||||
$this->parameters = $parameters;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$parameters = $this->parseParameters($this->getSetting('domain'), $request->getHost(), '[^.]*');
|
||||
|
||||
if ($parameters !== null) {
|
||||
$this->settings['parameters'] = $parameters;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -45,47 +29,31 @@ class RouterGroup extends RouterEntry {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function renderRoute(Request $request) {
|
||||
// Check if request method is allowed
|
||||
$hasAccess = true;
|
||||
|
||||
if($this->getSetting('method') !== null) {
|
||||
if(is_array($this->getSetting('method'))) {
|
||||
$hasAccess = (in_array($request->getMethod(), $this->getRequestMethods()));
|
||||
} else {
|
||||
$hasAccess = strtolower($this->getRequestMethods()) == strtolower($request->getMethod());
|
||||
}
|
||||
}
|
||||
|
||||
if(!$hasAccess) {
|
||||
throw new RouterException('Method not allowed');
|
||||
}
|
||||
|
||||
$this->matchDomain($request);
|
||||
|
||||
return parent::renderRoute($request);
|
||||
}
|
||||
|
||||
public function matchRoute(Request $request) {
|
||||
// Skip if prefix doesn't match
|
||||
if($this->getPrefix() !== null && stripos($request->getUri(), $this->getPrefix()) === false) {
|
||||
if($this->prefix !== null && stripos($request->getUri(), $this->prefix) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->matchDomain($request);
|
||||
}
|
||||
|
||||
public function setExceptionHandlers($class) {
|
||||
$this->settings['exceptionHandlers'][] = $class;
|
||||
public function setExceptionHandlers(array $handlers) {
|
||||
$this->exceptionHandlers = $handlers;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getExceptionHandlers() {
|
||||
return $this->getSetting('exceptionHandlers');
|
||||
return $this->exceptionHandlers;
|
||||
}
|
||||
|
||||
public function getDomain() {
|
||||
return $this->getSetting('domain');
|
||||
public function getDomains() {
|
||||
return $this->domains;
|
||||
}
|
||||
|
||||
public function setDomains(array $domains) {
|
||||
$this->domains = $domains;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,7 +61,7 @@ class RouterGroup extends RouterEntry {
|
||||
* @return static
|
||||
*/
|
||||
public function setPrefix($prefix) {
|
||||
$this->settings['prefix'] = '/' . trim($prefix, '/');
|
||||
$this->prefix = '/' . trim($prefix, '/');
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -101,48 +69,26 @@ class RouterGroup extends RouterEntry {
|
||||
* @return string
|
||||
*/
|
||||
public function getPrefix() {
|
||||
return $this->getSetting('prefix');
|
||||
return $this->prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $settings
|
||||
* @return static
|
||||
*/
|
||||
public function addSettings(array $settings) {
|
||||
|
||||
if ($this->getNamespace() !== null && isset($settings['namespace'])) {
|
||||
unset($settings['namespace']);
|
||||
}
|
||||
|
||||
// Push middleware if multiple
|
||||
if ($this->getMiddleware() !== null && isset($settings['middleware'])) {
|
||||
|
||||
if (!is_array($settings['middleware'])) {
|
||||
$settings['middleware'] = array_merge($this->getMiddleware(), array($settings['middleware']));
|
||||
} else {
|
||||
$settings['middleware'][] = $this->getMiddleware();
|
||||
}
|
||||
|
||||
$settings['middleware'] = array_unique(array_reverse($settings['middleware']));
|
||||
}
|
||||
public function setData(array $settings) {
|
||||
|
||||
if(isset($settings['prefix'])) {
|
||||
$this->setPrefix($settings['prefix']);
|
||||
unset($settings['prefix']);
|
||||
}
|
||||
|
||||
$this->settings = array_merge($this->settings, $settings);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMergeableSettings() {
|
||||
$settings = $this->settings;
|
||||
|
||||
if(isset($settings['prefix'])) {
|
||||
unset($settings['prefix']);
|
||||
if(isset($settings['exceptionHandler'])) {
|
||||
$handlers = is_array($settings['exceptionHandler']) ? $settings['exceptionHandler'] : array($settings['exceptionHandler']);
|
||||
$this->setExceptionHandlers($handlers);
|
||||
}
|
||||
|
||||
return $settings;
|
||||
if(isset($settings['domain'])) {
|
||||
$domains = is_array($settings['domain']) ? $settings['domain'] : array($settings['domain']);
|
||||
$this->setDomains($domains);
|
||||
}
|
||||
|
||||
return parent::setData($settings);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user