From a648b240229e33a9157bc679e483758ea03f4fd4 Mon Sep 17 00:00:00 2001 From: MacFJA Date: Tue, 11 Aug 2015 22:29:35 +0200 Subject: [PATCH 1/3] Add comments (PHPDoc + inline comments) --- src/watoki/factory/Factory.php | 58 ++++++++++++-- src/watoki/factory/Injector.php | 80 ++++++++++++++++--- src/watoki/factory/Provider.php | 14 ++++ .../factory/exception/InjectionException.php | 8 +- .../factory/providers/CallbackProvider.php | 15 ++++ .../factory/providers/DefaultProvider.php | 48 +++++++++-- .../factory/providers/MinimalProvider.php | 22 ++++- .../factory/providers/SingletonProvider.php | 13 ++- 8 files changed, 228 insertions(+), 30 deletions(-) diff --git a/src/watoki/factory/Factory.php b/src/watoki/factory/Factory.php index 08711a0..d31be14 100755 --- a/src/watoki/factory/Factory.php +++ b/src/watoki/factory/Factory.php @@ -4,14 +4,24 @@ use watoki\factory\providers\DefaultProvider; use watoki\factory\providers\SingletonProvider; +/** + * Class Factory. + * The factory that build all of your object + * + * @package watoki\factory + */ class Factory { - - static $CLASS = __CLASS__; + /** @var string The Factory class name */ + public static $CLASS = __CLASS__; /** @var array|Provider[] */ private $providers = array(); - function __construct() { + /** + * Initialize the class. + * (Define the default provider, and self register as singleton) + */ + public function __construct() { $this->setSingleton($this); $this->setProvider('stdClass', new DefaultProvider($this)); } @@ -21,9 +31,11 @@ function __construct() { * * If the class was registed as singleton, the previous instance is returned regardless of the arguments. * - * @param $class - * @param array $args Constructor arguments that cannot be provided by the factory (indexed by parameter name) + * @param string $class The name (or alias) of the class to get + * @param array $args Constructor arguments that cannot be provided by the factory (indexed by parameter name) + * * @return mixed An instance of the given class + * * @throws \Exception If the class or an injected class cannot be constructed */ public function getInstance($class, $args = array()) { @@ -31,8 +43,11 @@ public function getInstance($class, $args = array()) { } /** - * @param object $instance - * @param string|null $class If omitted, the class of the instance is used + * Define a singleton + * + * @param object $instance The singleton instance + * @param string|null $class If omitted, the class of the instance is used + * * @return object The $instance */ public function setSingleton($instance, $class = null) { @@ -42,13 +57,30 @@ public function setSingleton($instance, $class = null) { return $instance; } + /** + * Define a provider of a class name + * + * @param string $class The name (or alias) of the class + * @param Provider $provider The class provider to use for this class name + */ public function setProvider($class, Provider $provider) { $this->providers[$this->normalizeClass($class)] = $provider; } + /** + * Find the provider to use to get a instance for the class $class + * + * @param string $class The name (or alias) of the class + * + * @return Provider The provider to use + */ private function findMatchingProvider($class) { $reflection = new \ReflectionClass($class); + /* + * First check if a provider is defined for a concrete class + * (check the class, and all parents class) + */ while ($reflection) { $normalized = $this->normalizeClass($reflection->getName()); if (array_key_exists($normalized, $this->providers)) { @@ -58,6 +90,9 @@ private function findMatchingProvider($class) { $reflection = $reflection->getParentClass(); } + /* + * Check if one of the class interface have a provider defined + */ $reflection = new \ReflectionClass($class); foreach ($reflection->getInterfaces() as $interface) { $normalized = $this->normalizeClass($interface->getName()); @@ -66,9 +101,18 @@ private function findMatchingProvider($class) { } } + /* + * Finally, fallback to the default provider (the provider defined for StdClass) + */ return $this->providers['stdclass']; } + /** + * Transform a class name into a string that can be used as a key (identifier) + * + * @param string $class The class name to transform + * @return string The "ready to use as key" class name + */ private function normalizeClass($class) { return trim(strtolower($class), '\\'); } diff --git a/src/watoki/factory/Injector.php b/src/watoki/factory/Injector.php index 9a0ea6a..320ffa4 100644 --- a/src/watoki/factory/Injector.php +++ b/src/watoki/factory/Injector.php @@ -5,6 +5,12 @@ use watoki\reflect\MethodAnalyzer; use watoki\reflect\ClassResolver; +/** + * Class Injector + * Inject objects, params into an instance + * + * @package watoki\factory + */ class Injector { /** @var bool */ @@ -19,15 +25,24 @@ public function __construct(Factory $factory, callable $internalInjector = null) }; } + /** + * Indicate if an exception must be thrown if an error occur when injecting + * + * @param bool $throw Indicate if an exception must be thrown + */ public function setThrowWhenCantInjectProperty($throw) { $this->throwException = $throw; } /** - * @param string $class - * @param array $args - * @param callable $parameterFilter + * Instantiate a new object by its constructor + * + * @param string $class The class name to instantiate + * @param array $args List of arguments to use + * @param callable $parameterFilter Callback function for filtering parameters to inject + * * @return object An instance of $class + * * @throws InjectionException */ public function injectConstructor($class, $args, $parameterFilter) { @@ -51,11 +66,15 @@ public function injectConstructor($class, $args, $parameterFilter) { } /** - * @param object $object Object to call method on - * @param string $method Name of the method - * @param array $args + * Inject object/params in an object method + * + * @param object $object Object to call method on + * @param string $method Name of the method + * @param array $args List of params to use * @param null|callable $parameterFilter If omitted, all missing arguments are injected + * * @return mixed The return value of the method + * * @throws InjectionException */ public function injectMethod($object, $method, $args = array(), $parameterFilter = null) { @@ -70,10 +89,14 @@ public function injectMethod($object, $method, $args = array(), $parameterFilter } /** - * @param \ReflectionMethod $method - * @param array $args - * @param callable $parameterFilter + * Create the list of all arguments to inject in a method + * + * @param \ReflectionMethod $method The reflection method + * @param array $args List of params to inject + * @param callable $parameterFilter Callback function for filtering parameters to inject + * * @return array Of the injected arguments + * * @throws InjectionException */ public function injectMethodArguments(\ReflectionMethod $method, array $args, $parameterFilter) { @@ -87,9 +110,12 @@ public function injectMethodArguments(\ReflectionMethod $method, array $args, $p } /** - * @param object $object The object that the properties are injected into - * @param callable $filter Function to determine if the passed property annotation should be included + * Inject value into object properties by reading "@property" (magic properties) annotation (include inherited). + * + * @param object $object The object that the properties are injected into + * @param callable $filter Function to determine if the passed property annotation should be included * @param \ReflectionClass $context The class to read the property annotations from (if not class of object) + * * @throws InjectionException */ public function injectPropertyAnnotations($object, $filter, \ReflectionClass $context = null) { @@ -99,9 +125,11 @@ public function injectPropertyAnnotations($object, $filter, \ReflectionClass $co $resolver = new ClassResolver($classReflection); $matches = array(); + // RegEx to find @property annotations preg_match_all('/@property\s+(\S+)\s+\$?(\S+).*/', $classReflection->getDocComment(), $matches); foreach ($matches[0] as $i => $match) { + // Filtering annotation with the filter class if (!call_user_func($filter, trim($match))) { continue; } @@ -111,14 +139,18 @@ public function injectPropertyAnnotations($object, $filter, \ReflectionClass $co $this->tryToInjectProperty($object, $propertyName, $resolver, $class); } + // Continue with the parent class (search for inherited properties) $classReflection = $classReflection->getParentClass(); } } /** - * @param object $object The object that the properties are injected into - * @param callable $filter Function to determine if the passed \ReflectionProperty should be included + * Inject value into object properties. + * + * @param object $object The object that the properties are injected into + * @param callable $filter Function to determine if the passed \ReflectionProperty should be included * @param \ReflectionClass $context The class to read the property annotations from (if not class of object) + * * @throws InjectionException */ public function injectProperties($object, $filter, \ReflectionClass $context = null) { @@ -126,6 +158,7 @@ public function injectProperties($object, $filter, \ReflectionClass $context = n foreach ($classReflection->getProperties() as $property) { $matches = array(); + // RegEx to find @var annotation preg_match('/@var\s+(\S+).*/', $property->getDocComment(), $matches); if (empty($matches) || !$filter($property)) { @@ -137,6 +170,17 @@ public function injectProperties($object, $filter, \ReflectionClass $context = n } } + /** + * Inject a property into an object. + * Wrapper of self::injectProperty, to enhance error message. + * + * @param object $targetObject The object where the property must be injected + * @param string $propertyName The name of the property to inject + * @param ClassResolver $resolver The class resolver + * @param string $class The type (class name) of the property + * + * @throws InjectionException + */ private function tryToInjectProperty($targetObject, $propertyName, ClassResolver $resolver, $class) { try { $this->injectProperty($targetObject, $propertyName, $resolver, $class); @@ -147,6 +191,16 @@ private function tryToInjectProperty($targetObject, $propertyName, ClassResolver } } + /** + * Inject a property into an object. + * + * @param object $targetObject The object where the property must be injected + * @param string $propertyName The name of the property to inject + * @param ClassResolver $resolver The class resolver + * @param string $class The type (class name) of the property + * + * @throws InjectionException + */ private function injectProperty($targetObject, $propertyName, ClassResolver $resolver, $class) { $type = $resolver->resolve($class); $classReflection = new \ReflectionClass($targetObject); diff --git a/src/watoki/factory/Provider.php b/src/watoki/factory/Provider.php index 2e38d98..f6c3b00 100644 --- a/src/watoki/factory/Provider.php +++ b/src/watoki/factory/Provider.php @@ -1,8 +1,22 @@ callback = $callback; } + /** {@inheritdoc} */ public function provide($class, array $args = array()) { return call_user_func($this->callback, $class, $args); } diff --git a/src/watoki/factory/providers/DefaultProvider.php b/src/watoki/factory/providers/DefaultProvider.php index b44111d..95ff418 100644 --- a/src/watoki/factory/providers/DefaultProvider.php +++ b/src/watoki/factory/providers/DefaultProvider.php @@ -3,33 +3,53 @@ use watoki\factory\Factory; +/** + * Class DefaultProvider + * The default provider. Use annotations. + * + * @package watoki\factory\providers + */ class DefaultProvider extends MinimalProvider { - + /** The injection token reads in annotation */ const INJECTION_TOKEN = '<-'; - + /** @var \Closure|callable The function to find if a property (magic variable) must be injected or not */ private $propertyFilter; - + /** @var \Closure|callable The function to find if the class variable must be injected on instantiation or not */ private $annotationFilter; - + /** @var string The name of the function to use (if exists) to inject properties(variables) into the new instance. */ private $injectionMethod = 'inject'; + /** + * Initialize the provider with the class factory. + * (initialize the injector, based on the factory, and "@var", "@param", "@property" filter functions) + * + * @param Factory $factory The classes factory + */ function __construct(Factory $factory) { parent::__construct($factory); + /* + * Change the default filter, to a new one that check if the param annotation contains the injection token + */ $this->setParameterFilter(function (\ReflectionParameter $parameter) { $pattern = '/@param.+\$' . $parameter->getName() . '.+' . DefaultProvider::INJECTION_TOKEN . '/'; return preg_match($pattern, $parameter->getDeclaringFunction()->getDocComment()); }); - + /* + * A callback function to check if the @property (magic variables) annotation contains teh injection token + */ $this->annotationFilter = function ($annotation) { return strpos($annotation, DefaultProvider::INJECTION_TOKEN) !== false; }; - + /* + * A callback function to check if the @var annotation contains teh injection token + */ $this->propertyFilter = function (\ReflectionProperty $property) { return strpos($property->getDocComment(), DefaultProvider::INJECTION_TOKEN) !== false; }; } + /** {@inheritdoc} */ public function provide($class, array $args = array()) { $instance = parent::provide($class, $args); @@ -45,6 +65,11 @@ public function provide($class, array $args = array()) { } /** + * Set the callback function to use to filter "@property" annotation. + * The filter function have this prototype: + * + * function(string $classname) : bool + * * @param callable $filter */ public function setAnnotationFilter($filter) { @@ -52,6 +77,8 @@ public function setAnnotationFilter($filter) { } /** + * Get the function used to filter "@property" annotation + * * @return callable */ public function getAnnotationFilter() { @@ -59,6 +86,11 @@ public function getAnnotationFilter() { } /** + * Set the callback function to use to filter "@var" annotation. + * The filter function have this prototype: + * + * function(string $classname) : bool + * * @return callable */ public function getPropertyFilter() { @@ -66,6 +98,8 @@ public function getPropertyFilter() { } /** + * Get the function used to filter "@property" annotation + * * @param callable $propertyFilter */ public function setPropertyFilter($propertyFilter) { @@ -73,6 +107,8 @@ public function setPropertyFilter($propertyFilter) { } /** + * Set the name of the instance function to used for injecting properties + * * @param string $injectionMethod */ public function setInjectionMethod($injectionMethod) { diff --git a/src/watoki/factory/providers/MinimalProvider.php b/src/watoki/factory/providers/MinimalProvider.php index 3eaabcd..a91557d 100644 --- a/src/watoki/factory/providers/MinimalProvider.php +++ b/src/watoki/factory/providers/MinimalProvider.php @@ -5,13 +5,25 @@ use watoki\factory\Injector; use watoki\factory\Provider; +/** + * Class MinimalProvider + * A base provider that use injector and a parameter injection filter. + * + * @package watoki\factory\providers + */ class MinimalProvider implements Provider { - + /** @var Injector */ protected $injector; /** @var callable */ private $parameterFilter; + /** + * Initialize the provider with the class factory. + * (initialize the injector, based on the factory, and the default filter function) + * + * @param Factory $factory The classes factory + */ public function __construct(Factory $factory) { $this->injector = new Injector($factory); $this->parameterFilter = function () { @@ -19,11 +31,14 @@ public function __construct(Factory $factory) { }; } + /** {@inheritdoc} */ public function provide($class, array $args = array()) { return $this->injector->injectConstructor($class, $args, $this->parameterFilter); } /** + * Get the function used to filter which parameter can be injected or not + * * @return callable */ public function getParameterFilter() { @@ -31,6 +46,11 @@ public function getParameterFilter() { } /** + * Set the function that will be use to filter parameters. + * The filter function have this prototype: + * + * function(\ReflectionParameter $parameter) : bool + * * @param callable $parameterFilter Receives a \ReflectionParameter as argument */ public function setParameterFilter($parameterFilter) { diff --git a/src/watoki/factory/providers/SingletonProvider.php b/src/watoki/factory/providers/SingletonProvider.php index 87fe1c2..568463b 100644 --- a/src/watoki/factory/providers/SingletonProvider.php +++ b/src/watoki/factory/providers/SingletonProvider.php @@ -3,17 +3,26 @@ use watoki\factory\Provider; +/** + * Class SingletonProvider + * A singleton provider. When use, the same instance of the class is return. + * + * @package watoki\factory\providers + */ class SingletonProvider implements Provider { private $instance; /** - * @param object $instance + * Initialize the provider with the instance to use a singleton + * + * @param object $instance The instance to use a singleton */ - function __construct($instance) { + public function __construct($instance) { $this->instance = $instance; } + /** {@inheritdoc} */ public function provide($class, array $args = array()) { return $this->instance; } From 330eae9bd1b36bbe5ca1dfad4469cbd3c3ccc101 Mon Sep 17 00:00:00 2001 From: MacFJA Date: Tue, 11 Aug 2015 22:41:19 +0200 Subject: [PATCH 2/3] PSR-1/PSR-2 code reformatting --- src/watoki/factory/Factory.php | 29 ++++--- src/watoki/factory/Injector.php | 85 +++++++++++-------- src/watoki/factory/Provider.php | 5 +- .../factory/exception/InjectionException.php | 3 +- .../factory/providers/CallbackProvider.php | 9 +- .../factory/providers/DefaultProvider.php | 24 ++++-- .../factory/providers/MinimalProvider.php | 15 ++-- .../factory/providers/SingletonProvider.php | 9 +- 8 files changed, 110 insertions(+), 69 deletions(-) diff --git a/src/watoki/factory/Factory.php b/src/watoki/factory/Factory.php index d31be14..ae3298c 100755 --- a/src/watoki/factory/Factory.php +++ b/src/watoki/factory/Factory.php @@ -10,7 +10,8 @@ * * @package watoki\factory */ -class Factory { +class Factory +{ /** @var string The Factory class name */ public static $CLASS = __CLASS__; @@ -21,7 +22,8 @@ class Factory { * Initialize the class. * (Define the default provider, and self register as singleton) */ - public function __construct() { + public function __construct() + { $this->setSingleton($this); $this->setProvider('stdClass', new DefaultProvider($this)); } @@ -32,25 +34,27 @@ public function __construct() { * If the class was registed as singleton, the previous instance is returned regardless of the arguments. * * @param string $class The name (or alias) of the class to get - * @param array $args Constructor arguments that cannot be provided by the factory (indexed by parameter name) + * @param array $args Constructor arguments that cannot be provided by the factory (indexed by parameter name) * * @return mixed An instance of the given class * * @throws \Exception If the class or an injected class cannot be constructed */ - public function getInstance($class, $args = array()) { + public function getInstance($class, $args = array()) + { return $this->findMatchingProvider($class)->provide($class, $args); } /** * Define a singleton * - * @param object $instance The singleton instance - * @param string|null $class If omitted, the class of the instance is used + * @param object $instance The singleton instance + * @param string|null $class If omitted, the class of the instance is used * * @return object The $instance */ - public function setSingleton($instance, $class = null) { + public function setSingleton($instance, $class = null) + { $class = $class ?: get_class($instance); $this->setProvider($class, new SingletonProvider($instance)); @@ -60,10 +64,11 @@ public function setSingleton($instance, $class = null) { /** * Define a provider of a class name * - * @param string $class The name (or alias) of the class + * @param string $class The name (or alias) of the class * @param Provider $provider The class provider to use for this class name */ - public function setProvider($class, Provider $provider) { + public function setProvider($class, Provider $provider) + { $this->providers[$this->normalizeClass($class)] = $provider; } @@ -74,7 +79,8 @@ public function setProvider($class, Provider $provider) { * * @return Provider The provider to use */ - private function findMatchingProvider($class) { + private function findMatchingProvider($class) + { $reflection = new \ReflectionClass($class); /* @@ -113,7 +119,8 @@ private function findMatchingProvider($class) { * @param string $class The class name to transform * @return string The "ready to use as key" class name */ - private function normalizeClass($class) { + private function normalizeClass($class) + { return trim(strtolower($class), '\\'); } } diff --git a/src/watoki/factory/Injector.php b/src/watoki/factory/Injector.php index 320ffa4..e4d69ba 100644 --- a/src/watoki/factory/Injector.php +++ b/src/watoki/factory/Injector.php @@ -11,7 +11,8 @@ * * @package watoki\factory */ -class Injector { +class Injector +{ /** @var bool */ private $throwException = true; @@ -19,7 +20,8 @@ class Injector { /** @var callable */ private $injector; - public function __construct(Factory $factory, callable $internalInjector = null) { + public function __construct(Factory $factory, callable $internalInjector = null) + { $this->injector = $internalInjector ?: function ($class) use ($factory) { return $factory->getInstance($class); }; @@ -30,22 +32,24 @@ public function __construct(Factory $factory, callable $internalInjector = null) * * @param bool $throw Indicate if an exception must be thrown */ - public function setThrowWhenCantInjectProperty($throw) { + public function setThrowWhenCantInjectProperty($throw) + { $this->throwException = $throw; } /** * Instantiate a new object by its constructor * - * @param string $class The class name to instantiate - * @param array $args List of arguments to use + * @param string $class The class name to instantiate + * @param array $args List of arguments to use * @param callable $parameterFilter Callback function for filtering parameters to inject * * @return object An instance of $class * * @throws InjectionException */ - public function injectConstructor($class, $args, $parameterFilter) { + public function injectConstructor($class, $args, $parameterFilter) + { $reflection = new \ReflectionClass($class); if ($reflection->isAbstract() || $reflection->isInterface()) { @@ -57,27 +61,31 @@ public function injectConstructor($class, $args, $parameterFilter) { } try { - return $reflection->newInstanceArgs($this->injectMethodArguments($reflection->getConstructor(), $args, $parameterFilter)); + return $reflection->newInstanceArgs($this->injectMethodArguments($reflection->getConstructor(), $args, + $parameterFilter)); } catch (InjectionException $e) { - throw new InjectionException('Error while injecting constructor of [' . $reflection->getName() . ']: ' . $e->getMessage(), 0, $e); + throw new InjectionException('Error while injecting constructor of [' . $reflection->getName() . ']: ' . $e->getMessage(), + 0, $e); } catch (\ReflectionException $re) { - throw new InjectionException('Error while injecting constructor of [' . $reflection->getName() . ']: ' . $re->getMessage(), 0, $re); + throw new InjectionException('Error while injecting constructor of [' . $reflection->getName() . ']: ' . $re->getMessage(), + 0, $re); } } /** * Inject object/params in an object method * - * @param object $object Object to call method on - * @param string $method Name of the method - * @param array $args List of params to use + * @param object $object Object to call method on + * @param string $method Name of the method + * @param array $args List of params to use * @param null|callable $parameterFilter If omitted, all missing arguments are injected * * @return mixed The return value of the method * * @throws InjectionException */ - public function injectMethod($object, $method, $args = array(), $parameterFilter = null) { + public function injectMethod($object, $method, $args = array(), $parameterFilter = null) + { $parameterFilter = $parameterFilter ?: function () { return true; }; @@ -91,15 +99,16 @@ public function injectMethod($object, $method, $args = array(), $parameterFilter /** * Create the list of all arguments to inject in a method * - * @param \ReflectionMethod $method The reflection method - * @param array $args List of params to inject - * @param callable $parameterFilter Callback function for filtering parameters to inject + * @param \ReflectionMethod $method The reflection method + * @param array $args List of params to inject + * @param callable $parameterFilter Callback function for filtering parameters to inject * * @return array Of the injected arguments * * @throws InjectionException */ - public function injectMethodArguments(\ReflectionMethod $method, array $args, $parameterFilter) { + public function injectMethodArguments(\ReflectionMethod $method, array $args, $parameterFilter) + { $analyzer = new MethodAnalyzer($method); try { return $analyzer->fillParameters($args, $this->injector, $parameterFilter); @@ -112,14 +121,15 @@ public function injectMethodArguments(\ReflectionMethod $method, array $args, $p /** * Inject value into object properties by reading "@property" (magic properties) annotation (include inherited). * - * @param object $object The object that the properties are injected into - * @param callable $filter Function to determine if the passed property annotation should be included + * @param object $object The object that the properties are injected into + * @param callable $filter Function to determine if the passed property annotation should be included * @param \ReflectionClass $context The class to read the property annotations from (if not class of object) * * @throws InjectionException */ - public function injectPropertyAnnotations($object, $filter, \ReflectionClass $context = null) { - $classReflection = $context ? : new \ReflectionClass($object); + public function injectPropertyAnnotations($object, $filter, \ReflectionClass $context = null) + { + $classReflection = $context ?: new \ReflectionClass($object); while ($classReflection) { $resolver = new ClassResolver($classReflection); @@ -147,14 +157,15 @@ public function injectPropertyAnnotations($object, $filter, \ReflectionClass $co /** * Inject value into object properties. * - * @param object $object The object that the properties are injected into - * @param callable $filter Function to determine if the passed \ReflectionProperty should be included + * @param object $object The object that the properties are injected into + * @param callable $filter Function to determine if the passed \ReflectionProperty should be included * @param \ReflectionClass $context The class to read the property annotations from (if not class of object) * * @throws InjectionException */ - public function injectProperties($object, $filter, \ReflectionClass $context = null) { - $classReflection = $context ? : new \ReflectionClass($object); + public function injectProperties($object, $filter, \ReflectionClass $context = null) + { + $classReflection = $context ?: new \ReflectionClass($object); foreach ($classReflection->getProperties() as $property) { $matches = array(); @@ -174,34 +185,36 @@ public function injectProperties($object, $filter, \ReflectionClass $context = n * Inject a property into an object. * Wrapper of self::injectProperty, to enhance error message. * - * @param object $targetObject The object where the property must be injected - * @param string $propertyName The name of the property to inject - * @param ClassResolver $resolver The class resolver - * @param string $class The type (class name) of the property + * @param object $targetObject The object where the property must be injected + * @param string $propertyName The name of the property to inject + * @param ClassResolver $resolver The class resolver + * @param string $class The type (class name) of the property * * @throws InjectionException */ - private function tryToInjectProperty($targetObject, $propertyName, ClassResolver $resolver, $class) { + private function tryToInjectProperty($targetObject, $propertyName, ClassResolver $resolver, $class) + { try { $this->injectProperty($targetObject, $propertyName, $resolver, $class); } catch (InjectionException $e) { $targetClass = get_class($targetObject); throw new InjectionException("Error while injecting dependency [$propertyName] " . - "of [$targetClass]: " . $e->getMessage(), 0, $e); + "of [$targetClass]: " . $e->getMessage(), 0, $e); } } /** * Inject a property into an object. * - * @param object $targetObject The object where the property must be injected - * @param string $propertyName The name of the property to inject - * @param ClassResolver $resolver The class resolver - * @param string $class The type (class name) of the property + * @param object $targetObject The object where the property must be injected + * @param string $propertyName The name of the property to inject + * @param ClassResolver $resolver The class resolver + * @param string $class The type (class name) of the property * * @throws InjectionException */ - private function injectProperty($targetObject, $propertyName, ClassResolver $resolver, $class) { + private function injectProperty($targetObject, $propertyName, ClassResolver $resolver, $class) + { $type = $resolver->resolve($class); $classReflection = new \ReflectionClass($targetObject); diff --git a/src/watoki/factory/Provider.php b/src/watoki/factory/Provider.php index f6c3b00..e6c4dfe 100644 --- a/src/watoki/factory/Provider.php +++ b/src/watoki/factory/Provider.php @@ -7,13 +7,14 @@ * * @package watoki\factory */ -interface Provider { +interface Provider +{ /** * Get an instance of the class $class with the parameters $args * * @param string $class - * @param array $args + * @param array $args * * @return object The $class instance */ diff --git a/src/watoki/factory/exception/InjectionException.php b/src/watoki/factory/exception/InjectionException.php index 421d295..1c9d563 100644 --- a/src/watoki/factory/exception/InjectionException.php +++ b/src/watoki/factory/exception/InjectionException.php @@ -7,7 +7,8 @@ * * @package watoki\factory\exception */ -class InjectionException extends \Exception { +class InjectionException extends \Exception +{ /** @var string The Factory class name */ public static $CLASS = __CLASS__; } \ No newline at end of file diff --git a/src/watoki/factory/providers/CallbackProvider.php b/src/watoki/factory/providers/CallbackProvider.php index 70e88fb..4876044 100644 --- a/src/watoki/factory/providers/CallbackProvider.php +++ b/src/watoki/factory/providers/CallbackProvider.php @@ -9,7 +9,8 @@ * * @package watoki\factory\providers */ -class CallbackProvider implements Provider { +class CallbackProvider implements Provider +{ /** @var callable */ private $callback; @@ -22,12 +23,14 @@ class CallbackProvider implements Provider { * * @param callable $callback */ - public function __construct($callback) { + public function __construct($callback) + { $this->callback = $callback; } /** {@inheritdoc} */ - public function provide($class, array $args = array()) { + public function provide($class, array $args = array()) + { return call_user_func($this->callback, $class, $args); } } \ No newline at end of file diff --git a/src/watoki/factory/providers/DefaultProvider.php b/src/watoki/factory/providers/DefaultProvider.php index 95ff418..97a1a3c 100644 --- a/src/watoki/factory/providers/DefaultProvider.php +++ b/src/watoki/factory/providers/DefaultProvider.php @@ -9,7 +9,8 @@ * * @package watoki\factory\providers */ -class DefaultProvider extends MinimalProvider { +class DefaultProvider extends MinimalProvider +{ /** The injection token reads in annotation */ const INJECTION_TOKEN = '<-'; /** @var \Closure|callable The function to find if a property (magic variable) must be injected or not */ @@ -25,7 +26,8 @@ class DefaultProvider extends MinimalProvider { * * @param Factory $factory The classes factory */ - function __construct(Factory $factory) { + function __construct(Factory $factory) + { parent::__construct($factory); /* @@ -50,7 +52,8 @@ function __construct(Factory $factory) { } /** {@inheritdoc} */ - public function provide($class, array $args = array()) { + public function provide($class, array $args = array()) + { $instance = parent::provide($class, $args); if ($this->injectionMethod && method_exists($instance, $this->injectionMethod)) { @@ -72,7 +75,8 @@ public function provide($class, array $args = array()) { * * @param callable $filter */ - public function setAnnotationFilter($filter) { + public function setAnnotationFilter($filter) + { $this->annotationFilter = $filter; } @@ -81,7 +85,8 @@ public function setAnnotationFilter($filter) { * * @return callable */ - public function getAnnotationFilter() { + public function getAnnotationFilter() + { return $this->annotationFilter; } @@ -93,7 +98,8 @@ public function getAnnotationFilter() { * * @return callable */ - public function getPropertyFilter() { + public function getPropertyFilter() + { return $this->propertyFilter; } @@ -102,7 +108,8 @@ public function getPropertyFilter() { * * @param callable $propertyFilter */ - public function setPropertyFilter($propertyFilter) { + public function setPropertyFilter($propertyFilter) + { $this->propertyFilter = $propertyFilter; } @@ -111,7 +118,8 @@ public function setPropertyFilter($propertyFilter) { * * @param string $injectionMethod */ - public function setInjectionMethod($injectionMethod) { + public function setInjectionMethod($injectionMethod) + { $this->injectionMethod = $injectionMethod; } } \ No newline at end of file diff --git a/src/watoki/factory/providers/MinimalProvider.php b/src/watoki/factory/providers/MinimalProvider.php index a91557d..74cca98 100644 --- a/src/watoki/factory/providers/MinimalProvider.php +++ b/src/watoki/factory/providers/MinimalProvider.php @@ -11,7 +11,8 @@ * * @package watoki\factory\providers */ -class MinimalProvider implements Provider { +class MinimalProvider implements Provider +{ /** @var Injector */ protected $injector; @@ -24,7 +25,8 @@ class MinimalProvider implements Provider { * * @param Factory $factory The classes factory */ - public function __construct(Factory $factory) { + public function __construct(Factory $factory) + { $this->injector = new Injector($factory); $this->parameterFilter = function () { return true; @@ -32,7 +34,8 @@ public function __construct(Factory $factory) { } /** {@inheritdoc} */ - public function provide($class, array $args = array()) { + public function provide($class, array $args = array()) + { return $this->injector->injectConstructor($class, $args, $this->parameterFilter); } @@ -41,7 +44,8 @@ public function provide($class, array $args = array()) { * * @return callable */ - public function getParameterFilter() { + public function getParameterFilter() + { return $this->parameterFilter; } @@ -53,7 +57,8 @@ public function getParameterFilter() { * * @param callable $parameterFilter Receives a \ReflectionParameter as argument */ - public function setParameterFilter($parameterFilter) { + public function setParameterFilter($parameterFilter) + { $this->parameterFilter = $parameterFilter; } } \ No newline at end of file diff --git a/src/watoki/factory/providers/SingletonProvider.php b/src/watoki/factory/providers/SingletonProvider.php index 568463b..3370986 100644 --- a/src/watoki/factory/providers/SingletonProvider.php +++ b/src/watoki/factory/providers/SingletonProvider.php @@ -9,7 +9,8 @@ * * @package watoki\factory\providers */ -class SingletonProvider implements Provider { +class SingletonProvider implements Provider +{ private $instance; @@ -18,12 +19,14 @@ class SingletonProvider implements Provider { * * @param object $instance The instance to use a singleton */ - public function __construct($instance) { + public function __construct($instance) + { $this->instance = $instance; } /** {@inheritdoc} */ - public function provide($class, array $args = array()) { + public function provide($class, array $args = array()) + { return $this->instance; } } \ No newline at end of file From 677f0cfec148aca49f4f1cd119eccd9eb96a4acb Mon Sep 17 00:00:00 2001 From: MacFJA Date: Tue, 11 Aug 2015 22:49:35 +0200 Subject: [PATCH 3/3] Add author and license PHPDoc --- src/watoki/factory/Factory.php | 2 ++ src/watoki/factory/Injector.php | 2 ++ src/watoki/factory/Provider.php | 2 ++ src/watoki/factory/exception/InjectionException.php | 2 ++ src/watoki/factory/providers/CallbackProvider.php | 2 ++ src/watoki/factory/providers/DefaultProvider.php | 2 ++ src/watoki/factory/providers/MinimalProvider.php | 2 ++ src/watoki/factory/providers/SingletonProvider.php | 2 ++ 8 files changed, 16 insertions(+) diff --git a/src/watoki/factory/Factory.php b/src/watoki/factory/Factory.php index ae3298c..fa6ade5 100755 --- a/src/watoki/factory/Factory.php +++ b/src/watoki/factory/Factory.php @@ -8,6 +8,8 @@ * Class Factory. * The factory that build all of your object * + * @author "Nikolas Martens" + * @license MIT * @package watoki\factory */ class Factory diff --git a/src/watoki/factory/Injector.php b/src/watoki/factory/Injector.php index e4d69ba..d713dac 100644 --- a/src/watoki/factory/Injector.php +++ b/src/watoki/factory/Injector.php @@ -9,6 +9,8 @@ * Class Injector * Inject objects, params into an instance * + * @author "Nikolas Martens" + * @license MIT * @package watoki\factory */ class Injector diff --git a/src/watoki/factory/Provider.php b/src/watoki/factory/Provider.php index e6c4dfe..2c20fd8 100644 --- a/src/watoki/factory/Provider.php +++ b/src/watoki/factory/Provider.php @@ -5,6 +5,8 @@ * Interface Provider * Describe how to get an instance from a class name and params * + * @author "Nikolas Martens" + * @license MIT * @package watoki\factory */ interface Provider diff --git a/src/watoki/factory/exception/InjectionException.php b/src/watoki/factory/exception/InjectionException.php index 1c9d563..a91602c 100644 --- a/src/watoki/factory/exception/InjectionException.php +++ b/src/watoki/factory/exception/InjectionException.php @@ -5,6 +5,8 @@ * Class InjectionException * A class to defined exception on injection * + * @author "Nikolas Martens" + * @license MIT * @package watoki\factory\exception */ class InjectionException extends \Exception diff --git a/src/watoki/factory/providers/CallbackProvider.php b/src/watoki/factory/providers/CallbackProvider.php index 4876044..16f0656 100644 --- a/src/watoki/factory/providers/CallbackProvider.php +++ b/src/watoki/factory/providers/CallbackProvider.php @@ -7,6 +7,8 @@ * Class CallbackProvider * The provider use a user define callback function to have an instance of a class * + * @author "Nikolas Martens" + * @license MIT * @package watoki\factory\providers */ class CallbackProvider implements Provider diff --git a/src/watoki/factory/providers/DefaultProvider.php b/src/watoki/factory/providers/DefaultProvider.php index 97a1a3c..5d57489 100644 --- a/src/watoki/factory/providers/DefaultProvider.php +++ b/src/watoki/factory/providers/DefaultProvider.php @@ -7,6 +7,8 @@ * Class DefaultProvider * The default provider. Use annotations. * + * @author "Nikolas Martens" + * @license MIT * @package watoki\factory\providers */ class DefaultProvider extends MinimalProvider diff --git a/src/watoki/factory/providers/MinimalProvider.php b/src/watoki/factory/providers/MinimalProvider.php index 74cca98..f27dcca 100644 --- a/src/watoki/factory/providers/MinimalProvider.php +++ b/src/watoki/factory/providers/MinimalProvider.php @@ -9,6 +9,8 @@ * Class MinimalProvider * A base provider that use injector and a parameter injection filter. * + * @author "Nikolas Martens" + * @license MIT * @package watoki\factory\providers */ class MinimalProvider implements Provider diff --git a/src/watoki/factory/providers/SingletonProvider.php b/src/watoki/factory/providers/SingletonProvider.php index 3370986..9e753ec 100644 --- a/src/watoki/factory/providers/SingletonProvider.php +++ b/src/watoki/factory/providers/SingletonProvider.php @@ -7,6 +7,8 @@ * Class SingletonProvider * A singleton provider. When use, the same instance of the class is return. * + * @author "Nikolas Martens" + * @license MIT * @package watoki\factory\providers */ class SingletonProvider implements Provider