2013-08-20 19:21:14 +04:00
|
|
|
<?php
|
2014-12-13 21:28:20 +03:00
|
|
|
/**
|
|
|
|
* ownCloud - App Framework
|
|
|
|
*
|
|
|
|
* @author Bernhard Posselt
|
|
|
|
* @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2013-08-20 19:21:14 +04:00
|
|
|
|
|
|
|
namespace OC\AppFramework\Utility;
|
|
|
|
|
2014-12-13 21:28:20 +03:00
|
|
|
use \OCP\AppFramework\QueryException;
|
|
|
|
|
2013-08-20 19:21:14 +04:00
|
|
|
/**
|
|
|
|
* Class SimpleContainer
|
|
|
|
*
|
|
|
|
* SimpleContainer is a simple implementation of IContainer on basis of \Pimple
|
|
|
|
*/
|
2014-11-03 15:10:03 +03:00
|
|
|
class SimpleContainer extends \Pimple\Container implements \OCP\IContainer {
|
2013-08-20 19:21:14 +04:00
|
|
|
|
2014-12-13 21:28:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ReflectionClass $class the class to instantiate
|
|
|
|
* @return stdClass the created class
|
|
|
|
*/
|
|
|
|
private function buildClass(\ReflectionClass $class) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters[] = $this->query($resolveName);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
* @throws QueryException if the class could not be found or instantiated
|
|
|
|
*/
|
|
|
|
private function resolve($name) {
|
|
|
|
$baseMsg = 'Could not resolve ' . $name . '!';
|
|
|
|
try {
|
|
|
|
$class = new \ReflectionClass($name);
|
|
|
|
if ($class->isInstantiable()) {
|
|
|
|
return $this->buildClass($class);
|
|
|
|
} else {
|
|
|
|
throw new QueryException($baseMsg .
|
|
|
|
' Class can not be instantiated');
|
|
|
|
}
|
|
|
|
} catch(\ReflectionException $e) {
|
|
|
|
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
|
2014-12-13 21:28:20 +03:00
|
|
|
* @throws QueryExcpetion if the query could not be resolved
|
2013-08-20 19:21:14 +04:00
|
|
|
*/
|
|
|
|
public function query($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
|
|
|
|
*/
|
|
|
|
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
|
2014-02-06 19:30:58 +04: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
|
|
|
*/
|
2014-10-22 17:07:16 +04:00
|
|
|
function registerService($name, \Closure $closure, $shared = true) {
|
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
|
|
|
|
|
|
|
|
2013-08-20 19:21:14 +04:00
|
|
|
}
|