Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
attogram committed Mar 29, 2019
1 parent 18e2fb6 commit 06474dc
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
class Router
{
const VERSION = '1.0.4';
const VERSION = '1.1.0';

private $allow = [];
private $control = '';
Expand All @@ -41,6 +41,22 @@ class Router
private $uriCount = 0;
private $vars = [];

/**
* @uses $this->uri
* @uses $this->uriBase
* @uses $this->uriCount
* @uses $this->uriRelative
*/
public function __construct()
{
$this->uriBase = strtr($this->getServer('SCRIPT_NAME'), ['index.php' => '']);
$this->uriRelative = preg_replace('/\?.*/', '', $this->getServer('REQUEST_URI')); // remove query from URI
$this->uriRelative = strtr($this->uriRelative, [$this->uriBase => '/']);
$this->uriBase = rtrim($this->uriBase, '/'); // remove trailing slash from base URI
$this->uri = $this->trimArray(explode('/', $this->uriRelative)); // make URI list
$this->uriCount = count($this->uri); // directory depth of URI
}

/**
* @param bool $forceSlash
*/
Expand All @@ -50,17 +66,19 @@ public function setForceSlash(bool $forceSlash)
}

/**
* Allow a route
* Allow a route only if it is the same size as the current Uri
*
* @param string $route
* @param string $control
* @uses $this->allow
*/
public function allow($route, $control)
public function allow(string $route, string $control)
{
$this->allow[] = [
'route' => $this->trimArray(explode('/', $route)),
'control' => $control,
];
$route = $this->trimArray(explode('/', $route));
if ($this->uriCount !== count($route)) {
return;
}
$this->allow[] = ['route' => $route, 'control' => $control];
}

/**
Expand All @@ -71,7 +89,7 @@ public function allow($route, $control)
*/
public function match(): string
{
$this->setUriProperties();
$this->checkForceSlash();
$this->controls = array_column($this->allow, 'control');
$this->setRouting();
if ($this->matchExact() || $this->matchVariable()) {
Expand All @@ -82,28 +100,24 @@ public function match(): string
}

/**
* @uses $this->uri
* @uses $_GET
* @uses $this->forceSlash
* @uses $this->uriBase
* @uses $this->uriCount
* @uses $this->uriRelative
*/
private function setUriProperties()
private function checkForceSlash()
{
$this->uriBase = strtr($this->getServer('SCRIPT_NAME'), ['index.php' => '']);
$rUri = preg_replace('/\?.*/', '', $this->getServer('REQUEST_URI')); // remove query from URI
$this->uriRelative = strtr($rUri, [$this->uriBase => '/']);
$this->uriBase = rtrim($this->uriBase, '/'); // remove trailing slash from base URI
if ($this->forceSlash && (1 !== preg_match('#/$#', $this->uriRelative))) { // If no slash at end of URI?
$redirectUrl = $this->uriBase . $this->uriRelative . '/';
if (!empty($_GET)) {
$redirectUrl .= '?' . http_build_query($_GET);
}
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirectUrl);
exit;
if (!$this->forceSlash || !(1 !== preg_match('#/$#', $this->uriRelative))) {
return;
}
$this->uri = $this->trimArray(explode('/', $this->uriRelative)); // make URI list
$this->uriCount = count($this->uri); // directory depth of URI
$redirectUrl = $this->uriBase . $this->uriRelative . '/';
if (!empty($_GET)) {
$redirectUrl .= '?' . http_build_query($_GET);
}
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirectUrl);

exit; // After a redirect, we must exit to halt any further script execution
}

/**
Expand Down Expand Up @@ -223,7 +237,7 @@ private function trimArray(array $array): array
* @uses $_SERVER
* @return string
*/
private function getServer($name): string
private function getServer(string $name): string
{
if (!empty($_SERVER[$name])) {
return $_SERVER[$name];
Expand Down

0 comments on commit 06474dc

Please sign in to comment.