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