ILIAS  release_8 Revision v8.24
ILIAS\BackgroundTasks\Dependencies\Injector Class Reference
+ Collaboration diagram for ILIAS\BackgroundTasks\Dependencies\Injector:

Public Member Functions

 __construct (Container $dic, DependencyMap $dependencyMap)
 Factory constructor. More...
 
 createInstance (string $fullyQualifiedClassName, bool $requireFile=false, callable $with=null)
 

Protected Member Functions

 createConstructorArguments (string $fullyQualifiedClassName, array $parameters, ?callable $with)
 
 getDependency (string $fullyQualifiedClassName, ReflectionParameter $parameter, ?callable $with=null)
 

Protected Attributes

ILIAS DI Container $dic
 
ILIAS BackgroundTasks Dependencies DependencyMap DependencyMap $dependencyMap
 

Detailed Description

Definition at line 32 of file Injector.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\BackgroundTasks\Dependencies\Injector::__construct ( Container  $dic,
DependencyMap  $dependencyMap 
)

Factory constructor.

Parameters
$dicContainer

Definition at line 41 of file Injector.php.

42 {
43 $this->dic = $dic;
44 $this->dependencyMap = $dependencyMap;
45 }
ILIAS BackgroundTasks Dependencies DependencyMap DependencyMap $dependencyMap
Definition: Injector.php:35

References ILIAS\BackgroundTasks\Dependencies\Injector\$dependencyMap, and ILIAS\BackgroundTasks\Dependencies\Injector\$dic.

Member Function Documentation

◆ createConstructorArguments()

ILIAS\BackgroundTasks\Dependencies\Injector::createConstructorArguments ( string  $fullyQualifiedClassName,
array  $parameters,
?callable  $with 
)
protected
Parameters
ReflectionParameter[]$parameters

Definition at line 76 of file Injector.php.

80 : array {
81 $constructorArguments = [];
82
83 foreach ($parameters as $parameter) {
84 // As long as there are given arguments we take those.
85 $constructorArguments[] = $this->getDependency($fullyQualifiedClassName, $parameter, $with);
86 }
87
88 return $constructorArguments;
89 }
getDependency(string $fullyQualifiedClassName, ReflectionParameter $parameter, ?callable $with=null)
Definition: Injector.php:94

◆ createInstance()

ILIAS\BackgroundTasks\Dependencies\Injector::createInstance ( string  $fullyQualifiedClassName,
bool  $requireFile = false,
callable  $with = null 
)
Parameters
string$fullyQualifiedClassNameThe given class must type hint all its constructor arguments. Furthermore the types must exist in the DI-Container.

Definition at line 52 of file Injector.php.

56 : object {
57 // The reflection classes needed.
58 $reflectionClass = new \ReflectionClass($fullyQualifiedClassName);
59 $constructor = $reflectionClass->getConstructor();
60 if ($constructor === null) {
61 return $reflectionClass->newInstance();
62 }
63
64 $parameters = $constructor->getParameters();
65
66 // we get the arguments to construct the object from the DIC and Typehinting.
67 $constructorArguments = $this->createConstructorArguments($fullyQualifiedClassName, $parameters, $with);
68
69 // Crate the instance with the arguments.
70 return $reflectionClass->newInstanceArgs($constructorArguments);
71 }
createConstructorArguments(string $fullyQualifiedClassName, array $parameters, ?callable $with)
Definition: Injector.php:76

◆ getDependency()

ILIAS\BackgroundTasks\Dependencies\Injector::getDependency ( string  $fullyQualifiedClassName,
ReflectionParameter  $parameter,
?callable  $with = null 
)
protected
Exceptions
InvalidClassException

Definition at line 94 of file Injector.php.

98 {
99 // These Lines are currently commented while we cant use $parameter->getType() which will be part of PHP7
100 // if (!$parameter->getType()) {
101 // throw new InvalidClassException("The constructor of $fullyQualifiedClassName is not fully type hinted, or the type hints cannot be resolved.");
102 // }
103
104 // $type = $parameter->getType()->__toString();
105 $type = $parameter->getClass()->getName();
106
107 // if ($parameter->getType()->isBuiltin()) {
108 // throw new InvalidClassException("The DI cannot instantiate $fullyQualifiedClassName because some of the constructors arguments are built in types. Only interfaces (and objects) are stored in the DI-Container.");
109 // }
110
111 if (!$type) {
112 throw new InvalidClassException("The DI cannot instantiate $fullyQualifiedClassName because some of the constructors arguments are not type hinted. Make sure all parameters in the constructor have type hinting.");
113 }
114
115 if ($with) {
116 return $this->dependencyMap->getDependencyWith($this->dic, $type, $fullyQualifiedClassName, $with);
117 } else {
118 return $this->dependencyMap->getDependency($this->dic, $type, $fullyQualifiedClassName);
119 }
120 }
$type

References $type.

Field Documentation

◆ $dependencyMap

ILIAS BackgroundTasks Dependencies DependencyMap DependencyMap ILIAS\BackgroundTasks\Dependencies\Injector::$dependencyMap
protected

◆ $dic

ILIAS DI Container ILIAS\BackgroundTasks\Dependencies\Injector::$dic
protected

The documentation for this class was generated from the following file: