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