Test Setup Failed
Branch master (e0eff2)
by Alexey
06:56
created

AbstractServiceProvider::container()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace Venta\ServiceProvider;
4
5
use Venta\Contracts\Config\Config;
6
use Venta\Contracts\Config\ConfigFactory;
7
use Venta\Contracts\Console\CommandCollection;
8
use Venta\Contracts\Container\Container;
9
use Venta\Contracts\ServiceProvider\ServiceProvider;
10
11
/**
12
 * Class AbstractServiceProvider.
13
 *
14
 * @package Venta\ServiceProvider
15
 */
16
abstract class AbstractServiceProvider implements ServiceProvider
17
{
18
    /**
19
     * Service provider name.
20
     *
21
     * @var string
22
     */
23
    protected static $name;
24
25
    /**
26
     * Application config.
27
     *
28
     * @var Config
29
     */
30
    private $appConfig;
31
32
    /**
33
     * Container instance.
34
     *
35
     * @var Container
36
     */
37
    private $container;
38
39
    /**
40
     * AbstractServiceProvider constructor.
41
     *
42
     * @param Container $container
43
     * @param Config $appConfig
44
     */
45
    public function __construct(Container $container, Config $appConfig)
46
    {
47
        $this->container = $container;
48
        $this->appConfig = $appConfig;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public static function dependencies(): array
55
    {
56
        return [];
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    final public static function name(): string
63
    {
64
        return static::$name ?: static::class;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    abstract public function boot();
71
72
    /**
73
     * @return Container
74
     */
75
    protected function container(): Container
76
    {
77
        return $this->container;
78
    }
79
80
    /**
81
     * Merges config params from service provider with the global configuration.
82
     *
83
     * @param string[] ...$configFiles
84
     */
85
    protected function loadConfigFromFiles(string ...$configFiles)
86
    {
87
        /** @var Config $config */
88
        $config = $this->container->get(Config::class);
89
90
        /** @var ConfigFactory $configFactory */
91
        $configFactory = $this->container->get(ConfigFactory::class);
92
        foreach ($configFiles as $configFile) {
93
            $config = $config->merge($configFactory->createFromFile($configFile));
94
        }
95
96
        $config = $config->merge($this->appConfig);
97
98
        $this->container->bindInstance(Config::class, $config);
99
100
    }
101
102
    /**
103
     * Registers commands exposed by service provider.
104
     *
105
     * @param string[] ...$commandClasses
106
     */
107
    protected function provideCommands(string ...$commandClasses)
108
    {
109
        /** @var CommandCollection $commandCollector */
110
        $commandCollector = $this->container->get(CommandCollection::class);
111
        foreach ($commandClasses as $commandClass) {
112
            $commandCollector->add($commandClass);
113
        }
114
    }
115
}