/**
  * @param ContainerInterface $app
  * @throws \InvalidArgumentException
  */
 public function register(ContainerInterface $app)
 {
     $app->singleton([Logger::class => 'Logger'], function () use($app) {
         $logger = new Logger($app['Config']['logger.name']);
         if (!$app['Config']->offsetExists('logger.handler')) {
             throw new \InvalidArgumentException('expected at least one logger handler in array(class, array config) format');
         }
         /**
          * register logger handler
          */
         foreach ($app['Config']['logger.handler'] as $class => $config) {
             $handler = (new \ReflectionClass($class))->newInstanceArgs($config['args']);
             if (!$handler instanceof HandlerInterface) {
                 throw new \InvalidArgumentException(sprintf('logger handler must implement %s', HandlerInterface::class));
             }
             $formatter = (new \ReflectionClass(key($config['formatter'])))->newInstance($config['formatter'][key($config['formatter'])]);
             if (!$formatter instanceof FormatterInterface) {
                 throw new InvalidArgumentException(sprintf('logger formatter must implement %s', FormatterInterface::class));
             }
             $handler->setFormatter($formatter);
             $logger->pushHandler($handler);
         }
         return $logger;
     });
     $app->bind(LoggerInterface::class, 'Logger');
 }
 /**
  * @param ContainerInterface $app
  */
 public function register(ContainerInterface $app)
 {
     $this->app = $app;
     $app->singleton('MiddlewareResolver', function () {
         return [$this, 'middlewareResolver'];
     });
     $app->singleton([RelayBuilder::class => 'RelayBuilder'], function () use($app) {
         return new RelayBuilder($app['MiddlewareResolver']);
     });
 }
 public function it_should_able_handle_exception(ContainerInterface $container, ResponseInterface $response, StreamInterface $stream, ServerResponderInterface $serverResponder, ConfigurationInterface $config, StreamInterface $stream, LoggerInterface $logger, \Exception $e)
 {
     $container->offsetGet('Response')->willReturn($response);
     $container->offsetGet('ServerResponder')->willReturn($serverResponder);
     $container->offsetGet('Logger')->willReturn($logger);
     $container->offsetGet('Config')->willReturn($config);
     $response->getBody()->willReturn($stream);
     $response->withHeader('Content-Type', 'text/html')->willReturn($response);
     $response->withStatus('500')->willReturn($response);
     $this->handleException($e);
 }
 public function it_should_handle_class_when_route_is_match(ServerRequestInterface $request, UriInterface $uri, ResponseInterface $response, GroupCountBased $dispatcher, ContainerInterface $container)
 {
     $request->getMethod()->willReturn('GET');
     $request->getUri()->willReturn($uri);
     $uri->getPath()->willReturn('/');
     $dispatcher->dispatch('GET', '/')->willReturn([Dispatcher::FOUND, 'Controller', []]);
     $request->withAttribute('param', [])->willReturn($request);
     $container->offsetGet('Controller')->willReturn(new Controller());
     $this->__invoke($request, $response, function ($request, $response) {
         return $response;
     })->shouldReturn($response);
 }
 public function it_should_able_send_output_to_client(ContainerInterface $container, ResponseInterface $response, \swoole_http_response $swoole_res, ConfigurationInterface $config)
 {
     $response->getHeaders()->willReturn(['Content-Type' => ['text/html']]);
     $container->offsetGet('SwooleResponder')->willReturn($swoole_res);
     $swoole_res->header('Content-Type', 'text/html')->shouldBeCalled();
     $container->offsetGet('Config')->willReturn($config);
     $config->offsetExists('server.gzip')->willReturn(true);
     $config->offsetGet('server.gzip')->willReturn(3);
     $swoole_res->gzip(3)->shouldBeCalled();
     $swoole_res->status(200)->shouldBeCalled();
     $swoole_res->header('Server', 'vinnige-app-server')->shouldBeCalled();
     $response->getBody()->willReturn('hello world');
     $response->getStatusCode()->willReturn(200);
     $swoole_res->end('hello world')->shouldBeCalled();
     $this->send($response);
 }
 private function registerEventDispatcherProvider()
 {
     $this->app->singleton([EventDispatcher::class => 'EventDispatcher'], function () {
         return new EventDispatcher();
     });
     $this->app->bind(EventDispatcherInterface::class, 'EventDispatcher');
 }
Example #7
0
 /**
  * @return bool
  * @throws \RuntimeException
  */
 public function isServerExist()
 {
     if (!$this->container->offsetExists('Server')) {
         throw new \RuntimeException('server must be set in container');
     }
     return true;
 }
 /**
  * @param ContainerInterface $application
  * @throws \InvalidArgumentException
  */
 public function register(ContainerInterface $application)
 {
     $this->app = $application;
     /**
      * register whoops when debug mode enabled
      */
     $this->app->singleton([ErrorHandler::class => 'ErrorHandler'], function () {
         if ($this->errorHandler instanceof PrettyPageHandler) {
             $this->errorHandler->handleUnconditionally(true);
         }
         $this->whoops = new Run();
         $this->whoops->allowQuit(false);
         $this->whoops->pushHandler($this->errorHandler);
         return new ErrorHandler($this->app, $this->whoops, $this->contentType);
     });
     /**
      * catch fatal error
      */
     register_shutdown_function([$this->app['ErrorHandler'], 'handleError']);
 }
 /**
  * @param ContainerInterface $app
  */
 public function register(ContainerInterface $app)
 {
     $app->singleton([RouteParser::class => 'RouteParser']);
     $app->singleton([DataGenerator::class => 'DataGenerator']);
     $app->singleton([RouteCollector::class => 'RouteCollector']);
     /**
      * contextual binding
      */
     $app->bind(RouteParserInterface::class, 'RouteParser');
     $app->bind(DataGeneratorInterface::class, 'DataGenerator');
     /**
      * register routing dispatcher
      */
     $app->singleton([RouteDispatcher::class => 'RouteDispatcher'], function () use($app) {
         return new RouteDispatcher($app['RouteCollector']->getData());
     });
 }
 public function it_should_throw_exception_when_given_invalid_middleware_dispatcher(\swoole_http_request $swoole_req, \swoole_http_response $swoole_res, ServerRequestInterface $request, ResponseInterface $response, RelayBuilder $relayBuilder, ContainerInterface $container)
 {
     unset($GLOBALS['_COOKIE']);
     unset($GLOBALS['_GET']);
     unset($GLOBALS['_POST']);
     unset($GLOBALS['_FILES']);
     $container->offsetSet('SwooleResponder', $swoole_res)->shouldBeCalled();
     $container->offsetGet('Request')->willReturn($request);
     $container->offsetGet('Response')->willReturn($response);
     $container->offsetGet('RelayBuilder')->willReturn($relayBuilder);
     $container->offsetGet('Middlewares')->willReturn([]);
     $relayBuilder->newInstance([])->willReturn(null);
     $this->shouldThrow('\\RuntimeException')->duringHandleRequest($swoole_req, $swoole_res);
 }
 /**
  * @param ContainerInterface $app
  */
 public function register(ContainerInterface $app)
 {
     /**
      * swoole server
      */
     $app->singleton('Server', function () use($app) {
         return new Server($app);
     });
     /**
      * swoole server request handler
      */
     $app->singleton('ServerRequestHandler', function () use($app) {
         return new ServerRequestHandler($app, $app['EventDispatcher']);
     });
     /**
      * swoole server responder
      */
     $app->singleton('ServerResponder', function () use($app) {
         return new ServerResponder($app);
     });
     $app->bind(ServerInterface::class, 'Server');
     $app->bind(ServerRequestHandlerInterface::class, 'ServerRequestHandler');
     $app->bind(ServerResponderInterface::class, 'ServerResponder');
 }
Example #12
0
 public function it_should_able_to_run(ContainerInterface $container, RouteCollector $collector, ServerInterface $server, EventDispatcherInterface $event)
 {
     /**
      * predictions
      */
     $container->offsetSet('Middlewares', [])->shouldBeCalled();
     $container->offsetSet('ServiceProviders', [])->shouldBeCalled();
     $container->offsetSet('Routes', [])->shouldBeCalled();
     $collector->addRoute('GET', '/', 'test')->shouldBeCalled();
     $server->run()->shouldBeCalled();
     /**
      * mocks
      */
     $container->offsetGet('Routes')->willReturn([['method' => 'GET', 'route' => '/', 'handler' => 'test']]);
     $container->offsetGet('Server')->willReturn($server);
     $container->offsetGet('RouteCollector')->willReturn($collector);
     $container->offsetGet('EventDispatcher')->willReturn($event);
     /**
      * test method
      */
     $this->run();
 }