diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2016-04-22 15:28:09 +0200 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-04-22 15:28:09 +0200 |
commit | 1d33a5ef133d917074a94d40a939b6cbbe698711 (patch) | |
tree | 6880e3e6eb5b5c5be2a39d078416f92e7b359b91 /lib/private/AppFramework/Utility | |
parent | 606b756a94643eaae87e18b39f6c75e6d18fec7e (diff) | |
download | nextcloud-server-1d33a5ef133d917074a94d40a939b6cbbe698711.tar.gz nextcloud-server-1d33a5ef133d917074a94d40a939b6cbbe698711.zip |
Move \OC\AppFramework to PSR-4
* Also moved the autoloader setup a bit up since we need it in initpaths
Diffstat (limited to 'lib/private/AppFramework/Utility')
3 files changed, 324 insertions, 0 deletions
diff --git a/lib/private/AppFramework/Utility/ControllerMethodReflector.php b/lib/private/AppFramework/Utility/ControllerMethodReflector.php new file mode 100644 index 00000000000..de83749fbaf --- /dev/null +++ b/lib/private/AppFramework/Utility/ControllerMethodReflector.php @@ -0,0 +1,118 @@ +<?php +/** + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Olivier Paroz <github@oparoz.com> + * @author Robin McCorkell <robin@mccorkell.me.uk> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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. + * + * This program 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace OC\AppFramework\Utility; + +use \OCP\AppFramework\Utility\IControllerMethodReflector; + + +/** + * Reads and parses annotations from doc comments + */ +class ControllerMethodReflector implements IControllerMethodReflector{ + + private $annotations; + private $types; + private $parameters; + + public function __construct() { + $this->types = array(); + $this->parameters = array(); + $this->annotations = array(); + } + + + /** + * @param object $object an object or classname + * @param string $method the method which we want to inspect + */ + public function reflect($object, $method){ + $reflection = new \ReflectionMethod($object, $method); + $docs = $reflection->getDocComment(); + + // extract everything prefixed by @ and first letter uppercase + preg_match_all('/@([A-Z]\w+)/', $docs, $matches); + $this->annotations = $matches[1]; + + // extract type parameter information + preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches); + $this->types = array_combine($matches['var'], $matches['type']); + + foreach ($reflection->getParameters() as $param) { + // extract type information from PHP 7 scalar types and prefer them + // over phpdoc annotations + if (method_exists($param, 'getType')) { + $type = $param->getType(); + if ($type !== null) { + $this->types[$param->getName()] = (string) $type; + } + } + + if($param->isOptional()) { + $default = $param->getDefaultValue(); + } else { + $default = null; + } + $this->parameters[$param->name] = $default; + } + } + + + /** + * Inspects the PHPDoc parameters for types + * @param string $parameter the parameter whose type comments should be + * parsed + * @return string|null type in the type parameters (@param int $something) + * would return int or null if not existing + */ + public function getType($parameter) { + if(array_key_exists($parameter, $this->types)) { + return $this->types[$parameter]; + } else { + return null; + } + } + + + /** + * @return array the arguments of the method with key => default value + */ + public function getParameters() { + return $this->parameters; + } + + + /** + * Check if a method contains an annotation + * @param string $name the name of the annotation + * @return bool true if the annotation is found + */ + public function hasAnnotation($name){ + return in_array($name, $this->annotations); + } + + +} diff --git a/lib/private/AppFramework/Utility/SimpleContainer.php b/lib/private/AppFramework/Utility/SimpleContainer.php new file mode 100644 index 00000000000..78ded39735e --- /dev/null +++ b/lib/private/AppFramework/Utility/SimpleContainer.php @@ -0,0 +1,162 @@ +<?php +/** + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Lukas Reschke <lukas@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Robin McCorkell <robin@mccorkell.me.uk> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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. + * + * This program 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\AppFramework\Utility; + +use ReflectionClass; +use ReflectionException; +use Closure; +use Pimple\Container; +use OCP\AppFramework\QueryException; +use OCP\IContainer; + +/** + * Class SimpleContainer + * + * SimpleContainer is a simple implementation of IContainer on basis of Pimple + */ +class SimpleContainer extends Container implements IContainer { + + + /** + * @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 + * @return \stdClass + * @throws QueryException if the class could not be found or instantiated + */ + public 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()); + } + } + + + /** + * @param string $name name of the service to query for + * @return mixed registered service for the given $name + * @throws QueryException if the query could not be resolved + */ + public function query($name) { + $name = $this->sanitizeName($name); + if ($this->offsetExists($name)) { + return $this->offsetGet($name); + } else { + $object = $this->resolve($name); + $this->registerService($name, function () use ($object) { + return $object; + }); + return $object; + } + } + + /** + * @param string $name + * @param mixed $value + */ + public function registerParameter($name, $value) { + $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 + * @param Closure $closure the closure to be called on service creation + * @param bool $shared + */ + public function registerService($name, Closure $closure, $shared = true) { + $name = $this->sanitizeName($name); + if (isset($this[$name])) { + unset($this[$name]); + } + if ($shared) { + $this[$name] = $closure; + } else { + $this[$name] = parent::factory($closure); + } + } + + /** + * 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); + }, false); + } + + /* + * @param string $name + * @return string + */ + protected function sanitizeName($name) { + return ltrim($name, '\\'); + } + +} diff --git a/lib/private/AppFramework/Utility/TimeFactory.php b/lib/private/AppFramework/Utility/TimeFactory.php new file mode 100644 index 00000000000..5241b367069 --- /dev/null +++ b/lib/private/AppFramework/Utility/TimeFactory.php @@ -0,0 +1,44 @@ +<?php +/** + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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. + * + * This program 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace OC\AppFramework\Utility; + +use OCP\AppFramework\Utility\ITimeFactory; + + +/** + * Needed to mock calls to time() + */ +class TimeFactory implements ITimeFactory { + + + /** + * @return int the result of a call to time() + */ + public function getTime() { + return time(); + } + + +} |