1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Application; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Exception\StitcherException; |
6
|
|
|
use Brendt\Stitcher\Factory\AdapterFactory; |
7
|
|
|
use Brendt\Stitcher\Stitcher; |
8
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
9
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
10
|
|
|
use Symfony\Component\Routing\RequestContext; |
11
|
|
|
use Symfony\Component\Routing\Route; |
12
|
|
|
use Symfony\Component\Routing\RouteCollection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The developer controller is used to render pages on the fly (on an HTTP request). This controller enables a |
16
|
|
|
* developer make code changes and see those changes real-time without re-compiling the whole site. |
17
|
|
|
*/ |
18
|
|
|
class DevController |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Stitcher |
23
|
|
|
*/ |
24
|
|
|
protected $stitcher; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new developer controller with optional configuration path and -file. |
28
|
|
|
* |
29
|
|
|
* @param Stitcher $stitcher |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Stitcher $stitcher) { |
32
|
|
|
$this->stitcher = $stitcher; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Run the developer controller. This function will read the request URL and dispatch the according route. |
37
|
|
|
* |
38
|
|
|
* @param string $url |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function run($url = null) { |
|
|
|
|
43
|
|
|
if ($url === null) { |
44
|
|
|
$request = explode('?', $_SERVER['REQUEST_URI']); |
45
|
|
|
$url = reset($request); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
return $this->getPage($url); |
50
|
|
|
} catch (StitcherException $e) { |
51
|
|
|
return $e->getMessage(); |
52
|
|
|
} catch (ResourceNotFoundException $e) { |
53
|
|
|
return "404"; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create the route container |
59
|
|
|
* |
60
|
|
|
* @return \Symfony\Component\Routing\RouteCollection |
61
|
|
|
*/ |
62
|
|
|
protected function createRouteCollection() { |
63
|
|
|
$routeCollection = new RouteCollection(); |
64
|
|
|
$site = $this->stitcher->loadSite(); |
65
|
|
|
|
66
|
|
|
foreach ($site as $page) { |
67
|
|
|
$route = $page->getId(); |
68
|
|
|
|
69
|
|
|
$routeCollection->add($route, new Route($route)); |
70
|
|
|
|
71
|
|
|
if ($page->getAdapterConfig(AdapterFactory::PAGINATION_ADAPTER)) { |
72
|
|
|
$paginationRoute = $route . '/page-{page}'; |
73
|
|
|
$routeCollection->add($paginationRoute, new Route($paginationRoute)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $routeCollection; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $url |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
protected function getPage($url) { |
86
|
|
|
$routeCollection = $this->createRouteCollection(); |
87
|
|
|
$matcher = new UrlMatcher($routeCollection, new RequestContext()); |
88
|
|
|
$routeResult = $matcher->match($url); |
89
|
|
|
$route = $routeResult['_route']; |
90
|
|
|
|
91
|
|
|
$id = isset($routeResult['id']) ? $routeResult['id'] : null; |
92
|
|
|
|
93
|
|
|
if (isset($routeResult['page'])) { |
94
|
|
|
$route = str_replace('/page-{page}', '', $route); |
95
|
|
|
$id = $routeResult['page']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$blanket = $this->stitcher->stitch($route, $id); |
99
|
|
|
|
100
|
|
|
if (isset($blanket[$route])) { |
101
|
|
|
return $blanket[$route]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($blanket[$url])) { |
105
|
|
|
return $blanket[$url]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
throw new ResourceNotFoundException(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: