Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
51 / 51 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| Application | |
100.00% |
51 / 51 |
|
100.00% |
9 / 9 |
16 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| registerBaseBindings | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| register | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| bootstrap | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| registerPaths | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| setAppPath | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| path | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isBooted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMiddleware | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem; |
| 6 | |
| 7 | use Engelsystem\Config\Config; |
| 8 | use Engelsystem\Container\Container; |
| 9 | use Engelsystem\Container\ServiceProvider; |
| 10 | use Illuminate\Container\Container as IlluminateContainer; |
| 11 | use Illuminate\Contracts\Container\Container as IlluminateContainerContract; |
| 12 | use Psr\Container\ContainerInterface; |
| 13 | use Psr\Http\Server\MiddlewareInterface; |
| 14 | |
| 15 | class Application extends Container |
| 16 | { |
| 17 | protected ?string $appPath = null; |
| 18 | |
| 19 | protected bool $isBootstrapped = false; |
| 20 | |
| 21 | /** @var MiddlewareInterface[]|string[] */ |
| 22 | protected array $middleware; |
| 23 | |
| 24 | /** |
| 25 | * Registered service providers |
| 26 | */ |
| 27 | protected array $serviceProviders = []; |
| 28 | |
| 29 | /** |
| 30 | * Application constructor. |
| 31 | */ |
| 32 | public function __construct(?string $appPath = null) |
| 33 | { |
| 34 | if (!is_null($appPath)) { |
| 35 | $this->setAppPath($appPath); |
| 36 | } |
| 37 | |
| 38 | $this->registerBaseBindings(); |
| 39 | } |
| 40 | |
| 41 | protected function registerBaseBindings(): void |
| 42 | { |
| 43 | static::setInstance($this); |
| 44 | Container::setInstance($this); |
| 45 | $this->instance('app', $this); |
| 46 | $this->instance('container', $this); |
| 47 | $this->instance(Container::class, $this); |
| 48 | $this->instance(Application::class, $this); |
| 49 | $this->instance(IlluminateContainer::class, $this); |
| 50 | $this->instance(IlluminateContainerContract::class, $this); |
| 51 | $this->bind(ContainerInterface::class, self::class); |
| 52 | } |
| 53 | |
| 54 | public function register(string|ServiceProvider $provider): ServiceProvider |
| 55 | { |
| 56 | if (is_string($provider)) { |
| 57 | $provider = $this->make($provider); |
| 58 | } |
| 59 | |
| 60 | $this->serviceProviders[] = $provider; |
| 61 | |
| 62 | $provider->register(); |
| 63 | |
| 64 | if ($this->isBootstrapped) { |
| 65 | $this->call([$provider, 'boot']); |
| 66 | } |
| 67 | |
| 68 | return $provider; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Boot service providers |
| 73 | * |
| 74 | */ |
| 75 | public function bootstrap(?Config $config = null): void |
| 76 | { |
| 77 | if ($this->isBootstrapped) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if ($config instanceof Config) { |
| 82 | foreach ($config->get('providers', []) as $provider) { |
| 83 | $this->register($provider); |
| 84 | } |
| 85 | |
| 86 | $this->middleware = $config->get('middleware', []); |
| 87 | } |
| 88 | |
| 89 | foreach ($this->serviceProviders as $provider) { |
| 90 | $this->call([$provider, 'boot']); |
| 91 | } |
| 92 | |
| 93 | $this->isBootstrapped = true; |
| 94 | } |
| 95 | |
| 96 | protected function registerPaths(): void |
| 97 | { |
| 98 | $appPath = $this->appPath; |
| 99 | |
| 100 | $this->instance('path', $appPath); |
| 101 | $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config'); |
| 102 | $this->instance('path.resources', $appPath . DIRECTORY_SEPARATOR . 'resources'); |
| 103 | $this->instance('path.resources.api', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'api'); |
| 104 | $this->instance('path.public', $appPath . DIRECTORY_SEPARATOR . 'public'); |
| 105 | $this->instance('path.assets', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'assets'); |
| 106 | $this->instance('path.assets.public', $this->get('path.public') . DIRECTORY_SEPARATOR . 'assets'); |
| 107 | $this->instance('path.lang', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'lang'); |
| 108 | $this->instance('path.views', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'views'); |
| 109 | $this->instance('path.storage', $appPath . DIRECTORY_SEPARATOR . 'storage'); |
| 110 | $this->instance('path.storage.app', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'app'); |
| 111 | $this->instance('path.cache', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'cache'); |
| 112 | $this->instance('path.cache.routes', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'routes.cache.php'); |
| 113 | $this->instance('path.cache.views', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'views'); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set app base path |
| 118 | */ |
| 119 | public function setAppPath(string $appPath): static |
| 120 | { |
| 121 | $appPath = realpath($appPath); |
| 122 | $appPath = rtrim($appPath, DIRECTORY_SEPARATOR); |
| 123 | |
| 124 | $this->appPath = $appPath; |
| 125 | |
| 126 | $this->registerPaths(); |
| 127 | |
| 128 | return $this; |
| 129 | } |
| 130 | |
| 131 | public function path(): ?string |
| 132 | { |
| 133 | return $this->appPath; |
| 134 | } |
| 135 | |
| 136 | public function isBooted(): bool |
| 137 | { |
| 138 | return $this->isBootstrapped; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return MiddlewareInterface[]|string[] |
| 143 | */ |
| 144 | public function getMiddleware(): array |
| 145 | { |
| 146 | return $this->middleware; |
| 147 | } |
| 148 | } |