/** * @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(); } }
/** * @param String $method * @param Uri $uri * @param array $post */ protected function runApp($method, $uri, $post = []) { $this->buildApp(); $this->buildRequest($method, $uri, $post); $this->app->getContainer()['request'] = $this->request; $this->app->getContainer()['response'] = $this->response; $this->response = $this->app->run(true); $this->response->getBody()->rewind(); $this->html = $this->response->getBody()->getContents(); }
private function getResponseToDispatch(ErrorController $errorController, LoggerInterface $logger) : ResponseInterface { try { if (!$this->hasRoutesConfigured()) { throw new CannotRunWithoutRoutes(); } return $this->slimApp->run(true); } catch (\Exception $exception) { return $this->generateErrorResponse($errorController, $exception, $logger); } }
/** * @return ListenerInterface[] */ public function getActionListeners() { $routeDispatcher = function () { $postTypeSettings = $this->app->getContainer()->get('postTypes'); foreach ($postTypeSettings as $postType => $settings) { $this->setRouteForPostTypes($postType, $settings['defaultController']); } $this->app->run(); }; return [$this->factory->make('ActionListener', ['names' => 'template_include', 'callable' => $routeDispatcher, 'priority' => 99])]; }
/** * @inheritdoc */ public function run($appNamespace = self::ROOT_NAMESPACE) { // // Each application must exist inside its own namespace. Chubby uses that namespace to search for modules. $this->appNamespace = $appNamespace; // // Slim can be initiated in one of two ways: // 1. Without a container. Slim will create the default container. // 2. Receiving a container in the constructor. We can pass Slim some settings and services // by passing a pre-created container. We do this here via a configuration file. $container = $this->getContainerConfig(); $this->slim = new \Slim\App($container); $container = $this->slim->getContainer(); // $this->modules = \Chubby\PackageLoader::loadModules($container); if (!is_array($this->modules) || !count($this->modules)) { throw new \Exception("Chubby Framework requires at least one module."); } // // Initialize the modules following the order given by each module's priority. foreach ($this->modules as $priority => $modules) { foreach ($modules as $module) { $module['object']->setApp($this); $module['object']->init(); } } // $this->slim->run(); return $this; }
/** * * The constructor is called in index.php and sets up the app * */ public function __construct() { // we need to change the session_save_path // due to issues with how data is handled on AWS EC2 instances $dir = sys_get_temp_dir(); session_save_path($dir); //session_cache_limiter(false); //start the session session_start(); //get settings and instantiate app $this->app = new App(AppConfig::$slimSettings); //configure app $this->setUpDatabase(); $this->setUpRoutes(); //run all the apps! $this->app->run(); }
public static function Route() { $app = new App(); // Reminder: the request is processed from the bottom up, // the response is processed from the top down. $app->add(SlimMiddleware::class); $app->add(Grover::class); $app->run(); }
public function bootstrap() { // Load configuration files into ConfigRepository $config = new ConfigRepository(base_path() . '/config'); // Initialize container for dependency injection $container = $this->initContainer($config); // Instantiate Slim $slim = new Slim($container); // Load routes into Slim router $this->initRoutes($config, $container, $slim->router); // Run app $slim->run(); }
protected function dispatchApplication(array $server, array $pipe = []) { $app = new App(); $app->getContainer()['environment'] = function () use($server) { return new Environment($server); }; $middlewareFactory = new PhpDebugBarMiddlewareFactory(); $middleware = $middlewareFactory(); $app->add($middleware); foreach ($pipe as $pattern => $middleware) { $app->get($pattern, $middleware); } return $app->run(true); }
public function testMiddlewareIsWorkingAndEditorIsSet() { $app = new App(['settings' => ['debug' => true, 'whoops.editor' => 'sublime']]); $container = $app->getContainer(); $container['environment'] = function () { return Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET']); }; $app->get('/foo', function ($req, $res, $args) { return $res; }); $app->add(new WhoopsMiddleware()); // Invoke app $response = $app->run(); // Get added whoops handlers $handlers = $container['whoops']->getHandlers(); $this->assertEquals(2, count($handlers)); $this->assertEquals('subl://open?url=file://test_path&line=169', $handlers[0]->getEditorHref('test_path', 169)); }
/** * Integration test IpRestrictMiddleware::_invoke() when the given IP is allowed. */ public function testIpAllow() { // Prepare the Request and the application. $app = new App(); // Setup a demo environment $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.2']); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $headers->set('Accept', 'text/html'); $cookies = []; $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); $app->getContainer()['request'] = $req; $app->getContainer()['response'] = $res; // Set the options value. $this->options = ['error_code' => 403, 'exception_message' => 'NOT ALLOWED']; $app->add(new IpRestrictMiddleware($this->ipSet, false, $this->options)); $appMessage = 'I am In'; $app->get('/foo', function ($req, $res) use($appMessage) { $res->write($appMessage); return $res; }); $resOut = $app->run(); $body = (string) $resOut->getBody(); $this->assertEquals($appMessage, $body, 'The client is allowed to access the application.'); }
/** * @return \Psr\Http\Message\ResponseInterface */ public function run() { return $this->slimApp->run(); }
<?php require_once dirname(dirname(__FILE__)) . '/vendor/autoload.php'; use Slim\App; use Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware; $app = new App(['settings' => ['debug' => true, 'whoops.editor' => 'sublime']]); $app->add(new WhoopsMiddleware()); // Throw exception, Named route does not exist for name: hello $app->get('/', function ($request, $response, $args) { return $this->router->pathFor('hello'); }); // $app->get('/hello', function($request, $response, $args) { // $response->write("Hello Slim"); // return $response; // })->setName('hello'); $app->run();
/** * Start the application. */ public function run() { $this->app->run(); }
/** * @runInSeparateProcess */ public function testExceptionErrorHandlerDisplaysErrorDetails() { $app = new App(['settings' => ['displayErrorDetails' => true]]); // Prepare request and response objects $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET']); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); $app->getContainer()['request'] = $req; $app->getContainer()['response'] = $res; $mw = function ($req, $res, $next) { throw new \Exception('middleware exception'); }; $app->add($mw); $app->get('/foo', function ($req, $res) { return $res; }); $resOut = $app->run(); $this->assertEquals(500, $resOut->getStatusCode()); $this->expectOutputRegex('/.*middleware exception.*/'); }
/** * Run application */ public static function run() { self::$app->run(); }
public function run() { $this->slim->run(); }
public function run($silent = false) { $this->onStar(); parent::run($silent); $this->onFinish(); }
/** * Run application. * * Initialize the Charcoal application before running (with SlimApp). * * @uses self::setup() * @param boolean $silent If true, will run in silent mode (no response). * @return ResponseInterface The PSR7 HTTP response. */ public function run($silent = false) { $this->setup(); return parent::run($silent); }
<?php require_once "vendor/autoload.php"; use App\Lib\Helpers\Config; use App\Lib\Helpers\RouteLoader; use Slim\App; use Slim\Container; $api = new App(new Container(Config::get('app.boot'))); // Here you can add all the middleware $api->add(new RKA\Middleware\IpAddress()); $api->add(new \CorsSlim\CorsSlim()); $routes = RouteLoader::load(); foreach ($routes as $route) { require_once $route; } $api->run();