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>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* 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;
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
use ArrayAccess;
|
2015-07-17 18:18:37 +03:00
|
|
|
use Closure;
|
|
|
|
use OCP\AppFramework\QueryException;
|
|
|
|
use OCP\IContainer;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Pimple\Container;
|
2020-07-13 12:18:14 +03:00
|
|
|
use Psr\Container\ContainerInterface;
|
2019-11-22 22:52:10 +03:00
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionException;
|
2020-07-13 12:18:14 +03:00
|
|
|
use ReflectionParameter;
|
|
|
|
use function class_exists;
|
2014-12-13 21:28:20 +03:00
|
|
|
|
2013-08-20 19:21:14 +04:00
|
|
|
/**
|
2020-07-13 12:18:14 +03:00
|
|
|
* SimpleContainer is a simple implementation of a container on basis of Pimple
|
2013-08-20 19:21:14 +04:00
|
|
|
*/
|
2020-07-13 12:18:14 +03:00
|
|
|
class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer {
|
|
|
|
|
|
|
|
/** @var Container */
|
|
|
|
private $container;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->container = new Container();
|
|
|
|
}
|
|
|
|
|
2021-03-08 22:06:10 +03:00
|
|
|
public function get(string $id) {
|
2020-07-13 12:18:14 +03:00
|
|
|
return $this->query($id);
|
|
|
|
}
|
2013-08-20 19:21:14 +04:00
|
|
|
|
2021-03-08 22:06:10 +03:00
|
|
|
public function has(string $id): bool {
|
2020-07-13 12:18:14 +03:00
|
|
|
// If a service is no registered but is an existing class, we can probably load it
|
|
|
|
return isset($this->container[$id]) || class_exists($id);
|
|
|
|
}
|
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();
|
2020-07-13 12:18:14 +03:00
|
|
|
}
|
2014-12-13 21:28:20 +03:00
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
return $class->newInstanceArgs(array_map(function (ReflectionParameter $parameter) {
|
2020-11-10 23:05:32 +03:00
|
|
|
$parameterType = $parameter->getType();
|
|
|
|
|
|
|
|
$resolveName = $parameter->getName();
|
2020-07-13 12:18:14 +03:00
|
|
|
|
|
|
|
// try to find out if it is a class or a simple parameter
|
2020-11-10 23:05:32 +03:00
|
|
|
if ($parameterType !== null && !$parameterType->isBuiltin()) {
|
|
|
|
$resolveName = $parameterType->getName();
|
2020-07-13 12:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$builtIn = $parameter->hasType() && $parameter->getType()->isBuiltin();
|
|
|
|
return $this->query($resolveName, !$builtIn);
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
// Service not found, use the default value when available
|
|
|
|
if ($parameter->isDefaultValueAvailable()) {
|
|
|
|
return $parameter->getDefaultValue();
|
2014-12-13 21:28:20 +03:00
|
|
|
}
|
|
|
|
|
2020-11-10 23:05:32 +03:00
|
|
|
if ($parameterType !== null && !$parameterType->isBuiltin()) {
|
2020-07-13 12:18:14 +03:00
|
|
|
$resolveName = $parameter->getName();
|
2021-02-08 21:13:25 +03:00
|
|
|
try {
|
|
|
|
return $this->query($resolveName);
|
|
|
|
} catch (QueryException $e2) {
|
|
|
|
// don't lose the error we got while trying to query by type
|
2021-02-08 21:14:27 +03:00
|
|
|
throw new QueryException($e2->getMessage(), (int) $e2->getCode(), $e);
|
2021-02-08 21:13:25 +03:00
|
|
|
}
|
2016-09-20 14:26:06 +03:00
|
|
|
}
|
2020-07-13 12:18:14 +03:00
|
|
|
|
|
|
|
throw $e;
|
2014-12-13 21:28:20 +03:00
|
|
|
}
|
2020-07-13 12:18:14 +03:00
|
|
|
}, $constructor->getParameters()));
|
2014-12-13 21:28:20 +03:00
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (ReflectionException $e) {
|
2014-12-13 21:28:20 +03:00
|
|
|
throw new QueryException($baseMsg . ' ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 15:20:15 +03:00
|
|
|
public function query(string $name, bool $autoload = true) {
|
2015-07-30 13:29:06 +03:00
|
|
|
$name = $this->sanitizeName($name);
|
2020-07-13 12:18:14 +03:00
|
|
|
if (isset($this->container[$name])) {
|
|
|
|
return $this->container[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($autoload) {
|
2014-12-13 21:28:20 +03:00
|
|
|
$object = $this->resolve($name);
|
|
|
|
$this->registerService($name, function () use ($object) {
|
|
|
|
return $object;
|
|
|
|
});
|
|
|
|
return $object;
|
|
|
|
}
|
2020-07-13 12:18:14 +03:00
|
|
|
|
2019-05-30 15:20:15 +03:00
|
|
|
throw new QueryException('Could not resolve ' . $name . '!');
|
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) {
|
2020-07-13 12:18:14 +03:00
|
|
|
$wrapped = function () use ($closure) {
|
|
|
|
return $closure($this);
|
|
|
|
};
|
2015-07-30 13:29:06 +03:00
|
|
|
$name = $this->sanitizeName($name);
|
2020-04-10 15:19:56 +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) {
|
2020-07-13 12:18:14 +03:00
|
|
|
$this[$name] = $wrapped;
|
2014-11-03 15:10:03 +03:00
|
|
|
} else {
|
2020-07-13 12:18:14 +03:00
|
|
|
$this[$name] = $this->container->factory($wrapped);
|
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) {
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService($alias, function (ContainerInterface $container) use ($target) {
|
|
|
|
return $container->get($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
|
|
|
}
|
2020-07-13 12:18:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has
|
|
|
|
*/
|
|
|
|
public function offsetExists($id) {
|
|
|
|
return $this->container->offsetExists($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
|
|
|
|
*/
|
|
|
|
public function offsetGet($id) {
|
|
|
|
return $this->container->offsetGet($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0 use \OCP\IContainer::registerService
|
|
|
|
*/
|
|
|
|
public function offsetSet($id, $service) {
|
|
|
|
$this->container->offsetSet($id, $service);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset) {
|
|
|
|
$this->container->offsetUnset($offset);
|
|
|
|
}
|
2013-08-20 19:21:14 +04:00
|
|
|
}
|