2020-04-28 16:34:17 +08:00
|
|
|
<?php
|
2020-04-28 22:02:03 +08:00
|
|
|
namespace catcher\generate\factory;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
2020-11-19 17:31:31 +08:00
|
|
|
use catcher\facade\FileSystem;
|
2020-04-28 22:02:03 +08:00
|
|
|
use catcher\generate\template\Content;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
|
|
class Route extends Factory
|
|
|
|
{
|
|
|
|
use Content;
|
|
|
|
|
|
|
|
protected $controllerName;
|
|
|
|
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
protected $restful;
|
|
|
|
|
|
|
|
protected $methods = [];
|
|
|
|
|
2021-03-14 07:41:15 +08:00
|
|
|
public function done(array $params = [])
|
2020-04-28 16:34:17 +08:00
|
|
|
{
|
|
|
|
$route = [];
|
|
|
|
|
|
|
|
if ($this->restful) {
|
2020-09-11 18:19:06 +08:00
|
|
|
$route[] = sprintf("\$router->resource('%s', '\%s');", $this->controllerName, $this->controller);
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->methods)) {
|
|
|
|
foreach ($this->methods as $method) {
|
2020-09-11 18:19:06 +08:00
|
|
|
$route[] = sprintf("\$router->%s('%s/%s', '\%s@%s');", $method[1], $this->controllerName, $method[0], $this->controller, $method[0] );
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:52:44 +08:00
|
|
|
$router = $this->getModulePath($this->controller) . DIRECTORY_SEPARATOR . 'route.php';
|
|
|
|
|
2020-09-11 18:19:06 +08:00
|
|
|
$comment = '// ' . $this->controllerName . '路由';
|
2020-06-30 10:46:39 +08:00
|
|
|
|
2020-09-11 18:19:06 +08:00
|
|
|
array_unshift($route, $comment);
|
2020-11-19 17:31:31 +08:00
|
|
|
|
2020-05-05 21:52:44 +08:00
|
|
|
if (file_exists($router)) {
|
2020-11-19 17:31:31 +08:00
|
|
|
return FileSystem::put($router, $this->parseRoute($router, $route));
|
2020-05-05 21:52:44 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 17:31:31 +08:00
|
|
|
return FileSystem::put($router, $this->header() . $comment. implode(';'. PHP_EOL , $route) . ';');
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:19:06 +08:00
|
|
|
protected function parseRoute($path, $route)
|
|
|
|
{
|
|
|
|
$file = new \SplFileObject($path);
|
|
|
|
// 保留所有行
|
|
|
|
$lines = [];
|
|
|
|
// 结尾之后的数据
|
|
|
|
$down = [];
|
|
|
|
// 结尾数据
|
|
|
|
$end = '';
|
|
|
|
while (!$file->eof()) {
|
|
|
|
$lines[] = rtrim($file->current(), PHP_EOL);
|
|
|
|
$file->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count($lines)) {
|
|
|
|
$line = array_pop($lines);
|
|
|
|
if (strpos($line, '})') !== false) {
|
|
|
|
$end = $line;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
array_unshift($down, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
$router = implode(PHP_EOL, $lines) . PHP_EOL;
|
|
|
|
|
|
|
|
$routes = array_merge($down, $route);
|
|
|
|
|
|
|
|
foreach ($routes as $r) {
|
|
|
|
if ($r) {
|
|
|
|
$router .= "\t" . $r . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $router .= $end;
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:34:17 +08:00
|
|
|
/**
|
|
|
|
* set class
|
|
|
|
*
|
|
|
|
* @time 2020年04月28日
|
|
|
|
* @param $class
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function controller($class)
|
|
|
|
{
|
|
|
|
$this->controller = $class;
|
|
|
|
|
|
|
|
$class = explode('\\', $class);
|
|
|
|
|
|
|
|
$this->controllerName = lcfirst(array_pop($class));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set restful
|
|
|
|
*
|
|
|
|
* @time 2020年04月28日
|
|
|
|
* @param $restful
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function restful($restful)
|
|
|
|
{
|
|
|
|
$this->restful = $restful;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set methods
|
|
|
|
*
|
|
|
|
* @time 2020年04月28日
|
|
|
|
* @param $methods
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function methods($methods)
|
|
|
|
{
|
|
|
|
$this->methods = $methods;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|