ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 ($fullyQualifiedClassName, $requireFile=false, callable $with=null)
 

Protected Member Functions

 createConstructorArguments ($fullyQualifiedClassName, $parameters, $with)
 
 getDependency ($fullyQualifiedClassName, $parameter, callable $with=null)
 

Protected Attributes

 $dic
 
 $dependencyMap
 

Detailed Description

Definition at line 19 of file Injector.php.

Constructor & Destructor Documentation

◆ __construct()

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

Factory constructor.

Parameters
$dicContainer
DependencyMap$dependencyMap

Definition at line 38 of file Injector.php.

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

39  {
40  $this->dic = $dic;
41  $this->dependencyMap = $dependencyMap;
42  }

Member Function Documentation

◆ createConstructorArguments()

ILIAS\BackgroundTasks\Dependencies\Injector::createConstructorArguments (   $fullyQualifiedClassName,
  $parameters,
  $with 
)
protected
Parameters
$fullyQualifiedClassNamestring
$parametersReflectionParameter[]
Returns
array

Definition at line 84 of file Injector.php.

References ILIAS\BackgroundTasks\Dependencies\Injector\getDependency().

Referenced by ILIAS\BackgroundTasks\Dependencies\Injector\createInstance().

85  {
86  $constructorArguments = [];
87 
88  foreach ($parameters as $parameter) {
89  // As long as there are given arguments we take those.
90  $constructorArguments[] = $this->getDependency($fullyQualifiedClassName, $parameter, $with);
91  }
92 
93  return $constructorArguments;
94  }
getDependency($fullyQualifiedClassName, $parameter, callable $with=null)
Definition: Injector.php:106
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createInstance()

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

PhpIncludeInspection

Definition at line 54 of file Injector.php.

References ILIAS\BackgroundTasks\Dependencies\Injector\createConstructorArguments().

55  {
56  if ($requireFile) {
57  require_once($requireFile);
58  }
59 
60  // The reflection classes needed.
61  $reflectionClass = new \ReflectionClass($fullyQualifiedClassName);
62  $constructor = $reflectionClass->getConstructor();
63  if (!$constructor) {
64  return $reflectionClass->newInstance();
65  }
66 
67  $parameters = $constructor->getParameters();
68 
69  // we get the arguments to construct the object from the DIC and Typehinting.
70  $constructorArguments = $this->createConstructorArguments($fullyQualifiedClassName, $parameters, $with);
71 
72  // Crate the instance with the arguments.
73  return $reflectionClass->newInstanceArgs($constructorArguments);
74  }
createConstructorArguments($fullyQualifiedClassName, $parameters, $with)
Definition: Injector.php:84
+ Here is the call graph for this function:

◆ getDependency()

ILIAS\BackgroundTasks\Dependencies\Injector::getDependency (   $fullyQualifiedClassName,
  $parameter,
callable  $with = null 
)
protected
Parameters
$fullyQualifiedClassNamestring
$parameterReflectionParameter
callable$with
Returns
mixed
Exceptions
InvalidClassException

Definition at line 106 of file Injector.php.

References $type.

Referenced by ILIAS\BackgroundTasks\Dependencies\Injector\createConstructorArguments().

107  {
108  // These Lines are currently commented while we cant use $parameter->getType() which will be part of PHP7
109  // if (!$parameter->getType()) {
110  // throw new InvalidClassException("The constructor of $fullyQualifiedClassName is not fully type hinted, or the type hints cannot be resolved.");
111  // }
112 
113  // $type = $parameter->getType()->__toString();
114  $type = $parameter->getClass()->getName();
115 
116  // if ($parameter->getType()->isBuiltin()) {
117  // 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.");
118  // }
119 
120  if (!$type) {
121  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.");
122  }
123 
124  if ($with) {
125  return $this->dependencyMap->getDependencyWith($this->dic, $type, $fullyQualifiedClassName, $with);
126  } else {
127  return $this->dependencyMap->getDependency($this->dic, $type, $fullyQualifiedClassName);
128  }
129  }
$type
+ Here is the caller graph for this function:

Field Documentation

◆ $dependencyMap

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

◆ $dic

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

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