@@ -47,191 +47,191 @@ |
||
47 | 47 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
48 | 48 | |
49 | 49 | class Application { |
50 | - /** @var IConfig */ |
|
51 | - private $config; |
|
52 | - /** @var EventDispatcherInterface */ |
|
53 | - private $dispatcher; |
|
54 | - /** @var IRequest */ |
|
55 | - private $request; |
|
56 | - /** @var ILogger */ |
|
57 | - private $logger; |
|
58 | - /** @var MemoryInfo */ |
|
59 | - private $memoryInfo; |
|
50 | + /** @var IConfig */ |
|
51 | + private $config; |
|
52 | + /** @var EventDispatcherInterface */ |
|
53 | + private $dispatcher; |
|
54 | + /** @var IRequest */ |
|
55 | + private $request; |
|
56 | + /** @var ILogger */ |
|
57 | + private $logger; |
|
58 | + /** @var MemoryInfo */ |
|
59 | + private $memoryInfo; |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param IConfig $config |
|
63 | - * @param EventDispatcherInterface $dispatcher |
|
64 | - * @param IRequest $request |
|
65 | - * @param ILogger $logger |
|
66 | - * @param MemoryInfo $memoryInfo |
|
67 | - */ |
|
68 | - public function __construct(IConfig $config, |
|
69 | - EventDispatcherInterface $dispatcher, |
|
70 | - IRequest $request, |
|
71 | - ILogger $logger, |
|
72 | - MemoryInfo $memoryInfo) { |
|
73 | - $defaults = \OC::$server->getThemingDefaults(); |
|
74 | - $this->config = $config; |
|
75 | - $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString()); |
|
76 | - $this->dispatcher = $dispatcher; |
|
77 | - $this->request = $request; |
|
78 | - $this->logger = $logger; |
|
79 | - $this->memoryInfo = $memoryInfo; |
|
80 | - } |
|
61 | + /** |
|
62 | + * @param IConfig $config |
|
63 | + * @param EventDispatcherInterface $dispatcher |
|
64 | + * @param IRequest $request |
|
65 | + * @param ILogger $logger |
|
66 | + * @param MemoryInfo $memoryInfo |
|
67 | + */ |
|
68 | + public function __construct(IConfig $config, |
|
69 | + EventDispatcherInterface $dispatcher, |
|
70 | + IRequest $request, |
|
71 | + ILogger $logger, |
|
72 | + MemoryInfo $memoryInfo) { |
|
73 | + $defaults = \OC::$server->getThemingDefaults(); |
|
74 | + $this->config = $config; |
|
75 | + $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString()); |
|
76 | + $this->dispatcher = $dispatcher; |
|
77 | + $this->request = $request; |
|
78 | + $this->logger = $logger; |
|
79 | + $this->memoryInfo = $memoryInfo; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @param InputInterface $input |
|
84 | - * @param ConsoleOutputInterface $output |
|
85 | - * @throws \Exception |
|
86 | - */ |
|
87 | - public function loadCommands( |
|
88 | - InputInterface $input, |
|
89 | - ConsoleOutputInterface $output |
|
90 | - ) { |
|
91 | - // $application is required to be defined in the register_command scripts |
|
92 | - $application = $this->application; |
|
93 | - $inputDefinition = $application->getDefinition(); |
|
94 | - $inputDefinition->addOption( |
|
95 | - new InputOption( |
|
96 | - 'no-warnings', |
|
97 | - null, |
|
98 | - InputOption::VALUE_NONE, |
|
99 | - 'Skip global warnings, show command output only', |
|
100 | - null |
|
101 | - ) |
|
102 | - ); |
|
103 | - try { |
|
104 | - $input->bind($inputDefinition); |
|
105 | - } catch (\RuntimeException $e) { |
|
106 | - //expected if there are extra options |
|
107 | - } |
|
108 | - if ($input->getOption('no-warnings')) { |
|
109 | - $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); |
|
110 | - } |
|
82 | + /** |
|
83 | + * @param InputInterface $input |
|
84 | + * @param ConsoleOutputInterface $output |
|
85 | + * @throws \Exception |
|
86 | + */ |
|
87 | + public function loadCommands( |
|
88 | + InputInterface $input, |
|
89 | + ConsoleOutputInterface $output |
|
90 | + ) { |
|
91 | + // $application is required to be defined in the register_command scripts |
|
92 | + $application = $this->application; |
|
93 | + $inputDefinition = $application->getDefinition(); |
|
94 | + $inputDefinition->addOption( |
|
95 | + new InputOption( |
|
96 | + 'no-warnings', |
|
97 | + null, |
|
98 | + InputOption::VALUE_NONE, |
|
99 | + 'Skip global warnings, show command output only', |
|
100 | + null |
|
101 | + ) |
|
102 | + ); |
|
103 | + try { |
|
104 | + $input->bind($inputDefinition); |
|
105 | + } catch (\RuntimeException $e) { |
|
106 | + //expected if there are extra options |
|
107 | + } |
|
108 | + if ($input->getOption('no-warnings')) { |
|
109 | + $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); |
|
110 | + } |
|
111 | 111 | |
112 | - if ($this->memoryInfo->isMemoryLimitSufficient() === false) { |
|
113 | - $output->getErrorOutput()->writeln( |
|
114 | - '<comment>The current PHP memory limit ' . |
|
115 | - 'is below the recommended value of 512MB.</comment>' |
|
116 | - ); |
|
117 | - } |
|
112 | + if ($this->memoryInfo->isMemoryLimitSufficient() === false) { |
|
113 | + $output->getErrorOutput()->writeln( |
|
114 | + '<comment>The current PHP memory limit ' . |
|
115 | + 'is below the recommended value of 512MB.</comment>' |
|
116 | + ); |
|
117 | + } |
|
118 | 118 | |
119 | - try { |
|
120 | - require_once __DIR__ . '/../../../core/register_command.php'; |
|
121 | - if ($this->config->getSystemValue('installed', false)) { |
|
122 | - if (\OCP\Util::needUpgrade()) { |
|
123 | - throw new NeedsUpdateException(); |
|
124 | - } elseif ($this->config->getSystemValueBool('maintenance')) { |
|
125 | - $this->writeMaintenanceModeInfo($input, $output); |
|
126 | - } else { |
|
127 | - OC_App::loadApps(); |
|
128 | - foreach (\OC::$server->getAppManager()->getInstalledApps() as $app) { |
|
129 | - $appPath = \OC_App::getAppPath($app); |
|
130 | - if ($appPath === false) { |
|
131 | - continue; |
|
132 | - } |
|
133 | - // load commands using info.xml |
|
134 | - $info = \OC_App::getAppInfo($app); |
|
135 | - if (isset($info['commands'])) { |
|
136 | - $this->loadCommandsFromInfoXml($info['commands']); |
|
137 | - } |
|
138 | - // load from register_command.php |
|
139 | - \OC_App::registerAutoloading($app, $appPath); |
|
140 | - $file = $appPath . '/appinfo/register_command.php'; |
|
141 | - if (file_exists($file)) { |
|
142 | - try { |
|
143 | - require $file; |
|
144 | - } catch (\Exception $e) { |
|
145 | - $this->logger->logException($e); |
|
146 | - } |
|
147 | - } |
|
148 | - } |
|
149 | - } |
|
150 | - } elseif ($input->getArgument('command') !== '_completion' && $input->getArgument('command') !== 'maintenance:install') { |
|
151 | - $output->writeln("Nextcloud is not installed - only a limited number of commands are available"); |
|
152 | - } |
|
153 | - } catch (NeedsUpdateException $e) { |
|
154 | - if ($input->getArgument('command') !== '_completion') { |
|
155 | - $output->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available"); |
|
156 | - $output->writeln("You may use your browser or the occ upgrade command to do the upgrade"); |
|
157 | - } |
|
158 | - } |
|
119 | + try { |
|
120 | + require_once __DIR__ . '/../../../core/register_command.php'; |
|
121 | + if ($this->config->getSystemValue('installed', false)) { |
|
122 | + if (\OCP\Util::needUpgrade()) { |
|
123 | + throw new NeedsUpdateException(); |
|
124 | + } elseif ($this->config->getSystemValueBool('maintenance')) { |
|
125 | + $this->writeMaintenanceModeInfo($input, $output); |
|
126 | + } else { |
|
127 | + OC_App::loadApps(); |
|
128 | + foreach (\OC::$server->getAppManager()->getInstalledApps() as $app) { |
|
129 | + $appPath = \OC_App::getAppPath($app); |
|
130 | + if ($appPath === false) { |
|
131 | + continue; |
|
132 | + } |
|
133 | + // load commands using info.xml |
|
134 | + $info = \OC_App::getAppInfo($app); |
|
135 | + if (isset($info['commands'])) { |
|
136 | + $this->loadCommandsFromInfoXml($info['commands']); |
|
137 | + } |
|
138 | + // load from register_command.php |
|
139 | + \OC_App::registerAutoloading($app, $appPath); |
|
140 | + $file = $appPath . '/appinfo/register_command.php'; |
|
141 | + if (file_exists($file)) { |
|
142 | + try { |
|
143 | + require $file; |
|
144 | + } catch (\Exception $e) { |
|
145 | + $this->logger->logException($e); |
|
146 | + } |
|
147 | + } |
|
148 | + } |
|
149 | + } |
|
150 | + } elseif ($input->getArgument('command') !== '_completion' && $input->getArgument('command') !== 'maintenance:install') { |
|
151 | + $output->writeln("Nextcloud is not installed - only a limited number of commands are available"); |
|
152 | + } |
|
153 | + } catch (NeedsUpdateException $e) { |
|
154 | + if ($input->getArgument('command') !== '_completion') { |
|
155 | + $output->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available"); |
|
156 | + $output->writeln("You may use your browser or the occ upgrade command to do the upgrade"); |
|
157 | + } |
|
158 | + } |
|
159 | 159 | |
160 | - if ($input->getFirstArgument() !== 'check') { |
|
161 | - $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig()); |
|
162 | - if (!empty($errors)) { |
|
163 | - foreach ($errors as $error) { |
|
164 | - $output->writeln((string)$error['error']); |
|
165 | - $output->writeln((string)$error['hint']); |
|
166 | - $output->writeln(''); |
|
167 | - } |
|
168 | - throw new \Exception("Environment not properly prepared."); |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
160 | + if ($input->getFirstArgument() !== 'check') { |
|
161 | + $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig()); |
|
162 | + if (!empty($errors)) { |
|
163 | + foreach ($errors as $error) { |
|
164 | + $output->writeln((string)$error['error']); |
|
165 | + $output->writeln((string)$error['hint']); |
|
166 | + $output->writeln(''); |
|
167 | + } |
|
168 | + throw new \Exception("Environment not properly prepared."); |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | 172 | |
173 | - /** |
|
174 | - * Write a maintenance mode info. |
|
175 | - * The commands "_completion" and "maintenance:mode" are excluded. |
|
176 | - * |
|
177 | - * @param InputInterface $input The input implementation for reading inputs. |
|
178 | - * @param ConsoleOutputInterface $output The output implementation |
|
179 | - * for writing outputs. |
|
180 | - * @return void |
|
181 | - */ |
|
182 | - private function writeMaintenanceModeInfo( |
|
183 | - InputInterface $input, ConsoleOutputInterface $output |
|
184 | - ) { |
|
185 | - if ($input->getArgument('command') !== '_completion' |
|
186 | - && $input->getArgument('command') !== 'maintenance:mode') { |
|
187 | - $errOutput = $output->getErrorOutput(); |
|
188 | - $errOutput->writeln( |
|
189 | - '<comment>Nextcloud is in maintenance mode - ' . |
|
190 | - 'no apps have been loaded</comment>' . PHP_EOL |
|
191 | - ); |
|
192 | - } |
|
193 | - } |
|
173 | + /** |
|
174 | + * Write a maintenance mode info. |
|
175 | + * The commands "_completion" and "maintenance:mode" are excluded. |
|
176 | + * |
|
177 | + * @param InputInterface $input The input implementation for reading inputs. |
|
178 | + * @param ConsoleOutputInterface $output The output implementation |
|
179 | + * for writing outputs. |
|
180 | + * @return void |
|
181 | + */ |
|
182 | + private function writeMaintenanceModeInfo( |
|
183 | + InputInterface $input, ConsoleOutputInterface $output |
|
184 | + ) { |
|
185 | + if ($input->getArgument('command') !== '_completion' |
|
186 | + && $input->getArgument('command') !== 'maintenance:mode') { |
|
187 | + $errOutput = $output->getErrorOutput(); |
|
188 | + $errOutput->writeln( |
|
189 | + '<comment>Nextcloud is in maintenance mode - ' . |
|
190 | + 'no apps have been loaded</comment>' . PHP_EOL |
|
191 | + ); |
|
192 | + } |
|
193 | + } |
|
194 | 194 | |
195 | - /** |
|
196 | - * Sets whether to automatically exit after a command execution or not. |
|
197 | - * |
|
198 | - * @param bool $boolean Whether to automatically exit after a command execution or not |
|
199 | - */ |
|
200 | - public function setAutoExit($boolean) { |
|
201 | - $this->application->setAutoExit($boolean); |
|
202 | - } |
|
195 | + /** |
|
196 | + * Sets whether to automatically exit after a command execution or not. |
|
197 | + * |
|
198 | + * @param bool $boolean Whether to automatically exit after a command execution or not |
|
199 | + */ |
|
200 | + public function setAutoExit($boolean) { |
|
201 | + $this->application->setAutoExit($boolean); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * @param InputInterface $input |
|
206 | - * @param OutputInterface $output |
|
207 | - * @return int |
|
208 | - * @throws \Exception |
|
209 | - */ |
|
210 | - public function run(InputInterface $input = null, OutputInterface $output = null) { |
|
211 | - $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent( |
|
212 | - ConsoleEvent::EVENT_RUN, |
|
213 | - $this->request->server['argv'] |
|
214 | - )); |
|
215 | - return $this->application->run($input, $output); |
|
216 | - } |
|
204 | + /** |
|
205 | + * @param InputInterface $input |
|
206 | + * @param OutputInterface $output |
|
207 | + * @return int |
|
208 | + * @throws \Exception |
|
209 | + */ |
|
210 | + public function run(InputInterface $input = null, OutputInterface $output = null) { |
|
211 | + $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent( |
|
212 | + ConsoleEvent::EVENT_RUN, |
|
213 | + $this->request->server['argv'] |
|
214 | + )); |
|
215 | + return $this->application->run($input, $output); |
|
216 | + } |
|
217 | 217 | |
218 | - private function loadCommandsFromInfoXml($commands) { |
|
219 | - foreach ($commands as $command) { |
|
220 | - try { |
|
221 | - $c = \OC::$server->query($command); |
|
222 | - } catch (QueryException $e) { |
|
223 | - if (class_exists($command)) { |
|
224 | - try { |
|
225 | - $c = new $command(); |
|
226 | - } catch (\ArgumentCountError $e2) { |
|
227 | - throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e); |
|
228 | - } |
|
229 | - } else { |
|
230 | - throw new \Exception("Console command '$command' is unknown and could not be loaded"); |
|
231 | - } |
|
232 | - } |
|
218 | + private function loadCommandsFromInfoXml($commands) { |
|
219 | + foreach ($commands as $command) { |
|
220 | + try { |
|
221 | + $c = \OC::$server->query($command); |
|
222 | + } catch (QueryException $e) { |
|
223 | + if (class_exists($command)) { |
|
224 | + try { |
|
225 | + $c = new $command(); |
|
226 | + } catch (\ArgumentCountError $e2) { |
|
227 | + throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e); |
|
228 | + } |
|
229 | + } else { |
|
230 | + throw new \Exception("Console command '$command' is unknown and could not be loaded"); |
|
231 | + } |
|
232 | + } |
|
233 | 233 | |
234 | - $this->application->add($c); |
|
235 | - } |
|
236 | - } |
|
234 | + $this->application->add($c); |
|
235 | + } |
|
236 | + } |
|
237 | 237 | } |
@@ -111,13 +111,13 @@ discard block |
||
111 | 111 | |
112 | 112 | if ($this->memoryInfo->isMemoryLimitSufficient() === false) { |
113 | 113 | $output->getErrorOutput()->writeln( |
114 | - '<comment>The current PHP memory limit ' . |
|
114 | + '<comment>The current PHP memory limit '. |
|
115 | 115 | 'is below the recommended value of 512MB.</comment>' |
116 | 116 | ); |
117 | 117 | } |
118 | 118 | |
119 | 119 | try { |
120 | - require_once __DIR__ . '/../../../core/register_command.php'; |
|
120 | + require_once __DIR__.'/../../../core/register_command.php'; |
|
121 | 121 | if ($this->config->getSystemValue('installed', false)) { |
122 | 122 | if (\OCP\Util::needUpgrade()) { |
123 | 123 | throw new NeedsUpdateException(); |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | } |
138 | 138 | // load from register_command.php |
139 | 139 | \OC_App::registerAutoloading($app, $appPath); |
140 | - $file = $appPath . '/appinfo/register_command.php'; |
|
140 | + $file = $appPath.'/appinfo/register_command.php'; |
|
141 | 141 | if (file_exists($file)) { |
142 | 142 | try { |
143 | 143 | require $file; |
@@ -161,8 +161,8 @@ discard block |
||
161 | 161 | $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig()); |
162 | 162 | if (!empty($errors)) { |
163 | 163 | foreach ($errors as $error) { |
164 | - $output->writeln((string)$error['error']); |
|
165 | - $output->writeln((string)$error['hint']); |
|
164 | + $output->writeln((string) $error['error']); |
|
165 | + $output->writeln((string) $error['hint']); |
|
166 | 166 | $output->writeln(''); |
167 | 167 | } |
168 | 168 | throw new \Exception("Environment not properly prepared."); |
@@ -186,8 +186,8 @@ discard block |
||
186 | 186 | && $input->getArgument('command') !== 'maintenance:mode') { |
187 | 187 | $errOutput = $output->getErrorOutput(); |
188 | 188 | $errOutput->writeln( |
189 | - '<comment>Nextcloud is in maintenance mode - ' . |
|
190 | - 'no apps have been loaded</comment>' . PHP_EOL |
|
189 | + '<comment>Nextcloud is in maintenance mode - '. |
|
190 | + 'no apps have been loaded</comment>'.PHP_EOL |
|
191 | 191 | ); |
192 | 192 | } |
193 | 193 | } |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | try { |
225 | 225 | $c = new $command(); |
226 | 226 | } catch (\ArgumentCountError $e2) { |
227 | - throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e); |
|
227 | + throw new \Exception("Failed to construct console command '$command': ".$e->getMessage(), 0, $e); |
|
228 | 228 | } |
229 | 229 | } else { |
230 | 230 | throw new \Exception("Console command '$command' is unknown and could not be loaded"); |
@@ -45,180 +45,180 @@ |
||
45 | 45 | */ |
46 | 46 | class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer { |
47 | 47 | |
48 | - /** @var Container */ |
|
49 | - private $container; |
|
50 | - |
|
51 | - public function __construct() { |
|
52 | - $this->container = new Container(); |
|
53 | - } |
|
54 | - |
|
55 | - public function get($id) { |
|
56 | - return $this->query($id); |
|
57 | - } |
|
58 | - |
|
59 | - public function has($id): bool { |
|
60 | - // If a service is no registered but is an existing class, we can probably load it |
|
61 | - return isset($this->container[$id]) || class_exists($id); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * @param ReflectionClass $class the class to instantiate |
|
66 | - * @return \stdClass the created class |
|
67 | - * @suppress PhanUndeclaredClassInstanceof |
|
68 | - */ |
|
69 | - private function buildClass(ReflectionClass $class) { |
|
70 | - $constructor = $class->getConstructor(); |
|
71 | - if ($constructor === null) { |
|
72 | - return $class->newInstance(); |
|
73 | - } |
|
74 | - |
|
75 | - return $class->newInstanceArgs(array_map(function (ReflectionParameter $parameter) { |
|
76 | - $parameterType = $parameter->getType(); |
|
77 | - |
|
78 | - $resolveName = $parameter->getName(); |
|
79 | - |
|
80 | - // try to find out if it is a class or a simple parameter |
|
81 | - if ($parameterType !== null && !$parameterType->isBuiltin()) { |
|
82 | - $resolveName = $parameterType->getName(); |
|
83 | - } |
|
84 | - |
|
85 | - try { |
|
86 | - $builtIn = $parameter->hasType() && $parameter->getType()->isBuiltin(); |
|
87 | - return $this->query($resolveName, !$builtIn); |
|
88 | - } catch (QueryException $e) { |
|
89 | - // Service not found, use the default value when available |
|
90 | - if ($parameter->isDefaultValueAvailable()) { |
|
91 | - return $parameter->getDefaultValue(); |
|
92 | - } |
|
93 | - |
|
94 | - if ($parameterType !== null && !$parameterType->isBuiltin()) { |
|
95 | - $resolveName = $parameter->getName(); |
|
96 | - try { |
|
97 | - return $this->query($resolveName); |
|
98 | - } catch (QueryException $e2) { |
|
99 | - // don't lose the error we got while trying to query by type |
|
100 | - throw new QueryException($e2->getMessage(), (int) $e2->getCode(), $e); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - throw $e; |
|
105 | - } |
|
106 | - }, $constructor->getParameters())); |
|
107 | - } |
|
108 | - |
|
109 | - public function resolve($name) { |
|
110 | - $baseMsg = 'Could not resolve ' . $name . '!'; |
|
111 | - try { |
|
112 | - $class = new ReflectionClass($name); |
|
113 | - if ($class->isInstantiable()) { |
|
114 | - return $this->buildClass($class); |
|
115 | - } else { |
|
116 | - throw new QueryException($baseMsg . |
|
117 | - ' Class can not be instantiated'); |
|
118 | - } |
|
119 | - } catch (ReflectionException $e) { |
|
120 | - throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - public function query(string $name, bool $autoload = true) { |
|
125 | - $name = $this->sanitizeName($name); |
|
126 | - if (isset($this->container[$name])) { |
|
127 | - return $this->container[$name]; |
|
128 | - } |
|
129 | - |
|
130 | - if ($autoload) { |
|
131 | - $object = $this->resolve($name); |
|
132 | - $this->registerService($name, function () use ($object) { |
|
133 | - return $object; |
|
134 | - }); |
|
135 | - return $object; |
|
136 | - } |
|
137 | - |
|
138 | - throw new QueryException('Could not resolve ' . $name . '!'); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param string $name |
|
143 | - * @param mixed $value |
|
144 | - */ |
|
145 | - public function registerParameter($name, $value) { |
|
146 | - $this[$name] = $value; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * The given closure is call the first time the given service is queried. |
|
151 | - * The closure has to return the instance for the given service. |
|
152 | - * Created instance will be cached in case $shared is true. |
|
153 | - * |
|
154 | - * @param string $name name of the service to register another backend for |
|
155 | - * @param Closure $closure the closure to be called on service creation |
|
156 | - * @param bool $shared |
|
157 | - */ |
|
158 | - public function registerService($name, Closure $closure, $shared = true) { |
|
159 | - $wrapped = function () use ($closure) { |
|
160 | - return $closure($this); |
|
161 | - }; |
|
162 | - $name = $this->sanitizeName($name); |
|
163 | - if (isset($this[$name])) { |
|
164 | - unset($this[$name]); |
|
165 | - } |
|
166 | - if ($shared) { |
|
167 | - $this[$name] = $wrapped; |
|
168 | - } else { |
|
169 | - $this[$name] = $this->container->factory($wrapped); |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Shortcut for returning a service from a service under a different key, |
|
175 | - * e.g. to tell the container to return a class when queried for an |
|
176 | - * interface |
|
177 | - * @param string $alias the alias that should be registered |
|
178 | - * @param string $target the target that should be resolved instead |
|
179 | - */ |
|
180 | - public function registerAlias($alias, $target) { |
|
181 | - $this->registerService($alias, function (ContainerInterface $container) use ($target) { |
|
182 | - return $container->get($target); |
|
183 | - }, false); |
|
184 | - } |
|
185 | - |
|
186 | - /* |
|
48 | + /** @var Container */ |
|
49 | + private $container; |
|
50 | + |
|
51 | + public function __construct() { |
|
52 | + $this->container = new Container(); |
|
53 | + } |
|
54 | + |
|
55 | + public function get($id) { |
|
56 | + return $this->query($id); |
|
57 | + } |
|
58 | + |
|
59 | + public function has($id): bool { |
|
60 | + // If a service is no registered but is an existing class, we can probably load it |
|
61 | + return isset($this->container[$id]) || class_exists($id); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * @param ReflectionClass $class the class to instantiate |
|
66 | + * @return \stdClass the created class |
|
67 | + * @suppress PhanUndeclaredClassInstanceof |
|
68 | + */ |
|
69 | + private function buildClass(ReflectionClass $class) { |
|
70 | + $constructor = $class->getConstructor(); |
|
71 | + if ($constructor === null) { |
|
72 | + return $class->newInstance(); |
|
73 | + } |
|
74 | + |
|
75 | + return $class->newInstanceArgs(array_map(function (ReflectionParameter $parameter) { |
|
76 | + $parameterType = $parameter->getType(); |
|
77 | + |
|
78 | + $resolveName = $parameter->getName(); |
|
79 | + |
|
80 | + // try to find out if it is a class or a simple parameter |
|
81 | + if ($parameterType !== null && !$parameterType->isBuiltin()) { |
|
82 | + $resolveName = $parameterType->getName(); |
|
83 | + } |
|
84 | + |
|
85 | + try { |
|
86 | + $builtIn = $parameter->hasType() && $parameter->getType()->isBuiltin(); |
|
87 | + return $this->query($resolveName, !$builtIn); |
|
88 | + } catch (QueryException $e) { |
|
89 | + // Service not found, use the default value when available |
|
90 | + if ($parameter->isDefaultValueAvailable()) { |
|
91 | + return $parameter->getDefaultValue(); |
|
92 | + } |
|
93 | + |
|
94 | + if ($parameterType !== null && !$parameterType->isBuiltin()) { |
|
95 | + $resolveName = $parameter->getName(); |
|
96 | + try { |
|
97 | + return $this->query($resolveName); |
|
98 | + } catch (QueryException $e2) { |
|
99 | + // don't lose the error we got while trying to query by type |
|
100 | + throw new QueryException($e2->getMessage(), (int) $e2->getCode(), $e); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + throw $e; |
|
105 | + } |
|
106 | + }, $constructor->getParameters())); |
|
107 | + } |
|
108 | + |
|
109 | + public function resolve($name) { |
|
110 | + $baseMsg = 'Could not resolve ' . $name . '!'; |
|
111 | + try { |
|
112 | + $class = new ReflectionClass($name); |
|
113 | + if ($class->isInstantiable()) { |
|
114 | + return $this->buildClass($class); |
|
115 | + } else { |
|
116 | + throw new QueryException($baseMsg . |
|
117 | + ' Class can not be instantiated'); |
|
118 | + } |
|
119 | + } catch (ReflectionException $e) { |
|
120 | + throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + public function query(string $name, bool $autoload = true) { |
|
125 | + $name = $this->sanitizeName($name); |
|
126 | + if (isset($this->container[$name])) { |
|
127 | + return $this->container[$name]; |
|
128 | + } |
|
129 | + |
|
130 | + if ($autoload) { |
|
131 | + $object = $this->resolve($name); |
|
132 | + $this->registerService($name, function () use ($object) { |
|
133 | + return $object; |
|
134 | + }); |
|
135 | + return $object; |
|
136 | + } |
|
137 | + |
|
138 | + throw new QueryException('Could not resolve ' . $name . '!'); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param string $name |
|
143 | + * @param mixed $value |
|
144 | + */ |
|
145 | + public function registerParameter($name, $value) { |
|
146 | + $this[$name] = $value; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * The given closure is call the first time the given service is queried. |
|
151 | + * The closure has to return the instance for the given service. |
|
152 | + * Created instance will be cached in case $shared is true. |
|
153 | + * |
|
154 | + * @param string $name name of the service to register another backend for |
|
155 | + * @param Closure $closure the closure to be called on service creation |
|
156 | + * @param bool $shared |
|
157 | + */ |
|
158 | + public function registerService($name, Closure $closure, $shared = true) { |
|
159 | + $wrapped = function () use ($closure) { |
|
160 | + return $closure($this); |
|
161 | + }; |
|
162 | + $name = $this->sanitizeName($name); |
|
163 | + if (isset($this[$name])) { |
|
164 | + unset($this[$name]); |
|
165 | + } |
|
166 | + if ($shared) { |
|
167 | + $this[$name] = $wrapped; |
|
168 | + } else { |
|
169 | + $this[$name] = $this->container->factory($wrapped); |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Shortcut for returning a service from a service under a different key, |
|
175 | + * e.g. to tell the container to return a class when queried for an |
|
176 | + * interface |
|
177 | + * @param string $alias the alias that should be registered |
|
178 | + * @param string $target the target that should be resolved instead |
|
179 | + */ |
|
180 | + public function registerAlias($alias, $target) { |
|
181 | + $this->registerService($alias, function (ContainerInterface $container) use ($target) { |
|
182 | + return $container->get($target); |
|
183 | + }, false); |
|
184 | + } |
|
185 | + |
|
186 | + /* |
|
187 | 187 | * @param string $name |
188 | 188 | * @return string |
189 | 189 | */ |
190 | - protected function sanitizeName($name) { |
|
191 | - if (isset($name[0]) && $name[0] === '\\') { |
|
192 | - return ltrim($name, '\\'); |
|
193 | - } |
|
194 | - return $name; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has |
|
199 | - */ |
|
200 | - public function offsetExists($id) { |
|
201 | - return $this->container->offsetExists($id); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get |
|
206 | - */ |
|
207 | - public function offsetGet($id) { |
|
208 | - return $this->container->offsetGet($id); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @deprecated 20.0.0 use \OCP\IContainer::registerService |
|
213 | - */ |
|
214 | - public function offsetSet($id, $service) { |
|
215 | - $this->container->offsetSet($id, $service); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * @deprecated 20.0.0 |
|
220 | - */ |
|
221 | - public function offsetUnset($offset) { |
|
222 | - $this->container->offsetUnset($offset); |
|
223 | - } |
|
190 | + protected function sanitizeName($name) { |
|
191 | + if (isset($name[0]) && $name[0] === '\\') { |
|
192 | + return ltrim($name, '\\'); |
|
193 | + } |
|
194 | + return $name; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has |
|
199 | + */ |
|
200 | + public function offsetExists($id) { |
|
201 | + return $this->container->offsetExists($id); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get |
|
206 | + */ |
|
207 | + public function offsetGet($id) { |
|
208 | + return $this->container->offsetGet($id); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @deprecated 20.0.0 use \OCP\IContainer::registerService |
|
213 | + */ |
|
214 | + public function offsetSet($id, $service) { |
|
215 | + $this->container->offsetSet($id, $service); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * @deprecated 20.0.0 |
|
220 | + */ |
|
221 | + public function offsetUnset($offset) { |
|
222 | + $this->container->offsetUnset($offset); |
|
223 | + } |
|
224 | 224 | } |