public function run() { $router = $this->injector->getInstance(Router::class); $controllerName = ucfirst(strtolower($router->getController())) . 'Controller'; try { $controller = $this->injector->getInstance($controllerName); } catch (DependencyInjection_Exception $ex) { exit('Erreur 404'); } catch (Exception $ex) { exit('Erreur 500'); } $actionName = strtolower($router->getAction()) . 'Action'; if (!method_exists($controller, $actionName)) { exit('Erreur 404'); } $controller->{$actionName}(); }
/** * @covers DependencyInjection::getInstance * @todo Implement testGetInstance(). */ public function testGetInstance() { $this->assertTrue($this->object->getInstance(A::class) instanceof A); $this->object->setModule(I::class, B::class); $this->assertTrue($this->object->getInstance(I::class) instanceof B); $this->object->setModule(B::class, C::class); $this->assertTrue($this->object->getInstance(B::class) instanceof C); try { $this->object->getInstance('InexistantClassName'); $this->fail('Expected DependencyInjection_Exception'); } catch (DependencyInjection_Exception $ex) { } $p = $this->object->getInstance(P::class); $this->assertSame($p, $this->object->getInstance(P::class)); try { $this->object->getInstance(NI::class); $this->fail('Expected DependencyInjection_Exception'); } catch (DependencyInjection_Exception $ex) { } $ni = new NI(); $this->object->setInstance($ni); $this->assertSame($ni, $this->object->getInstance(NI::class)); $inject = $this->object->getInstance(Inject::class); $this->assertTrue($inject->i instanceof I); $this->assertTrue($inject->a instanceof A); $withoutFactory = $this->object->getInstance(WithoutCustomFactory::class); $this->assertSame(0, $withoutFactory->a); $this->assertSame(1, $withoutFactory->b); $withFactory = $this->object->getInstance(WithCustomFactory::class); $this->assertSame(5, $withFactory->a); $this->assertSame(3, $withFactory->b); $this->object->setModule(MyAbstractFactory::class, InvalidInterfaceFactory::class); try { $this->object->getInstance(WithAbstractFactory::class); $this->fail('Expected DependencyInjection_Exception'); } catch (DependencyInjection_Exception $ex) { } $this->object->setModule(MyAbstractFactory::class, InvalidImplFactory::class); try { $this->object->getInstance(WithAbstractFactory::class); $this->fail('Expected DependencyInjection_Exception'); } catch (DependencyInjection_Exception $ex) { } $this->object->setModule(MyAbstractFactory::class, ValidFactory::class); $withAbstract = $this->object->getInstance(WithAbstractFactory::class); $this->assertTrue($withAbstract->a instanceof A); $this->assertSame(9, $withAbstract->value); $this->object->setModule(MyAbstractFactory::class, ValidFactory2::class); $withAbstract = $this->object->getInstance(WithAbstractFactory::class); $this->assertTrue($withAbstract->a instanceof A); $this->assertSame(9 * 2, $withAbstract->value); }
public function createInstance(DependencyInjection $injector, ReflectionClass $class) { if (!$class->isInstantiable()) { throw new DependencyInjection_Exception('The class ' . $class->name . ' is not instantiable'); } $constructor = $class->getConstructor(); if ($constructor === null) { //Pas de constructeur => créé tout simplement l'instance return $class->newInstanceWithoutConstructor(); } //S'il y a une constructeur essaye de récupérer les arguments $args = []; foreach ($constructor->getParameters() as $param) { if ($param->getClass() === null) { //Pas de classe définie if ($param->isDefaultValueAvailable()) { //essaye de mettre la valeur par défaut $args[] = $param->getDefaultValue(); } else { if (!$param->allowsNull()) { //Pas de valeur par défaut, essaye de mettre null throw new DependencyInjection_Exception('The parameter ' . $param->name . ' of ' . $class->name . ' constructor don\'t have a default value'); } $args[] = null; } } else { //Une classe est disponible, utilise l'injecteur $args[] = $injector->getInstance($param->getClass()->name); } } //Créé l'instance avec les arguments récupérés return $class->newInstanceArgs($args); }
public function createInstance(\DependencyInjection $injector, \ReflectionClass $class) { $config = $injector->getInstance(Config::class)->database; return new Database($config->dsn, $config->username, $config->password); }