1 | <?php |
||
22 | abstract class File |
||
23 | { |
||
24 | /** |
||
25 | * Path to config file. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $filename; |
||
30 | |||
31 | /** |
||
32 | * Path to config file. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $adapters; |
||
37 | |||
38 | /** |
||
39 | * Handling the bootstrapping. |
||
40 | * |
||
41 | * @var \phpbu\App\Configuration\Bootstrapper |
||
42 | */ |
||
43 | protected $bootstrapper; |
||
44 | |||
45 | /** |
||
46 | * File constructor. |
||
47 | * |
||
48 | * @param string $file |
||
49 | * @param \phpbu\App\Configuration\Bootstrapper $bootstrapper |
||
50 | */ |
||
51 | 39 | public function __construct(string $file, Configuration\Bootstrapper $bootstrapper) |
|
52 | { |
||
53 | 39 | $this->filename = $file; |
|
54 | 39 | $this->bootstrapper = $bootstrapper; |
|
55 | 39 | } |
|
56 | |||
57 | /** |
||
58 | * Returns the phpbu Configuration. |
||
59 | * |
||
60 | * @param \phpbu\App\Factory $factory |
||
61 | * @return \phpbu\App\Configuration |
||
62 | * @throws \phpbu\App\Exception |
||
63 | */ |
||
64 | 33 | public function getConfiguration(AppFactory $factory) |
|
65 | { |
||
66 | // create configuration first so the working directory is available for all adapters |
||
67 | 33 | $configuration = new Configuration(); |
|
68 | 33 | $configuration->setFilename($this->filename); |
|
69 | |||
70 | 33 | $this->setAppSettings($configuration); |
|
71 | 33 | $this->handleBootstrap($configuration); |
|
72 | 33 | $this->setupAdapters($factory); |
|
73 | |||
74 | 29 | $this->setLoggers($configuration); |
|
75 | 27 | $this->setBackups($configuration); |
|
76 | |||
77 | 13 | return $configuration; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Load all available config adapters. |
||
82 | * |
||
83 | * @param \phpbu\App\Factory $factory |
||
84 | * @throws \phpbu\App\Exception |
||
85 | */ |
||
86 | 33 | protected function setupAdapters(AppFactory $factory) |
|
87 | { |
||
88 | 33 | foreach ($this->getAdapterConfigs() as $config) { |
|
89 | 4 | $this->adapters[$config->name] = $factory->createAdapter($config->type, $config->options); |
|
90 | } |
||
91 | 29 | } |
|
92 | |||
93 | /** |
||
94 | * Return a registered adapter. |
||
95 | * |
||
96 | * @param string $name |
||
97 | * @return \phpbu\App\Adapter |
||
98 | * @throws \phpbu\App\Exception |
||
99 | */ |
||
100 | 4 | protected function getAdapter($name) |
|
101 | { |
||
102 | 4 | if (!isset($this->adapters[$name])) { |
|
103 | 1 | throw new Exception('no adapter registered with name: ' . $name); |
|
104 | } |
||
105 | 3 | return $this->adapters[$name]; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Return list of adapter configs. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | abstract protected function getAdapterConfigs(); |
||
114 | |||
115 | /** |
||
116 | * Set the phpbu application settings. |
||
117 | * |
||
118 | * @param \phpbu\App\Configuration $configuration |
||
119 | */ |
||
120 | abstract public function setAppSettings(Configuration $configuration); |
||
121 | |||
122 | /** |
||
123 | * Set the log configuration. |
||
124 | * |
||
125 | * @param \phpbu\App\Configuration $configuration |
||
126 | * @throws \phpbu\App\Exception |
||
127 | */ |
||
128 | abstract public function setLoggers(Configuration $configuration); |
||
129 | |||
130 | /** |
||
131 | * Set the backup configurations. |
||
132 | * |
||
133 | * @param \phpbu\App\Configuration $configuration |
||
134 | * @throws \phpbu\App\Exception |
||
135 | */ |
||
136 | abstract public function setBackups(Configuration $configuration); |
||
137 | |||
138 | /** |
||
139 | * Handles the bootstrap file inclusion. |
||
140 | * |
||
141 | * @param \phpbu\App\Configuration $configuration |
||
142 | * @throws \phpbu\App\Exception |
||
143 | */ |
||
144 | 33 | protected function handleBootstrap(Configuration $configuration) |
|
148 | |||
149 | /** |
||
150 | * Converts a path to an absolute one if necessary. |
||
151 | * |
||
152 | * @param string $path |
||
153 | * @param boolean $useIncludePath |
||
154 | * @return string |
||
155 | */ |
||
156 | 32 | protected function toAbsolutePath($path, $useIncludePath = false) |
|
160 | |||
161 | /** |
||
162 | * Return option value. |
||
163 | * Checks if the value should be fetched from an Adapter, if not it just returns the value. |
||
164 | * |
||
165 | * @param string $value |
||
166 | * @return string |
||
167 | * @throws \phpbu\App\Exception |
||
168 | */ |
||
169 | 29 | protected function getAdapterizedValue($value) |
|
178 | |||
179 | /** |
||
180 | * Load the file. |
||
181 | * |
||
182 | * @param string $filename |
||
183 | * @throws \phpbu\App\Exception |
||
184 | * @return \stdClass |
||
185 | */ |
||
186 | 39 | protected function loadFile($filename) |
|
197 | } |
||
198 |