1: <?php
2: /**
3: * Horde_Core_Db_Migration provides a wrapper for all migration scripts
4: * distributed through Horde applications or libraries.
5: *
6: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10: *
11: * @author Jan Schneider <jan@horde.org>
12: * @package Core
13: */
14:
15: /**
16: * @author Jan Schneider <jan@horde.org>
17: * @package Core
18: */
19: class Horde_Core_Db_Migration
20: {
21: /**
22: * List of all migration directories.
23: *
24: * @var array
25: */
26: public $dirs = array();
27:
28: /**
29: * List of all module names matching the directories in $dirs.
30: *
31: * @var array
32: */
33: public $apps = array();
34:
35: /**
36: * Constructor.
37: *
38: * Searches all installed applications and libraries for migration
39: * directories and builds lists of migrateable modules and directories.
40: *
41: * @param string $basedir Base directory of a Git checkout. If provided
42: * a framework/ sub directory is searched for
43: * migration scripts too.
44: * @param string $pearconf Path to a PEAR configuration file.
45: */
46: public function __construct($basedir = null, $pearconf = null)
47: {
48: // Loop through all applications.
49: foreach ($GLOBALS['registry']->listApps(array('hidden', 'notoolbar', 'admin', 'noadmin', 'active', 'inactive'), false, null) as $app) {
50: $dir = $GLOBALS['registry']->get('fileroot', $app) . '/migration';
51: if (is_dir($dir)) {
52: $this->apps[] = $app;
53: $this->dirs[] = realpath($dir);
54: }
55: }
56:
57: // Loop through local framework checkout.
58: if ($basedir) {
59: foreach (glob($basedir . '/framework/*/migration') as $dir) {
60: $this->apps[] = 'horde_' . Horde_String::lower(basename(dirname($dir)));
61: $this->dirs[] = realpath($dir);
62: }
63: }
64:
65: // Loop through installed PEAR packages.
66: $old_error_reporting = error_reporting();
67: error_reporting($old_error_reporting & ~E_STRICT);
68: $pear = new PEAR_Config($pearconf);
69: foreach (glob($pear->get('data_dir') . '/*/migration') as $dir) {
70: $app = Horde_String::lower(basename(dirname($dir)));;
71: if (!in_array($app, $this->apps)) {
72: $this->apps[] = $app;
73: $this->dirs[] = realpath($dir);
74: }
75: }
76: error_reporting($old_error_reporting);
77: }
78:
79: /**
80: * Returns a migrator for a module.
81: *
82: * @param string $app An application or library name.
83: * @param Horde_Log_Logger $logger A logger instance.
84: *
85: * @return Horde_Db_Migration_Migrator A migrator for the specified module.
86: */
87: public function getMigrator($app, Horde_Log_Logger $logger = null)
88: {
89: return new Horde_Db_Migration_Migrator(
90: $GLOBALS['injector']->getInstance('Horde_Db_Adapter'),
91: $logger,
92: array('migrationsPath' => $this->dirs[array_search($app, $this->apps)],
93: 'schemaTableName' => $app . '_schema_info'));
94: }
95: }
96: