ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Injector.php
Go to the documentation of this file.
1 <?php
2 
20 
25 
32 class Injector
33 {
38  public function __construct(protected Container $dic, protected DependencyMap $dependencyMap)
39  {
40  }
41 
47  public function createInstance(
48  string $fullyQualifiedClassName,
49  bool $requireFile = false,
50  ?callable $with = 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  }
67 
71  protected function createConstructorArguments(
72  string $fullyQualifiedClassName,
73  array $parameters,
74  ?callable $with
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  }
85 
89  protected function getDependency(
90  string $fullyQualifiedClassName,
91  ReflectionParameter $parameter,
92  ?callable $with = null
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  }
115 }
createInstance(string $fullyQualifiedClassName, bool $requireFile=false, ?callable $with=null)
Definition: Injector.php:47
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
createConstructorArguments(string $fullyQualifiedClassName, array $parameters, ?callable $with)
Definition: Injector.php:71
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(protected Container $dic, protected DependencyMap $dependencyMap)
Factory constructor.
Definition: Injector.php:38
getDependency(string $fullyQualifiedClassName, ReflectionParameter $parameter, ?callable $with=null)
Definition: Injector.php:89
$dic
Definition: result.php:31