Example #1
0
 /**
  * {@inheritdoc}
  */
 public function register()
 {
     $this->getContainer()->share('settings', function () {
         return new Collection($this->defaultSettings);
     });
     $this->getContainer()->share('environment', function () {
         return new Environment($_SERVER);
     });
     $this->getContainer()->share('request', function () {
         return Request::createFromEnvironment($this->getContainer()->get('environment'));
     });
     $this->getContainer()->share('response', function () {
         $headers = new Headers(['Content-Type' => 'text/html']);
         $response = new Response(200, $headers);
         return $response->withProtocolVersion($this->getContainer()->get('settings')['httpVersion']);
     });
     $this->getContainer()->share('router', function () {
         return new Router();
     });
     $this->getContainer()->share('foundHandler', function () {
         return new RequestResponse();
     });
     $this->getContainer()->share('errorHandler', function () {
         return new Error($this->getContainer()->get('settings')['displayErrorDetails']);
     });
     $this->getContainer()->share('notFoundHandler', function () {
         return new NotFound();
     });
     $this->getContainer()->share('notAllowedHandler', function () {
         return new NotAllowed();
     });
     $this->getContainer()->share('callableResolver', function () {
         return new CallableResolver($this->getContainer());
     });
 }
Example #2
0
 /**
  * @param \swoole_http_request $request
  * @param \swoole_http_response $response
  * @throws \Exception
  */
 public function __invoke($request, $response)
 {
     $this->app->getContainer()['environment'] = $this->app->getContainer()->factory(function () {
         return new Environment($_SERVER);
     });
     $this->app->getContainer()['request'] = $this->app->getContainer()->factory(function ($container) {
         return Request::createFromEnvironment($container['environment']);
     });
     $this->app->getContainer()['response'] = $this->app->getContainer()->factory(function ($container) {
         $headers = new Headers(['Content-Type' => 'text/html']);
         $response = new Response(200, $headers);
         return $response->withProtocolVersion($container->get('settings')['httpVersion']);
     });
     /**
      * @var ResponseInterface $appResponse
      */
     $appResponse = $this->app->run(true);
     // set http header
     foreach ($appResponse->getHeaders() as $key => $value) {
         $filter_header = function ($header) {
             $filtered = str_replace('-', ' ', $header);
             $filtered = ucwords($filtered);
             return str_replace(' ', '-', $filtered);
         };
         $name = $filter_header($key);
         foreach ($value as $v) {
             $response->header($name, $v);
         }
     }
     // set http status
     $response->status($appResponse->getStatusCode());
     // send response to browser
     if (!$this->isEmptyResponse($appResponse)) {
         $body = $appResponse->getBody();
         if ($body->isSeekable()) {
             $body->rewind();
         }
         $settings = $this->app->getContainer()->get('settings');
         $chunkSize = $settings['responseChunkSize'];
         $contentLength = $appResponse->getHeaderLine('Content-Length');
         if (!$contentLength) {
             $contentLength = $body->getSize();
         }
         $totalChunks = ceil($contentLength / $chunkSize);
         $lastChunkSize = $contentLength % $chunkSize;
         $currentChunk = 0;
         while (!$body->eof() && $currentChunk < $totalChunks) {
             if (++$currentChunk == $totalChunks && $lastChunkSize > 0) {
                 $chunkSize = $lastChunkSize;
             }
             $response->write($body->read($chunkSize));
             if (connection_status() != CONNECTION_NORMAL) {
                 break;
             }
         }
         $response->end();
     }
 }
 /**
  * Create new container
  *
  * @param array $settings Associative array of settings. User settings are in a 'settings' sub-array
  */
 public function __construct($settings = [])
 {
     $userSettings = [];
     if (isset($settings['settings'])) {
         $userSettings = $settings['settings'];
         unset($settings['settings']);
     }
     // Add settings factory that also collects the default settings
     $defaultSettings = $this->defaultSettings;
     $settings['factories']['settings'] = function ($c) use($userSettings, $defaultSettings) {
         return array_merge($defaultSettings, $userSettings);
     };
     // Add default services if they aren't added already
     if (!isset($settings['environment'])) {
         $settings['factories']['environment'] = function ($c) {
             return new Environment($_SERVER);
         };
     }
     if (!isset($settings['request'])) {
         $settings['factories']['request'] = function ($c) {
             return Request::createFromEnvironment($c['environment']);
         };
     }
     if (!isset($settings['response'])) {
         $settings['factories']['response'] = function ($c) {
             $headers = new Headers(['Content-Type' => 'text/html']);
             $response = new Response(200, $headers);
             return $response->withProtocolVersion($c['settings']['httpVersion']);
         };
     }
     if (!isset($settings['router'])) {
         $settings['factories']['router'] = function ($c) {
             return new Router();
         };
     }
     if (!isset($settings['callableResolver'])) {
         $settings['factories']['callableResolver'] = function ($c) {
             return new CallableResolver($c);
         };
     }
     if (!isset($settings['foundHandler'])) {
         $settings['invokables']['foundHandler'] = RequestResponse::class;
     }
     if (!isset($settings['errorHandler'])) {
         $settings['factories']['errorHandler'] = function ($c) {
             return new Error($c->get('settings')['displayErrorDetails']);
         };
     }
     if (!isset($settings['notFoundHandler'])) {
         $settings['invokables']['notFoundHandler'] = NotFound::class;
     }
     if (!isset($settings['notAllowedHandler'])) {
         $settings['invokables']['notAllowedHandler'] = NotAllowed::class;
     }
     parent::__construct($settings);
 }
Example #4
0
File: App.php Project: zither/memo
 public function __construct(array $userSettings = [])
 {
     parent::__construct();
     $this["settings"] = function ($c) use($userSettings) {
         return array_merge($c->defaultSettings, $userSettings);
     };
     $this["environment"] = function () {
         return new Environment($_SERVER);
     };
     $this["request"] = $this->factory(function ($c) {
         return Request::createFromEnvironment($c["environment"]);
     });
     $this["response"] = $this->factory(function ($c) {
         $headers = new Headers(["Content-Type" => "text/html"]);
         $response = new Response(200, $headers);
         return $response->withProtocolVersion($c["settings"]["httpVersion"]);
     });
     $this["router"] = function ($c) {
         return (new Router())->setContainer($c);
     };
 }
 /**
  * This function registers the default services that Slim needs to work.
  *
  * All services are shared - that is, they are registered such that the
  * same instance is returned on subsequent calls.
  *
  * @param array $userSettings Associative array of application settings
  *
  * @return void
  */
 private function registerDefaultServices($userSettings)
 {
     $defaultSettings = $this->defaultSettings;
     /**
      * This service MUST return an array or an
      * instance of \ArrayAccess.
      *
      * @return array|\ArrayAccess
      */
     $this['settings'] = function () use($userSettings, $defaultSettings) {
         return new Collection(array_merge($defaultSettings, $userSettings));
     };
     if (!isset($this['environment'])) {
         /**
          * This service MUST return a shared instance
          * of \Slim\Interfaces\Http\EnvironmentInterface.
          *
          * @return EnvironmentInterface
          */
         $this['environment'] = function () {
             return new Environment($_SERVER);
         };
     }
     if (!isset($this['request'])) {
         /**
          * PSR-7 Request object
          *
          * @param Container $c
          *
          * @return ServerRequestInterface
          */
         $this['request'] = function ($c) {
             return Request::createFromEnvironment($c->get('environment'));
         };
     }
     if (!isset($this['response'])) {
         /**
          * PSR-7 Response object
          *
          * @param Container $c
          *
          * @return ResponseInterface
          */
         $this['response'] = function ($c) {
             $headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
             $response = new Response(200, $headers);
             return $response->withProtocolVersion($c->get('settings')['httpVersion']);
         };
     }
     if (!isset($this['router'])) {
         /**
          * This service MUST return a SHARED instance
          * of \Slim\Interfaces\RouterInterface.
          *
          * @return RouterInterface
          */
         $this['router'] = function () {
             return new Router();
         };
     }
     if (!isset($this['foundHandler'])) {
         /**
          * This service MUST return a SHARED instance
          * of \Slim\Interfaces\InvocationStrategyInterface.
          *
          * @return InvocationStrategyInterface
          */
         $this['foundHandler'] = function () {
             return new RequestResponse();
         };
     }
     if (!isset($this['errorHandler'])) {
         /**
          * This service MUST return a callable
          * that accepts three arguments:
          *
          * 1. Instance of \Psr\Http\Message\ServerRequestInterface
          * 2. Instance of \Psr\Http\Message\ResponseInterface
          * 3. Instance of \Exception
          *
          * The callable MUST return an instance of
          * \Psr\Http\Message\ResponseInterface.
          *
          * @param Container $c
          *
          * @return callable
          */
         $this['errorHandler'] = function ($c) {
             return new Error($c->get('settings')['displayErrorDetails']);
         };
     }
     if (!isset($this['notFoundHandler'])) {
         /**
          * This service MUST return a callable
          * that accepts two arguments:
          *
          * 1. Instance of \Psr\Http\Message\ServerRequestInterface
          * 2. Instance of \Psr\Http\Message\ResponseInterface
          *
          * The callable MUST return an instance of
          * \Psr\Http\Message\ResponseInterface.
          *
          * @return callable
          */
         $this['notFoundHandler'] = function () {
             return new NotFound();
         };
     }
     if (!isset($this['notAllowedHandler'])) {
         /**
          * This service MUST return a callable
          * that accepts three arguments:
          *
          * 1. Instance of \Psr\Http\Message\ServerRequestInterface
          * 2. Instance of \Psr\Http\Message\ResponseInterface
          * 3. Array of allowed HTTP methods
          *
          * The callable MUST return an instance of
          * \Psr\Http\Message\ResponseInterface.
          *
          * @return callable
          */
         $this['notAllowedHandler'] = function () {
             return new NotAllowed();
         };
     }
     if (!isset($this['callableResolver'])) {
         /**
          * Instance of \Slim\Interfaces\CallableResolverInterface
          *
          * @param Container $c
          *
          * @return CallableResolverInterface
          */
         $this['callableResolver'] = function ($c) {
             return new CallableResolver($c);
         };
     }
 }
Example #6
0
<?php

use DI\Bridge\Slim\CallableResolver;
use DI\Bridge\Slim\ControllerInvoker;
use DI\Container;
use DI\Scope;
use Interop\Container\ContainerInterface;
use Invoker\Invoker;
use Invoker\ParameterResolver\AssociativeArrayResolver;
use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
use Invoker\ParameterResolver\ResolverChain;
use Slim\Http\Headers;
use Slim\Http\Request;
use Slim\Http\Response;
use function DI\factory;
use function DI\get;
use function DI\object;
return ['settings.httpVersion' => '1.1', 'settings.responseChunkSize' => 4096, 'settings.outputBuffering' => 'append', 'settings.determineRouteBeforeAppMiddleware' => false, 'settings.displayErrorDetails' => false, 'settings' => ['httpVersion' => get('settings.httpVersion'), 'responseChunkSize' => get('settings.responseChunkSize'), 'outputBuffering' => get('settings.outputBuffering'), 'determineRouteBeforeAppMiddleware' => get('settings.determineRouteBeforeAppMiddleware'), 'displayErrorDetails' => get('settings.displayErrorDetails')], 'router' => object(Slim\Router::class), 'errorHandler' => object(Slim\Handlers\Error::class)->constructor(get('settings.displayErrorDetails')), 'notFoundHandler' => object(Slim\Handlers\NotFound::class), 'notAllowedHandler' => object(Slim\Handlers\NotAllowed::class), 'environment' => function () {
    return new Slim\Http\Environment($_SERVER);
}, 'request' => factory(function (ContainerInterface $c) {
    return Request::createFromEnvironment($c->get('environment'));
})->scope(Scope::SINGLETON), 'response' => factory(function (ContainerInterface $c) {
    $headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
    $response = new Response(200, $headers);
    return $response->withProtocolVersion($c->get('settings')['httpVersion']);
})->scope(Scope::SINGLETON), 'foundHandler' => object(ControllerInvoker::class)->constructor(get('foundHandler.invoker')), 'foundHandler.invoker' => function (ContainerInterface $c) {
    $resolvers = [new AssociativeArrayResolver(), new TypeHintContainerResolver($c)];
    return new Invoker(new ResolverChain($resolvers), $c);
}, 'callableResolver' => object(CallableResolver::class), ContainerInterface::class => get(Container::class)];
 /**
  * Register Slim's default services.
  *
  * @param Container $container A DI container implementing ArrayAccess and container-interop.
  */
 public function register($container)
 {
     if (!isset($container['environment'])) {
         /**
          * This service MUST return a shared instance
          * of \Slim\Interfaces\Http\EnvironmentInterface.
          *
          * @return EnvironmentInterface
          */
         $container['environment'] = function () {
             return new Environment($_SERVER);
         };
     }
     if (!isset($container['request'])) {
         /**
          * PSR-7 Request object
          *
          * @param Container $container
          *
          * @return ServerRequestInterface
          */
         $container['request'] = function ($container) {
             return Request::createFromEnvironment($container->get('environment'));
         };
     }
     if (!isset($container['response'])) {
         /**
          * PSR-7 Response object
          *
          * @param Container $container
          *
          * @return ResponseInterface
          */
         $container['response'] = function ($container) {
             $headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
             $response = new Response(200, $headers);
             return $response->withProtocolVersion($container->get('settings')['httpVersion']);
         };
     }
     if (!isset($container['router'])) {
         /**
          * This service MUST return a SHARED instance
          * of \Slim\Interfaces\RouterInterface.
          *
          * @param Container $container
          *
          * @return RouterInterface
          */
         $container['router'] = function ($container) {
             $routerCacheFile = false;
             if (isset($container->get('settings')['routerCacheFile'])) {
                 $routerCacheFile = $container->get('settings')['routerCacheFile'];
             }
             return (new Router())->setCacheFile($routerCacheFile);
         };
     }
     if (!isset($container['foundHandler'])) {
         /**
          * This service MUST return a SHARED instance
          * of \Slim\Interfaces\InvocationStrategyInterface.
          *
          * @return InvocationStrategyInterface
          */
         $container['foundHandler'] = function () {
             return new RequestResponse();
         };
     }
     if (!isset($container['phpErrorHandler'])) {
         /**
          * This service MUST return a callable
          * that accepts three arguments:
          *
          * 1. Instance of \Psr\Http\Message\ServerRequestInterface
          * 2. Instance of \Psr\Http\Message\ResponseInterface
          * 3. Instance of \Error
          *
          * The callable MUST return an instance of
          * \Psr\Http\Message\ResponseInterface.
          *
          * @param Container $container
          *
          * @return callable
          */
         $container['phpErrorHandler'] = function ($container) {
             return new PhpError($container->get('settings')['displayErrorDetails']);
         };
     }
     if (!isset($container['errorHandler'])) {
         /**
          * This service MUST return a callable
          * that accepts three arguments:
          *
          * 1. Instance of \Psr\Http\Message\ServerRequestInterface
          * 2. Instance of \Psr\Http\Message\ResponseInterface
          * 3. Instance of \Exception
          *
          * The callable MUST return an instance of
          * \Psr\Http\Message\ResponseInterface.
          *
          * @param Container $container
          *
          * @return callable
          */
         $container['errorHandler'] = function ($container) {
             return new Error($container->get('settings')['displayErrorDetails']);
         };
     }
     if (!isset($container['notFoundHandler'])) {
         /**
          * This service MUST return a callable
          * that accepts two arguments:
          *
          * 1. Instance of \Psr\Http\Message\ServerRequestInterface
          * 2. Instance of \Psr\Http\Message\ResponseInterface
          *
          * The callable MUST return an instance of
          * \Psr\Http\Message\ResponseInterface.
          *
          * @return callable
          */
         $container['notFoundHandler'] = function () {
             return new NotFound();
         };
     }
     if (!isset($container['notAllowedHandler'])) {
         /**
          * This service MUST return a callable
          * that accepts three arguments:
          *
          * 1. Instance of \Psr\Http\Message\ServerRequestInterface
          * 2. Instance of \Psr\Http\Message\ResponseInterface
          * 3. Array of allowed HTTP methods
          *
          * The callable MUST return an instance of
          * \Psr\Http\Message\ResponseInterface.
          *
          * @return callable
          */
         $container['notAllowedHandler'] = function () {
             return new NotAllowed();
         };
     }
     if (!isset($container['callableResolver'])) {
         /**
          * Instance of \Slim\Interfaces\CallableResolverInterface
          *
          * @param Container $container
          *
          * @return CallableResolverInterface
          */
         $container['callableResolver'] = function ($container) {
             return new CallableResolver($container);
         };
     }
 }
 /**
  * Create new container
  *
  * @param array $userSettings Associative array of application settings
  */
 public function __construct(array $userSettings = [])
 {
     parent::__construct();
     $defaultSettings = $this->defaultSettings;
     /**
      * This service MUST return an array or an
      * instance of \ArrayAccess.
      *
      * @param Container $c
      *
      * @return array|\ArrayAccess
      */
     $this->setFactory('settings', function ($c) use($userSettings, $defaultSettings) {
         return array_merge($defaultSettings, $userSettings);
     });
     /**
      * This service MUST return a shared instance
      * of \Slim\Interfaces\Http\EnvironmentInterface.
      *
      * @param Container $c
      *
      * @return EnvironmentInterface
      */
     $this->setFactory('environment', function ($c) {
         return new Environment($_SERVER);
     });
     /**
      * \Psr\Http\Message\ServerRequestInterface.
      */
     $this->setFactory('request', function ($c) {
         return Request::createFromEnvironment($c['environment']);
     });
     /**
      * \Psr\Http\Message\ResponseInterface.
      */
     $this->setFactory('response', function ($c) {
         $headers = new Headers(['Content-Type' => 'text/html']);
         $response = new Response(200, $headers);
         return $response->withProtocolVersion($c['settings']['httpVersion']);
     });
     /**
      * This service MUST return a SHARED instance
      * of \Slim\Interfaces\RouterInterface.
      *
      * @param Container $c
      *
      * @return RouterInterface
      */
     $this->setFactory('router', function ($c) {
         return new Router();
     });
     /**
      * This service MUST return a SHARED instance
      * of \Slim\Interfaces\InvocationStrategyInterface.
      *
      * @param Container $c
      *
      * @return InvocationStrategyInterface
      */
     $this->setFactory('foundHandler', function ($c) {
         return new RequestResponse();
     });
     /**
      * This service MUST return a callable
      * that accepts three arguments:
      *
      * 1. Instance of \Psr\Http\Message\ServerRequestInterface
      * 2. Instance of \Psr\Http\Message\ResponseInterface
      * 3. Instance of \Exception
      *
      * The callable MUST return an instance of
      * \Psr\Http\Message\ResponseInterface.
      *
      * @param Container $c
      *
      * @return callable
      */
     $this->setFactory('errorHandler', function ($c) {
         return new Error();
     });
     /**
      * This service MUST return a callable
      * that accepts two arguments:
      *
      * 1. Instance of \Psr\Http\Message\ServerRequestInterface
      * 2. Instance of \Psr\Http\Message\ResponseInterface
      *
      * The callable MUST return an instance of
      * \Psr\Http\Message\ResponseInterface.
      *
      * @param Container $c
      *
      * @return callable
      */
     $this->setFactory('notFoundHandler', function ($c) {
         return new NotFound();
     });
     /**
      * This service MUST return a callable
      * that accepts three arguments:
      *
      * 1. Instance of \Psr\Http\Message\ServerRequestInterface
      * 2. Instance of \Psr\Http\Message\ResponseInterface
      * 3. Array of allowed HTTP methods
      *
      * The callable MUST return an instance of
      * \Psr\Http\Message\ResponseInterface.
      *
      * @param Container $c
      *
      * @return callable
      */
     $this->setFactory('notAllowedHandler', function ($c) {
         return new NotAllowed();
     });
     /**
      * \Slim\Interfaces\CallableResolverInterface
      */
     $this->setFactory('callableResolver', function ($c) {
         return new CallableResolver($c);
     });
 }
 /**
  * Get definitions for Slim's default services
  *
  * @param array $userSettings
  *
  * @throws \InvalidArgumentException
  *
  * @return callable[]
  */
 private static function getDefaultServicesDefinitions(array $userSettings)
 {
     $defaultSettings = self::$defaultSettings;
     return ['settings' => function () use($defaultSettings, $userSettings) {
         return new Collection(array_merge($defaultSettings, $userSettings));
     }, 'environment' => function () {
         return new Environment($_SERVER);
     }, 'request' => function (ContainerInterface $container) {
         return Request::createFromEnvironment($container->get('environment'));
     }, 'response' => function (ContainerInterface $container) {
         $headers = new Headers(['Content-Type' => 'text/html; charset=utf-8']);
         $response = new Response(200, $headers);
         return $response->withProtocolVersion($container->get('settings')['httpVersion']);
     }, 'router' => function (ContainerInterface $container) {
         $routerCacheFile = false;
         if (isset($container->get('settings')['routerCacheFile'])) {
             $routerCacheFile = $container->get('settings')['routerCacheFile'];
         }
         return (new Router())->setCacheFile($routerCacheFile);
     }, 'foundHandler' => function () {
         return new RequestResponse();
     }, 'phpErrorHandler' => function (ContainerInterface $container) {
         return new PhpError($container->get('settings')['displayErrorDetails']);
     }, 'errorHandler' => function (ContainerInterface $container) {
         return new Error($container->get('settings')['displayErrorDetails']);
     }, 'notFoundHandler' => function () {
         return new NotFound();
     }, 'notAllowedHandler' => function () {
         return new NotAllowed();
     }, 'callableResolver' => function (ContainerInterface $container) {
         return new CallableResolver($container);
     }];
 }
Example #10
0
use Slim\Router;
use Slim\Handlers\Error;
return ['settings' => function (ContainerInterface $container) {
    $config = $container->get(ConfigRepository::class);
    return $config->get('slim');
}, 'environment' => function () {
    return new Environment($_SERVER);
}, 'request' => function (ContainerInterface $container) {
    $environment = $container->get('environment');
    return Request::createFromEnvironment($environment);
}, 'response' => function (ContainerInterface $container) {
    $config = $container->get(ConfigRepository::class);
    $slimConfig = $config->get('slim');
    $headers = new Headers(['Content-Type' => 'text/html']);
    $response = new Response(200, $headers);
    return $response->withProtocolVersion($slimConfig['httpVersion']);
}, 'router' => function () {
    return new Router();
}, 'foundHandler' => function () {
    return new RequestResponse();
}, 'errorHandler' => function (ContainerInterface $container) {
    $config = $container->get(ConfigRepository::class);
    $slimConfig = $config->get('slim');
    return new Error($slimConfig['displayErrorDetails']);
}, 'notFoundHandler' => function () {
    return new NotFound();
}, 'notAllowedHandler' => function () {
    return new NotAllowed();
}, 'callableResolver' => function (ContainerInterface $container) {
    return new CallableResolver($container);
}];