modulediscovery.class.inc.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. class ModuleDiscovery
  17. {
  18. static $m_aModuleArgs = array(
  19. 'label' => 'One line description shown during the interactive setup',
  20. 'dependencies' => 'array of module ids',
  21. 'mandatory' => 'boolean',
  22. 'visible' => 'boolean',
  23. 'datamodel' => 'array of data model files',
  24. //'dictionary' => 'array of dictionary files', // No longer mandatory, now automated
  25. 'data.struct' => 'array of structural data files',
  26. 'data.sample' => 'array of sample data files',
  27. 'doc.manual_setup' => 'url',
  28. 'doc.more_information' => 'url',
  29. );
  30. // Cache the results and the source directory
  31. // Note that, as class can be declared within the module files, they cannot be loaded twice.
  32. // Then the following assumption is made: within the same execution page, the module
  33. // discovery CANNOT be executed on several different paths
  34. protected static $m_sModulesRoot = null;
  35. protected static $m_aModules = array();
  36. // All the entries below are list of file paths relative to the module directory
  37. protected static $m_aFilesList = array('datamodel', 'webservice', 'dictionary', 'data.struct', 'data.sample');
  38. // ModulePath is used by AddModule to get the path of the module being included (in ListModuleFiles)
  39. protected static $m_sModulePath = null;
  40. protected static function SetModulePath($sModulePath)
  41. {
  42. self::$m_sModulePath = $sModulePath;
  43. }
  44. public static function AddModule($sFilePath, $sId, $aArgs)
  45. {
  46. if (!array_key_exists('itop_version', $aArgs))
  47. {
  48. // Assume 1.0.2
  49. $aArgs['itop_version'] = '1.0.2';
  50. }
  51. foreach (self::$m_aModuleArgs as $sArgName => $sArgDesc)
  52. {
  53. if (!array_key_exists($sArgName, $aArgs))
  54. {
  55. throw new Exception("Module '$sId': missing argument '$sArgName'");
  56. }
  57. }
  58. $aArgs['root_dir'] = dirname($sFilePath);
  59. $aArgs['module_file'] = $sFilePath;
  60. self::$m_aModules[$sId] = $aArgs;
  61. foreach(self::$m_aFilesList as $sAttribute)
  62. {
  63. if (isset(self::$m_aModules[$sId][$sAttribute]))
  64. {
  65. // All the items below are list of files, that are relative to the current file
  66. // being loaded, let's update their path to store path relative to the application directory
  67. foreach(self::$m_aModules[$sId][$sAttribute] as $idx => $sRelativePath)
  68. {
  69. self::$m_aModules[$sId][$sAttribute][$idx] = self::$m_sModulePath.'/'.$sRelativePath;
  70. }
  71. }
  72. }
  73. // Populate automatically the list of dictionary files
  74. if(preg_match('|^([^/]+)|', $sId, $aMatches)) // ModuleName = everything before the first forward slash
  75. {
  76. $sModuleName = $aMatches[1];
  77. $sDir = dirname($sFilePath);
  78. if ($hDir = opendir($sDir))
  79. {
  80. while (($sFile = readdir($hDir)) !== false)
  81. {
  82. $aMatches = array();
  83. if (preg_match("/^[^\\.]+.dict.$sModuleName.php$/i", $sFile, $aMatches)) // Dictionary files named like <Lang>.dict.<ModuleName>.php are loaded automatically
  84. {
  85. self::$m_aModules[$sId]['dictionary'][] = self::$m_sModulePath.'/'.$sFile;
  86. }
  87. }
  88. closedir($hDir);
  89. }
  90. }
  91. }
  92. protected static function GetModules($oP = null)
  93. {
  94. // Order the modules to take into account their inter-dependencies
  95. $aDependencies = array();
  96. foreach(self::$m_aModules as $sId => $aModule)
  97. {
  98. $aDependencies[$sId] = $aModule['dependencies'];
  99. }
  100. $aOrderedModules = array();
  101. $iLoopCount = 1;
  102. while(($iLoopCount < count(self::$m_aModules)) && (count($aDependencies) > 0) )
  103. {
  104. foreach($aDependencies as $sId => $aRemainingDeps)
  105. {
  106. $bDependenciesSolved = true;
  107. foreach($aRemainingDeps as $sDepId)
  108. {
  109. if (!in_array($sDepId, $aOrderedModules))
  110. {
  111. $bDependenciesSolved = false;
  112. }
  113. }
  114. if ($bDependenciesSolved)
  115. {
  116. $aOrderedModules[] = $sId;
  117. unset($aDependencies[$sId]);
  118. }
  119. }
  120. $iLoopCount++;
  121. }
  122. if (count($aDependencies) >0)
  123. {
  124. $sHtml = "<ul><b>Warning: the following modules have unmet dependencies, and have been ignored:</b>\n";
  125. foreach($aDependencies as $sId => $aDeps)
  126. {
  127. $aModule = self::$m_aModules[$sId];
  128. $sHtml.= "<li>{$aModule['label']} (id: $sId), depends on: ".implode(', ', $aDeps)."</li>";
  129. }
  130. $sHtml .= "</ul>\n";
  131. if ($oP instanceof SetupPage)
  132. {
  133. $oP->warning($sHtml); // used in the context of the installation
  134. }
  135. elseif (class_exists('SetupPage'))
  136. {
  137. SetupPage::log_warning($sHtml); // used in the context of ?
  138. }
  139. else
  140. {
  141. echo $sHtml; // used in the context of the compiler
  142. }
  143. }
  144. // Return the ordered list, so that the dependencies are met...
  145. $aResult = array();
  146. foreach($aOrderedModules as $sId)
  147. {
  148. $aResult[$sId] = self::$m_aModules[$sId];
  149. }
  150. return $aResult;
  151. }
  152. /**
  153. * Search (on the disk) for all defined iTop modules, load them and returns the list (as an array)
  154. * of the possible iTop modules to install
  155. * @param sRootDir Application root directory
  156. * @param sSearchDir Directory to search (relative to root dir)
  157. * @return Hash A big array moduleID => ModuleData
  158. */
  159. public static function GetAvailableModules($sRootDir, $sSearchDir, $oP = null)
  160. {
  161. $sLookupDir = realpath($sRootDir.'/'.$sSearchDir);
  162. if (is_null(self::$m_sModulesRoot))
  163. {
  164. // First call
  165. //
  166. if ($sLookupDir == '')
  167. {
  168. throw new Exception("Invalid directory '$sRootDir/$sSearchDir'");
  169. }
  170. self::$m_sModulesRoot = $sLookupDir;
  171. clearstatcache();
  172. self::ListModuleFiles($sSearchDir, $sRootDir);
  173. return self::GetModules($oP);
  174. }
  175. elseif (self::$m_sModulesRoot != $sLookupDir)
  176. {
  177. throw new Exception("Design issue: the discovery of modules cannot be made on two different paths (previous: ".self::$m_sModulesRoot.", new: $sLookupDir)");
  178. }
  179. else
  180. {
  181. // Reuse the previous results
  182. //
  183. return self::GetModules($oP);
  184. }
  185. }
  186. /**
  187. * Helper function to interpret the name of a module
  188. * @param $sModuleId string Identifier of the module, in the form 'name/version'
  189. * @return array(name, version)
  190. */
  191. public static function GetModuleName($sModuleId)
  192. {
  193. if (preg_match('!^(.*)/(.*)$!', $sModuleId, $aMatches))
  194. {
  195. $sName = $aMatches[1];
  196. $sVersion = $aMatches[2];
  197. }
  198. else
  199. {
  200. $sName = $sModuleId;
  201. $sVersion = "";
  202. }
  203. return array($sName, $sVersion);
  204. }
  205. /**
  206. * Helper function to browse a directory and get the modules
  207. * @param $sRelDir string Directory to start from
  208. * @return array(name, version)
  209. */
  210. protected static function ListModuleFiles($sRelDir, $sRootDir)
  211. {
  212. $sDirectory = $sRootDir.'/'.$sRelDir;
  213. //echo "<p>$sDirectory</p>\n";
  214. if ($hDir = opendir($sDirectory))
  215. {
  216. // This is the correct way to loop over the directory. (according to the documentation)
  217. while (($sFile = readdir($hDir)) !== false)
  218. {
  219. $aMatches = array();
  220. if (is_dir($sDirectory.'/'.$sFile))
  221. {
  222. if (($sFile != '.') && ($sFile != '..') && ($sFile != '.svn'))
  223. {
  224. self::ListModuleFiles($sRelDir.'/'.$sFile, $sRootDir);
  225. }
  226. }
  227. else if (preg_match('/^module\.(.*).php$/i', $sFile, $aMatches))
  228. {
  229. self::SetModulePath($sRelDir);
  230. try
  231. {
  232. //echo "<p>Loading: $sDirectory/$sFile...</p>\n";
  233. //SetupPage::log_info("Discovered module $sFile");
  234. require_once($sDirectory.'/'.$sFile);
  235. //echo "<p>Done.</p>\n";
  236. }
  237. catch(Exception $e)
  238. {
  239. // Continue...
  240. }
  241. }
  242. }
  243. closedir($hDir);
  244. }
  245. else
  246. {
  247. throw new Exception("Data directory (".$sDirectory.") not found or not readable.");
  248. }
  249. }
  250. } // End of class
  251. /** Alias for backward compatibility with old module files in which
  252. * the declaration of a module invokes SetupWebPage::AddModule()
  253. * whereas the new form is ModuleDiscovery::AddModule()
  254. */
  255. class SetupWebPage extends ModuleDiscovery{}
  256. ?>