Mark V
1: <?php
2: namespace MOC\V\Component\Router\Component\Bridge\Repository;
3:
4: use MOC\V\Component\Router\Component\Bridge\Bridge;
5: use MOC\V\Component\Router\Component\Exception\ComponentException;
6: use MOC\V\Component\Router\Component\IBridgeInterface;
7: use MOC\V\Component\Router\Component\Parameter\Repository\RouteParameter;
8: use MOC\V\Core\AutoLoader\AutoLoader;
9: use Symfony\Component\EventDispatcher\EventDispatcher;
10: use Symfony\Component\HttpFoundation\Request;
11: use Symfony\Component\HttpKernel\Controller\ControllerResolver;
12: use Symfony\Component\HttpKernel\EventListener\RouterListener;
13: use Symfony\Component\HttpKernel\HttpKernel;
14: use Symfony\Component\Routing\Matcher\UrlMatcher;
15: use Symfony\Component\Routing\RequestContext;
16: use Symfony\Component\Routing\Route;
17: use Symfony\Component\Routing\RouteCollection;
18:
19: /**
20: * Class SymfonyRouter
21: *
22: * @package MOC\V\Component\Router\Component\Bridge
23: */
24: class SymfonyRouter extends Bridge implements IBridgeInterface
25: {
26:
27: /** @var null|RequestContext $SymfonyRequestContext */
28: private $SymfonyRequestContext = null;
29: /** @var null|RouteCollection $SymfonyRouteCollection */
30: private $SymfonyRouteCollection = null;
31: /** @var null|UrlMatcher $SymfonyUrlMatcher */
32: private $SymfonyUrlMatcher = null;
33: /** @var null|EventDispatcher $SymfonyEventDispatcher */
34: private $SymfonyEventDispatcher = null;
35: /** @var null|HttpKernel $SymfonyHttpKernel */
36: private $SymfonyHttpKernel = null;
37:
38: /**
39: * @param string $BaseUrl
40: */
41: public function __construct($BaseUrl = '')
42: {
43:
44: AutoLoader::getNamespaceAutoLoader('Symfony\Component', __DIR__.'/../../../Vendor/');
45: AutoLoader::getNamespaceAutoLoader('Symfony\Component', __DIR__.'/../../../../../Core/HttpKernel/Vendor/');
46:
47: $this->SymfonyRouteCollection = new RouteCollection();
48: $this->SymfonyRequestContext = new RequestContext();
49: $this->SymfonyRequestContext->setBaseUrl($BaseUrl);
50: $this->SymfonyUrlMatcher = new UrlMatcher($this->SymfonyRouteCollection, $this->SymfonyRequestContext);
51:
52: $this->SymfonyEventDispatcher = new EventDispatcher();
53: $this->SymfonyEventDispatcher->addSubscriber(new RouterListener($this->SymfonyUrlMatcher));
54: $this->SymfonyHttpKernel = new HttpKernel($this->SymfonyEventDispatcher, new ControllerResolver());
55: }
56:
57: /**
58: * @param RouteParameter $RouteOption
59: *
60: * @return IBridgeInterface
61: */
62: public function addRoute(RouteParameter $RouteOption)
63: {
64:
65: $this->SymfonyRouteCollection->add($RouteOption->getPath(),
66: new Route(
67: $RouteOption->getPath(),
68: array_merge(
69: array('_controller' => $RouteOption->getController()),
70: $RouteOption->getParameterDefault()
71: ),
72: $RouteOption->getParameterPattern()
73: )
74: );
75: return $this;
76: }
77:
78: /**
79: * @return array
80: */
81: public function getRouteList()
82: {
83:
84: return $this->SymfonyRouteCollection->getIterator()->getArrayCopy();
85: }
86:
87: /**
88: * @param null|string $Path
89: *
90: * @return \Symfony\Component\HttpFoundation\Response
91: * @throws \Exception
92: */
93: public function getRoute($Path = null)
94: {
95:
96: $Path = null;
97: try {
98: return $this->SymfonyHttpKernel->handle(Request::createFromGlobals())->getContent();
99: } catch (\Exception $E) {
100: throw new ComponentException($E->getMessage(), $E->getCode(), $E);
101: }
102: }
103: }
104: