Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract Symfony routing configuration from a Silex application
#!/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

This comment has been minimized.

Copy link

@stof stof commented Dec 20, 2017

note that this won't work if you use closures as controllers (as they cannot be dumped to YAML)

@derrabus

This comment has been minimized.

Copy link
Owner Author

@derrabus derrabus commented Dec 21, 2017

@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