|
dmBridge PHP API
|
00001 <?php 00002 # 00003 # dmBridge: a data access framework for CONTENTdm(R) 00004 # 00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher 00006 # Education, on behalf of the University of Nevada, Las Vegas 00007 # 00008 00013 class DMModuleManager { 00014 00018 private static $all_modules; 00019 00023 private static $instance; 00024 00028 private $config_xml; 00029 00033 private $modules_pathname; 00034 00040 public static function destroyInstance() { 00041 self::$instance = null; 00042 } 00043 00047 public static function getInstance() { 00048 if (!self::$instance) { 00049 self::$instance = new DMModuleManager(); 00050 } 00051 return self::$instance; 00052 } 00053 00054 protected function __construct() { 00055 $this->config_xml = DMConfigXML::getInstance(); 00056 $this->setModulesPathname( 00057 dirname(__FILE__) . "/../../extensions/modules"); 00058 } 00059 00060 public function __clone() { 00061 //trigger_error("Cannot clone a Singleton"); 00062 } 00063 00064 public function __wakeup() { 00065 //trigger_error("Cannot deserialize a Singleton"); 00066 } 00067 00072 public function bootstrapAllModules() { 00073 $this->getAllModules(); 00074 } 00075 00082 public function getAllModules() { 00083 if (self::$all_modules == null) { 00084 self::$all_modules = array(); 00085 00086 $it = new RecursiveDirectoryIterator($this->getModulesPathname()); 00087 foreach (new RecursiveIteratorIterator($it) as $filename) { 00088 if (substr(basename($filename), -4, 4) == ".php" 00089 && basename($filename, ".php") == basename(dirname($filename))) { 00090 00091 $class = basename($filename, ".php"); 00092 include_once(dirname($filename) . "/" 00093 . DMAbstractDMBridgeModule::BOOTSTRAP_FILENAME); 00094 self::$all_modules[] = new $class; 00095 } 00096 } 00097 } 00098 return self::$all_modules; 00099 } 00100 00104 public function setConfigXML(DMConfigXML $config_xml) { 00105 $this->config_xml = $config_xml; 00106 self::$all_modules = null; 00107 } 00108 00113 public function getEnabledModules() { 00114 $modules = array(); 00115 foreach ($this->getAllModules() as $module) { 00116 if ($this->isModuleEnabled($module)) { 00117 $modules[] = $module; 00118 } 00119 } 00120 return $modules; 00121 } 00122 00127 public function getModuleByName($name) { 00128 foreach ($this->getAllModules() as $module) { 00129 if ($module->getName() == $name) { 00130 return $module; 00131 } 00132 } 00133 return null; 00134 } 00135 00140 public function isModuleEnabled($module) { 00141 return $this->config_xml->isModuleEnabled($module); 00142 } 00143 00150 public function setModuleEnabled(DMBridgeModule $module, $enabled) { 00151 // is the module compatible with the current system version? 00152 if ($module->getMinSupportedVersionSequence() 00153 > DMBridgeVersion::getDmBridgeVersionSequence()) { 00154 throw new DMModuleActivationException( 00155 DMLocalizedString::getString("INCOMPATIBLE_MODULE")); 00156 } 00157 00158 // do we need to prepare the data store? 00159 if ($enabled && $module instanceof DMBridgeDataStoreModule) { 00160 $ds = DMDataStoreFactory::getDataStore(); 00161 $stmts = array(); 00162 // Re-run the setup just to make sure the tables are set up. This 00163 // will probably fail but that's OK. 00164 switch ($ds->getType()) { 00165 case DMDataStoreType::PDO_MySQL: 00166 $stmts = $module->getSetupSQLForMySQL(); 00167 break; 00168 case DMDataStoreType::PDO_SQLite: 00169 $stmts = $module->getSetupSQLForSQLite(); 00170 break; 00171 } 00172 00173 $ds->beginTransaction(); 00174 foreach ($stmts as $sql) { 00175 try { 00176 $ds->write($sql, array()); 00177 } catch (DMPDOException $e) { 00178 // if a create command failed for some reason OTHER than 00179 // the tables already existing, roll back and throw an 00180 // exception. 00181 if (strpos($e->getMessage(), "already exists") === false) { 00182 $ds->rollBack(); 00183 throw new DMModuleActivationException($e->getMessage()); 00184 } 00185 } 00186 } 00187 $ds->commit(); 00188 00189 if (!$this->config_xml->isModuleOfSameNameAndVersionInstalled($module)) { 00190 $stmts = array(); 00191 if ($this->config_xml->isModuleOfSameNameInstalled($module)) { // upgrade 00192 switch ($ds->getType()) { 00193 case DMDataStoreType::PDO_MySQL: 00194 $stmts = $module->getUpgradeSQLForMySQL(); 00195 break; 00196 case DMDataStoreType::PDO_SQLite: 00197 $stmts = $module->getUpgradeSQLForSQLite(); 00198 break; 00199 } 00200 } 00201 $ds->beginTransaction(); 00202 foreach ($stmts as $sql) { 00203 try { 00204 $ds->write($sql, array()); 00205 } catch (DMPDOException $e) { 00206 $msg = DMLocalizedString::getString( 00207 sprintf("MODULE_ACTIVATION_SQL_FAILED", $sql)); 00208 $ds->rollBack(); 00209 throw new DMModuleActivationException($msg); 00210 } 00211 } 00212 $ds->commit(); 00213 } 00214 } 00215 return $this->config_xml->setModuleEnabled($module, $enabled); 00216 } 00217 00221 public function getModulesPathname() { 00222 return $this->modules_pathname; 00223 } 00224 00228 public function setModulesPathname($pathname) { 00229 $this->modules_pathname = $pathname; 00230 } 00231 00232 }