2013-08-20 19:21:14 +04:00
|
|
|
<?php
|
2014-12-13 21:28:20 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2014-12-13 21:28:20 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2014-12-13 21:28:20 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2014-12-13 21:28:20 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-12-13 21:28:20 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-12-13 21:28:20 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-12-13 21:28:20 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-08-20 19:21:14 +04:00
|
|
|
namespace OC\AppFramework\Utility;
|
|
|
|
|
2015-07-17 18:18:37 +03:00
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionException;
|
|
|
|
use Closure;
|
|
|
|
use Pimple\Container;
|
|
|
|
use OCP\AppFramework\QueryException;
|
|
|
|
use OCP\IContainer;
|
2014-12-13 21:28:20 +03:00
|
|
|
|
2013-08-20 19:21:14 +04:00
|
|
|
/**
|
|
|
|
* Class SimpleContainer
|
|
|
|
*
|
2015-07-17 18:18:37 +03:00
|
|
|
* SimpleContainer is a simple implementation of IContainer on basis of Pimple
|
2013-08-20 19:21:14 +04:00
|
|
|
*/
|
2015-07-17 18:18:37 +03:00
|
|
|
class SimpleContainer extends Container implements IContainer {
|
2013-08-20 19:21:14 +04:00
|
|
|
|
2014-12-13 21:28:20 +03:00
|
|
|
|
|
|
|
/**
|
2015-07-17 18:18:37 +03:00
|
|
|
* @param ReflectionClass $class the class to instantiate
|
2015-09-23 11:13:41 +03:00
|
|
|
* @return \stdClass the created class
|
2018-03-06 16:02:50 +03:00
|
|
|
* @suppress PhanUndeclaredClassInstanceof
|
2014-12-13 21:28:20 +03:00
|
|
|
*/
|
2015-07-17 18:18:37 +03:00
|
|
|
private function buildClass(ReflectionClass $class) {
|
2014-12-13 21:28:20 +03:00
|
|
|
$constructor = $class->getConstructor();
|
|
|
|
if ($constructor === null) {
|
|
|
|
return $class->newInstance();
|
|
|
|
} else {
|
|
|
|
$parameters = [];
|
|
|
|
foreach ($constructor->getParameters() as $parameter) {
|
|
|
|
$parameterClass = $parameter->getClass();
|
|
|
|
|
|
|
|
// try to find out if it is a class or a simple parameter
|
|
|
|
if ($parameterClass === null) {
|
|
|
|
$resolveName = $parameter->getName();
|
|
|
|
} else {
|
|
|
|
$resolveName = $parameterClass->name;
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:26:06 +03:00
|
|
|
try {
|
|
|
|
$parameters[] = $this->query($resolveName);
|
2018-09-21 19:32:15 +03:00
|
|
|
} catch (QueryException $e) {
|
2016-09-20 14:26:06 +03:00
|
|
|
// Service not found, use the default value when available
|
|
|
|
if ($parameter->isDefaultValueAvailable()) {
|
|
|
|
$parameters[] = $parameter->getDefaultValue();
|
2016-10-05 17:32:23 +03:00
|
|
|
} else if ($parameterClass !== null) {
|
|
|
|
$resolveName = $parameter->getName();
|
|
|
|
$parameters[] = $this->query($resolveName);
|
2016-09-20 14:26:06 +03:00
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2014-12-13 21:28:20 +03:00
|
|
|
}
|
|
|
|
return $class->newInstanceArgs($parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If a parameter is not registered in the container try to instantiate it
|
|
|
|
* by using reflection to find out how to build the class
|
|
|
|
* @param string $name the class name to resolve
|
2015-09-23 11:13:41 +03:00
|
|
|
* @return \stdClass
|
2014-12-13 21:28:20 +03:00
|
|
|
* @throws QueryException if the class could not be found or instantiated
|
|
|
|
*/
|
2015-09-13 12:35:21 +03:00
|
|
|
public function resolve($name) {
|
2014-12-13 21:28:20 +03:00
|
|
|
$baseMsg = 'Could not resolve ' . $name . '!';
|
|
|
|
try {
|
2015-07-17 18:18:37 +03:00
|
|
|
$class = new ReflectionClass($name);
|
2014-12-13 21:28:20 +03:00
|
|
|
if ($class->isInstantiable()) {
|
|
|
|
return $this->buildClass($class);
|
|
|
|
} else {
|
|
|
|
throw new QueryException($baseMsg .
|
|
|
|
' Class can not be instantiated');
|
|
|
|
}
|
2015-07-17 18:18:37 +03:00
|
|
|
} catch(ReflectionException $e) {
|
2014-12-13 21:28:20 +03:00
|
|
|
throw new QueryException($baseMsg . ' ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-20 19:21:14 +04:00
|
|
|
/**
|
|
|
|
* @param string $name name of the service to query for
|
2014-10-22 17:07:16 +04:00
|
|
|
* @return mixed registered service for the given $name
|
2015-02-23 14:19:52 +03:00
|
|
|
* @throws QueryException if the query could not be resolved
|
2013-08-20 19:21:14 +04:00
|
|
|
*/
|
|
|
|
public function query($name) {
|
2015-07-30 13:29:06 +03:00
|
|
|
$name = $this->sanitizeName($name);
|
2014-12-13 21:28:20 +03:00
|
|
|
if ($this->offsetExists($name)) {
|
|
|
|
return $this->offsetGet($name);
|
|
|
|
} else {
|
|
|
|
$object = $this->resolve($name);
|
|
|
|
$this->registerService($name, function () use ($object) {
|
|
|
|
return $object;
|
|
|
|
});
|
|
|
|
return $object;
|
|
|
|
}
|
2013-08-20 19:21:14 +04:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:07:16 +04:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2015-04-20 13:52:40 +03:00
|
|
|
public function registerParameter($name, $value) {
|
2013-08-20 19:21:14 +04:00
|
|
|
$this[$name] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The given closure is call the first time the given service is queried.
|
|
|
|
* The closure has to return the instance for the given service.
|
|
|
|
* Created instance will be cached in case $shared is true.
|
|
|
|
*
|
|
|
|
* @param string $name name of the service to register another backend for
|
2015-07-17 18:18:37 +03:00
|
|
|
* @param Closure $closure the closure to be called on service creation
|
2014-10-22 17:07:16 +04:00
|
|
|
* @param bool $shared
|
2013-08-20 19:21:14 +04:00
|
|
|
*/
|
2015-07-17 18:18:37 +03:00
|
|
|
public function registerService($name, Closure $closure, $shared = true) {
|
2015-07-30 13:29:06 +03:00
|
|
|
$name = $this->sanitizeName($name);
|
2014-11-12 00:04:46 +03:00
|
|
|
if (isset($this[$name])) {
|
2014-11-03 15:10:03 +03:00
|
|
|
unset($this[$name]);
|
|
|
|
}
|
2013-08-20 19:21:14 +04:00
|
|
|
if ($shared) {
|
|
|
|
$this[$name] = $closure;
|
2014-11-03 15:10:03 +03:00
|
|
|
} else {
|
|
|
|
$this[$name] = parent::factory($closure);
|
2013-08-20 19:21:14 +04:00
|
|
|
}
|
|
|
|
}
|
2014-12-13 21:28:20 +03:00
|
|
|
|
2015-07-17 18:18:37 +03:00
|
|
|
/**
|
|
|
|
* Shortcut for returning a service from a service under a different key,
|
|
|
|
* e.g. to tell the container to return a class when queried for an
|
|
|
|
* interface
|
|
|
|
* @param string $alias the alias that should be registered
|
|
|
|
* @param string $target the target that should be resolved instead
|
|
|
|
*/
|
|
|
|
public function registerAlias($alias, $target) {
|
|
|
|
$this->registerService($alias, function (IContainer $container) use ($target) {
|
|
|
|
return $container->query($target);
|
2015-07-24 14:43:50 +03:00
|
|
|
}, false);
|
2015-07-17 18:18:37 +03:00
|
|
|
}
|
2014-12-13 21:28:20 +03:00
|
|
|
|
2015-07-30 13:29:06 +03:00
|
|
|
/*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function sanitizeName($name) {
|
2016-09-16 10:52:52 +03:00
|
|
|
if (isset($name[0]) && $name[0] === '\\') {
|
|
|
|
return ltrim($name, '\\');
|
|
|
|
}
|
|
|
|
return $name;
|
2015-07-30 13:29:06 +03:00
|
|
|
}
|
2013-08-20 19:21:14 +04:00
|
|
|
}
|