Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Application
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
9 / 9
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 registerBaseBindings
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 register
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 bootstrap
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 registerPaths
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 setAppPath
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isBooted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMiddleware
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem;
6
7use Engelsystem\Config\Config;
8use Engelsystem\Container\Container;
9use Engelsystem\Container\ServiceProvider;
10use Illuminate\Container\Container as IlluminateContainer;
11use Illuminate\Contracts\Container\Container as IlluminateContainerContract;
12use Psr\Container\ContainerInterface;
13use Psr\Http\Server\MiddlewareInterface;
14
15class 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     * @param Config|null $config
75     */
76    public function bootstrap(Config $config = null): void
77    {
78        if ($this->isBootstrapped) {
79            return;
80        }
81
82        if ($config instanceof Config) {
83            foreach ($config->get('providers', []) as $provider) {
84                $this->register($provider);
85            }
86
87            $this->middleware = $config->get('middleware', []);
88        }
89
90        foreach ($this->serviceProviders as $provider) {
91            $this->call([$provider, 'boot']);
92        }
93
94        $this->isBootstrapped = true;
95    }
96
97    protected function registerPaths(): void
98    {
99        $appPath = $this->appPath;
100
101        $this->instance('path', $appPath);
102        $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config');
103        $this->instance('path.resources', $appPath . DIRECTORY_SEPARATOR . 'resources');
104        $this->instance('path.resources.api', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'api');
105        $this->instance('path.public', $appPath . DIRECTORY_SEPARATOR . 'public');
106        $this->instance('path.assets', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'assets');
107        $this->instance('path.assets.public', $this->get('path.public') . DIRECTORY_SEPARATOR . 'assets');
108        $this->instance('path.lang', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'lang');
109        $this->instance('path.views', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'views');
110        $this->instance('path.storage', $appPath . DIRECTORY_SEPARATOR . 'storage');
111        $this->instance('path.storage.app', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'app');
112        $this->instance('path.cache', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'cache');
113        $this->instance('path.cache.routes', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'routes.cache.php');
114        $this->instance('path.cache.views', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'views');
115    }
116
117    /**
118     * Set app base path
119     */
120    public function setAppPath(string $appPath): static
121    {
122        $appPath = realpath($appPath);
123        $appPath = rtrim($appPath, DIRECTORY_SEPARATOR);
124
125        $this->appPath = $appPath;
126
127        $this->registerPaths();
128
129        return $this;
130    }
131
132    public function path(): ?string
133    {
134        return $this->appPath;
135    }
136
137    public function isBooted(): bool
138    {
139        return $this->isBootstrapped;
140    }
141
142    /**
143     * @return MiddlewareInterface[]|string[]
144     */
145    public function getMiddleware(): array
146    {
147        return $this->middleware;
148    }
149}