1 | <?php |
||
61 | class Server extends \Apparat\Server\Domain\Model\Server |
||
62 | { |
||
63 | /** |
||
64 | * View resources |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $viewResources = []; |
||
69 | |||
70 | /** |
||
71 | * Enable the object route for a particular repository |
||
72 | * |
||
73 | * @param string $repositoryPath Repository path |
||
74 | * @param int $enable Enable / disable default actions |
||
75 | */ |
||
76 | 1 | public function enableObjectRoute($repositoryPath = '', $enable = ObjectRoute::ALL) |
|
77 | { |
||
78 | 1 | $routeName = ObjectRoute::OBJECT_STR; |
|
79 | 1 | $datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
|
80 | 1 | $selectorRegex = SelectorFactory::getSelectorRegex($datePrecision); |
|
81 | |||
82 | 1 | $selectorRegex .= '(?('.($datePrecision + 3).')(?:/(?P<slug>[a-z][a-z\d\-]*))?)'; |
|
83 | |||
84 | 1 | $objectRouteActions = array_filter( |
|
85 | array_merge( |
||
86 | 1 | self::getEnabledObjectActionClasses($enable), |
|
87 | 1 | self::getEnabledDateActionClasses($enable, $datePrecision) |
|
88 | ) |
||
89 | ); |
||
90 | |||
91 | // Add a repository sub-pattern |
||
92 | 1 | if (strlen($repositoryPath)) { |
|
93 | 1 | $selectorRegex = '(?:/(?P<repository>.+?))'.$selectorRegex; |
|
94 | 1 | $routeName .= '.'.$repositoryPath; |
|
95 | } |
||
96 | |||
97 | 1 | $route = new Route(Route::GET, $routeName, $selectorRegex, $objectRouteActions); |
|
98 | 1 | $route->setWildcard('slug'); |
|
99 | 1 | $route->setAccepts(ObjectRoute::$accept); |
|
100 | 1 | $route->setObject(true); |
|
101 | 1 | $this->registerRoute($route); |
|
102 | 1 | } |
|
103 | |||
104 | /** |
||
105 | * Return the enabled object action classes |
||
106 | * |
||
107 | * @param int $enable Enable / disable default actions |
||
108 | * @return array Enabled object action classes |
||
109 | */ |
||
110 | 1 | protected function getEnabledObjectActionClasses($enable) |
|
111 | { |
||
112 | return [ |
||
113 | 1 | ObjectRoute::OBJECT_STR => (ObjectRoute::OBJECT & $enable) ? ObjectAction::class : null, |
|
114 | 1 | ObjectRoute::TYPE_STR => (ObjectRoute::TYPE & $enable) ? TypeAction::class : null, |
|
115 | 1 | ObjectRoute::OBJECTS_STR => (ObjectRoute::OBJECTS & $enable) ? ObjectsAction::class : null, |
|
116 | ]; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Return the enabled date action classes |
||
121 | * |
||
122 | * @param int $enable Enable / disable default actions |
||
123 | * @param int $datePrecision Date precision |
||
124 | * @return array Enabled date action classes |
||
125 | */ |
||
126 | 1 | protected function getEnabledDateActionClasses($enable, $datePrecision) |
|
127 | { |
||
128 | 1 | return array_slice( |
|
129 | [ |
||
130 | 1 | ObjectRoute::SECOND_STR => (ObjectRoute::SECOND & $enable) ? SecondAction::class : null, |
|
131 | 1 | ObjectRoute::MINUTE_STR => (ObjectRoute::MINUTE & $enable) ? MinuteAction::class : null, |
|
132 | 1 | ObjectRoute::HOUR_STR => (ObjectRoute::HOUR & $enable) ? HourAction::class : null, |
|
133 | 1 | ObjectRoute::DAY_STR => (ObjectRoute::DAY & $enable) ? DayAction::class : null, |
|
134 | 1 | ObjectRoute::MONTH_STR => (ObjectRoute::MONTH & $enable) ? MonthAction::class : null, |
|
135 | 1 | ObjectRoute::YEAR_STR => (ObjectRoute::YEAR & $enable) ? YearAction::class : null, |
|
136 | ], |
||
137 | 1 | 6 - $datePrecision |
|
138 | ); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Prepare and return a route action |
||
143 | * |
||
144 | * @param ServerRequestInterface $request Request |
||
145 | * @param AbstractActionRoute $route Route |
||
146 | * @return ActionInterface|Callable $action Action |
||
147 | */ |
||
148 | 81 | public function getRouteAction(ServerRequestInterface $request, AbstractActionRoute $route) |
|
152 | |||
153 | /** |
||
154 | * Return view resources |
||
155 | * |
||
156 | * @param string|null $name Optional: view resource name |
||
157 | * @return array View resources |
||
158 | */ |
||
159 | 79 | public function getViewResources($name = null) |
|
160 | { |
||
161 | 79 | return ($name === null) ? |
|
162 | $this->viewResources : |
||
163 | 79 | (array_key_exists($name, $this->viewResources) ? $this->viewResources[$name] : null); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Set the view resources |
||
168 | * |
||
169 | * @param array $viewResources View resources |
||
170 | */ |
||
171 | 3 | public function setViewResources(array $viewResources) |
|
175 | } |
||
176 |