ILIAS  release_8 Revision v8.24
Injector.php
Go to the documentation of this file.
1<?php
2
20
24use ReflectionParameter;
25
33{
34 protected \ILIAS\DI\Container $dic;
35 protected \ILIAS\BackgroundTasks\Dependencies\DependencyMap\DependencyMap $dependencyMap;
36
42 {
43 $this->dic = $dic;
44 $this->dependencyMap = $dependencyMap;
45 }
46
52 public function createInstance(
53 string $fullyQualifiedClassName,
54 bool $requireFile = false,
55 callable $with = null
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 }
72
76 protected function createConstructorArguments(
77 string $fullyQualifiedClassName,
78 array $parameters,
79 ?callable $with
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 }
90
94 protected function getDependency(
95 string $fullyQualifiedClassName,
96 ReflectionParameter $parameter,
97 ?callable $with = null
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 }
121}
ILIAS BackgroundTasks Dependencies DependencyMap DependencyMap $dependencyMap
Definition: Injector.php:35
createConstructorArguments(string $fullyQualifiedClassName, array $parameters, ?callable $with)
Definition: Injector.php:76
getDependency(string $fullyQualifiedClassName, ReflectionParameter $parameter, ?callable $with=null)
Definition: Injector.php:94
__construct(Container $dic, DependencyMap $dependencyMap)
Factory constructor.
Definition: Injector.php:41
createInstance(string $fullyQualifiedClassName, bool $requireFile=false, callable $with=null)
Definition: Injector.php:52
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$type