@@ -43,170 +43,170 @@ |
||
43 | 43 | */ |
44 | 44 | interface IRegistrationContext { |
45 | 45 | |
46 | - /** |
|
47 | - * @param string $capability |
|
48 | - * @psalm-param class-string<ICapability> $capability |
|
49 | - * @see IAppContainer::registerCapability |
|
50 | - * |
|
51 | - * @since 20.0.0 |
|
52 | - */ |
|
53 | - public function registerCapability(string $capability): void; |
|
54 | - |
|
55 | - /** |
|
56 | - * Register an implementation of \OCP\Support\CrashReport\IReporter that |
|
57 | - * will receive unhandled exceptions and throwables |
|
58 | - * |
|
59 | - * @param string $reporterClass |
|
60 | - * @psalm-param class-string<\OCP\Support\CrashReport\IReporter> $reporterClass |
|
61 | - * @return void |
|
62 | - * @since 20.0.0 |
|
63 | - */ |
|
64 | - public function registerCrashReporter(string $reporterClass): void; |
|
65 | - |
|
66 | - /** |
|
67 | - * Register an implementation of \OCP\Dashboard\IWidget that |
|
68 | - * will handle the implementation of a dashboard widget |
|
69 | - * |
|
70 | - * @param string $widgetClass |
|
71 | - * @psalm-param class-string<\OCP\Dashboard\IWidget> $widgetClass |
|
72 | - * @return void |
|
73 | - * @since 20.0.0 |
|
74 | - */ |
|
75 | - public function registerDashboardWidget(string $widgetClass): void; |
|
76 | - /** |
|
77 | - * Register a service |
|
78 | - * |
|
79 | - * @param string $name |
|
80 | - * @param callable $factory |
|
81 | - * @psalm-param callable(\Psr\Container\ContainerInterface): mixed $factory |
|
82 | - * @param bool $shared |
|
83 | - * |
|
84 | - * @return void |
|
85 | - * @see IContainer::registerService() |
|
86 | - * |
|
87 | - * @since 20.0.0 |
|
88 | - */ |
|
89 | - public function registerService(string $name, callable $factory, bool $shared = true): void; |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $alias |
|
93 | - * @psalm-param string|class-string $alias |
|
94 | - * @param string $target |
|
95 | - * @psalm-param string|class-string $target |
|
96 | - * |
|
97 | - * @return void |
|
98 | - * @see IContainer::registerAlias() |
|
99 | - * |
|
100 | - * @since 20.0.0 |
|
101 | - */ |
|
102 | - public function registerServiceAlias(string $alias, string $target): void; |
|
103 | - |
|
104 | - /** |
|
105 | - * @param string $name |
|
106 | - * @param mixed $value |
|
107 | - * |
|
108 | - * @return void |
|
109 | - * @see IContainer::registerParameter() |
|
110 | - * |
|
111 | - * @since 20.0.0 |
|
112 | - */ |
|
113 | - public function registerParameter(string $name, $value): void; |
|
114 | - |
|
115 | - /** |
|
116 | - * Register a service listener |
|
117 | - * |
|
118 | - * This is equivalent to calling IEventDispatcher::addServiceListener |
|
119 | - * |
|
120 | - * @template T of \OCP\EventDispatcher\Event |
|
121 | - * @param string $event preferably the fully-qualified class name of the Event sub class to listen for |
|
122 | - * @psalm-param string|class-string<T> $event preferably the fully-qualified class name of the Event sub class to listen for |
|
123 | - * @param string $listener fully qualified class name (or ::class notation) of a \OCP\EventDispatcher\IEventListener that can be built by the DI container |
|
124 | - * @psalm-param class-string<\OCP\EventDispatcher\IEventListener<T>> $listener fully qualified class name that can be built by the DI container |
|
125 | - * @param int $priority The higher this value, the earlier an event |
|
126 | - * listener will be triggered in the chain (defaults to 0) |
|
127 | - * |
|
128 | - * @see IEventDispatcher::addServiceListener() |
|
129 | - * |
|
130 | - * @since 20.0.0 |
|
131 | - */ |
|
132 | - public function registerEventListener(string $event, string $listener, int $priority = 0): void; |
|
133 | - |
|
134 | - /** |
|
135 | - * @param string $class |
|
136 | - * @psalm-param class-string<\OCP\AppFramework\Middleware> $class |
|
137 | - * |
|
138 | - * @return void |
|
139 | - * @see IAppContainer::registerMiddleWare() |
|
140 | - * |
|
141 | - * @since 20.0.0 |
|
142 | - */ |
|
143 | - public function registerMiddleware(string $class): void; |
|
144 | - |
|
145 | - /** |
|
146 | - * Register a search provider for the unified search |
|
147 | - * |
|
148 | - * It is allowed to register more than one provider per app as the search |
|
149 | - * results can go into distinct sections, e.g. "Files" and "Files shared |
|
150 | - * with you" in the Files app. |
|
151 | - * |
|
152 | - * @param string $class |
|
153 | - * @psalm-param class-string<\OCP\Search\IProvider> $class |
|
154 | - * |
|
155 | - * @return void |
|
156 | - * |
|
157 | - * @since 20.0.0 |
|
158 | - */ |
|
159 | - public function registerSearchProvider(string $class): void; |
|
160 | - |
|
161 | - /** |
|
162 | - * Register an alternative login option |
|
163 | - * |
|
164 | - * It is allowed to register more than one option per app. |
|
165 | - * |
|
166 | - * @param string $class |
|
167 | - * @psalm-param class-string<\OCP\Authentication\IAlternativeLogin> $class |
|
168 | - * |
|
169 | - * @return void |
|
170 | - * |
|
171 | - * @since 20.0.0 |
|
172 | - */ |
|
173 | - public function registerAlternativeLogin(string $class): void; |
|
174 | - |
|
175 | - /** |
|
176 | - * Register an initialstate provider |
|
177 | - * |
|
178 | - * It is allowed to register more than one provider per app. |
|
179 | - * |
|
180 | - * @param string $class |
|
181 | - * @psalm-param class-string<\OCP\AppFramework\Services\InitialStateProvider> $class |
|
182 | - * |
|
183 | - * @return void |
|
184 | - * |
|
185 | - * @since 21.0.0 |
|
186 | - */ |
|
187 | - public function registerInitialStateProvider(string $class): void; |
|
188 | - |
|
189 | - /** |
|
190 | - * Register a well known protocol handler |
|
191 | - * |
|
192 | - * It is allowed to register more than one handler per app. |
|
193 | - * |
|
194 | - * @param string $class |
|
195 | - * @psalm-param class-string<\OCP\Http\WellKnown\IHandler> $class |
|
196 | - * |
|
197 | - * @return void |
|
198 | - * |
|
199 | - * @since 21.0.0 |
|
200 | - */ |
|
201 | - public function registerWellKnownHandler(string $class): void; |
|
202 | - |
|
203 | - /** |
|
204 | - * Register a custom template provider class that is able to inject custom templates |
|
205 | - * in addition to the user defined ones |
|
206 | - * |
|
207 | - * @param string $providerClass |
|
208 | - * @psalm-param class-string<ICustomTemplateProvider> $providerClass |
|
209 | - * @since 21.0.0 |
|
210 | - */ |
|
211 | - public function registerTemplateProvider(string $providerClass): void; |
|
46 | + /** |
|
47 | + * @param string $capability |
|
48 | + * @psalm-param class-string<ICapability> $capability |
|
49 | + * @see IAppContainer::registerCapability |
|
50 | + * |
|
51 | + * @since 20.0.0 |
|
52 | + */ |
|
53 | + public function registerCapability(string $capability): void; |
|
54 | + |
|
55 | + /** |
|
56 | + * Register an implementation of \OCP\Support\CrashReport\IReporter that |
|
57 | + * will receive unhandled exceptions and throwables |
|
58 | + * |
|
59 | + * @param string $reporterClass |
|
60 | + * @psalm-param class-string<\OCP\Support\CrashReport\IReporter> $reporterClass |
|
61 | + * @return void |
|
62 | + * @since 20.0.0 |
|
63 | + */ |
|
64 | + public function registerCrashReporter(string $reporterClass): void; |
|
65 | + |
|
66 | + /** |
|
67 | + * Register an implementation of \OCP\Dashboard\IWidget that |
|
68 | + * will handle the implementation of a dashboard widget |
|
69 | + * |
|
70 | + * @param string $widgetClass |
|
71 | + * @psalm-param class-string<\OCP\Dashboard\IWidget> $widgetClass |
|
72 | + * @return void |
|
73 | + * @since 20.0.0 |
|
74 | + */ |
|
75 | + public function registerDashboardWidget(string $widgetClass): void; |
|
76 | + /** |
|
77 | + * Register a service |
|
78 | + * |
|
79 | + * @param string $name |
|
80 | + * @param callable $factory |
|
81 | + * @psalm-param callable(\Psr\Container\ContainerInterface): mixed $factory |
|
82 | + * @param bool $shared |
|
83 | + * |
|
84 | + * @return void |
|
85 | + * @see IContainer::registerService() |
|
86 | + * |
|
87 | + * @since 20.0.0 |
|
88 | + */ |
|
89 | + public function registerService(string $name, callable $factory, bool $shared = true): void; |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $alias |
|
93 | + * @psalm-param string|class-string $alias |
|
94 | + * @param string $target |
|
95 | + * @psalm-param string|class-string $target |
|
96 | + * |
|
97 | + * @return void |
|
98 | + * @see IContainer::registerAlias() |
|
99 | + * |
|
100 | + * @since 20.0.0 |
|
101 | + */ |
|
102 | + public function registerServiceAlias(string $alias, string $target): void; |
|
103 | + |
|
104 | + /** |
|
105 | + * @param string $name |
|
106 | + * @param mixed $value |
|
107 | + * |
|
108 | + * @return void |
|
109 | + * @see IContainer::registerParameter() |
|
110 | + * |
|
111 | + * @since 20.0.0 |
|
112 | + */ |
|
113 | + public function registerParameter(string $name, $value): void; |
|
114 | + |
|
115 | + /** |
|
116 | + * Register a service listener |
|
117 | + * |
|
118 | + * This is equivalent to calling IEventDispatcher::addServiceListener |
|
119 | + * |
|
120 | + * @template T of \OCP\EventDispatcher\Event |
|
121 | + * @param string $event preferably the fully-qualified class name of the Event sub class to listen for |
|
122 | + * @psalm-param string|class-string<T> $event preferably the fully-qualified class name of the Event sub class to listen for |
|
123 | + * @param string $listener fully qualified class name (or ::class notation) of a \OCP\EventDispatcher\IEventListener that can be built by the DI container |
|
124 | + * @psalm-param class-string<\OCP\EventDispatcher\IEventListener<T>> $listener fully qualified class name that can be built by the DI container |
|
125 | + * @param int $priority The higher this value, the earlier an event |
|
126 | + * listener will be triggered in the chain (defaults to 0) |
|
127 | + * |
|
128 | + * @see IEventDispatcher::addServiceListener() |
|
129 | + * |
|
130 | + * @since 20.0.0 |
|
131 | + */ |
|
132 | + public function registerEventListener(string $event, string $listener, int $priority = 0): void; |
|
133 | + |
|
134 | + /** |
|
135 | + * @param string $class |
|
136 | + * @psalm-param class-string<\OCP\AppFramework\Middleware> $class |
|
137 | + * |
|
138 | + * @return void |
|
139 | + * @see IAppContainer::registerMiddleWare() |
|
140 | + * |
|
141 | + * @since 20.0.0 |
|
142 | + */ |
|
143 | + public function registerMiddleware(string $class): void; |
|
144 | + |
|
145 | + /** |
|
146 | + * Register a search provider for the unified search |
|
147 | + * |
|
148 | + * It is allowed to register more than one provider per app as the search |
|
149 | + * results can go into distinct sections, e.g. "Files" and "Files shared |
|
150 | + * with you" in the Files app. |
|
151 | + * |
|
152 | + * @param string $class |
|
153 | + * @psalm-param class-string<\OCP\Search\IProvider> $class |
|
154 | + * |
|
155 | + * @return void |
|
156 | + * |
|
157 | + * @since 20.0.0 |
|
158 | + */ |
|
159 | + public function registerSearchProvider(string $class): void; |
|
160 | + |
|
161 | + /** |
|
162 | + * Register an alternative login option |
|
163 | + * |
|
164 | + * It is allowed to register more than one option per app. |
|
165 | + * |
|
166 | + * @param string $class |
|
167 | + * @psalm-param class-string<\OCP\Authentication\IAlternativeLogin> $class |
|
168 | + * |
|
169 | + * @return void |
|
170 | + * |
|
171 | + * @since 20.0.0 |
|
172 | + */ |
|
173 | + public function registerAlternativeLogin(string $class): void; |
|
174 | + |
|
175 | + /** |
|
176 | + * Register an initialstate provider |
|
177 | + * |
|
178 | + * It is allowed to register more than one provider per app. |
|
179 | + * |
|
180 | + * @param string $class |
|
181 | + * @psalm-param class-string<\OCP\AppFramework\Services\InitialStateProvider> $class |
|
182 | + * |
|
183 | + * @return void |
|
184 | + * |
|
185 | + * @since 21.0.0 |
|
186 | + */ |
|
187 | + public function registerInitialStateProvider(string $class): void; |
|
188 | + |
|
189 | + /** |
|
190 | + * Register a well known protocol handler |
|
191 | + * |
|
192 | + * It is allowed to register more than one handler per app. |
|
193 | + * |
|
194 | + * @param string $class |
|
195 | + * @psalm-param class-string<\OCP\Http\WellKnown\IHandler> $class |
|
196 | + * |
|
197 | + * @return void |
|
198 | + * |
|
199 | + * @since 21.0.0 |
|
200 | + */ |
|
201 | + public function registerWellKnownHandler(string $class): void; |
|
202 | + |
|
203 | + /** |
|
204 | + * Register a custom template provider class that is able to inject custom templates |
|
205 | + * in addition to the user defined ones |
|
206 | + * |
|
207 | + * @param string $providerClass |
|
208 | + * @psalm-param class-string<ICustomTemplateProvider> $providerClass |
|
209 | + * @since 21.0.0 |
|
210 | + */ |
|
211 | + public function registerTemplateProvider(string $providerClass): void; |
|
212 | 212 | } |
@@ -51,305 +51,305 @@ |
||
51 | 51 | use Psr\Log\LoggerInterface; |
52 | 52 | |
53 | 53 | class TemplateManager implements ITemplateManager { |
54 | - private $registeredTypes = []; |
|
55 | - private $types = []; |
|
56 | - |
|
57 | - /** @var array|null */ |
|
58 | - private $providers = null; |
|
59 | - |
|
60 | - private $serverContainer; |
|
61 | - private $eventDispatcher; |
|
62 | - private $rootFolder; |
|
63 | - private $userManager; |
|
64 | - private $previewManager; |
|
65 | - private $config; |
|
66 | - private $l10n; |
|
67 | - private $logger; |
|
68 | - private $userId; |
|
69 | - private $l10nFactory; |
|
70 | - /** @var Coordinator */ |
|
71 | - private $bootstrapCoordinator; |
|
72 | - |
|
73 | - public function __construct( |
|
74 | - IServerContainer $serverContainer, |
|
75 | - IEventDispatcher $eventDispatcher, |
|
76 | - Coordinator $coordinator, |
|
77 | - IRootFolder $rootFolder, |
|
78 | - IUserSession $userSession, |
|
79 | - IUserManager $userManager, |
|
80 | - IPreview $previewManager, |
|
81 | - IConfig $config, |
|
82 | - IFactory $l10nFactory, |
|
83 | - LoggerInterface $logger |
|
84 | - ) { |
|
85 | - $this->serverContainer = $serverContainer; |
|
86 | - $this->eventDispatcher = $eventDispatcher; |
|
87 | - $this->bootstrapCoordinator = $coordinator; |
|
88 | - $this->rootFolder = $rootFolder; |
|
89 | - $this->userManager = $userManager; |
|
90 | - $this->previewManager = $previewManager; |
|
91 | - $this->config = $config; |
|
92 | - $this->l10nFactory = $l10nFactory; |
|
93 | - $this->l10n = $l10nFactory->get('lib'); |
|
94 | - $this->logger = $logger; |
|
95 | - $user = $userSession->getUser(); |
|
96 | - $this->userId = $user ? $user->getUID() : null; |
|
97 | - } |
|
98 | - |
|
99 | - public function registerTemplateFileCreator(callable $callback): void { |
|
100 | - $this->registeredTypes[] = $callback; |
|
101 | - } |
|
102 | - |
|
103 | - public function getRegisteredProviders(): array { |
|
104 | - if ($this->providers !== null) { |
|
105 | - return $this->providers; |
|
106 | - } |
|
107 | - |
|
108 | - $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
109 | - |
|
110 | - $this->providers = []; |
|
111 | - foreach ($context->getTemplateProviders() as $provider) { |
|
112 | - $class = $provider->getService(); |
|
113 | - $this->providers[$class] = $this->serverContainer->get($class); |
|
114 | - } |
|
115 | - return $this->providers; |
|
116 | - } |
|
117 | - |
|
118 | - public function getTypes(): array { |
|
119 | - if (!empty($this->types)) { |
|
120 | - return $this->types; |
|
121 | - } |
|
122 | - foreach ($this->registeredTypes as $registeredType) { |
|
123 | - $this->types[] = $registeredType(); |
|
124 | - } |
|
125 | - return $this->types; |
|
126 | - } |
|
127 | - |
|
128 | - public function listCreators(): array { |
|
129 | - $types = $this->getTypes(); |
|
130 | - usort($types, function (TemplateFileCreator $a, TemplateFileCreator $b) { |
|
131 | - return $a->getOrder() - $b->getOrder(); |
|
132 | - }); |
|
133 | - return $types; |
|
134 | - } |
|
135 | - |
|
136 | - public function listTemplates(): array { |
|
137 | - return array_map(function (TemplateFileCreator $entry) { |
|
138 | - return array_merge($entry->jsonSerialize(), [ |
|
139 | - 'templates' => $this->getTemplateFiles($entry) |
|
140 | - ]); |
|
141 | - }, $this->listCreators()); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param string $filePath |
|
146 | - * @param string $templateId |
|
147 | - * @return array |
|
148 | - * @throws GenericFileException |
|
149 | - */ |
|
150 | - public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user'): array { |
|
151 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
152 | - try { |
|
153 | - $userFolder->get($filePath); |
|
154 | - throw new GenericFileException($this->l10n->t('File already exists')); |
|
155 | - } catch (NotFoundException $e) { |
|
156 | - } |
|
157 | - try { |
|
158 | - $targetFile = $userFolder->newFile($filePath); |
|
159 | - if ($templateType === 'user' && $templateId !== '') { |
|
160 | - $template = $userFolder->get($templateId); |
|
161 | - $template->copy($targetFile->getPath()); |
|
162 | - } else { |
|
163 | - $matchingProvider = array_filter($this->getRegisteredProviders(), function (ICustomTemplateProvider $provider) use ($templateType) { |
|
164 | - return $templateType === get_class($provider); |
|
165 | - }); |
|
166 | - $provider = array_shift($matchingProvider); |
|
167 | - if ($provider) { |
|
168 | - $template = $provider->getCustomTemplate($templateId); |
|
169 | - $template->copy($targetFile->getPath()); |
|
170 | - } |
|
171 | - } |
|
172 | - $this->eventDispatcher->dispatchTyped(new FileCreatedFromTemplateEvent($template, $targetFile)); |
|
173 | - return $this->formatFile($userFolder->get($filePath)); |
|
174 | - } catch (\Exception $e) { |
|
175 | - $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
176 | - throw new GenericFileException($this->l10n->t('Failed to create file from template')); |
|
177 | - } |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return Folder |
|
182 | - * @throws \OCP\Files\NotFoundException |
|
183 | - * @throws \OCP\Files\NotPermittedException |
|
184 | - * @throws \OC\User\NoUserException |
|
185 | - */ |
|
186 | - private function getTemplateFolder(): Node { |
|
187 | - if ($this->getTemplatePath() !== '') { |
|
188 | - return $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath()); |
|
189 | - } |
|
190 | - throw new NotFoundException(); |
|
191 | - } |
|
192 | - |
|
193 | - private function getTemplateFiles(TemplateFileCreator $type): array { |
|
194 | - $templates = []; |
|
195 | - foreach ($this->getRegisteredProviders() as $provider) { |
|
196 | - foreach ($type->getMimetypes() as $mimetype) { |
|
197 | - foreach ($provider->getCustomTemplates($mimetype) as $template) { |
|
198 | - $templates[] = $template; |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
202 | - try { |
|
203 | - $userTemplateFolder = $this->getTemplateFolder(); |
|
204 | - } catch (\Exception $e) { |
|
205 | - return $templates; |
|
206 | - } |
|
207 | - foreach ($type->getMimetypes() as $mimetype) { |
|
208 | - foreach ($userTemplateFolder->searchByMime($mimetype) as $templateFile) { |
|
209 | - $template = new Template( |
|
210 | - 'user', |
|
211 | - $this->rootFolder->getUserFolder($this->userId)->getRelativePath($templateFile->getPath()), |
|
212 | - $templateFile |
|
213 | - ); |
|
214 | - $template->setHasPreview($this->previewManager->isAvailable($templateFile)); |
|
215 | - $templates[] = $template; |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - return $templates; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @param Node|File $file |
|
224 | - * @return array |
|
225 | - * @throws NotFoundException |
|
226 | - * @throws \OCP\Files\InvalidPathException |
|
227 | - */ |
|
228 | - private function formatFile(Node $file): array { |
|
229 | - return [ |
|
230 | - 'basename' => $file->getName(), |
|
231 | - 'etag' => $file->getEtag(), |
|
232 | - 'fileid' => $file->getId(), |
|
233 | - 'filename' => $this->rootFolder->getUserFolder($this->userId)->getRelativePath($file->getPath()), |
|
234 | - 'lastmod' => $file->getMTime(), |
|
235 | - 'mime' => $file->getMimetype(), |
|
236 | - 'size' => $file->getSize(), |
|
237 | - 'type' => $file->getType(), |
|
238 | - 'hasPreview' => $this->previewManager->isAvailable($file) |
|
239 | - ]; |
|
240 | - } |
|
241 | - |
|
242 | - public function hasTemplateDirectory(): bool { |
|
243 | - try { |
|
244 | - $this->getTemplateFolder(); |
|
245 | - return true; |
|
246 | - } catch (\Exception $e) { |
|
247 | - } |
|
248 | - return false; |
|
249 | - } |
|
250 | - |
|
251 | - public function setTemplatePath(string $path): void { |
|
252 | - $this->config->setUserValue($this->userId, 'core', 'templateDirectory', $path); |
|
253 | - } |
|
254 | - |
|
255 | - public function getTemplatePath(): string { |
|
256 | - return $this->config->getUserValue($this->userId, 'core', 'templateDirectory', ''); |
|
257 | - } |
|
258 | - |
|
259 | - public function initializeTemplateDirectory(string $path = null, string $userId = null, $copyTemplates = true): string { |
|
260 | - if ($userId !== null) { |
|
261 | - $this->userId = $userId; |
|
262 | - } |
|
263 | - |
|
264 | - $defaultSkeletonDirectory = \OC::$SERVERROOT . '/core/skeleton'; |
|
265 | - $defaultTemplateDirectory = \OC::$SERVERROOT . '/core/skeleton/Templates'; |
|
266 | - $skeletonPath = $this->config->getSystemValue('skeletondirectory', $defaultSkeletonDirectory); |
|
267 | - $skeletonTemplatePath = $this->config->getSystemValue('templatedirectory', $defaultTemplateDirectory); |
|
268 | - $isDefaultSkeleton = $skeletonPath === $defaultSkeletonDirectory; |
|
269 | - $isDefaultTemplates = $skeletonTemplatePath === $defaultTemplateDirectory; |
|
270 | - $userLang = $this->l10nFactory->getUserLanguage($this->userManager->get($this->userId)); |
|
271 | - |
|
272 | - try { |
|
273 | - $l10n = $this->l10nFactory->get('lib', $userLang); |
|
274 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
275 | - $userTemplatePath = $path ?? $l10n->t('Templates') . '/'; |
|
276 | - |
|
277 | - // Initial user setup without a provided path |
|
278 | - if ($path === null) { |
|
279 | - // All locations are default so we just need to rename the directory to the users language |
|
280 | - if ($isDefaultSkeleton && $isDefaultTemplates) { |
|
281 | - if (!$userFolder->nodeExists('Templates')) { |
|
282 | - return ''; |
|
283 | - } |
|
284 | - $newPath = Filesystem::normalizePath($userFolder->getPath() . '/' . $userTemplatePath); |
|
285 | - if ($newPath !== $userFolder->get('Templates')->getPath()) { |
|
286 | - $userFolder->get('Templates')->move($newPath); |
|
287 | - } |
|
288 | - $this->setTemplatePath($userTemplatePath); |
|
289 | - return $userTemplatePath; |
|
290 | - } |
|
291 | - |
|
292 | - if ($isDefaultSkeleton && !empty($skeletonTemplatePath) && !$isDefaultTemplates && $userFolder->nodeExists('Templates')) { |
|
293 | - $shippedSkeletonTemplates = $userFolder->get('Templates'); |
|
294 | - $shippedSkeletonTemplates->delete(); |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - try { |
|
299 | - $folder = $userFolder->newFolder($userTemplatePath); |
|
300 | - } catch (NotPermittedException $e) { |
|
301 | - $folder = $userFolder->get($userTemplatePath); |
|
302 | - } |
|
303 | - |
|
304 | - $folderIsEmpty = count($folder->getDirectoryListing()) === 0; |
|
305 | - |
|
306 | - if (!$copyTemplates) { |
|
307 | - $this->setTemplatePath($userTemplatePath); |
|
308 | - return $userTemplatePath; |
|
309 | - } |
|
310 | - |
|
311 | - if (!$isDefaultTemplates && $folderIsEmpty) { |
|
312 | - $localizedSkeletonTemplatePath = $this->getLocalizedTemplatePath($skeletonTemplatePath, $userLang); |
|
313 | - if (!empty($localizedSkeletonTemplatePath) && file_exists($localizedSkeletonTemplatePath)) { |
|
314 | - \OC_Util::copyr($localizedSkeletonTemplatePath, $folder); |
|
315 | - $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
|
316 | - $this->setTemplatePath($userTemplatePath); |
|
317 | - return $userTemplatePath; |
|
318 | - } |
|
319 | - } |
|
320 | - |
|
321 | - if ($path !== null && $isDefaultSkeleton && $isDefaultTemplates && $folderIsEmpty) { |
|
322 | - $localizedSkeletonPath = $this->getLocalizedTemplatePath($skeletonPath . '/Templates', $userLang); |
|
323 | - if (!empty($localizedSkeletonPath) && file_exists($localizedSkeletonPath)) { |
|
324 | - \OC_Util::copyr($localizedSkeletonPath, $folder); |
|
325 | - $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
|
326 | - $this->setTemplatePath($userTemplatePath); |
|
327 | - return $userTemplatePath; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - $this->setTemplatePath($path ?? ''); |
|
332 | - return $this->getTemplatePath(); |
|
333 | - } catch (\Throwable $e) { |
|
334 | - $this->logger->error('Failed to initialize templates directory to user language ' . $userLang . ' for ' . $userId, ['app' => 'files_templates', 'exception' => $e]); |
|
335 | - } |
|
336 | - $this->setTemplatePath(''); |
|
337 | - return $this->getTemplatePath(); |
|
338 | - } |
|
339 | - |
|
340 | - private function getLocalizedTemplatePath(string $skeletonTemplatePath, string $userLang) { |
|
341 | - $localizedSkeletonTemplatePath = str_replace('{lang}', $userLang, $skeletonTemplatePath); |
|
342 | - |
|
343 | - if (!file_exists($localizedSkeletonTemplatePath)) { |
|
344 | - $dialectStart = strpos($userLang, '_'); |
|
345 | - if ($dialectStart !== false) { |
|
346 | - $localizedSkeletonTemplatePath = str_replace('{lang}', substr($userLang, 0, $dialectStart), $skeletonTemplatePath); |
|
347 | - } |
|
348 | - if ($dialectStart === false || !file_exists($localizedSkeletonTemplatePath)) { |
|
349 | - $localizedSkeletonTemplatePath = str_replace('{lang}', 'default', $skeletonTemplatePath); |
|
350 | - } |
|
351 | - } |
|
352 | - |
|
353 | - return $localizedSkeletonTemplatePath; |
|
354 | - } |
|
54 | + private $registeredTypes = []; |
|
55 | + private $types = []; |
|
56 | + |
|
57 | + /** @var array|null */ |
|
58 | + private $providers = null; |
|
59 | + |
|
60 | + private $serverContainer; |
|
61 | + private $eventDispatcher; |
|
62 | + private $rootFolder; |
|
63 | + private $userManager; |
|
64 | + private $previewManager; |
|
65 | + private $config; |
|
66 | + private $l10n; |
|
67 | + private $logger; |
|
68 | + private $userId; |
|
69 | + private $l10nFactory; |
|
70 | + /** @var Coordinator */ |
|
71 | + private $bootstrapCoordinator; |
|
72 | + |
|
73 | + public function __construct( |
|
74 | + IServerContainer $serverContainer, |
|
75 | + IEventDispatcher $eventDispatcher, |
|
76 | + Coordinator $coordinator, |
|
77 | + IRootFolder $rootFolder, |
|
78 | + IUserSession $userSession, |
|
79 | + IUserManager $userManager, |
|
80 | + IPreview $previewManager, |
|
81 | + IConfig $config, |
|
82 | + IFactory $l10nFactory, |
|
83 | + LoggerInterface $logger |
|
84 | + ) { |
|
85 | + $this->serverContainer = $serverContainer; |
|
86 | + $this->eventDispatcher = $eventDispatcher; |
|
87 | + $this->bootstrapCoordinator = $coordinator; |
|
88 | + $this->rootFolder = $rootFolder; |
|
89 | + $this->userManager = $userManager; |
|
90 | + $this->previewManager = $previewManager; |
|
91 | + $this->config = $config; |
|
92 | + $this->l10nFactory = $l10nFactory; |
|
93 | + $this->l10n = $l10nFactory->get('lib'); |
|
94 | + $this->logger = $logger; |
|
95 | + $user = $userSession->getUser(); |
|
96 | + $this->userId = $user ? $user->getUID() : null; |
|
97 | + } |
|
98 | + |
|
99 | + public function registerTemplateFileCreator(callable $callback): void { |
|
100 | + $this->registeredTypes[] = $callback; |
|
101 | + } |
|
102 | + |
|
103 | + public function getRegisteredProviders(): array { |
|
104 | + if ($this->providers !== null) { |
|
105 | + return $this->providers; |
|
106 | + } |
|
107 | + |
|
108 | + $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
109 | + |
|
110 | + $this->providers = []; |
|
111 | + foreach ($context->getTemplateProviders() as $provider) { |
|
112 | + $class = $provider->getService(); |
|
113 | + $this->providers[$class] = $this->serverContainer->get($class); |
|
114 | + } |
|
115 | + return $this->providers; |
|
116 | + } |
|
117 | + |
|
118 | + public function getTypes(): array { |
|
119 | + if (!empty($this->types)) { |
|
120 | + return $this->types; |
|
121 | + } |
|
122 | + foreach ($this->registeredTypes as $registeredType) { |
|
123 | + $this->types[] = $registeredType(); |
|
124 | + } |
|
125 | + return $this->types; |
|
126 | + } |
|
127 | + |
|
128 | + public function listCreators(): array { |
|
129 | + $types = $this->getTypes(); |
|
130 | + usort($types, function (TemplateFileCreator $a, TemplateFileCreator $b) { |
|
131 | + return $a->getOrder() - $b->getOrder(); |
|
132 | + }); |
|
133 | + return $types; |
|
134 | + } |
|
135 | + |
|
136 | + public function listTemplates(): array { |
|
137 | + return array_map(function (TemplateFileCreator $entry) { |
|
138 | + return array_merge($entry->jsonSerialize(), [ |
|
139 | + 'templates' => $this->getTemplateFiles($entry) |
|
140 | + ]); |
|
141 | + }, $this->listCreators()); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param string $filePath |
|
146 | + * @param string $templateId |
|
147 | + * @return array |
|
148 | + * @throws GenericFileException |
|
149 | + */ |
|
150 | + public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user'): array { |
|
151 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
152 | + try { |
|
153 | + $userFolder->get($filePath); |
|
154 | + throw new GenericFileException($this->l10n->t('File already exists')); |
|
155 | + } catch (NotFoundException $e) { |
|
156 | + } |
|
157 | + try { |
|
158 | + $targetFile = $userFolder->newFile($filePath); |
|
159 | + if ($templateType === 'user' && $templateId !== '') { |
|
160 | + $template = $userFolder->get($templateId); |
|
161 | + $template->copy($targetFile->getPath()); |
|
162 | + } else { |
|
163 | + $matchingProvider = array_filter($this->getRegisteredProviders(), function (ICustomTemplateProvider $provider) use ($templateType) { |
|
164 | + return $templateType === get_class($provider); |
|
165 | + }); |
|
166 | + $provider = array_shift($matchingProvider); |
|
167 | + if ($provider) { |
|
168 | + $template = $provider->getCustomTemplate($templateId); |
|
169 | + $template->copy($targetFile->getPath()); |
|
170 | + } |
|
171 | + } |
|
172 | + $this->eventDispatcher->dispatchTyped(new FileCreatedFromTemplateEvent($template, $targetFile)); |
|
173 | + return $this->formatFile($userFolder->get($filePath)); |
|
174 | + } catch (\Exception $e) { |
|
175 | + $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
176 | + throw new GenericFileException($this->l10n->t('Failed to create file from template')); |
|
177 | + } |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return Folder |
|
182 | + * @throws \OCP\Files\NotFoundException |
|
183 | + * @throws \OCP\Files\NotPermittedException |
|
184 | + * @throws \OC\User\NoUserException |
|
185 | + */ |
|
186 | + private function getTemplateFolder(): Node { |
|
187 | + if ($this->getTemplatePath() !== '') { |
|
188 | + return $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath()); |
|
189 | + } |
|
190 | + throw new NotFoundException(); |
|
191 | + } |
|
192 | + |
|
193 | + private function getTemplateFiles(TemplateFileCreator $type): array { |
|
194 | + $templates = []; |
|
195 | + foreach ($this->getRegisteredProviders() as $provider) { |
|
196 | + foreach ($type->getMimetypes() as $mimetype) { |
|
197 | + foreach ($provider->getCustomTemplates($mimetype) as $template) { |
|
198 | + $templates[] = $template; |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | + try { |
|
203 | + $userTemplateFolder = $this->getTemplateFolder(); |
|
204 | + } catch (\Exception $e) { |
|
205 | + return $templates; |
|
206 | + } |
|
207 | + foreach ($type->getMimetypes() as $mimetype) { |
|
208 | + foreach ($userTemplateFolder->searchByMime($mimetype) as $templateFile) { |
|
209 | + $template = new Template( |
|
210 | + 'user', |
|
211 | + $this->rootFolder->getUserFolder($this->userId)->getRelativePath($templateFile->getPath()), |
|
212 | + $templateFile |
|
213 | + ); |
|
214 | + $template->setHasPreview($this->previewManager->isAvailable($templateFile)); |
|
215 | + $templates[] = $template; |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + return $templates; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @param Node|File $file |
|
224 | + * @return array |
|
225 | + * @throws NotFoundException |
|
226 | + * @throws \OCP\Files\InvalidPathException |
|
227 | + */ |
|
228 | + private function formatFile(Node $file): array { |
|
229 | + return [ |
|
230 | + 'basename' => $file->getName(), |
|
231 | + 'etag' => $file->getEtag(), |
|
232 | + 'fileid' => $file->getId(), |
|
233 | + 'filename' => $this->rootFolder->getUserFolder($this->userId)->getRelativePath($file->getPath()), |
|
234 | + 'lastmod' => $file->getMTime(), |
|
235 | + 'mime' => $file->getMimetype(), |
|
236 | + 'size' => $file->getSize(), |
|
237 | + 'type' => $file->getType(), |
|
238 | + 'hasPreview' => $this->previewManager->isAvailable($file) |
|
239 | + ]; |
|
240 | + } |
|
241 | + |
|
242 | + public function hasTemplateDirectory(): bool { |
|
243 | + try { |
|
244 | + $this->getTemplateFolder(); |
|
245 | + return true; |
|
246 | + } catch (\Exception $e) { |
|
247 | + } |
|
248 | + return false; |
|
249 | + } |
|
250 | + |
|
251 | + public function setTemplatePath(string $path): void { |
|
252 | + $this->config->setUserValue($this->userId, 'core', 'templateDirectory', $path); |
|
253 | + } |
|
254 | + |
|
255 | + public function getTemplatePath(): string { |
|
256 | + return $this->config->getUserValue($this->userId, 'core', 'templateDirectory', ''); |
|
257 | + } |
|
258 | + |
|
259 | + public function initializeTemplateDirectory(string $path = null, string $userId = null, $copyTemplates = true): string { |
|
260 | + if ($userId !== null) { |
|
261 | + $this->userId = $userId; |
|
262 | + } |
|
263 | + |
|
264 | + $defaultSkeletonDirectory = \OC::$SERVERROOT . '/core/skeleton'; |
|
265 | + $defaultTemplateDirectory = \OC::$SERVERROOT . '/core/skeleton/Templates'; |
|
266 | + $skeletonPath = $this->config->getSystemValue('skeletondirectory', $defaultSkeletonDirectory); |
|
267 | + $skeletonTemplatePath = $this->config->getSystemValue('templatedirectory', $defaultTemplateDirectory); |
|
268 | + $isDefaultSkeleton = $skeletonPath === $defaultSkeletonDirectory; |
|
269 | + $isDefaultTemplates = $skeletonTemplatePath === $defaultTemplateDirectory; |
|
270 | + $userLang = $this->l10nFactory->getUserLanguage($this->userManager->get($this->userId)); |
|
271 | + |
|
272 | + try { |
|
273 | + $l10n = $this->l10nFactory->get('lib', $userLang); |
|
274 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
275 | + $userTemplatePath = $path ?? $l10n->t('Templates') . '/'; |
|
276 | + |
|
277 | + // Initial user setup without a provided path |
|
278 | + if ($path === null) { |
|
279 | + // All locations are default so we just need to rename the directory to the users language |
|
280 | + if ($isDefaultSkeleton && $isDefaultTemplates) { |
|
281 | + if (!$userFolder->nodeExists('Templates')) { |
|
282 | + return ''; |
|
283 | + } |
|
284 | + $newPath = Filesystem::normalizePath($userFolder->getPath() . '/' . $userTemplatePath); |
|
285 | + if ($newPath !== $userFolder->get('Templates')->getPath()) { |
|
286 | + $userFolder->get('Templates')->move($newPath); |
|
287 | + } |
|
288 | + $this->setTemplatePath($userTemplatePath); |
|
289 | + return $userTemplatePath; |
|
290 | + } |
|
291 | + |
|
292 | + if ($isDefaultSkeleton && !empty($skeletonTemplatePath) && !$isDefaultTemplates && $userFolder->nodeExists('Templates')) { |
|
293 | + $shippedSkeletonTemplates = $userFolder->get('Templates'); |
|
294 | + $shippedSkeletonTemplates->delete(); |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + try { |
|
299 | + $folder = $userFolder->newFolder($userTemplatePath); |
|
300 | + } catch (NotPermittedException $e) { |
|
301 | + $folder = $userFolder->get($userTemplatePath); |
|
302 | + } |
|
303 | + |
|
304 | + $folderIsEmpty = count($folder->getDirectoryListing()) === 0; |
|
305 | + |
|
306 | + if (!$copyTemplates) { |
|
307 | + $this->setTemplatePath($userTemplatePath); |
|
308 | + return $userTemplatePath; |
|
309 | + } |
|
310 | + |
|
311 | + if (!$isDefaultTemplates && $folderIsEmpty) { |
|
312 | + $localizedSkeletonTemplatePath = $this->getLocalizedTemplatePath($skeletonTemplatePath, $userLang); |
|
313 | + if (!empty($localizedSkeletonTemplatePath) && file_exists($localizedSkeletonTemplatePath)) { |
|
314 | + \OC_Util::copyr($localizedSkeletonTemplatePath, $folder); |
|
315 | + $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
|
316 | + $this->setTemplatePath($userTemplatePath); |
|
317 | + return $userTemplatePath; |
|
318 | + } |
|
319 | + } |
|
320 | + |
|
321 | + if ($path !== null && $isDefaultSkeleton && $isDefaultTemplates && $folderIsEmpty) { |
|
322 | + $localizedSkeletonPath = $this->getLocalizedTemplatePath($skeletonPath . '/Templates', $userLang); |
|
323 | + if (!empty($localizedSkeletonPath) && file_exists($localizedSkeletonPath)) { |
|
324 | + \OC_Util::copyr($localizedSkeletonPath, $folder); |
|
325 | + $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
|
326 | + $this->setTemplatePath($userTemplatePath); |
|
327 | + return $userTemplatePath; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + $this->setTemplatePath($path ?? ''); |
|
332 | + return $this->getTemplatePath(); |
|
333 | + } catch (\Throwable $e) { |
|
334 | + $this->logger->error('Failed to initialize templates directory to user language ' . $userLang . ' for ' . $userId, ['app' => 'files_templates', 'exception' => $e]); |
|
335 | + } |
|
336 | + $this->setTemplatePath(''); |
|
337 | + return $this->getTemplatePath(); |
|
338 | + } |
|
339 | + |
|
340 | + private function getLocalizedTemplatePath(string $skeletonTemplatePath, string $userLang) { |
|
341 | + $localizedSkeletonTemplatePath = str_replace('{lang}', $userLang, $skeletonTemplatePath); |
|
342 | + |
|
343 | + if (!file_exists($localizedSkeletonTemplatePath)) { |
|
344 | + $dialectStart = strpos($userLang, '_'); |
|
345 | + if ($dialectStart !== false) { |
|
346 | + $localizedSkeletonTemplatePath = str_replace('{lang}', substr($userLang, 0, $dialectStart), $skeletonTemplatePath); |
|
347 | + } |
|
348 | + if ($dialectStart === false || !file_exists($localizedSkeletonTemplatePath)) { |
|
349 | + $localizedSkeletonTemplatePath = str_replace('{lang}', 'default', $skeletonTemplatePath); |
|
350 | + } |
|
351 | + } |
|
352 | + |
|
353 | + return $localizedSkeletonTemplatePath; |
|
354 | + } |
|
355 | 355 | } |
@@ -41,88 +41,88 @@ |
||
41 | 41 | |
42 | 42 | class RequestManager { |
43 | 43 | |
44 | - /** @var Coordinator */ |
|
45 | - private $coordinator; |
|
46 | - |
|
47 | - /** @var IServerContainer */ |
|
48 | - private $container; |
|
49 | - |
|
50 | - /** @var LoggerInterface */ |
|
51 | - private $logger; |
|
52 | - |
|
53 | - public function __construct(Coordinator $coordinator, |
|
54 | - IServerContainer $container, |
|
55 | - LoggerInterface $logger) { |
|
56 | - $this->coordinator = $coordinator; |
|
57 | - $this->container = $container; |
|
58 | - $this->logger = $logger; |
|
59 | - } |
|
60 | - |
|
61 | - public function process(string $service, IRequest $request): ?IResponse { |
|
62 | - $handlers = $this->loadHandlers(); |
|
63 | - $context = new class($request) implements IRequestContext { |
|
64 | - /** @var IRequest */ |
|
65 | - private $request; |
|
66 | - |
|
67 | - public function __construct(IRequest $request) { |
|
68 | - $this->request = $request; |
|
69 | - } |
|
70 | - |
|
71 | - public function getHttpRequest(): IRequest { |
|
72 | - return $this->request; |
|
73 | - } |
|
74 | - }; |
|
75 | - |
|
76 | - $subject = $request->getParam('resource'); |
|
77 | - $initialResponse = new JrdResponse($subject ?? ''); |
|
78 | - $finalResponse = array_reduce($handlers, function (?IResponse $previousResponse, IHandler $handler) use ($context, $service) { |
|
79 | - return $handler->handle($service, $context, $previousResponse); |
|
80 | - }, $initialResponse); |
|
81 | - |
|
82 | - if ($finalResponse instanceof JrdResponse && $finalResponse->isEmpty()) { |
|
83 | - return null; |
|
84 | - } |
|
85 | - |
|
86 | - return $finalResponse; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @return IHandler[] |
|
91 | - */ |
|
92 | - private function loadHandlers(): array { |
|
93 | - $context = $this->coordinator->getRegistrationContext(); |
|
94 | - |
|
95 | - if ($context === null) { |
|
96 | - throw new RuntimeException("Well known handlers requested before the apps had been fully registered"); |
|
97 | - } |
|
98 | - |
|
99 | - $registrations = $context->getWellKnownHandlers(); |
|
100 | - $this->logger->debug(count($registrations) . " well known handlers registered"); |
|
101 | - |
|
102 | - return array_filter( |
|
103 | - array_map(function (ServiceRegistration $registration) { |
|
104 | - /** @var ServiceRegistration<IHandler> $registration */ |
|
105 | - $class = $registration->getService(); |
|
106 | - |
|
107 | - try { |
|
108 | - $handler = $this->container->get($class); |
|
109 | - |
|
110 | - if (!($handler) instanceof IHandler) { |
|
111 | - $this->logger->error("Well known handler $class is invalid"); |
|
112 | - |
|
113 | - return null; |
|
114 | - } |
|
115 | - |
|
116 | - return $handler; |
|
117 | - } catch (QueryException $e) { |
|
118 | - $this->logger->error("Could not load well known handler $class", [ |
|
119 | - 'exception' => $e, |
|
120 | - 'app' => $registration->getAppId(), |
|
121 | - ]); |
|
122 | - |
|
123 | - return null; |
|
124 | - } |
|
125 | - }, $registrations) |
|
126 | - ); |
|
127 | - } |
|
44 | + /** @var Coordinator */ |
|
45 | + private $coordinator; |
|
46 | + |
|
47 | + /** @var IServerContainer */ |
|
48 | + private $container; |
|
49 | + |
|
50 | + /** @var LoggerInterface */ |
|
51 | + private $logger; |
|
52 | + |
|
53 | + public function __construct(Coordinator $coordinator, |
|
54 | + IServerContainer $container, |
|
55 | + LoggerInterface $logger) { |
|
56 | + $this->coordinator = $coordinator; |
|
57 | + $this->container = $container; |
|
58 | + $this->logger = $logger; |
|
59 | + } |
|
60 | + |
|
61 | + public function process(string $service, IRequest $request): ?IResponse { |
|
62 | + $handlers = $this->loadHandlers(); |
|
63 | + $context = new class($request) implements IRequestContext { |
|
64 | + /** @var IRequest */ |
|
65 | + private $request; |
|
66 | + |
|
67 | + public function __construct(IRequest $request) { |
|
68 | + $this->request = $request; |
|
69 | + } |
|
70 | + |
|
71 | + public function getHttpRequest(): IRequest { |
|
72 | + return $this->request; |
|
73 | + } |
|
74 | + }; |
|
75 | + |
|
76 | + $subject = $request->getParam('resource'); |
|
77 | + $initialResponse = new JrdResponse($subject ?? ''); |
|
78 | + $finalResponse = array_reduce($handlers, function (?IResponse $previousResponse, IHandler $handler) use ($context, $service) { |
|
79 | + return $handler->handle($service, $context, $previousResponse); |
|
80 | + }, $initialResponse); |
|
81 | + |
|
82 | + if ($finalResponse instanceof JrdResponse && $finalResponse->isEmpty()) { |
|
83 | + return null; |
|
84 | + } |
|
85 | + |
|
86 | + return $finalResponse; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @return IHandler[] |
|
91 | + */ |
|
92 | + private function loadHandlers(): array { |
|
93 | + $context = $this->coordinator->getRegistrationContext(); |
|
94 | + |
|
95 | + if ($context === null) { |
|
96 | + throw new RuntimeException("Well known handlers requested before the apps had been fully registered"); |
|
97 | + } |
|
98 | + |
|
99 | + $registrations = $context->getWellKnownHandlers(); |
|
100 | + $this->logger->debug(count($registrations) . " well known handlers registered"); |
|
101 | + |
|
102 | + return array_filter( |
|
103 | + array_map(function (ServiceRegistration $registration) { |
|
104 | + /** @var ServiceRegistration<IHandler> $registration */ |
|
105 | + $class = $registration->getService(); |
|
106 | + |
|
107 | + try { |
|
108 | + $handler = $this->container->get($class); |
|
109 | + |
|
110 | + if (!($handler) instanceof IHandler) { |
|
111 | + $this->logger->error("Well known handler $class is invalid"); |
|
112 | + |
|
113 | + return null; |
|
114 | + } |
|
115 | + |
|
116 | + return $handler; |
|
117 | + } catch (QueryException $e) { |
|
118 | + $this->logger->error("Could not load well known handler $class", [ |
|
119 | + 'exception' => $e, |
|
120 | + 'app' => $registration->getAppId(), |
|
121 | + ]); |
|
122 | + |
|
123 | + return null; |
|
124 | + } |
|
125 | + }, $registrations) |
|
126 | + ); |
|
127 | + } |
|
128 | 128 | } |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | |
76 | 76 | $subject = $request->getParam('resource'); |
77 | 77 | $initialResponse = new JrdResponse($subject ?? ''); |
78 | - $finalResponse = array_reduce($handlers, function (?IResponse $previousResponse, IHandler $handler) use ($context, $service) { |
|
78 | + $finalResponse = array_reduce($handlers, function(?IResponse $previousResponse, IHandler $handler) use ($context, $service) { |
|
79 | 79 | return $handler->handle($service, $context, $previousResponse); |
80 | 80 | }, $initialResponse); |
81 | 81 | |
@@ -97,10 +97,10 @@ discard block |
||
97 | 97 | } |
98 | 98 | |
99 | 99 | $registrations = $context->getWellKnownHandlers(); |
100 | - $this->logger->debug(count($registrations) . " well known handlers registered"); |
|
100 | + $this->logger->debug(count($registrations)." well known handlers registered"); |
|
101 | 101 | |
102 | 102 | return array_filter( |
103 | - array_map(function (ServiceRegistration $registration) { |
|
103 | + array_map(function(ServiceRegistration $registration) { |
|
104 | 104 | /** @var ServiceRegistration<IHandler> $registration */ |
105 | 105 | $class = $registration->getService(); |
106 | 106 |
@@ -30,41 +30,41 @@ |
||
30 | 30 | */ |
31 | 31 | class ServiceAliasRegistration extends ARegistration { |
32 | 32 | |
33 | - /** |
|
34 | - * @var string |
|
35 | - * @psalm-var string|class-string |
|
36 | - */ |
|
37 | - private $alias; |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + * @psalm-var string|class-string |
|
36 | + */ |
|
37 | + private $alias; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var string |
|
41 | - * @psalm-var string|class-string |
|
42 | - */ |
|
43 | - private $target; |
|
39 | + /** |
|
40 | + * @var string |
|
41 | + * @psalm-var string|class-string |
|
42 | + */ |
|
43 | + private $target; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @psalm-param string|class-string $alias |
|
47 | - * @paslm-param string|class-string $target |
|
48 | - */ |
|
49 | - public function __construct(string $appId, |
|
50 | - string $alias, |
|
51 | - string $target) { |
|
52 | - parent::__construct($appId); |
|
53 | - $this->alias = $alias; |
|
54 | - $this->target = $target; |
|
55 | - } |
|
45 | + /** |
|
46 | + * @psalm-param string|class-string $alias |
|
47 | + * @paslm-param string|class-string $target |
|
48 | + */ |
|
49 | + public function __construct(string $appId, |
|
50 | + string $alias, |
|
51 | + string $target) { |
|
52 | + parent::__construct($appId); |
|
53 | + $this->alias = $alias; |
|
54 | + $this->target = $target; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @psalm-return string|class-string |
|
59 | - */ |
|
60 | - public function getAlias(): string { |
|
61 | - return $this->alias; |
|
62 | - } |
|
57 | + /** |
|
58 | + * @psalm-return string|class-string |
|
59 | + */ |
|
60 | + public function getAlias(): string { |
|
61 | + return $this->alias; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @psalm-return string|class-string |
|
66 | - */ |
|
67 | - public function getTarget(): string { |
|
68 | - return $this->target; |
|
69 | - } |
|
64 | + /** |
|
65 | + * @psalm-return string|class-string |
|
66 | + */ |
|
67 | + public function getTarget(): string { |
|
68 | + return $this->target; |
|
69 | + } |
|
70 | 70 | } |
@@ -30,24 +30,24 @@ |
||
30 | 30 | * @template T |
31 | 31 | */ |
32 | 32 | class ServiceRegistration extends ARegistration { |
33 | - /** |
|
34 | - * @var string |
|
35 | - * @psalm-var class-string<T> |
|
36 | - */ |
|
37 | - private $service; |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + * @psalm-var class-string<T> |
|
36 | + */ |
|
37 | + private $service; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @psalm-param class-string<T> $service |
|
41 | - */ |
|
42 | - public function __construct(string $appId, string $service) { |
|
43 | - parent::__construct($appId); |
|
44 | - $this->service = $service; |
|
45 | - } |
|
39 | + /** |
|
40 | + * @psalm-param class-string<T> $service |
|
41 | + */ |
|
42 | + public function __construct(string $appId, string $service) { |
|
43 | + parent::__construct($appId); |
|
44 | + $this->service = $service; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @psalm-return class-string<T> |
|
49 | - */ |
|
50 | - public function getService(): string { |
|
51 | - return $this->service; |
|
52 | - } |
|
47 | + /** |
|
48 | + * @psalm-return class-string<T> |
|
49 | + */ |
|
50 | + public function getService(): string { |
|
51 | + return $this->service; |
|
52 | + } |
|
53 | 53 | } |
@@ -30,28 +30,28 @@ |
||
30 | 30 | */ |
31 | 31 | final class ParameterRegistration extends ARegistration { |
32 | 32 | |
33 | - /** @var string */ |
|
34 | - private $name; |
|
35 | - |
|
36 | - /** @var mixed */ |
|
37 | - private $value; |
|
38 | - |
|
39 | - public function __construct(string $appId, |
|
40 | - string $name, |
|
41 | - $value) { |
|
42 | - parent::__construct($appId); |
|
43 | - $this->name = $name; |
|
44 | - $this->value = $value; |
|
45 | - } |
|
46 | - |
|
47 | - public function getName(): string { |
|
48 | - return $this->name; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @return mixed |
|
53 | - */ |
|
54 | - public function getValue() { |
|
55 | - return $this->value; |
|
56 | - } |
|
33 | + /** @var string */ |
|
34 | + private $name; |
|
35 | + |
|
36 | + /** @var mixed */ |
|
37 | + private $value; |
|
38 | + |
|
39 | + public function __construct(string $appId, |
|
40 | + string $name, |
|
41 | + $value) { |
|
42 | + parent::__construct($appId); |
|
43 | + $this->name = $name; |
|
44 | + $this->value = $value; |
|
45 | + } |
|
46 | + |
|
47 | + public function getName(): string { |
|
48 | + return $this->name; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @return mixed |
|
53 | + */ |
|
54 | + public function getValue() { |
|
55 | + return $this->value; |
|
56 | + } |
|
57 | 57 | } |
@@ -30,17 +30,17 @@ |
||
30 | 30 | */ |
31 | 31 | abstract class ARegistration { |
32 | 32 | |
33 | - /** @var string */ |
|
34 | - private $appId; |
|
33 | + /** @var string */ |
|
34 | + private $appId; |
|
35 | 35 | |
36 | - public function __construct(string $appId) { |
|
37 | - $this->appId = $appId; |
|
38 | - } |
|
36 | + public function __construct(string $appId) { |
|
37 | + $this->appId = $appId; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function getAppId(): string { |
|
44 | - return $this->appId; |
|
45 | - } |
|
40 | + /** |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function getAppId(): string { |
|
44 | + return $this->appId; |
|
45 | + } |
|
46 | 46 | } |
@@ -30,43 +30,43 @@ |
||
30 | 30 | */ |
31 | 31 | class ServiceFactoryRegistration extends ARegistration { |
32 | 32 | |
33 | - /** |
|
34 | - * @var string |
|
35 | - * @psalm-var string|class-string |
|
36 | - */ |
|
37 | - private $name; |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + * @psalm-var string|class-string |
|
36 | + */ |
|
37 | + private $name; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var callable |
|
41 | - * @psalm-var callable(\Psr\Container\ContainerInterface): mixed |
|
42 | - */ |
|
43 | - private $factory; |
|
39 | + /** |
|
40 | + * @var callable |
|
41 | + * @psalm-var callable(\Psr\Container\ContainerInterface): mixed |
|
42 | + */ |
|
43 | + private $factory; |
|
44 | 44 | |
45 | - /** @var bool */ |
|
46 | - private $shared; |
|
45 | + /** @var bool */ |
|
46 | + private $shared; |
|
47 | 47 | |
48 | - public function __construct(string $appId, |
|
49 | - string $alias, |
|
50 | - callable $target, |
|
51 | - bool $shared) { |
|
52 | - parent::__construct($appId); |
|
53 | - $this->name = $alias; |
|
54 | - $this->factory = $target; |
|
55 | - $this->shared = $shared; |
|
56 | - } |
|
48 | + public function __construct(string $appId, |
|
49 | + string $alias, |
|
50 | + callable $target, |
|
51 | + bool $shared) { |
|
52 | + parent::__construct($appId); |
|
53 | + $this->name = $alias; |
|
54 | + $this->factory = $target; |
|
55 | + $this->shared = $shared; |
|
56 | + } |
|
57 | 57 | |
58 | - public function getName(): string { |
|
59 | - return $this->name; |
|
60 | - } |
|
58 | + public function getName(): string { |
|
59 | + return $this->name; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @psalm-return callable(\Psr\Container\ContainerInterface): mixed |
|
64 | - */ |
|
65 | - public function getFactory(): callable { |
|
66 | - return $this->factory; |
|
67 | - } |
|
62 | + /** |
|
63 | + * @psalm-return callable(\Psr\Container\ContainerInterface): mixed |
|
64 | + */ |
|
65 | + public function getFactory(): callable { |
|
66 | + return $this->factory; |
|
67 | + } |
|
68 | 68 | |
69 | - public function isShared(): bool { |
|
70 | - return $this->shared; |
|
71 | - } |
|
69 | + public function isShared(): bool { |
|
70 | + return $this->shared; |
|
71 | + } |
|
72 | 72 | } |
@@ -31,32 +31,32 @@ |
||
31 | 31 | */ |
32 | 32 | class EventListenerRegistration extends ServiceRegistration { |
33 | 33 | |
34 | - /** @var string */ |
|
35 | - private $event; |
|
36 | - |
|
37 | - /** @var int */ |
|
38 | - private $priority; |
|
39 | - |
|
40 | - public function __construct(string $appId, |
|
41 | - string $event, |
|
42 | - string $service, |
|
43 | - int $priority) { |
|
44 | - parent::__construct($appId, $service); |
|
45 | - $this->event = $event; |
|
46 | - $this->priority = $priority; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function getEvent(): string { |
|
53 | - return $this->event; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @return int |
|
58 | - */ |
|
59 | - public function getPriority(): int { |
|
60 | - return $this->priority; |
|
61 | - } |
|
34 | + /** @var string */ |
|
35 | + private $event; |
|
36 | + |
|
37 | + /** @var int */ |
|
38 | + private $priority; |
|
39 | + |
|
40 | + public function __construct(string $appId, |
|
41 | + string $event, |
|
42 | + string $service, |
|
43 | + int $priority) { |
|
44 | + parent::__construct($appId, $service); |
|
45 | + $this->event = $event; |
|
46 | + $this->priority = $priority; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function getEvent(): string { |
|
53 | + return $this->event; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @return int |
|
58 | + */ |
|
59 | + public function getPriority(): int { |
|
60 | + return $this->priority; |
|
61 | + } |
|
62 | 62 | } |