Last active
February 4, 2020 04:17
-
-
Save Nixes/aa7f364a099fe05a5ced1bff7dc32ee8 to your computer and use it in GitHub Desktop.
Script that generates a symfony compatible routes.php file from an existing silex application for migration, including middlewares
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
#!/usr/bin/env php | |
<?php | |
/*** | |
* Dumps routes from silex route registation to yaml file for use with symfony | |
run with php silex_to_symfony_routes_migrate.php > ../app/routes.php | |
*/ | |
use Silex\Application; | |
use Symfony\Component\Routing\Route; | |
use Symfony\Component\Routing\RouteCompiler; | |
use Symfony\Component\Yaml\Yaml; | |
require_once __DIR__ . '/../vendor/autoload.php'; | |
class RouteDumper | |
{ | |
public static function dump(Application $app) | |
{ | |
$app->boot(); | |
$app->flush(); | |
$string= '<?php'."\n".' | |
// config/routes.php | |
use Symfony\Component\Routing\RouteCollection; | |
//use Symfony\Component\Routing\Route; | |
use App\Framework\Route; | |
include __DIR__.\'/../app/middleware.php\'; | |
$routes = new RouteCollection();'."\n"; | |
foreach ($app['routes'] as $name => $route) { | |
$string.= self::dumpRoute($name, $route); | |
} | |
file_put_contents('symfonyroutes.php',$string); | |
} | |
private static function dumpRoute(string $name, Route $route) | |
{ | |
$dumpArray = [ | |
'name' => $name, | |
'path' => $route->getPath(), | |
]; | |
if ($route->getMethods()) { | |
$dumpArray['methods'] = $route->getMethods(); | |
} | |
$defaults = $route->getDefaults(); | |
if (isset($defaults['_controller'])) { | |
$result = explode(':',$defaults['_controller']); | |
$dumpArray['controller'] = $result[0]; | |
$dumpArray['controller_method'] = $result[1]; | |
unset($defaults['controller']); | |
} | |
if ($defaults) { | |
$dumpArray['defaults'] = $defaults; | |
} | |
if ($route->getRequirements()) { | |
$dumpArray['requirements'] = $route->getRequirements(); | |
} | |
$options = $route->getOptions(); | |
if (isset($options['compiler_class']) && RouteCompiler::class === $options['compiler_class']) { | |
unset($options['compiler_class']); | |
} | |
if ($options) { | |
$dumpArray['options'] = $route->getOptions(); | |
// get middleware names | |
} | |
if ($route->getHost()) { | |
$dumpArray['host'] = $route->getHost(); | |
} | |
if ($route->getSchemes()) { | |
$dumpArray['schemes'] = $route->getSchemes(); | |
} | |
if ($route->getCondition()) { | |
$dumpArray['condition'] = $route->getCondition(); | |
} | |
//echo Yaml::dump([$name => $dumpArray]) . "\n"; | |
// $array = var_export([$name => $dumpArray], 1); | |
// echo "\$array = $array;\n"; | |
return self::formatDumpAsPHP($dumpArray); | |
} | |
private static function formatDumpAsPHP(array $dumpArray) { | |
/* example of expected output | |
$routes->add('queues', new Route('/summary', array( | |
'_controller' => [\App\Controllers\SomeController::class, 'get'], | |
'middleware' => ['beforeLoggedIn','beforeFeature'], | |
),array(), array(), '', array(), array('GET','POST'))); | |
*/ | |
$string = '$routes->add(\''.$dumpArray['name'].'\', new Route(\''.$dumpArray['path'].'\', array('."\n"; | |
$string.= '\'_controller\' => [\App\Controllers\\'.$dumpArray['controller'].'::class, \''.$dumpArray['controller_method'].'\'],'."\n"; | |
// process middlewares into symfony defaults | |
$string.= '\'middleware\' => ['; | |
if (isset($dumpArray['options']) && isset($dumpArray['options']['_before_middlewares'])) { | |
foreach ($dumpArray['options']['_before_middlewares'] as $middlewareName) { | |
$string.= '\''.$middlewareName.'\','; | |
} | |
} | |
$string.= ']'."\n"; | |
$string.= '),array(), array(), \'\', array(), array('; | |
// render get/post params | |
foreach ($dumpArray['methods'] as $method) { | |
$string.= '\''.$method.'\','; | |
} | |
$string.= ')));'."\n\n"; | |
return $string; | |
} | |
} | |
$app = new Application(); | |
require_once __DIR__ . '/../app/config/production.php'; | |
require_once __DIR__ . '/../app/dependencies.php'; | |
require __DIR__ . '/../app/middleware.php'; | |
require_once __DIR__ . '/../app/routes.php'; | |
RouteDumper::dump($app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment