Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

RouteCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Command;
4
5
use Brendt\Stitcher\Config;
6
use Brendt\Stitcher\Stitcher;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Routing\Matcher\UrlMatcher;
12
use Symfony\Component\Routing\RequestContext;
13
use Symfony\Component\Routing\Route;
14
use Symfony\Component\Routing\RouteCollection;
15
16
class RouteCommand extends Command {
17
18
    const URL = 'url';
19
20
    protected function configure() {
21
        $this->setName('router:dispatch')
22
            ->setDescription('Simulate routing of an URL')
23
            ->setHelp("Simulate routing of an URL.")
24
            ->addArgument(self::URL, InputArgument::REQUIRED);
25
    }
26
27
    /**
28
     * @param InputInterface  $input
29
     * @param OutputInterface $output
30
     *
31
     * @return int|null|void
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output) {
34
        Config::load();
35
        $stitcher = new Stitcher();
36
        $site = $stitcher->loadSite();
37
38
        $url = $input->getArgument(self::URL);
39
        $routes = array_keys($site);
40
        $routeCollection = new RouteCollection();
41
42
        foreach ($routes as $route) {
43
            $routeCollection->add($route, new Route($route));
44
        }
45
46
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
47
        $result = $matcher->match($url);
48
49
        $output->writeln("<fg=green>{$url}</> matches <fg=green>{$result['_route']}</>");
50
51
        return;
52
    }
53
}
54