This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce2c31a
commit e3593a5
Showing
4 changed files
with
212 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Copyright 2014 Krzysztof Magosa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
namespace KM\Saffron; | ||
|
||
class RouteCompiled | ||
{ | ||
protected $prefix; | ||
protected $uriRegex; | ||
protected $domainRegex; | ||
|
||
public function __construct($prefix, $uriRegex, $domainRegex) | ||
{ | ||
$this->prefix = $prefix; | ||
$this->uriRegex = $uriRegex; | ||
$this->domainRegex = $domainRegex; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPrefix() | ||
{ | ||
return $this->prefix; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getUriRegex() | ||
{ | ||
return $this->uriRegex; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDomainRegex() | ||
{ | ||
return $this->domainRegex; | ||
} | ||
|
||
public function hasUriRegex() | ||
{ | ||
return null !== $this->uriRegex; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Copyright 2014 Krzysztof Magosa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
namespace KM\Saffron; | ||
|
||
use KM\Saffron\Route; | ||
use KM\Saffron\RouteCompiled; | ||
|
||
class RouteCompiler | ||
{ | ||
protected function getPrefix(Route $route) | ||
{ | ||
// @TODO make it not ugly :) | ||
$pos = strpos($route->getUri(), '{'); | ||
|
||
if (false !== $pos) { | ||
$length = max($pos - 1, 1); | ||
} | ||
else { | ||
$length = strlen($route->getUri()); | ||
} | ||
|
||
return substr($route->getUri(), 0, $length); | ||
} | ||
|
||
protected function getUriRegex(Route $route) | ||
{ | ||
if ($this->getPrefix($route) == $route->getUri()) { | ||
return null; | ||
} | ||
|
||
$tokens = preg_split( | ||
'#([^}]?\{\w+\})#s', | ||
substr($route->getUri(), 1), | ||
-1, | ||
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY | ||
); | ||
|
||
$regex = preg_quote(substr($route->getUri(), 0, 1), '#'); | ||
foreach ($tokens as $token) { | ||
if (preg_match('#^(?P<delimiter>.)?\{(?P<placeholder>\w+)\}$#s', $token, $match)) { | ||
$regex .= sprintf( | ||
'(%s(?P<%s>%s))%s', | ||
isset($match['delimiter']) ? preg_quote($match['delimiter'], '#') : '', | ||
preg_quote($match['placeholder'], '#'), | ||
$route->getRequire($match['placeholder']), | ||
$route->hasDefault($match['placeholder']) ? '?' : '' | ||
); | ||
} | ||
else { | ||
$regex .= preg_quote($token, '#'); | ||
} | ||
} | ||
|
||
return '#^'.$regex.'$#s'; | ||
} | ||
|
||
protected function getDomainRegex(Route $route) | ||
{ | ||
if (!$route->hasDomain()) { | ||
return null; | ||
} | ||
|
||
$tokens = preg_split( | ||
'#({\w+\}[^{]?)#s', | ||
$route->getDomain(), | ||
-1, | ||
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY | ||
); | ||
|
||
$regex = ''; | ||
foreach ($tokens as $token) { | ||
if (preg_match('#^\{(?P<placeholder>\w+)\}(?P<delimiter>.)?$#s', $token, $match)) { | ||
$regex .= sprintf( | ||
'((?P<%s>%s)%s)%s', | ||
preg_quote($match['placeholder'], '#'), | ||
$route->getRequire($match['placeholder']), | ||
isset($match['delimiter']) ? preg_quote($match['delimiter'], '#') : '', | ||
$route->hasDefault($match['placeholder']) ? '?' : '' | ||
); | ||
} | ||
else { | ||
$regex .= preg_quote($token, '#'); | ||
} | ||
} | ||
|
||
return '#^'.$regex.'$#s'; | ||
} | ||
|
||
public function compile(Route $route) | ||
{ | ||
$compiled = new RouteCompiled( | ||
$this->getPrefix($route), | ||
$this->getUriRegex($route), | ||
$this->getDomainRegex($route) | ||
); | ||
|
||
return $compiled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters