Skip to content

Commit dbda58c

Browse files
Runner class improvements. (#94)
1 parent 42194e0 commit dbda58c

13 files changed

Lines changed: 304 additions & 113 deletions

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
"codeception/codeception": "^4.1",
153153
"codeception/lib-innerbrowser": "^1.3",
154154
"codeception/module-asserts": "^1.0.0",
155+
"codeception/module-cli": "^1.1",
155156
"codeception/module-db": "^1.0",
156157
"codeception/module-phpbrowser": "^1.0.0",
157158
"codeception/module-rest": "^1.2",

config/web/application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use App\NotFoundHandler;
5+
use App\Handler\NotFoundHandler;
66
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
77
use Yiisoft\Injector\Injector;
88
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;

public/index-test.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use App\ApplicationRunner;
5+
use App\Runner\WebApplicationRunner;
66

77
// PHP built-in server routing.
88
if (PHP_SAPI === 'cli-server') {
@@ -21,12 +21,21 @@
2121
require_once $c3;
2222
}
2323

24-
define('YII_ENV', getenv('env') ?: 'test');
25-
2624
require_once dirname(__DIR__) . '/vendor/autoload.php';
2725

28-
$runner = new ApplicationRunner();
29-
// Development mode:
30-
$runner->debug();
31-
// Run application:
26+
/**
27+
* Set debug value for web application runner, for default its `true` add additionally the validation of the
28+
* container-di configurations (debug mode).
29+
*/
30+
define('YII_DEBUG', getenv('YII_DEBUG') ?: true);
31+
32+
/**
33+
* Set environment value for web application runner, for default its `null`.
34+
*
35+
* @link https://github.com/yiisoft/config#environments
36+
*/
37+
define('YII_ENV', getenv('YII_ENV') ?: null);
38+
39+
// Run web application runner
40+
$runner = new WebApplicationRunner(YII_DEBUG, YII_ENV);
3241
$runner->run();

public/index.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use App\ApplicationRunner;
5+
use App\Runner\WebApplicationRunner;
66

77
// PHP built-in server routing.
88
if (PHP_SAPI === 'cli-server') {
@@ -15,12 +15,21 @@
1515
$_SERVER['SCRIPT_NAME'] = '/index.php';
1616
}
1717

18-
define('YII_ENV', getenv('env') ?: 'production');
19-
2018
require_once dirname(__DIR__) . '/vendor/autoload.php';
2119

22-
$runner = new ApplicationRunner();
23-
// Development mode:
24-
$runner->debug();
25-
// Run application:
20+
/**
21+
* Set debug value for web application runner, for default its `true` add additionally the validation of the
22+
* container-di configurations (debug mode).
23+
*/
24+
define('YII_DEBUG', getenv('YII_DEBUG') ?: true);
25+
26+
/**
27+
* Set environment value for web application runner, for default its `null`.
28+
*
29+
* @link https://github.com/yiisoft/config#environments
30+
*/
31+
define('YII_ENV', getenv('YII_ENV') ?: null);
32+
33+
// Run web application runner
34+
$runner = new WebApplicationRunner(YII_DEBUG, YII_ENV);
2635
$runner->run();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App;
5+
namespace App\Handler;
66

77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\ServerRequestInterface;

src/Handler/ThrowableHandler.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Handler;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use Psr\Http\Server\RequestHandlerInterface;
10+
use Throwable;
11+
12+
final class ThrowableHandler implements RequestHandlerInterface
13+
{
14+
private Throwable $throwable;
15+
16+
public function __construct(Throwable $throwable)
17+
{
18+
$this->throwable = $throwable;
19+
}
20+
21+
public function handle(ServerRequestInterface $request): ResponseInterface
22+
{
23+
throw $this->throwable;
24+
}
25+
}

src/Runner/BootstrapRunner.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Runner;
6+
7+
use Psr\Container\ContainerInterface;
8+
use RuntimeException;
9+
10+
use function get_debug_type;
11+
use function is_callable;
12+
13+
final class BootstrapRunner
14+
{
15+
private ContainerInterface $container;
16+
private array $bootstrapList;
17+
18+
public function __construct(ContainerInterface $container, array $bootstrapList = [])
19+
{
20+
$this->container = $container;
21+
$this->bootstrapList = $bootstrapList;
22+
}
23+
24+
public function run(): void
25+
{
26+
foreach ($this->bootstrapList as $callback) {
27+
if (!(is_callable($callback))) {
28+
$type = get_debug_type($callback);
29+
throw new RuntimeException("Bootstrap callback must be callable, $type given.");
30+
}
31+
$callback($this->container);
32+
}
33+
}
34+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Runner;
6+
7+
use Error;
8+
use ErrorException;
9+
use Exception;
10+
use Psr\Container\ContainerInterface;
11+
use Yiisoft\Config\Config;
12+
use Yiisoft\Di\Container;
13+
use Yiisoft\Definitions\Exception\CircularReferenceException;
14+
use Yiisoft\Definitions\Exception\InvalidConfigException;
15+
use Yiisoft\Definitions\Exception\NotFoundException;
16+
use Yiisoft\Definitions\Exception\NotInstantiableException;
17+
use Yiisoft\Yii\Console\Application;
18+
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput;
19+
20+
final class ConsoleApplicationRunner
21+
{
22+
private bool $debug;
23+
private ?string $environment;
24+
25+
public function __construct(bool $debug, ?string $environment)
26+
{
27+
$this->debug = $debug;
28+
$this->environment = $environment;
29+
}
30+
31+
/**
32+
* @throws CircularReferenceException|ErrorException|Exception|InvalidConfigException|NotFoundException
33+
* @throws NotInstantiableException
34+
*/
35+
public function run(): void
36+
{
37+
$config = new Config(
38+
dirname(__DIR__, 2),
39+
'/config/packages', // Configs path.
40+
$this->environment,
41+
[
42+
'params',
43+
'events',
44+
'events-web',
45+
'events-console',
46+
],
47+
);
48+
49+
$container = new Container(
50+
$config->get('console'),
51+
$config->get('providers-console'),
52+
[],
53+
$this->debug,
54+
$config->get('delegates-console')
55+
);
56+
57+
$container = $container->get(ContainerInterface::class);
58+
59+
// Run bootstrap
60+
$this->runBootstrap($container, $config->get('bootstrap-console'));
61+
62+
/** @var Application */
63+
$application = $container->get(Application::class);
64+
$exitCode = 1;
65+
66+
try {
67+
$application->start();
68+
$exitCode = $application->run(null, new ConsoleBufferedOutput());
69+
} catch (Error $error) {
70+
$application->renderThrowable($error, new ConsoleBufferedOutput());
71+
} finally {
72+
$application->shutdown($exitCode);
73+
exit($exitCode);
74+
}
75+
}
76+
77+
private function runBootstrap(ContainerInterface $container, array $bootstrapList): void
78+
{
79+
(new BootstrapRunner($container, $bootstrapList))->run();
80+
}
81+
}

0 commit comments

Comments
 (0)