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
|
|
|
|