Created
December 20, 2017 16:30
-
-
Save derrabus/4d7b7b3a6ffc0c1ccc1037ce2a66b13c to your computer and use it in GitHub Desktop.
Extract Symfony routing configuration from a Silex application
This file contains hidden or 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 | |
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(); | |
foreach ($app['routes'] as $name => $route) { | |
self::dumpRoute($name, $route); | |
} | |
} | |
private static function dumpRoute(string $name, Route $route) | |
{ | |
$dumpArray = ['path' => $route->getPath()]; | |
if ($route->getMethods()) { | |
$dumpArray['methods'] = $route->getMethods(); | |
} | |
$defaults = $route->getDefaults(); | |
if (isset($defaults['_controller'])) { | |
$dumpArray['controller'] = $defaults['_controller']; | |
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(); | |
} | |
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"; | |
} | |
} | |
// Initialize your application here. | |
$app = new Application(); | |
RouteDumper::dump($app); |
@stof Correct. Those closure controllers should be converted to classes first, if you want to use this script. Also, any route middlewares need to be removed first.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note that this won't work if you use closures as controllers (as they cannot be dumped to YAML)