|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Mouf\Mvc\Splash\Annotations\Action; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class is in charge of registering controller's routes. |
|
9
|
|
|
*/ |
|
10
|
|
|
class ControllerRegistry implements UrlProviderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $controllers; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var ControllerDetector |
|
16
|
|
|
*/ |
|
17
|
|
|
private $controllerDetector; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var ControllerAnalyzer |
|
21
|
|
|
*/ |
|
22
|
|
|
private $controllerAnalyzer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initializes the registry with an array of container instances names. |
|
26
|
|
|
* |
|
27
|
|
|
* @param ControllerAnalyzer $controllerAnalyzer |
|
28
|
|
|
* @param string[] $controllers An array of controller instance name (as declared in the container) |
|
29
|
|
|
* @param ControllerDetector $controllerDetector |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(ControllerAnalyzer $controllerAnalyzer, array $controllers = [], ControllerDetector $controllerDetector = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->controllerAnalyzer = $controllerAnalyzer; |
|
34
|
|
|
$controllersArr = array_values($controllers); |
|
35
|
|
|
$this->controllers = array_combine($controllersArr, $controllersArr); |
|
36
|
|
|
$this->controllerDetector = $controllerDetector; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Adds a container to the registry (by its instance name). |
|
41
|
|
|
* Note: any object that has a @Action or @URL annotation is a controller. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $controller |
|
44
|
|
|
* |
|
45
|
|
|
* @return ControllerRegistry |
|
46
|
|
|
*/ |
|
47
|
|
|
public function addController(string $controller) : ControllerRegistry |
|
48
|
|
|
{ |
|
49
|
|
|
$this->controllers[$controller] = $controller; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the list of URLs that can be accessed, and the function/method that should be called when the URL is called. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $instanceName The identifier for this object in the container. |
|
58
|
|
|
* |
|
59
|
|
|
* @return SplashRoute[] |
|
60
|
|
|
* |
|
61
|
|
|
* @throws \Mouf\Mvc\Splash\Utils\SplashException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getUrlsList($instanceName) |
|
64
|
|
|
{ |
|
65
|
|
|
// FIXME: $instanceName is no more needed! o_O |
|
66
|
|
|
$urlsList = []; |
|
67
|
|
|
|
|
68
|
|
|
if ($this->controllerDetector) { |
|
69
|
|
|
$detectedControllers = array_values($this->controllerDetector->getControllerIdentifiers($this->controllerAnalyzer)); |
|
70
|
|
|
$detectedControllers = array_combine($detectedControllers, $detectedControllers); |
|
71
|
|
|
|
|
72
|
|
|
$controllers = $this->controllers + $detectedControllers; |
|
73
|
|
|
} else { |
|
74
|
|
|
$controllers = $this->controllers; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
foreach ($controllers as $controllerInstanceName) { |
|
78
|
|
|
$routes = $this->controllerAnalyzer->analyzeController($controllerInstanceName); |
|
79
|
|
|
|
|
80
|
|
|
$urlsList = array_merge($urlsList, $routes); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $urlsList; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns a unique tag representing the list of SplashRoutes returned. |
|
88
|
|
|
* If the tag changes, the cache is flushed by Splash. |
|
89
|
|
|
* |
|
90
|
|
|
* Important! This must be quick to compute. |
|
91
|
|
|
* |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getExpirationTag() : string |
|
95
|
|
|
{ |
|
96
|
|
|
// An approximate, quick-to-compute rule that will force renewing the cache if a controller is added are a parameter is fetched. |
|
97
|
|
|
return implode('-/-', $this->controllers).($this->controllerDetector !== null ? $this->controllerDetector->getExpirationTag() : ''); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|