<commit_before><?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/3/18
 * Time: 下午4:32
 * Github: https://www.github.com/janhuang 
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: <EMAIL>
 */

namespace FastD\Framework\Commands;

use FastD\Console\Command;
use FastD\Console\IO\Input;
use FastD\Console\IO\Output;
use FastD\Routing\Router;

/**
 * Class RouteDump
 *
 * @package FastD\Framework\Commands
 */
class RouteDump extends Command
{
    /**
     * @return string
     */
    public function getName()
    {
        return 'route:dump';
    }

    /**
     * @return void|$this
     */
    public function configure()
    {
        $this
            ->setOption('name', null)
            ->setDescription('Thank for you use routing dump tool.')
        ;
    }

    /**
     * @param Input  $input
     * @param Output $output
     * @return void
     */
    public function execute(Input $input, Output $output)
    {
        $router = \Routes::getRouter();

        $output->writeln('');

        $name = $input->getParameterArgument(0);

        if ('' == $name) {
            $this->showRouteCollections($router, $output);
        } else {
            $route = $router->getRoute($name);
            $output->write('Route [');
            $output->write('"' . $name . '"', Output::STYLE_SUCCESS);
            $output->writeln(']');
            $output->writeln("Name:\t\t" . $route->getName());
            $output->writeln("Group:\t\t" . str_replace('//', '/', $route->getGroup()));
            $output->writeln("Path:\t\t" . $route->getPath());
            $output->writeln("Method:\t\t" . implode(', ', $route->getMethods()));
            $output->writeln("Format:\t\t" . implode(', ', $route->getFormats()));
            $output->writeln("Callback:\t" . (is_callable($route->getCallback()) ? 'Closure' : $route->getCallback()));
            $output->writeln("Defaults:\t" . implode(', ', $route->getDefaults()));
            $output->writeln("Requirements:\t" . implode(', ', $route->getRequirements()));
            $output->writeln("Path-Regex:\t" . $route->getPathRegex());
        }

        $output->writeln('');
    }

    public function showRouteCollections(Router $router, Output $output)
    {
        $length = 30;
        $output->writeln("Name" . str_repeat(' ', 26) . "Method" . str_repeat(' ', 24) . "Group" . str_repeat(' ', 25) . "Path", Output::STYLE_SUCCESS);
        foreach ($router->getCollections() as $name => $route) {
            $method = implode(', ', $route->getMethods());
            $group = str_replace('//', '/', $route->getGroup());
            $group = empty($group) ? '/' : $group;
            $output->writeln(
                $route->getName() . str_repeat(' ', ($length - strlen($route->getName()))) .
                $method . str_repeat(' ', ($length - strlen($method))) .
                str_replace('//', '/', $group . str_repeat(' ', ($length - strlen($group)))) .
                str_replace('//', '/', $route->getPath())
            );
        }
    }
}<commit_msg>optizming route dump commands.<commit_after><?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/3/18
 * Time: 下午4:32
 * Github: https://www.github.com/janhuang 
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: <EMAIL>
 */

namespace FastD\Framework\Commands;

use FastD\Console\Command;
use FastD\Console\IO\Input;
use FastD\Console\IO\Output;
use FastD\Routing\RouteInterface;
use FastD\Routing\Router;

/**
 * Class RouteDump
 *
 * @package FastD\Framework\Commands
 */
class RouteDump extends Command
{
    /**
     * @return string
     */
    public function getName()
    {
        return 'route:dump';
    }

    /**
     * @return void|$this
     */
    public function configure()
    {
        $this
            ->setOption('bundle', null)
            ->setDescription('Thank for you use routing dump tool.')
        ;
    }

    /**
     * @param Input  $input
     * @param Output $output
     * @return void
     */
    public function execute(Input $input, Output $output)
    {
        $router = \Routes::getRouter();

        $output->writeln('');

        $name = $input->getParameterArgument(0);

        $bundle = '' == ($bundle = $input->getParameterOption('bundle')) ? null : $bundle;

        if (false !== strpos($bundle, ':')) {
            $bundle = str_replace(':', '\\', $bundle);
        }

        if ('' == $name) {
            $this->showRouteCollections($router, $output, $bundle);
        } else {
            $route = $router->getRoute($name);
            $this->formatOutput($route);
        }

        return 0;
    }

    public function showRouteCollections(Router $router, Output $output, $bundleName = null)
    {
        $allRoutes = [];
        $bundles = $this->getContainer()->get('kernel')->getBundles();
        foreach ($router->getCollections() as $name => $route) {
            $callback = $route->getCallback();
            if (is_array($callback)) {
                $callback = get_class($callback[0]) . '@' . $callback[1];
            }
            foreach ($bundles as $bundle) {
                if (0 === strpos($callback, $bundle->getNamespace())) {
                    $allRoutes[$bundle->getFullName()][] = $route;
                    break;
                }
            }
        }

        if (null === $bundleName) {
            foreach ($allRoutes as $name => $routes) {
                $output->writeln($name, Output::STYLE_SUCCESS);
                foreach ($routes as $route) {
                    $this->formatOutput($route, $output);
                }
            }
            return 0;
        }

        foreach ($allRoutes as $name => $routes) {
            if ($name == $bundleName) {
                $output->writeln($name, Output::STYLE_SUCCESS);
                foreach ($routes as $route) {
                    $this->formatOutput($route, $output);
                }
            }

        }
    }

    public function formatOutput(RouteInterface $routeInterface, Output $output)
    {
        $output->write('Route [');
        $output->write('"' . $routeInterface->getName() . '"', Output::STYLE_SUCCESS);
        $output->writeln(']');
        $output->writeln("Group:\t\t" . ('' == ($group = str_replace('//', '/', $routeInterface->getGroup())) ? '/' : $group));
        $output->writeln("Path:\t\t" . $routeInterface->getPath());
        $output->writeln("Method:\t\t" . implode(', ', $routeInterface->getMethods()));
        $output->writeln("Format:\t\t" . implode(', ', $routeInterface->getFormats()));
        $output->writeln("Callback:\t" . (is_callable($routeInterface->getCallback()) ? 'Closure' : $routeInterface->getCallback()));
        $output->writeln("Defaults:\t" . implode(', ', $routeInterface->getDefaults()));
        $output->writeln("Requirements:\t" . implode(', ', $routeInterface->getRequirements()));
        $output->writeln("Path-Regex:\t" . $routeInterface->getPathRegex());
        $output->writeln('');
    }
}
{'fraction_non_alphanumeric': 0.18888424571667364, 'fraction_numerical': 0.004318150160189441, 'mean_word_length': 2.201069995541685, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 87, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7648369', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2140, 'n_words': 377}