-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ApplicationRunner and delete src/functions.php
- Loading branch information
Showing
6 changed files
with
137 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ checks: | |
filter: | ||
paths: | ||
- "src/*" | ||
excluded_paths: | ||
- "src/ApplicationRunner.php" | ||
|
||
build: | ||
nodes: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Psr\Log\NullLogger; | ||
use Throwable; | ||
use Yiisoft\Composer\Config\Builder; | ||
use Yiisoft\Di\Container; | ||
use Yiisoft\ErrorHandler\ErrorHandler; | ||
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher; | ||
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer; | ||
use Yiisoft\Files\FileHelper; | ||
use Yiisoft\Http\Method; | ||
use Yiisoft\Yii\Web\Application; | ||
use Yiisoft\Yii\Web\SapiEmitter; | ||
use Yiisoft\Yii\Web\ServerRequestFactory; | ||
|
||
use function dirname; | ||
use function microtime; | ||
|
||
final class ApplicationRunner | ||
{ | ||
private bool $debug = false; | ||
|
||
public function debug(bool $enable = true): void | ||
{ | ||
$this->debug = $enable; | ||
} | ||
|
||
public function run(): void | ||
{ | ||
$startTime = microtime(true); | ||
// Register temporary error handler to catch error while container is building. | ||
$errorHandler = new ErrorHandler(new NullLogger(), new HtmlRenderer()); | ||
$this->registerErrorHandler($errorHandler); | ||
|
||
if ($this->debug && $this->shouldRebuildConfigs()) { | ||
Builder::rebuild(); | ||
} | ||
|
||
$container = new Container( | ||
require Builder::path('web'), | ||
require Builder::path('providers-web') | ||
); | ||
|
||
// Register error handler with real container-configured dependencies. | ||
$this->registerErrorHandler($container->get(ErrorHandler::class), $errorHandler); | ||
|
||
$container = $container->get(ContainerInterface::class); | ||
$application = $container->get(Application::class); | ||
|
||
$request = $container->get(ServerRequestFactory::class)->createFromGlobals(); | ||
$request = $request->withAttribute('applicationStartTime', $startTime); | ||
|
||
try { | ||
$application->start(); | ||
$response = $application->handle($request); | ||
$this->emit($request, $response); | ||
} catch (Throwable $throwable) { | ||
$handler = $this->createThrowableHandler($throwable); | ||
$response = $container->get(ErrorCatcher::class)->process($request, $handler); | ||
$this->emit($request, $response); | ||
} finally { | ||
$application->afterEmit($response ?? null); | ||
$application->shutdown(); | ||
} | ||
} | ||
|
||
private function emit(RequestInterface $request, ResponseInterface $response): void | ||
{ | ||
(new SapiEmitter())->emit($response, $request->getMethod() === Method::HEAD); | ||
} | ||
|
||
private function createThrowableHandler(Throwable $throwable): RequestHandlerInterface | ||
{ | ||
return new class($throwable) implements RequestHandlerInterface { | ||
private Throwable $throwable; | ||
|
||
public function __construct(Throwable $throwable) | ||
{ | ||
$this->throwable = $throwable; | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
throw $this->throwable; | ||
} | ||
}; | ||
} | ||
|
||
private function registerErrorHandler(ErrorHandler $registered, ErrorHandler $unregistered = null): void | ||
{ | ||
if ($unregistered !== null) { | ||
$unregistered->unregister(); | ||
} | ||
|
||
if ($this->debug) { | ||
$registered->debug(); | ||
} | ||
|
||
$registered->register(); | ||
} | ||
|
||
private function shouldRebuildConfigs(): bool | ||
{ | ||
$sourceDirectory = dirname(__DIR__) . '/config/'; | ||
$buildDirectory = dirname(__DIR__) . '/runtime/build/config/'; | ||
|
||
if (FileHelper::isEmptyDirectory($buildDirectory)) { | ||
return true; | ||
} | ||
|
||
$sourceTime = FileHelper::lastModifiedTime($sourceDirectory); | ||
$buildTime = FileHelper::lastModifiedTime($buildDirectory); | ||
return $buildTime < $sourceTime; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.