1: <?php
2:
3: class DatasourceDumperManager {
4:
5: 6: 7: 8: 9:
10: public static function getDumper(\DataSource $ds) {
11: $path = explode('/', $ds->config['datasource']);
12: $className = end($path) . 'Dumper';
13: array_pop($path);
14: $path = array_merge(array('Lib', 'DatasourceDumper'), $path);
15: $location = implode('/', $path);
16: $plugins = array_merge(
17: array(false)
18: , CakePlugin::loaded()
19: );
20: foreach ($plugins as $plugin) {
21: App::uses($className, $plugin ? "$plugin.$location" : $location);
22: if (class_exists($className)) {
23: return new $className;
24: }
25: }
26: throw new Exception("Class \"$className\" not found");
27: }
28:
29: private static function _listDumps() {
30: $dir = new DirectoryIterator($this->_getDumpsDirectory());
31: $dumps = array();
32: while ($dir->valid()) {
33: if ($dir->isFile()) {
34: $dumps[] = $this->_parseDumpFile($dir->getPathname());
35: }
36: $dir->next();
37: }
38: return $dumps;
39: }
40:
41: private static function _findDump() {
42: foreach (self::_listDumps() as $dump) {
43: if ($dump['name'] == $this->dumpName) {
44: return $dump;
45: }
46: }
47: return false;
48: }
49:
50: private function _parseDumpFile($filepath) {
51: $connection = '[_a-zA-Z][_a-zA-Z0-9]*';
52: $date = '\d{4}\-\d{2}\-\d{2}_\d{2}\-\d{2}\-\d{2}';
53: $datasource = '([_a-zA-Z][_a-zA-Z0-9]*)(\-[_a-zA-Z][_a-zA-Z0-9]*)*';
54: $pattern = "/($connection)_($date)_($datasource)/";
55: if (preg_match($pattern, basename($filepath), $matches)) {
56: return array(
57: 'name' => basename($filepath),
58: 'path' => $filepath,
59: 'connection' => $matches[1],
60: 'date' => str_replace('_', ' ', $matches[2]),
61: 'datasource' => str_replace('-', '/', $matches[3])
62: );
63: } else {
64: throw new Exception("File \"$filepath\" has no dump format");
65: }
66: }
67:
68: }
69: