1 | <?php |
||
49 | class DataSourceManager |
||
50 | { |
||
51 | /** |
||
52 | * @access protected |
||
53 | * @var Framework |
||
54 | */ |
||
55 | protected $framework; |
||
56 | |||
57 | /** |
||
58 | * @access protected |
||
59 | * @var \Zepi\Turbo\Backend\ObjectBackendAbstract |
||
60 | */ |
||
61 | protected $dataSourceObjectBackend; |
||
62 | |||
63 | /** |
||
64 | * @access protected |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $dataSources = array(); |
||
68 | |||
69 | /** |
||
70 | * @access protected |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $definitions = array(); |
||
74 | |||
75 | /** |
||
76 | * Constructs the object |
||
77 | * |
||
78 | * @access public |
||
79 | * @param \Zepi\Turbo\Framework $framework |
||
80 | * @param \Zepi\Turbo\Backend\ObjectBackendAbstract $dataSourceObjectBackend |
||
81 | */ |
||
82 | 17 | public function __construct( |
|
89 | |||
90 | /** |
||
91 | * Initializes the data source manager. The function loads all saved |
||
92 | * events from the object backend. |
||
93 | * |
||
94 | * @access public |
||
95 | */ |
||
96 | 17 | public function initializeDataSourceManager() |
|
105 | |||
106 | /** |
||
107 | * Adds a data source to the repository |
||
108 | * |
||
109 | * @access public |
||
110 | * @param string $interfaceName |
||
111 | * @param string $driver |
||
112 | * @param string $className |
||
113 | */ |
||
114 | public function addDataSource($interfaceName, $driver, $className) |
||
125 | |||
126 | /** |
||
127 | * Removes a data source from the repository |
||
128 | * |
||
129 | * @access public |
||
130 | * @param stirng $interfaceName |
||
131 | * @param string $driver |
||
132 | * @param string $className |
||
133 | * @return boolean |
||
134 | */ |
||
135 | public function removeDataSource($interfaceName, $driver, $className) |
||
146 | |||
147 | /** |
||
148 | * Saves the registred data sources in the object backend. |
||
149 | * |
||
150 | * @access protected |
||
151 | */ |
||
152 | protected function saveDataSources() |
||
156 | |||
157 | /** |
||
158 | * Adds a definition |
||
159 | * |
||
160 | * @access public |
||
161 | * @param string $selector |
||
162 | * @param string $driver |
||
163 | */ |
||
164 | public function addDefinition($selector, $driver) |
||
168 | |||
169 | /** |
||
170 | * Removes a definition |
||
171 | * |
||
172 | * @access public |
||
173 | * @param string $selector |
||
174 | * @return boolean |
||
175 | */ |
||
176 | public function removeDefinition($selector) |
||
185 | |||
186 | /** |
||
187 | * Returns the data source for the given type class. |
||
188 | * |
||
189 | * @access public |
||
190 | * @param string $typeClass |
||
191 | * @return mixed |
||
192 | * |
||
193 | * @throws \Zepi\Turbo\Exception Cannot find a driver for the given type class. |
||
194 | * @throws \Zepi\Turbo\Exception Cannot find a data source for the given type class. |
||
195 | */ |
||
196 | public function getDataSource($typeClass) |
||
197 | { |
||
198 | $driver = $this->getDriver($typeClass); |
||
199 | |||
200 | // If there is no driver for the given type class throw an exception |
||
201 | if ($driver === false) { |
||
202 | throw new Exception('Cannot find a driver for the given type class "' . $typeClass . '".'); |
||
203 | } |
||
204 | |||
205 | $dataSourceClass = $this->searchDataSourceClass($typeClass, $driver); |
||
206 | |||
207 | // If there is no data source class for the given type class throw an exception |
||
208 | if ($dataSourceClass === false) { |
||
209 | throw new Exception('Cannot find a data source for the given type class "' . $typeClass . '" (selected driver: "' . $driver . '").'); |
||
210 | } |
||
211 | |||
212 | return $this->framework->getInstance($dataSourceClass); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Returns an array with all DataSource type classes |
||
217 | * |
||
218 | * @access public |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getDataSourceTypeClasses() |
||
222 | { |
||
223 | return array_keys($this->dataSources); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Returns the driver for the given type class or false if no |
||
228 | * driver is available. |
||
229 | * |
||
230 | * @access protected |
||
231 | * @param string $typeClass |
||
232 | * @return false|string |
||
233 | */ |
||
234 | protected function getDriver($typeClass) |
||
255 | |||
256 | /** |
||
257 | * Returns the number of parts |
||
258 | * |
||
259 | * @access protected |
||
260 | * @param string $selector |
||
261 | * @return integer |
||
262 | */ |
||
263 | protected function countNumberOfParts($selector) |
||
273 | |||
274 | /** |
||
275 | * Returns the DataSource class for the given type class and driver |
||
276 | * @param string $typeClass |
||
277 | * @param string $driver |
||
278 | * @return false|string |
||
279 | */ |
||
280 | protected function searchDataSourceClass($typeClass, $driver) |
||
288 | } |
||
289 |