Skip to content

Commit

Permalink
Renamed Factory to Injector
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Oct 20, 2013
1 parent 8c89ea3 commit 3c04c79
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
1 change: 1 addition & 0 deletions change-log.md
Expand Up @@ -13,6 +13,7 @@ All changes:
* [#110](https://github.com/mnapoli/PHP-DI/issues/110) XML definitions are not supported anymore
* [#122](https://github.com/mnapoli/PHP-DI/issues/122) JSON definitions are not supported anymore
* `ContainerSingleton` has finally been removed
* `FactoryInterface` has been renamed to `Injector`, only people implementing their own factory/injector should care

## 3.4

Expand Down
9 changes: 6 additions & 3 deletions doc/how-it-works.md
Expand Up @@ -17,7 +17,7 @@ $entry = $container->get('entryName');
A container instance has the following sub-components:

- a `DefinitionManager` that returns a `Definition` for an entry name
- a `Factory` to create instances from the definitions
- an `Injector` to create instances from the definitions

### Definitions

Expand Down Expand Up @@ -62,6 +62,9 @@ It answers the following needs:

*This could be improved by splitting this into 2 separate classes to follow the [Single Responsibility Principle](http://en.wikipedia.org/wiki/Single_responsibility_principle).*

### Factory
### Injector

The role of the `Factory` is to return an object instance from a `ClassDefinition`.
The role of the `Injector` is to process "Definitions".

It can return a new instance (with dependencies injected) from a `ClassDefinition`, or also inject dependencies on an
existing instance.
32 changes: 16 additions & 16 deletions src/DI/Container.php
Expand Up @@ -38,9 +38,9 @@ class Container
private $definitionManager;

/**
* @var FactoryInterface
* @var Injector
*/
private $factory;
private $injector;

/**
* @var LazyLoadingValueHolderFactory
Expand All @@ -58,16 +58,16 @@ class Container
* Parameters are optional, use them to override a dependency.
*
* @param DefinitionManager|null $definitionManager
* @param FactoryInterface|null $factory
* @param Injector|null $injector
* @param LazyLoadingValueHolderFactory|null $proxyFactory
*/
public function __construct(
DefinitionManager $definitionManager = null,
FactoryInterface $factory = null,
Injector $injector = null,
LazyLoadingValueHolderFactory $proxyFactory = null
) {
$this->definitionManager = $definitionManager ?: $this->createDefaultDefinitionManager();
$this->factory = $factory ?: $this->createDefaultFactory();
$this->injector = $injector ?: $this->createDefaultInjector();
$this->proxyFactory = $proxyFactory ?: $this->createDefaultProxyFactory();

// Auto-register the container
Expand Down Expand Up @@ -161,7 +161,7 @@ public function injectOn($instance)

// Check that the definition is a class definition
if ($definition instanceof ClassDefinition) {
$instance = $this->factory->injectOnInstance($definition, $instance);
$instance = $this->injector->injectOnInstance($definition, $instance);
}

return $instance;
Expand Down Expand Up @@ -195,19 +195,19 @@ public function addDefinitions(array $definitions)
}

/**
* @param FactoryInterface $factory
* @param Injector $injector
*/
public function setFactory(FactoryInterface $factory)
public function setInjector(Injector $injector)
{
$this->factory = $factory;
$this->injector = $injector;
}

/**
* @return FactoryInterface
* @return Injector
*/
public function getFactory()
public function getInjector()
{
return $this->factory;
return $this->injector;
}

/**
Expand Down Expand Up @@ -250,7 +250,7 @@ private function getNewInstance(ClassDefinition $classDefinition)
$this->classesBeingInstantiated[$classname] = true;

try {
$instance = $this->factory->createInstance($classDefinition);
$instance = $this->injector->createInstance($classDefinition);
} catch (Exception $exception) {
unset($this->classesBeingInstantiated[$classname]);
throw $exception;
Expand Down Expand Up @@ -293,11 +293,11 @@ private function createDefaultDefinitionManager()
}

/**
* @return FactoryInterface
* @return Injector
*/
private function createDefaultFactory()
private function createDefaultInjector()
{
return new Factory($this);
return new DefaultInjector($this);
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/DI/Factory.php → src/DI/DefaultInjector.php
Expand Up @@ -23,11 +23,12 @@
use ReflectionParameter;

/**
* Factory class, responsible of instantiating classes
* Component responsible of creating and injecting dependencies as defined in Definitions.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
* @since 4.0
*/
class Factory implements FactoryInterface
class DefaultInjector implements Injector
{
/**
* @var Container
Expand Down Expand Up @@ -82,7 +83,7 @@ public function injectOnInstance(ClassDefinition $classDefinition, $instance)
}

/**
* Creates an instance and inject dependencies through the constructor
* Creates an instance and inject dependencies through the constructor.
*
* @param ReflectionClass $classReflection
* @param MethodInjection|null $constructorInjection
Expand Down Expand Up @@ -177,7 +178,7 @@ private function prepareMethodParameters(MethodInjection $methodInjection = null
}

/**
* Inject dependencies into properties
* Inject dependencies into properties.
*
* @param object $object Object to inject dependencies into
* @param PropertyInjection $propertyInjection Property injection definition
Expand Down Expand Up @@ -213,9 +214,11 @@ private function injectProperty($object, PropertyInjection $propertyInjection)
}

/**
* Returns the default value of a function parameter
* Returns the default value of a function parameter.
*
* @param ReflectionParameter $reflectionParameter
* @param ReflectionMethod $reflectionMethod
*
* @throws DefinitionException Can't get default values from PHP internal classes and methods
* @return mixed
*/
Expand Down
13 changes: 8 additions & 5 deletions src/DI/FactoryInterface.php → src/DI/Injector.php
Expand Up @@ -12,26 +12,29 @@
use DI\Definition\ClassDefinition;

/**
* Factory class, responsible of instantiating classes
* Component responsible of creating and injecting dependencies as defined in Definitions.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
* @since 4.0
*/
interface FactoryInterface
interface Injector
{
/**
* Create a new instance of the class
* Create a new instance of a class and injects all dependencies.
*
* @param ClassDefinition $classDefinition
*
* @return object The instance
*/
function createInstance(ClassDefinition $classDefinition);
public function createInstance(ClassDefinition $classDefinition);

/**
* Injects dependencies on an existing instance.
*
* @param ClassDefinition $classDefinition
* @param object $instance
*
* @return object The instance
*/
function injectOnInstance(ClassDefinition $classDefinition, $instance);
public function injectOnInstance(ClassDefinition $classDefinition, $instance);
}

0 comments on commit 3c04c79

Please sign in to comment.