File size: 5,677 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php

// phpcs:disable PSR1.Classes
declare(strict_types=1);

use Aikeedo\Integrity\SystemIntegrityManager;
use Easy\Container\Container;
use Easy\Container\Exceptions\NotFoundException;
use Laminas\Diactoros\ServerRequestFactory;
use Psr\Container\ContainerInterface;
use Shared\Infrastructure\BootstrapperInterface;
use Shared\Infrastructure\ServiceProviderInterface;

class Application
{
    /** @var array<ServiceProviderInterface|string> */
    private array $providers = [];

    /** @var array<BootstrapperInterface|string> */
    private array $bootstrappers = [];

    private static ContainerInterface $staticContainer;

    /**
     * @template T
     * @param string|class-string<T> $id
     * @return ($id is class-string<T> ? T : mixed)
     */
    public static function make(string $id): mixed
    {
        return self::$staticContainer->get($id);
    }

    /**
     * @param Container $container
     * @return void
     */
    public function __construct(
        private Container $container,
        private bool $isDebugModeEnabled = false,
    ) {
        $this->configErrorReporting();
        $this->setDefaultTimezone();
        $this->container->set(Application::class, $this);

        self::$staticContainer = $container;
    }

    /**
     * @param (ServiceProviderInterface|string)[] ...$providers
     * @return Application
     */
    public function addServiceProvider(
        ServiceProviderInterface|string ...$providers
    ): self {
        $this->providers = array_merge($this->providers, $providers);
        return $this;
    }

    /**
     * @param (BootstrapperInterface|string)[] ...$bootstrappers
     * @return Application
     */
    public function addBootstrapper(
        BootstrapperInterface|string ...$bootstrappers
    ): self {
        $this->bootstrappers = array_merge($this->bootstrappers, $bootstrappers);
        return $this;
    }

    /**
     * @return void
     * @throws NotFoundException
     * @throws Throwable
     * @throws Exception
     */
    public function boot(): void
    {
        $this->invokeServiceProviders();
        $this->invokeBootstrappers();

        try {
            $this->integrity();
        } catch (Throwable) {
        }
    }

    /**
     * This is a mirror of Container::set(). The purpose of this method is to
     * decouple the ServiceProviderInterface implementation from the
     * ContainerInterface implementation.
     *
     * @param string $abstract
     * @param mixed $concrete
     * @return Application
     */
    public function set(
        string $abstract,
        mixed $concrete = null
    ): self {
        $this->container->set($abstract, $concrete);
        return $this;
    }

    /**
     * @return void
     * @throws NotFoundException
     * @throws Throwable
     * @throws Exception
     */
    private function invokeServiceProviders(): void
    {
        foreach ($this->providers as $provider) {
            if (is_string($provider)) {
                $provider = $this->container->get($provider);
            }

            if (!($provider instanceof ServiceProviderInterface)) {
                throw new \Exception(sprintf(
                    "%s must implement %s",
                    get_class($provider),
                    ServiceProviderInterface::class
                ));
            }

            $provider->register($this);
        }
    }

    /**
     * @return void
     * @throws NotFoundException
     * @throws Throwable
     * @throws Exception
     */
    private function invokeBootstrappers(): void
    {
        foreach ($this->bootstrappers as $bootstrapper) {
            if (is_string($bootstrapper)) {
                $bootstrapper = $this->container->get($bootstrapper);
            }

            if (!($bootstrapper instanceof BootstrapperInterface)) {
                throw new \Exception(sprintf(
                    "%s must implement %s",
                    get_class($bootstrapper),
                    BootstrapperInterface::class
                ));
            }

            $bootstrapper->bootstrap();
        }
    }

    private function integrity(): void
    {
        if (PHP_SAPI === 'cli' || env('ENVIRONMENT', 'install') === 'install') {
            return;
        }

        $iam = $this->container->get(SystemIntegrityManager::class);
        $iam->audit(
            ServerRequestFactory::fromGlobals()->getUri()->getHost(),
            $this->container->get('version'),
            $this->container->get('license')
        );
    }

    /**
     * Configure error reporting
     * @return void
     */
    private function configErrorReporting(): void
    {
        // Report all errors
        error_reporting(E_ALL);

        // Display error only if debug mode is enabled
        ini_set('display_errors', $this->isDebugModeEnabled);

        set_error_handler($this->warningHandler(...), E_WARNING);
    }

    /**
     * @param int $errno
     * @param string $errstr
     * @param null|string $errfile
     * @param null|int $errline
     * @return false
     * @throws ErrorException
     */
    private function warningHandler(
        int $errno,
        string $errstr,
        ?string $errfile = null,
        ?int $errline = null
    ) {
        if (!(error_reporting() & $errno)) {
            // This error code is not included in error_reporting, so let it fall
            // through to the standard PHP error handler
            return false;
        }

        throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
    }

    /** @return void  */
    private function setDefaultTimezone(): void
    {
        date_default_timezone_set('UTC');
    }
}