Passed
Push — master ( 86b52c...799108 )
by Rougin
01:38
created

Bootstrap::definitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace App;
4
5
use Zapheus\Application;
6
use Zapheus\Container\CompositeContainer;
7
use Zapheus\Container\Container;
8
use Zapheus\Container\ReflectionContainer;
9
use Zapheus\Provider\Configuration;
10
use Zapheus\Provider\FrameworkProvider;
11
12
/**
13
 * Bootstrap Container
14
 *
15
 * @package Zapheus
16
 * @author  Rougin Royce Gutib <[email protected]>
17
 */
18
class Bootstrap extends CompositeContainer
19
{
20
    const CONFIG = 'Zapheus\Provider\ConfigurationInterface';
21
22
    /**
23
     * Static instance of the application container.
24
     *
25
     * @var \Zapheus\Container\ContainerInterface
26
     */
27
    protected static $container;
28
29
    /**
30
     * Path of the configurations directory.
31
     *
32
     * @var string
33
     */
34
    protected $config = 'app/config';
35
36
    /**
37
     * Full path of the root directory.
38
     *
39
     * @var string
40
     */
41
    protected $root;
42
43
    /**
44
     * An instance of a Container\WritableInterface.
45
     *
46
     * @var \Zapheus\Container\WritableInterface
47
     */
48
    protected $writable;
49
50
    /**
51
     * Initializes the container instance.
52
     *
53
     * NOTE: If you want to autowire dependencies of classes, you may need
54
     * to use the ReflectionContainer instance but it might have an effect
55
     * regarding the performance of the application. Just uncomment line 72
56
     * in order to use the mentioned instance.
57
     *
58
     * @param string $root
59
     */
60 9
    public function __construct($root)
61
    {
62 9
        $this->root = $root;
63
64 9
        $this->writable = new Container;
65
66 9
        $config = new Configuration;
67
68 9
        $config->load($this->root . $this->config);
69
70 9
        $this->writable->set(self::CONFIG, $config);
71
72 9
        $this->add(new ReflectionContainer);
73
74 9
        $this->definitions();
75 9
    }
76
77
    /**
78
     * Prepares the providers and returns the application instance.
79
     *
80
     * @return \Zapheus\Application
81
     */
82 9
    public function initialize()
83
    {
84 9
        $application = new Application($this->writable);
85
86 9
        $config = $application->get(self::CONFIG);
87
88 9
        $zapheus = $config->get('app.providers.zapheus');
89
90 9
        foreach ((array) $zapheus as $provider) {
91 9
            $string = is_string($provider);
92
93 9
            $string && $provider = new $provider;
94
95 9
            $application->add($provider);
96 9
        }
97
98 9
        $application->add(new FrameworkProvider($this));
99
100 9
        return static::$container = $application;
101
    }
102
103
    /**
104
     * Finds an entry of the container by its identifier and returns it.
105
     *
106
     * @param  string $id
107
     * @return mixed
108
     *
109
     * @throws \Zapheus\Container\Exception\NotFoundException
110
     * @throws \Zapheus\Container\Exception\ContainerException
111
     */
112 9
    public static function make($id)
113
    {
114 9
        return self::$container->get($id);
115
    }
116
117
    /**
118
     * Define your dependencies using the $this->writable->set() method.
119
     * See line 72 of Zapheus\Container\Container for documentation.
120
     *
121
     * NOTE: If you enabled the ReflectionContainer in line 74, you can
122
     * now enable to define string classses and instances automatically.
123
     *
124
     * @return void
125
     */
126 9
    protected function definitions()
127
    {
128 9
        $greet = 'App\Zapheus\GreetController';
129
130 9
        $this->writable->set($greet, new $greet);
131
132 9
        $test = 'App\Example\TestController';
133
134 9
        $this->writable->set($test, new $test);
135 9
    }
136
}
137