Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.61% covered (warning)
86.61%
110 / 127
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApieServiceProvider
86.61% covered (warning)
86.61%
110 / 127
25.00% covered (danger)
25.00%
1 / 4
28.75
0.00% covered (danger)
0.00%
0 / 1
 autoTagHashmapActions
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
4.00
 boot
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
4
 register
84.34% covered (warning)
84.34%
70 / 83
0.00% covered (danger)
0.00%
0 / 1
19.24
 sanitizeConfig
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\LaravelApie;
3
4use Apie\AiInstructor\AiInstructorServiceProvider;
5use Apie\ApieCommonPlugin\ApieCommonPluginServiceProvider;
6use Apie\ApieFileSystem\ApieFileSystemServiceProvider;
7use Apie\CmsApiDropdownOption\CmsDropdownServiceProvider;
8use Apie\Common\AddBasicAuthServiceProvider;
9use Apie\Common\CommonServiceProvider;
10use Apie\Common\ContextBuilders\FrameworkContextBuilder;
11use Apie\Common\Events\AddAuthenticationCookie;
12use Apie\Common\Interfaces\BoundedContextSelection;
13use Apie\Common\Interfaces\DashboardContentFactoryInterface;
14use Apie\Common\Wrappers\BoundedContextHashmapFactory;
15use Apie\Common\Wrappers\ConsoleCommandFactory as CommonConsoleCommandFactory;
16use Apie\Console\ConsoleServiceProvider;
17use Apie\Core\CoreServiceProvider;
18use Apie\Core\Session\CsrfTokenProvider;
19use Apie\DoctrineEntityConverter\DoctrineEntityConverterProvider;
20use Apie\DoctrineEntityDatalayer\Commands\ApieUpdateIdfCommand;
21use Apie\DoctrineEntityDatalayer\DoctrineEntityDatalayerServiceProvider;
22use Apie\DoctrineEntityDatalayer\EntityReindexer;
23use Apie\DoctrineEntityDatalayer\IndexStrategy\BackgroundIndexStrategy;
24use Apie\DoctrineEntityDatalayer\IndexStrategy\DirectIndexStrategy;
25use Apie\DoctrineEntityDatalayer\IndexStrategy\IndexAfterResponseIsSentStrategy;
26use Apie\DoctrineEntityDatalayer\IndexStrategy\IndexStrategyInterface;
27use Apie\Export\ExportServiceProvider;
28use Apie\Faker\FakerServiceProvider;
29use Apie\FtpServer\FtpServerServiceProvider;
30use Apie\Graphql\GraphqlServiceProvider;
31use Apie\HtmlBuilders\ErrorHandler\CmsErrorRenderer;
32use Apie\HtmlBuilders\HtmlBuilderServiceProvider;
33use Apie\LaravelApie\Config\LaravelConfiguration;
34use Apie\LaravelApie\Config\ValidateAndSanitizeConfig;
35use Apie\LaravelApie\ContextBuilders\ApieCurrentUserContextBuilder;
36use Apie\LaravelApie\ContextBuilders\CsrfTokenContextBuilder;
37use Apie\LaravelApie\ContextBuilders\RegisterBoundedContextActionContextBuilder;
38use Apie\LaravelApie\ContextBuilders\SessionContextBuilder;
39use Apie\LaravelApie\ErrorHandler\ApieErrorRenderer;
40use Apie\LaravelApie\ErrorHandler\Handler;
41use Apie\LaravelApie\Providers\CmsServiceProvider;
42use Apie\LaravelApie\Providers\SecurityServiceProvider;
43use Apie\LaravelApie\Wrappers\Cms\DashboardContentFactory;
44use Apie\LaravelApie\Wrappers\Core\BoundedContextSelected;
45use Apie\LaravelApie\Wrappers\Queue\BackgroundProcessPersistListener;
46use Apie\Maker\MakerServiceProvider;
47use Apie\McpServer\McpServerServiceProvider;
48use Apie\RestApi\RestApiServiceProvider;
49use Apie\SchemaGenerator\SchemaGeneratorServiceProvider;
50use Apie\Serializer\SerializerServiceProvider;
51use Apie\ServiceProviderGenerator\TagMap;
52use Apie\TypescriptClientBuilder\TypescriptClientBuilderServiceProvider;
53use Apie\Webdav\WebdavServiceProvider;
54use Illuminate\Config\Repository;
55use Illuminate\Contracts\Debug\ExceptionHandler;
56use Illuminate\Contracts\Events\Dispatcher;
57use Illuminate\Cookie\Middleware\EncryptCookies;
58use Illuminate\Support\ServiceProvider;
59use Psr\EventDispatcher\EventDispatcherInterface;
60use Psr\Http\Message\ServerRequestInterface;
61use Symfony\Component\Config\ConfigCache;
62use Symfony\Component\Config\Definition\Processor;
63use Symfony\Component\Config\Resource\ReflectionClassResource;
64use Symfony\Component\Console\Application;
65use Symfony\Component\EventDispatcher\EventDispatcher;
66use Symfony\Component\Lock\LockFactory;
67
68class ApieServiceProvider extends ServiceProvider
69{
70    /**
71     * @var array<string, class-string<ServiceProvider>> $alreadyRegistered
72     */
73    private array $alreadyRegistered = [];
74    /**
75     * @var array<string, array<int, class-string<ServiceProvider>>> $dependencies
76     */
77    private array $dependencies = [
78        'enable_ai_instructor' => [
79            AiInstructorServiceProvider::class,
80        ],
81        'enable_basic_auth' => [
82            AddBasicAuthServiceProvider::class,
83        ],
84        'enable_common_plugin' => [
85            ApieCommonPluginServiceProvider::class,
86        ],
87        'enable_cms' => [
88            CommonServiceProvider::class,
89            HtmlBuilderServiceProvider::class, // it's important that this loads before CmsServiceProvider!!!
90            CmsServiceProvider::class,
91            SerializerServiceProvider::class,
92        ],
93        'enable_cms_dropdown' => [
94            CommonServiceProvider::class,
95            CmsDropdownServiceProvider::class,
96        ],
97        'enable_core' => [
98            CoreServiceProvider::class,
99        ],
100        'enable_console' => [
101            CommonServiceProvider::class,
102            ConsoleServiceProvider::class, // it's important that this loads after CommonServiceProvider!!!
103            SerializerServiceProvider::class,
104        ],
105        'enable_doctrine_entity_converter' => [
106            CoreServiceProvider::class,
107            DoctrineEntityConverterProvider::class,
108        ],
109        'enable_doctrine_entity_datalayer' => [
110            CoreServiceProvider::class,
111            DoctrineEntityConverterProvider::class,
112            DoctrineEntityDatalayerServiceProvider::class,
113        ],
114        'enable_export' => [
115            SerializerServiceProvider::class,
116            ExportServiceProvider::class,
117        ],
118        'enable_security' => [
119            CommonServiceProvider::class,
120            SerializerServiceProvider::class,
121            SecurityServiceProvider::class,
122        ],
123        'enable_rest_api' => [
124            CommonServiceProvider::class,
125            RestApiServiceProvider::class,
126            SchemaGeneratorServiceProvider::class,
127            SerializerServiceProvider::class,
128        ],
129        'enable_faker' => [
130            FakerServiceProvider::class,
131        ],
132        'enable_ftp' => [
133            ApieFileSystemServiceProvider::class,
134            FtpServerServiceProvider::class,
135        ],
136        'enable_graphql' => [
137            CommonServiceProvider::class,
138            SerializerServiceProvider::class,
139            GraphqlServiceProvider::class,
140        ],
141        'enable_maker' => [
142            MakerServiceProvider::class,
143        ],
144        'enable_mcp_server' => [
145            CommonServiceProvider::class,
146            SerializerServiceProvider::class,
147            McpServerServiceProvider::class,
148        ],
149        'enable_typescript_client_builder' => [
150            TypescriptClientBuilderServiceProvider::class,
151        ],
152        'enable_webdav' => [
153            ApieFileSystemServiceProvider::class,
154            WebdavServiceProvider::class,
155        ]
156    ];
157
158    private function autoTagHashmapActions(): void
159    {
160        $boundedContextConfig = config('apie.bounded_contexts');
161        $scanBoundedContextConfig = config('apie.scan_bounded_contexts');
162        $factory = new BoundedContextHashmapFactory(
163            $boundedContextConfig ?? [],
164            $scanBoundedContextConfig ?? [],
165            new EventDispatcher(),
166        );
167        $hashmap = $factory->create();
168        foreach ($hashmap as $boundedContext) {
169            foreach ($boundedContext->actions as $action) {
170                $class = $action->getDeclaringClass();
171                if (!$class->isInstantiable()) {
172                    continue;
173                }
174                $className = $class->name;
175                TagMap::register(
176                    $this->app,
177                    $className,
178                    ['apie.context']
179                );
180            }
181        }
182    }
183
184    public function boot(): void
185    {
186        $this->autoTagHashmapActions();
187        $this->loadViewsFrom(__DIR__ . '/../templates', 'apie');
188        $this->loadRoutesFrom(__DIR__.'/../resources/routes.php');
189
190        if ($this->app->runningInConsole()) {
191            $commands = [];
192            $commands[] = ApieUpdateIdfCommand::class;
193            // for some reason these are not called in integration tests without re-registering them
194            foreach (TagMap::getServiceIdsWithTag($this->app, 'console.command') as $taggedCommand) {
195                $serviceId = 'apie.console.tagged.' . $taggedCommand;
196                $this->app->singleton($serviceId, function () use ($taggedCommand) {
197                    return $this->app->get($taggedCommand);
198                });
199                $commands[] = $serviceId;
200            }
201            /** @var CommonConsoleCommandFactory $factory */
202            $factory = $this->app->get('apie.console.factory');
203            foreach ($factory->create($this->app->get(Application::class)) as $command) {
204                $serviceId = 'apie.console.registered.' . $command->getName();
205                $this->app->instance($serviceId, $command);
206                $commands[] = $serviceId;
207            }
208            $this->commands($commands);
209        }
210
211        $this->app->booted(function () {
212            TagMap::markBooted($this->app);
213            TagMap::registerEvents($this->app);
214        });
215    }
216
217    public function register()
218    {
219        EncryptCookies::except(AddAuthenticationCookie::COOKIE_NAME);
220        $this->mergeConfigFrom(__DIR__ . '/../resources/apie.php', 'apie');
221
222        $this->app->bind(FrameworkContextBuilder::class, function () {
223            return new FrameworkContextBuilder('laravel');
224        });
225        TagMap::register($this->app, FrameworkContextBuilder::class, ['apie.core.context_builder']);
226
227        // add PSR-14 support if needed:
228        if (!$this->app->bound(EventDispatcherInterface::class)) {
229            $this->app->bind(EventDispatcherInterface::class, function () {
230                return new class($this->app->make(Dispatcher::class)) implements EventDispatcherInterface {
231                    public function __construct(private readonly Dispatcher $dispatcher)
232                    {
233                    }
234
235                    public function dispatch(object $event): object
236                    {
237                        $this->dispatcher->dispatch($event);
238                        return $event;
239                    }
240                };
241            });
242        }
243
244        // fix for https://github.com/laravel/framework/issues/30415
245        $this->app->extend(
246            ServerRequestInterface::class,
247            function (ServerRequestInterface $psrRequest) {
248                $route = $this->app->make('request')->route();
249                if ($route) {
250                    $parameters = $route->parameters();
251                    foreach ($parameters as $key => $value) {
252                        $psrRequest = $psrRequest->withAttribute($key, $value);
253                    }
254                }
255                return $psrRequest;
256            }
257        );
258
259        $this->app->bind(IndexStrategyInterface::class, function () {
260            $config = config();
261            if ($config->get('apie.enable_doctrine_entity_datalayer')) {
262                $type = $config->get('apie.doctrine.indexing.type', 'direct');
263                return match ($type) {
264                    'direct' => new DirectIndexStrategy($this->app->get(EntityReindexer::class)),
265                    'late' => new IndexAfterResponseIsSentStrategy($this->app->get(EntityReindexer::class)),
266                    'background' => new BackgroundIndexStrategy(),
267                    default => $this->app->get(config('apie.doctrine.indexing.service', DirectIndexStrategy::class)),
268                };
269            }
270
271            return new DirectIndexStrategy($this->app->get(EntityReindexer::class));
272        });
273
274        $this->app->bind(ApieErrorRenderer::class, function () {
275            return new ApieErrorRenderer(
276                $this->app->bound(CmsErrorRenderer::class) ? $this->app->make(CmsErrorRenderer::class) : null,
277                $this->app->make(\Apie\Common\ErrorHandler\ApiErrorRenderer::class),
278                config('apie.cms.base_url')
279            );
280        });
281
282        $this->app->extend(ExceptionHandler::class, function (ExceptionHandler $service) {
283            return new Handler($this->app, $service);
284        });
285
286        $this->app->bind(LockFactory::class, function () {
287            $config = config('apie.lock_store');
288            return new LockFactory($this->app->get($config));
289        });
290        
291        $this->app->bind(DashboardContentFactoryInterface::class, DashboardContentFactory::class);
292        $this->app->bind(BoundedContextSelection::class, BoundedContextSelected::class);
293
294        $this->alreadyRegistered = [];
295        $parsedConfig = ValidateAndSanitizeConfig::process(config('apie'));
296        foreach ($this->dependencies as $configKey => $dependencies) {
297            if ($parsedConfig[$configKey] ?? false) {
298                foreach ($dependencies as $dependency) {
299                    if (!isset($this->alreadyRegistered[$dependency])) {
300                        $this->alreadyRegistered[$dependency] = $dependency;
301                        $this->app->register($dependency);
302                    }
303                }
304            }
305        }
306
307        //$this->app->bind(CsrfTokenProvider::class, CsrfTokenContextBuilder::class);
308        TagMap::register($this->app, CsrfTokenContextBuilder::class, ['apie.core.context_builder']);
309        $this->app->tag(CsrfTokenContextBuilder::class, ['apie.core.context_builder']);
310
311        // this has to be added after CsrfTokenContextBuilder!
312        $this->app->bind(SessionContextBuilder::class);
313        TagMap::register($this->app, SessionContextBuilder::class, ['apie.core.context_builder']);
314        $this->app->tag(SessionContextBuilder::class, ['apie.core.context_builder']);
315
316        $this->app->bind(ApieCurrentUserContextBuilder::class);
317        TagMap::register($this->app, ApieCurrentUserContextBuilder::class, ['apie.core.context_builder', 'kernel.event_subscriber']);
318        $this->app->tag(ApieCurrentUserContextBuilder::class, ['apie.core.context_builder', 'kernel.event_subscriber']);
319
320        TagMap::register($this->app, RegisterBoundedContextActionContextBuilder::class, ['apie.core.context_builder']);
321        $this->app->tag(RegisterBoundedContextActionContextBuilder::class, ['apie.core.context_builder']);
322        $this->app->resolving('config', function (Repository $config) {
323            $this->sanitizeConfig($config);
324            $newParsedConfig = $config->get('apie');
325            foreach ($this->dependencies as $configKey => $dependencies) {
326                if ($newParsedConfig[$configKey] ?? false) {
327                    foreach ($dependencies as $dependency) {
328                        if (!isset($this->alreadyRegistered[$dependency])) {
329                            $this->alreadyRegistered[$dependency] = $dependency;
330                            $this->app->register($dependency);
331                        }
332                    }
333                }
334            }
335            return $config;
336        });
337
338        TagMap::register($this->app, BackgroundProcessPersistListener::class, ['kernel.event_subscriber']);
339    }
340
341    private function sanitizeConfig(Repository $config): void
342    {
343        $rawConfig = $config->get('apie');
344        $processedConfig = ValidateAndSanitizeConfig::process($rawConfig);
345
346        $config->set('apie', $processedConfig);
347    }
348}