Completed
Push — master ( d310cf...1228ca )
by Rougin
02:55
created

BridgeProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Zapheus\Bridge\Illuminate;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Container\Container as IlluminateContainer;
7
use Zapheus\Container\WritableInterface;
8
use Zapheus\Provider\ProviderInterface;
9
10
/**
11
 * Illuminate Bridge Provider
12
 *
13
 * @package Zapheus
14
 * @author  Rougin Royce Gutib <[email protected]>
15
 */
16
class BridgeProvider implements ProviderInterface
17
{
18
    const CONTAINER = 'Illuminate\Container\Container';
19
20
    const LOADER = 'Illuminate\Config\LoaderInterface';
21
22
    /**
23
     * @var string[]
24
     */
25
    protected $providers;
26
27
    /**
28
     * Initializes the provider instance.
29
     *
30
     * @param string[] $providers
31
     */
32
    public function __construct(array $providers)
33
    {
34
        $this->providers = $providers;
35
    }
36
37
    /**
38
     * Registers the bindings in the container.
39
     *
40
     * @param  \Zapheus\Container\WritableInterface $container
41
     * @return \Zapheus\Container\ContainerInterface
42
     */
43
    public function register(WritableInterface $container)
44
    {
45
        $illuminate = $this->container($container);
46
47
        foreach ($this->providers as $item) {
48
            $provider = new $item($illuminate);
49
50
            $exists = method_exists($provider, 'boot');
51
52
            $provider->register();
53
54
            $exists === true && $provider->boot();
55
        }
56
57
        $container->set(self::CONTAINER, $illuminate);
58
59
        return $container;
60
    }
61
62
    /**
63
     * Returns a \Illuminate\Container\Container instance.
64
     *
65
     * @param  \Zapheus\Container\WritableInterface $container
66
     * @return \Illuminate\Container\Container
67
     */
68
    protected function container(WritableInterface $container)
69
    {
70
        $laravel = new IlluminateContainer;
71
72
        if (interface_exists(self::LOADER) === false) {
73
            $config = $container->get(self::CONFIG);
74
75
            $items = $config->get('illuminate', array());
76
77
            $laravel['config'] = new Repository($items);
78
        }
79
80
        return $laravel;
81
    }
82
}
83