Passed
Push — master ( 003724...f241d1 )
by Brent
02:47
created

RouterDispatchCommand::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\Site\Page;
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 RouterDispatchCommand extends Command
17
{
18
19
    const URL = 'url';
20
21
    protected function configure() {
22
        $this->setName('router:dispatch')
23
            ->setDescription('Simulate routing of an URL')
24
            ->setHelp("Simulate routing of an URL.")
25
            ->addArgument(self::URL, InputArgument::REQUIRED);
26
    }
27
28
    /**
29
     * @param InputInterface  $input
30
     * @param OutputInterface $output
31
     *
32
     * @return void
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output) {
35
        $stitcher = Stitcher::create();
36
        $site = $stitcher->loadSite();
37
38
        $url = $input->getArgument(self::URL);
39
        $routes = [];
40
        foreach ($site as $page) {
41
            $routes[] = $page->getId();
42
        }
43
        $routeCollection = new RouteCollection();
44
45
        foreach ($routes as $route) {
46
            $routeCollection->add($route, new Route($route));
47
        }
48
49
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
50
        $result = $matcher->match($url);
51
52
        $output->writeln("<fg=green>{$url}</> matches <fg=green>{$result['_route']}</>");
53
    }
54
}
55