/** * Retrieves the handler for a specified command * * @param string $commandName * * @throws MissingHandlerException * @return object */ public function getHandlerForCommand($commandName) { $handler = $this->getHandlerClassName($commandName); if (!class_exists($handler)) { throw MissingHandlerException::forCommand($commandName); } return $this->container->make($handler); }
public function test_cannot_dispatch_when_method_does_not_exist() { $this->setExpectedException(NotFoundHttpException::class); $controller = new ControllerStub(); $this->container->shouldReceive('make')->with('Mosaic\\Tests\\Routing\\Dispatchers\\ControllerStub')->once()->andReturn($controller); $route = new Route(['GET'], '/', ['uses' => 'Mosaic\\Tests\\Routing\\Dispatchers\\ControllerStub@nonExisting']); $this->dispatcher->dispatch($route); }
/** * Resolve the middleware stack from the laravel container * @param array $middleware * @param Container $container * @return array */ protected function resolveMiddleware(array $middleware = [], Container $container) { $m = []; foreach ($middleware as $class) { $m[] = $container->make($class); } return $m; }
public function test_can_resolve_method_parameters_with_typehints_and_route_parameters() { $this->container->shouldReceive('make')->with(Container::class)->once()->andReturn($this->container); $parameters = $this->resolver->resolve(new \ReflectionMethod(ControllerStubWithParams::class, 'index'), ['id' => 1]); $this->assertArrayHasKey('container', $parameters); $this->assertArrayHasKey('id', $parameters); $this->assertEquals($this->container, $parameters['container']); $this->assertEquals(1, $parameters['id']); }
/** * @param Application $app */ public function bootstrap(Application $app) { foreach ($app->getRegistry()->getDefinitions() as $abstract => $concrete) { if (is_string($concrete) || is_callable($concrete)) { $this->container->bind($abstract, $concrete); } else { $this->container->instance($abstract, $concrete); } } }
/** * @param Router $router * * @return mixed */ public function loadRoutes(Router $router) { $binders = $this->config->get($this->app->getContext() . '.routes', []); foreach ($binders as $binder) { if (!class_exists($binder)) { throw new InvalidArgumentException('RouteBinder [' . $binder . '] does not exist'); } $this->container->make($binder)->bind($router); } }
/** * Dispatch the request * * @param Route $route * * @throws NotFoundHttpException * @return mixed */ public function dispatch(Route $route) { $action = $route->action(); list($class, $method) = explode('@', $action['uses']); if (!method_exists($instance = $this->container->make($class), $method)) { throw new NotFoundHttpException(); } $parameters = $this->resolver->resolve(new ReflectionMethod($instance, $method), $route->parameters()); return $this->container->call([$instance, $method], $parameters); }
/** * @param ReflectionFunctionAbstract $reflector * @param array $parameters * * @return mixed */ public function resolve(ReflectionFunctionAbstract $reflector, array $parameters = []) : array { $reflected = $reflector->getParameters(); $resolved = []; foreach ($reflected as $param) { if ($class = $param->getClass()) { $resolved[$param->name] = $this->container->make($class->name); } elseif (isset($parameters[$param->name])) { $resolved[$param->name] = $parameters[$param->name]; } } return $resolved; }
/** * Define a container implementation * * @param ContainerDefinition $definition * * @return Container */ public function defineContainer(ContainerDefinition $definition) : Container { $this->container = $definition->getDefinition(); $this->container->instance(Container::class, $this->container); $this->container->instance(ApplicationContract::class, $this); return $this->container; }
public function test_cannot_get_a_service_that_was_not_bound() { $abstract = 'abstract'; $this->wrappedMock->shouldReceive('bound')->with($abstract)->once()->andReturn(false); $this->setExpectedException(ContainerException::class); $this->wrappedMock->shouldReceive('make')->with($abstract, [])->never(); $this->container->get($abstract); }