Skip to content

Instantly share code, notes, and snippets.

@derrabus
Created December 20, 2017 16:30
Show Gist options
  • Select an option

  • Save derrabus/4d7b7b3a6ffc0c1ccc1037ce2a66b13c to your computer and use it in GitHub Desktop.

Select an option

Save derrabus/4d7b7b3a6ffc0c1ccc1037ce2a66b13c to your computer and use it in GitHub Desktop.

Revisions

  1. derrabus created this gist Dec 20, 2017.
    74 changes: 74 additions & 0 deletions dump_routes.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    #!/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);