• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Entropyphp / EntropyPHP / 16864124910

10 Aug 2025 04:56PM UTC coverage: 95.258% (-2.3%) from 97.537%
16864124910

push

github

willy68
First commit

66 of 79 new or added lines in 1 file covered. (83.54%)

462 of 485 relevant lines covered (95.26%)

4.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.54
/src/AbstractApplication.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Entropy;
6

7
use DI\ContainerBuilder;
8
use Entropy\Utils\Environnement\Env;
9
use Entropy\Utils\File\FileUtils;
10
use Exception;
11
use Psr\Http\Server\MiddlewareInterface;
12
use GuzzleHttp\Psr7\ServerRequest;
13
use Psr\Container\ContainerInterface;
14
use Entropy\Kernel\KernelInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Throwable;
18

19
/**
20
 * AbstractApplication
21
 */
22
abstract class AbstractApplication implements ApplicationInterface
23
{
24
    public const PROXY_DIRECTORY = '/tmp/proxies';
25
    public const COMPILED_CONTAINER_DIRECTORY = '/tmp/di';
26
    public const CACHE_DIRECTORY = '/tmp/cache';
27
    public static ?ApplicationInterface $app = null;
28
    private ?ContainerInterface $container = null;
29
    private ?KernelInterface $kernel;
30
    private array $config = [];
31
    private array $modules = [];
32
    private array $middlewares = [];
33
    private array $listeners = [];
34
    private ServerRequestInterface $request;
35
    private string $projectDir;
36
    private string $configDir;
37

38
    public function __construct(?KernelInterface $kernel = null)
15✔
39
    {
40
        $this->config[] = __DIR__ . '/config.php';
15✔
41
        $this->projectDir = FileUtils::getRootPath();
15✔
42

43
        self::$app = $this;
15✔
44

45
        $this->kernel = $kernel;
15✔
46
    }
47

48
    public function getProjectDir(): string
2✔
49
    {
50
        return $this->projectDir;
2✔
51
    }
52

53
    /**
54
     *
55
     * @param array $modules
56
     * @return self
57
     */
58
    public function addModules(array $modules): self
1✔
59
    {
60
        foreach ($modules as $module) {
1✔
61
            $this->addModule($module);
1✔
62
        }
63
        return $this;
1✔
64
    }
65

66
    /**
67
     *
68
     * @param string $module
69
     * @return self
70
     */
71
    public function addModule(string $module): self
2✔
72
    {
73
        $this->modules[] = $module;
2✔
74
        return $this;
2✔
75
    }
76

77
    /**
78
     * @param string $listener
79
     * @return $this
80
     */
81
    public function addListener(string $listener): self
1✔
82
    {
83
        $this->listeners[] = $listener;
1✔
84
        return $this;
1✔
85
    }
86

87
    /**
88
     *
89
     * @param array $listeners
90
     * @return self
91
     */
92
    public function addListeners(array $listeners): self
1✔
93
    {
94
        $this->listeners = array_merge($this->listeners, $listeners);
1✔
95
        return $this;
1✔
96
    }
97

98
    public function getListeners(): array
2✔
99
    {
100
        return $this->listeners;
2✔
101
    }
102

103
    /**
104
     * @param string|callable|MiddlewareInterface $middleware
105
     * @return $this
106
     */
107
    public function addMiddleware(string|callable|MiddlewareInterface $middleware): self
1✔
108
    {
109
        $this->middlewares[] = $middleware;
1✔
110
        return $this;
1✔
111
    }
112

113
    /**
114
     *
115
     * @param array $middlewares
116
     * @return self
117
     */
118
    public function addMiddlewares(array $middlewares): self
1✔
119
    {
120
        $this->middlewares = array_merge($this->middlewares, $middlewares);
1✔
121
        return $this;
1✔
122
    }
123

124
    public function getMiddlewares(): array
2✔
125
    {
126
        return $this->middlewares;
2✔
127
    }
128

129
    /**
130
     * @throws Exception
131
     */
132
    public function init(?ServerRequestInterface $request = null): static
2✔
133
    {
134
        if ($request === null) {
2✔
135
            $request = ServerRequest::fromGlobals();
1✔
136
        }
137

138
        /** @var ServerRequestInterface $request */
139
        $this->request = $request->withAttribute(ApplicationInterface::class, $this);
2✔
140

141
        $container = $this->getContainer();
2✔
142

143
        $this->initModules($container);
2✔
144

145
        $this->initKernel($container);
2✔
146

147
        return $this;
2✔
148
    }
149

150
    /**
151
     * Get DI Injection Container
152
     *
153
     * @return ContainerInterface
154
     * @throws Exception
155
     */
156
    public function getContainer(): ContainerInterface
1✔
157
    {
158
        if ($this->container === null) {
1✔
NEW
159
            $builder = new ContainerBuilder();
×
160

NEW
161
            $env = Env::getEnv('APP_ENV', 'prod');
×
NEW
162
            if ($env === 'prod') {
×
NEW
163
                $projectDir = $this->projectDir;
×
NEW
164
                $builder->enableCompilation($projectDir . self::COMPILED_CONTAINER_DIRECTORY);
×
NEW
165
                $builder->writeProxiesToFile(true, $projectDir . self::PROXY_DIRECTORY);
×
166
            }
167

NEW
168
            $builder->addDefinitions($this->getRunTimeDefinitions());
×
NEW
169
            foreach ($this->config as $config) {
×
NEW
170
                $builder->addDefinitions($config);
×
171
            }
172

173
            /** @var Module $module */
NEW
174
            foreach ($this->modules as $module) {
×
NEW
175
                if ($module::DEFINITIONS) {
×
NEW
176
                    $builder->addDefinitions($module::DEFINITIONS);
×
177
                }
178
            }
NEW
179
            $this->container = $builder->build();
×
180
        }
181
        return $this->container;
1✔
182
    }
183

184
    protected function getRunTimeDefinitions(): array
1✔
185
    {
186
        // Get all config file definitions
187
        $config = FileUtils::getFiles($this->getConfigDir(), 'php', '.dist.');
1✔
188
        $this->config = array_merge($this->config, array_keys($config));
1✔
189

190
        return [
1✔
191
            ApplicationInterface::class => $this,
1✔
192
            'app.project.dir' => $this->projectDir,
1✔
193
            'app.cache.dir' => $this->projectDir . self::CACHE_DIRECTORY,
1✔
194
        ];
1✔
195
    }
196

197
    public function getConfigDir(): string
2✔
198
    {
199
        if (!isset($this->configDir)) {
2✔
200
            $this->configDir = $this->projectDir . '/config';
2✔
201
        }
202
        return $this->configDir;
2✔
203
    }
204

205
    abstract public function initModules(ContainerInterface $container): ApplicationInterface;
206

207
    abstract public function initKernel(ContainerInterface $container): ApplicationInterface;
208

209
    /**
210
     *
211
     * @return ResponseInterface
212
     */
213
    public function run(): ResponseInterface
2✔
214
    {
215
        try {
216
            return $this->kernel->handle($this->request);
2✔
217
        } catch (Throwable $e) {
1✔
218
            return $this->kernel->handleException($e, $this->kernel->getRequest());
1✔
219
        }
220
    }
221

222
    /**
223
     * Get the value of the request
224
     *
225
     * @return  ServerRequestInterface
226
     */
227
    public function getRequest(): ServerRequestInterface
2✔
228
    {
229
        return $this->request;
2✔
230
    }
231

232
    /**
233
     * Set the value of the request
234
     *
235
     * @param ServerRequestInterface $request
236
     *
237
     * @return  self
238
     */
239
    public function setRequest(ServerRequestInterface $request): self
2✔
240
    {
241
        $this->request = $request;
2✔
242

243
        return $this;
2✔
244
    }
245

246
    /**
247
     *
248
     * @return KernelInterface|null
249
     */
250
    public function getKernel(): ?KernelInterface
1✔
251
    {
252
        return $this->kernel;
1✔
253
    }
254

255
    /**
256
     *
257
     * @return array
258
     */
259
    public function getModules(): array
2✔
260
    {
261
        return $this->modules;
2✔
262
    }
263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc