Skip to content

Instantly share code, notes, and snippets.

@Big-Shark
Last active February 21, 2016 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Big-Shark/a5d19144560e1b66b2de to your computer and use it in GitHub Desktop.
Save Big-Shark/a5d19144560e1b66b2de to your computer and use it in GitHub Desktop.
demo generate route file
<?php
namespace App\Console\Commands;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Console\Command;
class GenerateRouteFileCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'route:generate';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate file with all registered routes';
/**
* The router instance.
*
* @var \Illuminate\Routing\Router
*/
protected $router;
/**
* An array of all the registered routes.
*
* @var \Illuminate\Routing\RouteCollection
*/
protected $routes;
/**
* Create a new route command instance.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function __construct(Router $router)
{
parent::__construct();
$this->router = $router;
$this->routes = $router->getRoutes();
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
if (count($this->routes) == 0) {
return $this->error("Your application doesn't have any routes.");
}
$class = new \gossi\codegen\model\PhpClass('R');
/* @var Route $route */
foreach ($this->routes as $route) {
$camelCase = camel_case(str_replace(['.', '-'], '_', $route->getName()));
if (strlen($camelCase) === 0) {
continue;
}
$method = \gossi\codegen\model\PhpMethod::create($camelCase)
->setStatic(true)
->setType('string')
;
$parameters = [];
foreach($route->parameterNames() as $parameter) {
$method->addSimpleDescParameter($parameter);
$parameters[] = '\''.$parameter.'\' => $'.$parameter;
}
$parameters = implode(', ', $parameters);
$method->setBody("return route('{$route->getName()}', [{$parameters}]);");
$class->setMethod($method);
}
$generator = new \gossi\codegen\generator\CodeGenerator();
$code = $generator->generate($class);
file_put_contents(storage_path('r.php'), '<?php'.PHP_EOL.(string) $code);
}
}
<?php
/**
*/
class R {
/**
* @param mixed $id
* @return string
*/
public static function cityShow($id) {
return route('city.show', ['id' => $id]);
}
/**
* @return string
*/
public static function debugbarAssetsCss() {
return route('debugbar.assets.css', []);
}
/**
* @return string
*/
public static function debugbarAssetsJs() {
return route('debugbar.assets.js', []);
}
/**
* @param mixed $id
* @return string
*/
public static function debugbarClockwork($id) {
return route('debugbar.clockwork', ['id' => $id]);
}
/**
* @return string
*/
public static function debugbarOpenhandler() {
return route('debugbar.openhandler', []);
}
/**
* @return string
*/
public static function guideIndex() {
return route('guide.index', []);
}
/**
* @return string
*/
public static function help() {
return route('help', []);
}
/**
* @return string
*/
public static function placeIndex() {
return route('place.index', []);
}
/**
* @return string
*/
public static function search() {
return route('search', []);
}
/**
* @return string
*/
public static function tourIndex() {
return route('tour.index', []);
}
/**
* @param mixed $id
* @return string
*/
public static function tourShow($id) {
return route('tour.show', ['id' => $id]);
}
/**
* @return string
*/
public static function tourStore() {
return route('tour.store', []);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment