modulediscovery.class.inc.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. self::$m_aModules[$sId] = $aArgs;
  60. foreach(self::$m_aFilesList as $sAttribute)
  61. {
  62. if (isset(self::$m_aModules[$sId][$sAttribute]))
  63. {
  64. // All the items below are list of files, that are relative to the current file
  65. // being loaded, let's update their path to store path relative to the application directory
  66. foreach(self::$m_aModules[$sId][$sAttribute] as $idx => $sRelativePath)
  67. {
  68. self::$m_aModules[$sId][$sAttribute][$idx] = self::$m_sModulePath.'/'.$sRelativePath;
  69. }
  70. }
  71. }
  72. // Populate automatically the list of dictionary files
  73. if(preg_match('|^([^/]+)|', $sId, $aMatches)) // ModuleName = everything before the first forward slash
  74. {
  75. $sModuleName = $aMatches[1];
  76. $sDir = dirname($sFilePath);
  77. if ($hDir = opendir($sDir))
  78. {
  79. while (($sFile = readdir($hDir)) !== false)
  80. {
  81. $aMatches = array();
  82. if (preg_match("/^[^\\.]+.dict.$sModuleName.php$/i", $sFile, $aMatches)) // Dictionary files named like <Lang>.dict.<ModuleName>.php are loaded automatically
  83. {
  84. self::$m_aModules[$sId]['dictionary'][] = self::$m_sModulePath.'/'.$sFile;
  85. }
  86. }
  87. closedir($hDir);
  88. }
  89. }
  90. }
  91. protected static function GetModules($oP = null)
  92. {
  93. // Order the modules to take into account their inter-dependencies
  94. $aDependencies = array();
  95. foreach(self::$m_aModules as $sId => $aModule)
  96. {
  97. $aDependencies[$sId] = $aModule['dependencies'];
  98. }
  99. $aOrderedModules = array();
  100. $iLoopCount = 1;
  101. while(($iLoopCount < count(self::$m_aModules)) && (count($aDependencies) > 0) )
  102. {
  103. foreach($aDependencies as $sId => $aRemainingDeps)
  104. {
  105. $bDependenciesSolved = true;
  106. foreach($aRemainingDeps as $sDepId)
  107. {
  108. if (!in_array($sDepId, $aOrderedModules))
  109. {
  110. $bDependenciesSolved = false;
  111. }
  112. }
  113. if ($bDependenciesSolved)
  114. {
  115. $aOrderedModules[] = $sId;
  116. unset($aDependencies[$sId]);
  117. }
  118. }
  119. $iLoopCount++;
  120. }
  121. if (count($aDependencies) >0)
  122. {
  123. $sHtml = "<ul><b>Warning: the following modules have unmet dependencies, and have been ignored:</b>\n";
  124. foreach($aDependencies as $sId => $aDeps)
  125. {
  126. $aModule = self::$m_aModules[$sId];
  127. $sHtml.= "<li>{$aModule['label']} (id: $sId), depends on: ".implode(', ', $aDeps)."</li>";
  128. }
  129. $sHtml .= "</ul>\n";
  130. if ($oP instanceof SetupPage)
  131. {
  132. $oP->warning($sHtml); // used in the context of the installation
  133. }
  134. elseif (class_exists('SetupPage'))
  135. {
  136. SetupPage::log_warning($sHtml); // used in the context of ?
  137. }
  138. else
  139. {
  140. echo $sHtml; // used in the context of the compiler
  141. }
  142. }
  143. // Return the ordered list, so that the dependencies are met...
  144. $aResult = array();
  145. foreach($aOrderedModules as $sId)
  146. {
  147. $aResult[$sId] = self::$m_aModules[$sId];
  148. }
  149. return $aResult;
  150. }
  151. /**
  152. * Search (on the disk) for all defined iTop modules, load them and returns the list (as an array)
  153. * of the possible iTop modules to install
  154. * @param sRootDir Application root directory
  155. * @param sSearchDir Directory to search (relative to root dir)
  156. * @return Hash A big array moduleID => ModuleData
  157. */
  158. public static function GetAvailableModules($sRootDir, $sSearchDir, $oP = null)
  159. {
  160. $sLookupDir = realpath($sRootDir.'/'.$sSearchDir);
  161. if (is_null(self::$m_sModulesRoot))
  162. {
  163. // First call
  164. //
  165. if ($sLookupDir == '')
  166. {
  167. throw new Exception("Invalid directory '$sRootDir/$sSearchDir'");
  168. }
  169. self::$m_sModulesRoot = $sLookupDir;
  170. clearstatcache();
  171. self::ListModuleFiles($sSearchDir, $sRootDir);
  172. return self::GetModules($oP);
  173. }
  174. elseif (self::$m_sModulesRoot != $sLookupDir)
  175. {
  176. throw new Exception("Design issue: the discovery of modules cannot be made on two different paths (previous: ".self::$m_sModulesRoot.", new: $sLookupDir)");
  177. }
  178. else
  179. {
  180. // Reuse the previous results
  181. //
  182. return self::GetModules($oP);
  183. }
  184. }
  185. /**
  186. * Helper function to interpret the name of a module
  187. * @param $sModuleId string Identifier of the module, in the form 'name/version'
  188. * @return array(name, version)
  189. */
  190. public static function GetModuleName($sModuleId)
  191. {
  192. if (preg_match('!^(.*)/(.*)$!', $sModuleId, $aMatches))
  193. {
  194. $sName = $aMatches[1];
  195. $sVersion = $aMatches[2];
  196. }
  197. else
  198. {
  199. $sName = $sModuleId;
  200. $sVersion = "";
  201. }
  202. return array($sName, $sVersion);
  203. }
  204. /**
  205. * Helper function to browse a directory and get the modules
  206. * @param $sRelDir string Directory to start from
  207. * @return array(name, version)
  208. */
  209. protected static function ListModuleFiles($sRelDir, $sRootDir)
  210. {
  211. $sDirectory = $sRootDir.'/'.$sRelDir;
  212. //echo "<p>$sDirectory</p>\n";
  213. if ($hDir = opendir($sDirectory))
  214. {
  215. // This is the correct way to loop over the directory. (according to the documentation)
  216. while (($sFile = readdir($hDir)) !== false)
  217. {
  218. $aMatches = array();
  219. if (is_dir($sDirectory.'/'.$sFile))
  220. {
  221. if (($sFile != '.') && ($sFile != '..') && ($sFile != '.svn'))
  222. {
  223. self::ListModuleFiles($sRelDir.'/'.$sFile, $sRootDir);
  224. }
  225. }
  226. else if (preg_match('/^module\.(.*).php$/i', $sFile, $aMatches))
  227. {
  228. self::SetModulePath($sRelDir);
  229. try
  230. {
  231. //echo "<p>Loading: $sDirectory/$sFile...</p>\n";
  232. //SetupPage::log_info("Discovered module $sFile");
  233. require_once($sDirectory.'/'.$sFile);
  234. //echo "<p>Done.</p>\n";
  235. }
  236. catch(Exception $e)
  237. {
  238. // Continue...
  239. }
  240. }
  241. }
  242. closedir($hDir);
  243. }
  244. else
  245. {
  246. throw new Exception("Data directory (".$sDirectory.") not found or not readable.");
  247. }
  248. }
  249. } // End of class
  250. /** Alias for backward compatibility with old module files in which
  251. * the declaration of a module invokes SetupWebPage::AddModule()
  252. * whereas the new form is ModuleDiscovery::AddModule()
  253. */
  254. class SetupWebPage extends ModuleDiscovery{}
  255. ?>