1 | <?php |
||
10 | class ZeroConfDriver implements DriverInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var Source\AbstractSource |
||
14 | */ |
||
15 | protected $metadata; |
||
16 | |||
17 | /** |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $params; |
||
22 | |||
23 | /** |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $default_options = array( |
||
28 | 'alias' => 'default', |
||
29 | 'path' => null, |
||
30 | 'version' => 'latest', |
||
31 | 'schema' => null, |
||
32 | 'permissions' => 0666 |
||
33 | ); |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected static $metadataCache = array(); |
||
40 | |||
41 | /** |
||
42 | * Underlying database adapter |
||
43 | * @var Adapter |
||
44 | */ |
||
45 | protected $adapter; |
||
46 | |||
47 | /** |
||
48 | * Construct a new Zero configuration driver |
||
49 | * |
||
50 | * $params allows you to specify the |
||
51 | * path : where to store the model definition (default to sys_get_temp_dir()) |
||
52 | * alias : the alias to use when using multiple schemas, default: 'default' |
||
53 | * version : the version to use, default to 'latest' |
||
54 | * schema : the database schema name, default to current adapter connection |
||
55 | * permissions: by default the model file is created with permission 0666 |
||
56 | * |
||
57 | * |
||
58 | * @param Adapter $adapter |
||
59 | * @param array|Traversable $params [alias,path,version] |
||
60 | * @throws Exception\ModelPathNotFoundException |
||
61 | * @throws Exception\InvalidArgumentException |
||
62 | */ |
||
63 | 151 | public function __construct(Adapter $adapter, $params = array()) |
|
64 | { |
||
65 | 151 | $this->setDbAdapter($adapter); |
|
66 | |||
67 | 151 | if (!is_array($params) && !$params instanceof \Traversable) { |
|
68 | 1 | throw new Exception\InvalidArgumentException(__METHOD__ . ' $params parameter expects an array or Traversable object'); |
|
69 | 1 | } |
|
70 | |||
71 | 151 | $this->params = array_merge($this->default_options, (array) $params); |
|
72 | 151 | if ($this->params['path'] == '') { |
|
73 | 151 | $this->params['path'] = sys_get_temp_dir(); |
|
74 | 151 | } |
|
75 | 151 | if (!is_dir($this->params['path'])) { |
|
76 | 1 | $path = (string) $this->params['path']; |
|
77 | 1 | throw new Exception\ModelPathNotFoundException(__METHOD__ . " Model directory not found '" . $path . "'"); |
|
78 | } |
||
79 | 151 | $this->checkParams(); |
|
80 | 151 | } |
|
81 | |||
82 | /** |
||
83 | * Checks model configuration params |
||
84 | * @throws Exception\InvalidArgumentException |
||
85 | * |
||
86 | */ |
||
87 | 151 | protected function checkParams() |
|
106 | |||
107 | /** |
||
108 | * Return models configuration file |
||
109 | * @return string |
||
110 | */ |
||
111 | 17 | public function getModelsConfigFile() |
|
117 | |||
118 | /** |
||
119 | * Get models definition according to options |
||
120 | * |
||
121 | * @throws Exception\ModelFileNotFoundException |
||
122 | * @throws Exception\ModelFileCorruptedException |
||
123 | * @return array |
||
124 | */ |
||
125 | 17 | public function getModelsDefinition() |
|
154 | |||
155 | /** |
||
156 | * Save model definition |
||
157 | * |
||
158 | * @throws Exception\ModelFileNotWritableException |
||
159 | * @param array $models_definition |
||
160 | * @return DriverInterface |
||
161 | */ |
||
162 | 6 | protected function saveModelsDefinition(array $models_definition) |
|
182 | |||
183 | /** |
||
184 | * Set underlying database adapter |
||
185 | * |
||
186 | * @param Adapter $adapter |
||
187 | * @return DriverInterface |
||
188 | */ |
||
189 | 151 | protected function setDbAdapter(Adapter $adapter) |
|
194 | |||
195 | /** |
||
196 | * Get underlying database adapter |
||
197 | * |
||
198 | * @return Adapter |
||
199 | */ |
||
200 | 132 | public function getDbAdapter() |
|
204 | |||
205 | /** |
||
206 | * Get internal metadata reader |
||
207 | * |
||
208 | * @return Source\AbstractSource |
||
209 | */ |
||
210 | 146 | public function getMetadata() |
|
222 | |||
223 | /** |
||
224 | * |
||
225 | * @return ZeroConfDriver |
||
226 | */ |
||
227 | 20 | public function clearMetadataCache() |
|
232 | |||
233 | /** |
||
234 | * |
||
235 | * @return Metadata\NormalistModels |
||
236 | */ |
||
237 | 17 | protected function getDefaultMetadata() |
|
258 | |||
259 | /** |
||
260 | * Set internal metadata reader |
||
261 | * |
||
262 | * @param Source\AbstractSource $metadata |
||
263 | * @return ZeroConfDriver |
||
264 | */ |
||
265 | 1 | public function setMetadata(Source\AbstractSource $metadata) |
|
270 | } |
||
271 |