1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Command; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\App; |
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
|
|
|
/** |
22
|
|
|
* @var Stitcher |
23
|
|
|
*/ |
24
|
|
|
private $stitcher; |
25
|
|
|
|
26
|
|
|
public function __construct(Stitcher $stitcher) { |
27
|
|
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
$this->stitcher = $stitcher; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function configure() { |
33
|
|
|
$this->setName('router:dispatch') |
34
|
|
|
->setDescription('Simulate routing of an URL') |
35
|
|
|
->setHelp("Simulate routing of an URL.") |
36
|
|
|
->addArgument(self::URL, InputArgument::REQUIRED); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param InputInterface $input |
41
|
|
|
* @param OutputInterface $output |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
46
|
|
|
$site = $this->stitcher->loadSite(); |
47
|
|
|
|
48
|
|
|
$url = $input->getArgument(self::URL); |
49
|
|
|
$routes = []; |
50
|
|
|
foreach ($site as $page) { |
51
|
|
|
$routes[] = $page->getId(); |
52
|
|
|
} |
53
|
|
|
$routeCollection = new RouteCollection(); |
54
|
|
|
|
55
|
|
|
foreach ($routes as $route) { |
56
|
|
|
$routeCollection->add($route, new Route($route)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$matcher = new UrlMatcher($routeCollection, new RequestContext()); |
60
|
|
|
$result = $matcher->match($url); |
61
|
|
|
|
62
|
|
|
$output->writeln("<fg=green>{$url}</> matches <fg=green>{$result['_route']}</>"); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|