Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ServiceContextBuilder
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2namespace Apie\Common\ContextBuilders;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\ContextBuilders\ContextBuilderInterface;
6use ReflectionClass;
7use Symfony\Component\DependencyInjection\ServiceLocator;
8
9/**
10 * @TODO lazy initialization
11 *
12 * Adds services from service container in the ApieContext.
13 * Services with the tag 'apie.context' will be added.
14 */
15final class ServiceContextBuilder implements ContextBuilderInterface
16{
17    /**
18     * @param ServiceLocator<mixed> $serviceLocator
19     */
20    public function __construct(private readonly ServiceLocator $serviceLocator)
21    {
22    }
23    
24    public function process(ApieContext $context): ApieContext
25    {
26        foreach (array_keys($this->serviceLocator->getProvidedServices()) as $serviceId) {
27            $service = $this->serviceLocator->get($serviceId);
28            $refl = new ReflectionClass($service);
29            foreach ($refl->getInterfaceNames() as $interfaceName) {
30                if (!$context->hasContext($interfaceName)) {
31                    $context = $context->withContext($interfaceName, $service);
32                }
33            }
34            $context = $context->withContext($serviceId, $service);
35        }
36        
37        return $context;
38    }
39}