DependencyContainerFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 24
c 0
b 0
f 0
dl 0
loc 77
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 7 2
A __construct() 0 3 1
A container() 0 7 2
A loadApplicationServices() 0 7 1
A load() 0 4 1
A withBuilder() 0 9 2
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Infrastructure;
13
14
use Slick\Di\ContainerBuilder;
15
use Slick\Di\ContainerBuilderInterface;
16
use Slick\Di\ContainerInterface;
17
use Slick\Di\DefinitionLoader\AutowireDefinitionLoader;
18
use Slick\Di\DefinitionLoader\DirectoryDefinitionLoader;
19
use Slick\Di\DefinitionLoader\FileDefinitionLoader;
20
use Slick\Di\DefinitionLoaderInterface;
21
use Slick\Di\Exception;
22
23
/**
24
 * DependencyContainerFactory
25
 *
26
 * @package Slick\WebStack\Infrastructure
27
 */
28
final class DependencyContainerFactory
29
{
30
    private static ?DependencyContainerFactory $instance = null;
31
32
    private ?ContainerInterface $container = null;
33
34
    private ContainerBuilderInterface $builder;
35
    /**
36
     *
37
     */
38
    private function __construct()
39
    {
40
        $this->builder = new ContainerBuilder();
41
    }
42
43
    /**
44
     * ContainerFactory instance
45
     *
46
     * @return DependencyContainerFactory
47
     */
48
    public static function instance(): DependencyContainerFactory
49
    {
50
51
        if (null === self::$instance) {
52
            self::$instance = new DependencyContainerFactory();
53
        }
54
        return self::$instance;
55
    }
56
57
    /**
58
     * ContainerFactory container
59
     *
60
     * @return ContainerInterface
61
     */
62
    public function container(): ContainerInterface
63
    {
64
        if (null === $this->container) {
65
            $this->container = $this->builder->getContainer();
66
            $this->container->register(ContainerInterface::class, $this->container);
67
        }
68
        return $this->container;
69
    }
70
71
    public function withBuilder(ContainerBuilderInterface $builder): DependencyContainerFactory
72
    {
73
        if (null !== $this->container) {
74
            $builder->setContainer($this->container);
75
            $this->container = null;
76
        }
77
78
        $this->builder = $builder;
79
        return $this;
80
    }
81
82
    /**
83
     * @throws Exception
84
     */
85
    public function load(DefinitionLoaderInterface $definitionLoader): DependencyContainerFactory
86
    {
87
        $this->builder->load($definitionLoader);
88
        return $this;
89
    }
90
91
    /**
92
     * Loads application services from given source path and services file.
93
     *
94
     * @param string $sourcePath Path to the directory containing service definitions
95
     * @param array<string, mixed> $services Array of service definitions file paths
96
     * @return DependencyContainerFactory
97
     */
98
    public function loadApplicationServices(string $sourcePath, array $services): DependencyContainerFactory
99
    {
100
        $this
101
            ->load(new DirectoryDefinitionLoader($sourcePath . '/config/services'))
102
            ->load(new AutowireDefinitionLoader($sourcePath . '/src'))
103
            ->load(new FileDefinitionLoader($services));
104
        return $this;
105
    }
106
}
107