/** * @inheritDoc */ public function resolveArguments(ReflectionFunctionAbstract $function) : Closure { $parameters = $function->getParameters(); return function (array $arguments = []) use($function, $parameters) { // Use passed arguments in place of reflected parameters. $provided = array_intersect_key($arguments, $parameters); // Remaining parameters will be resolved by container. $remaining = array_diff_key($parameters, $arguments); $resolved = array_map(function (ReflectionParameter $parameter) use($function) { // Recursively resolve function arguments. $class = $parameter->getClass(); if ($class !== null && $this->container->has($class->getName())) { return $this->container->get($class->getName()); } // Use argument default value if possible. if ($parameter->isDefaultValueAvailable()) { return $parameter->getDefaultValue(); } // The argument can't be resolved by this resolver. throw new ArgumentResolverException($parameter, $function); }, $remaining); $arguments = $provided + $resolved; // Sort combined result array by parameter indexes. ksort($arguments); return $arguments; }; }
/** * Saving Application instance for later use * * @param Container $container * @return void */ public function provideServices(Container $container) { $this->container = $container; /* * Creating and registering our error handler */ $whoops = new Run(); $whoops->register(); /** @var \Venta\Contracts\Kernel\Kernel $kernel */ $kernel = $container->get(Kernel::class); if ($kernel->isCli()) { // We don't need pretty pages in cli mode $whoops->allowQuit(true); $whoops->sendHttpCode(false); $whoops->writeToOutput(true); $whoops->pushHandler(new PlainTextHandler()); } else { // Push pretty page handler only for local environment $whoops->pushHandler($kernel->getEnvironment() === 'local' ? new PrettyPageHandler() : new PlainTextHandler()); } /** * Bind error handler */ $container->bindClass(RunInterface::class, $whoops, ['error_handler']); /* * Bind PSR-3 logger */ $container->share(\Monolog\Logger::class, function (Container $c) { $logger = new \Monolog\Logger('venta'); $handler = new \Monolog\Handler\StreamHandler(__DIR__ . '/../storage/logs/app.log'); $handler->pushProcessor(function ($record) use($c) { /** @var \Venta\Contracts\Kernel\Kernel $kernel */ $kernel = $c->get(Kernel::class); if ($kernel->isCli()) { // Add cli command related extra info /** @var \Symfony\Component\Console\Input\InputInterface $input */ $input = $c->get(InputInterface::class); $record['extra']['command'] = $input->getFirstArgument(); $record['extra']['arguments'] = $input->getArguments(); $record['extra']['options'] = $input->getOptions(); } else { // Add HTTP request related extra info /** @var \Psr\Http\Message\ServerRequestInterface $request */ $request = $c->get(ServerRequestInterface::class); $server = $request->getServerParams(); $record['extra']['url'] = $request->getUri()->getPath(); $record['extra']['http_method'] = $request->getMethod(); $record['extra']['host'] = $request->getUri()->getHost(); $record['extra']['referer'] = $request->getHeader('referer'); $record['extra']['user_agent'] = $request->getHeader('user-agent'); $record['extra']['ip'] = $server['REMOTE_ADDR'] ?? null; } return $record; }); $handler->setFormatter(new \Monolog\Formatter\LineFormatter()); $logger->pushHandler($handler); return $logger; }, ['logger', LoggerInterface::class]); }
function it_calls_responder_only_when_domain_is_not_callable(Container $container, Route $route, Request $request, Response $response, Responder $responder) { $route->getDomain()->willReturn(''); $route->getResponder()->willReturn(Responder::class); $container->isCallable('')->willReturn(false); $container->get(Responder::class)->willReturn($responder); $responder->run($request, null)->willReturn($response); $this->next($request)->shouldBe($response); }
public function handle(InputInterface $input, OutputInterface $output) { /** @var $em EventDispatcher */ $em = $this->container->get(EventDispatcher::class); /** @var $mailer \Venta\Mail\Mailer */ $mailer = $this->container->get('mailer'); $transport = $input->getOption('transport'); $mailer->spoolWithTransport($transport); $em->trigger($mailer::SPOOL_SEND_EVENT); }
/** * @inheritDoc */ public function run(ServerRequestInterface $request) { /** @var MiddlewarePipelineFactory $factory */ $factory = $this->container->get(MiddlewarePipelineFactory::class); $pipeline = $factory->create($this->container->get(Config::class)->get('middlewares', [])); /** @var Router $router */ $router = $this->container->get(Router::class); $response = $pipeline->process($request, $router); /** @var EmitterContract $emitter */ $emitter = $this->container->get(EmitterContract::class); $emitter->emit($response); }
/** * @inheritDoc */ public final function run(InputInterface $input = null, OutputInterface $output = null) { /* |-------------------------------------------------------------------------- | Bind input |-------------------------------------------------------------------------- | | Rebind input instance, if passed as argument */ if ($input) { $this->container->bindInstance(InputInterface::class, $input); } /* |-------------------------------------------------------------------------- | Bind output |-------------------------------------------------------------------------- | | Rebind output instance, if passed as argument */ if ($output) { $this->container->bindInstance(OutputInterface::class, $output); } /* |-------------------------------------------------------------------------- | Add commands |-------------------------------------------------------------------------- | | Add collected commands to application */ /** @var \Venta\Contracts\Console\CommandCollector $collector */ $collector = $this->container->get(CommandCollector::class); $this->addCommands($collector->getCommands()); /* |-------------------------------------------------------------------------- | Run application |-------------------------------------------------------------------------- | | Run console application using bound Input and Output instances */ parent::run($this->container->get(InputInterface::class), $this->container->get(OutputInterface::class)); }
/** * @inheritDoc */ public function next(ServerRequestInterface $request) : ResponseInterface { if (!$request instanceof RequestContract) { // Decorate PSR-7 ServerRequest. $request = new Request($request); } if ($this->container->isCallable($this->route->getDomain())) { if ($this->container->has($this->route->getInput())) { /** @var Input $input */ $input = $this->container->get($this->route->getInput()); $arguments = $input->process($request); } /** @var Payload $payload */ $payload = $this->container->call($this->route->getDomain(), $arguments ?? []); } /** @var Responder $responder */ $responder = $this->container->get($this->route->getResponder()); return $responder->run($request, $payload ?? null); }
function it_creates_pipeline_from_array(Container $container, Middleware $middleware) { $container->get('middleware')->willReturn($middleware); $this->create(['middleware'])->shouldBeLike((new MiddlewarePipeline())->withMiddleware($middleware->getWrappedObject())); }
function it_creates_route_dispatcher(Route $route, Container $container) { $this->create($route)->shouldBeLike(new RouteDispatcher($route->getWrappedObject(), $container->getWrappedObject())); }