Файл: MobileCMS-2.7.0-beta/System/Kernel/Routing/RouteDumper.php
Строк: 112
<?php
/**
* MobileCMS
*
* Open source content management system for mobile sites
*
* @author KpuTuK <bykputuk@ya.ru>
* @copyright Copyright (c) 2015, MobileCMS Team
* @license MIT License
*/
namespace SystemKernelRouting;
/**
* Загрузчик настроек
* @author KpuTuK <bykputuk@ya.ru>
* @author Fabien Potencier <fabien@symfony.com>
* @version 1.0.0
* @package MobileCMS
* @category Configuration component
*/
class RouteDumper {
protected $collection;
public function __construct(RouteCollection $collection = null) {
$this->collection = $collection;
}
public function dumpClass() {
$time = (new DateTime)->format('D, d M Y H:i:s');
$read = <<<EOF
<?php
namespace ApplicationCacheClassCache;
/**
* Create by RouteDumper {$time}
* Кеш роутов
*/
class RouteCacheMather {
public function getCachedRoutes() {
return [
{$this->generateNames()}
];
}
public function match($uri, $method) {
$matches = [];
$pathInfo = $uri;
{$this->dumpRoutes()}
return false;
}
protected function filterParams($params) {
return $params;
}
}
EOF;
file_put_contents(APPS .'Cache/ClassCache/RouteCacheMather.php', $read);
}
protected function generateNames() {
$write = '';
foreach (array_keys($this->collection->getCollection()) as $name) {
$write .= "t'$name',n";
}
return $write;
}
protected function dumpRoutes() {
foreach ($this->collection->getCollection() as $route) {
$compile = $route->compile();
if (true === $compile['match']) {
$write .= $this->dumpRouteMatcher(
$route['name'],
$compile['pattern'],
$route['handler'],
$route['methods'],
$route['params']
);
continue;
}
$write .= $this->dumpRoute(
$route['name'],
$compile['pattern'],
$route['handler'],
$route['methods'],
$route['params']
);
}
return $write;
}
public function dumpRoute($name, $pattern, $handler, $methods, array $params = []) {
if (is_array($methods)) {
$methods = implode("', '", $methods);
}
if ( ! empty($methods)) {
return <<<EOF
t/** Create for "{$name}" route **/
tif (in_array($method, ['{$methods}'])) {
t if ($pathInfo === '{$pattern}') {
t return ['handler' => '{$handler}', 'params' => [{$this->createParams($params)}]];
t }
t}
EOF;
}
return <<<EOF
t/** Create for "{$name}" route **/
tif ($pathInfo === '{$pattern}') {
t return ['handler' => '{$handler}', 'params' => [{$this->createParams($params)}]];
t}
EOF;
}
protected function dumpRouteMatcher($name, $pattern, $handler, $methods, array $params = []) {
if (is_array($methods)) {
$methods = implode("', '", $methods);
}
if ( ! empty($methods)) {
return <<<EOF
t/** Create for "{$name}" route **/
tif (in_array($method, ['{$methods}'])) {
t if (preg_match('#^{$pattern}$#s', $pathInfo, $matches)) {
t return ['handler' => '{$handler}', 'params' => array_merge($this->filterParams($matches), [{$this->createParams($params)}])];
t }
t}
EOF;
}
return <<<EOF
t/** Create for "{$name}" route **/
tif (preg_match('#^{$pattern}$#s', $pathInfo, $matches)) {
t return ['handler' => '{$handler}', 'params' => array_merge($this->filterParams($matches), [{$this->createParams($params)}])];
t}
EOF;
}
public function createParams(array $params = []) {
$write = '';
foreach ($params as $key => $value) {
$write .= '''.$key.'' => ''.$value.'', ';
}
return $write;
}
}