ILIAS  trunk Revision v11.0_alpha-2645-g16283d3b3f8
ILIAS\BackgroundTasks\Dependencies\Injector Class Reference
+ Collaboration diagram for ILIAS\BackgroundTasks\Dependencies\Injector:

Public Member Functions

 __construct (protected Container $dic, protected 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)
 

Detailed Description

Definition at line 32 of file Injector.php.

Constructor & Destructor Documentation

◆ __construct()

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

Factory constructor.

Parameters
$dicContainer

Definition at line 38 of file Injector.php.

39  {
40  }

Member Function Documentation

◆ createConstructorArguments()

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

Definition at line 71 of file Injector.php.

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

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

75  : array {
76  $constructorArguments = [];
77 
78  foreach ($parameters as $parameter) {
79  // As long as there are given arguments we take those.
80  $constructorArguments[] = $this->getDependency($fullyQualifiedClassName, $parameter, $with);
81  }
82 
83  return $constructorArguments;
84  }
getDependency(string $fullyQualifiedClassName, ReflectionParameter $parameter, ?callable $with=null)
Definition: Injector.php:89
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 47 of file Injector.php.

References ILIAS\BackgroundTasks\Dependencies\Injector\createConstructorArguments(), and null.

51  : object {
52  // The reflection classes needed.
53  $reflectionClass = new \ReflectionClass($fullyQualifiedClassName);
54  $constructor = $reflectionClass->getConstructor();
55  if ($constructor === null) {
56  return $reflectionClass->newInstance();
57  }
58 
59  $parameters = $constructor->getParameters();
60 
61  // we get the arguments to construct the object from the DIC and Typehinting.
62  $constructorArguments = $this->createConstructorArguments($fullyQualifiedClassName, $parameters, $with);
63 
64  // Crate the instance with the arguments.
65  return $reflectionClass->newInstanceArgs($constructorArguments);
66  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
createConstructorArguments(string $fullyQualifiedClassName, array $parameters, ?callable $with)
Definition: Injector.php:71
+ Here is the call graph for this function:

◆ getDependency()

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

Definition at line 89 of file Injector.php.

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

93  {
94  // These Lines are currently commented while we cant use $parameter->getType() which will be part of PHP7
95  // if (!$parameter->getType()) {
96  // throw new InvalidClassException("The constructor of $fullyQualifiedClassName is not fully type hinted, or the type hints cannot be resolved.");
97  // }
98 
99  // $type = $parameter->getType()->__toString();
100  $type = $parameter->getClass()->getName();
101 
102  // if ($parameter->getType()->isBuiltin()) {
103  // 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.");
104  // }
105 
106  if (!$type) {
107  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.");
108  }
109 
110  if ($with) {
111  return $this->dependencyMap->getDependencyWith($this->dic, $type, $fullyQualifiedClassName, $with);
112  }
113  return $this->dependencyMap->getDependency($this->dic, $type, $fullyQualifiedClassName);
114  }
+ Here is the caller graph for this function:

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