1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Application\Bootstrap; |
4
|
|
|
|
5
|
|
|
use Nip\Application\Bootstrap\Bootstrapers\AbstractBootstraper; |
6
|
|
|
use Nip\Container\ContainerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class BootstrapAwareTrait |
10
|
|
|
* @package Nip\Application\Bootstrap |
11
|
|
|
*/ |
12
|
|
|
trait BootstrapAwareTrait |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Indicates if the application has been bootstrapped before. |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $hasBeenBootstrapped = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The bootstrap classes for the application. |
24
|
|
|
* |
25
|
|
|
* @var null|AbstractBootstraper[] |
26
|
|
|
*/ |
27
|
|
|
protected $bootstrappers = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Bootstrap the application for HTTP requests. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function bootstrap() |
35
|
|
|
{ |
36
|
|
|
if (!$this->hasBeenBootstrapped()) { |
37
|
|
|
$this->bootstrapWith($this->bootstrappers()); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Determine if the application has been bootstrapped before. |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function hasBeenBootstrapped() |
47
|
|
|
{ |
48
|
|
|
return $this->hasBeenBootstrapped; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Run the given array of bootstrap classes. |
53
|
|
|
* |
54
|
|
|
* @param array $bootstrappers |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function bootstrapWith(array $bootstrappers) |
58
|
|
|
{ |
59
|
|
|
$this->hasBeenBootstrapped = true; |
60
|
|
|
foreach ($bootstrappers as $bootstrapper) { |
61
|
|
|
$this->getBootstrap($bootstrapper)->bootstrap($this); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $bootstrapper |
67
|
|
|
* @return AbstractBootstraper |
68
|
|
|
*/ |
69
|
|
|
public function getBootstrap($bootstrapper) |
70
|
|
|
{ |
71
|
|
|
if ($this->getContainer() instanceof ContainerInterface) { |
|
|
|
|
72
|
|
|
return $this->get($bootstrapper); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
return new $bootstrapper; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the bootstrap classes for the application. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function bootstrappers() |
83
|
|
|
{ |
84
|
|
|
if ($this->bootstrappers === null) { |
85
|
|
|
$this->initBootstrappers(); |
86
|
|
|
} |
87
|
|
|
return $this->bootstrappers; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param AbstractBootstraper $bootstrapper |
92
|
|
|
*/ |
93
|
|
|
protected function addBootstrapper($bootstrapper) |
94
|
|
|
{ |
95
|
|
|
$this->bootstrappers[] = $bootstrapper; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function initBootstrappers() |
99
|
|
|
{ |
100
|
|
|
$this->bootstrappers = $this->getDefaultBootstrappers(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return AbstractBootstraper[] |
105
|
|
|
*/ |
106
|
|
|
protected function getDefaultBootstrappers() |
107
|
|
|
{ |
108
|
|
|
return []; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.