modelfactory.class.inc.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. <?php
  2. // Copyright (C) 2011 Combodo SARL
  3. //
  4. /**
  5. * ModelFactory: in-memory manipulation of the XML MetaModel
  6. *
  7. * @author Erwan Taloc <erwan.taloc@combodo.com>
  8. * @author Romain Quetiez <romain.quetiez@combodo.com>
  9. * @author Denis Flaven <denis.flaven@combodo.com>
  10. * @license Combodo Private
  11. */
  12. require_once(APPROOT.'setup/moduleinstaller.class.inc.php');
  13. /**
  14. * ModelFactoryItem: an item managed by the ModuleFactory
  15. * @package ModelFactory
  16. */
  17. abstract class MFItem
  18. {
  19. public function __construct($sName, $sValue)
  20. {
  21. parent::__construct($sName, $sValue);
  22. }
  23. /**
  24. * List the source files for this item
  25. */
  26. public function ListSources()
  27. {
  28. }
  29. /**
  30. * List the rights/restrictions for this item
  31. */
  32. public function ListRights()
  33. {
  34. }
  35. }
  36. /**
  37. * ModelFactoryModule: the representation of a Module (i.e. element that can be selected during the setup)
  38. * @package ModelFactory
  39. */
  40. class MFModule extends MFItem
  41. {
  42. protected $sId;
  43. protected $sName;
  44. protected $sVersion;
  45. protected $sRootDir;
  46. protected $sLabel;
  47. protected $aDataModels;
  48. public function __construct($sId, $sRootDir, $sLabel)
  49. {
  50. $this->sId = $sId;
  51. list($this->sName, $this->sVersion) = ModuleDiscovery::GetModuleName($sId);
  52. if (strlen($this->sVersion) == 0)
  53. {
  54. $this->sVersion = '1.0.0';
  55. }
  56. $this->sRootDir = $sRootDir;
  57. $this->sLabel = $sLabel;
  58. $this->aDataModels = array();
  59. // Scan the module's root directory to find the datamodel(*).xml files
  60. if ($hDir = opendir($sRootDir))
  61. {
  62. // This is the correct way to loop over the directory. (according to the documentation)
  63. while (($sFile = readdir($hDir)) !== false)
  64. {
  65. if (preg_match('/^datamodel(.*)\.xml$/i', $sFile, $aMatches))
  66. {
  67. $this->aDataModels[] = $this->sRootDir.'/'.$aMatches[0];
  68. }
  69. }
  70. closedir($hDir);
  71. }
  72. }
  73. public function GetId()
  74. {
  75. return $this->sId;
  76. }
  77. public function GetName()
  78. {
  79. return $this->sName;
  80. }
  81. public function GetVersion()
  82. {
  83. return $this->sVersion;
  84. }
  85. public function GetLabel()
  86. {
  87. return $this->sLabel;
  88. }
  89. public function GetRootDir()
  90. {
  91. return $this->sRootDir;
  92. }
  93. public function GetModuleDir()
  94. {
  95. return basename($this->sRootDir);
  96. }
  97. public function GetDataModelFiles()
  98. {
  99. return $this->aDataModels;
  100. }
  101. /**
  102. * List all classes in this module
  103. */
  104. public function ListClasses()
  105. {
  106. return array();
  107. }
  108. }
  109. class MFWorkspace extends MFModule
  110. {
  111. public function __construct($sRootDir)
  112. {
  113. $this->sId = 'itop-workspace';
  114. $this->sName = 'workspace';
  115. $this->sVersion = '1.0';
  116. $this->sRootDir = $sRootDir;
  117. $this->sLabel = 'Workspace';
  118. $this->aDataModels = array();
  119. $this->aDataModels[] = $this->GetWorkspacePath();
  120. }
  121. public function GetWorkspacePath()
  122. {
  123. return $this->sRootDir.'/workspace.xml';
  124. }
  125. public function GetName()
  126. {
  127. return ''; // The workspace itself has no name so that objects created inside it retain their original module's name
  128. }
  129. }
  130. /**
  131. * ModelFactoryClass: the representation of a Class (i.e. a PHP class)
  132. * @package ModelFactory
  133. */
  134. class MFClass extends MFItem
  135. {
  136. /**
  137. * List all fields of this class
  138. */
  139. public function ListFields()
  140. {
  141. return array();
  142. }
  143. /**
  144. * List all methods of this class
  145. */
  146. public function ListMethods()
  147. {
  148. return array();
  149. }
  150. /**
  151. * Whether or not the class has a lifecycle
  152. * @return bool
  153. */
  154. public function HasLifeCycle()
  155. {
  156. return true; //TODO implement
  157. }
  158. /**
  159. * Returns the code of the attribute used to store the lifecycle state
  160. * @return string
  161. */
  162. public function GetLifeCycleAttCode()
  163. {
  164. if ($this->HasLifeCycle())
  165. {
  166. }
  167. return '';
  168. }
  169. /**
  170. * List all states of this class
  171. */
  172. public function ListStates()
  173. {
  174. return array();
  175. }
  176. /**
  177. * List all relations of this class
  178. */
  179. public function ListRelations()
  180. {
  181. return array();
  182. }
  183. /**
  184. * List all transitions of this class
  185. */
  186. public function ListTransitions()
  187. {
  188. return array();
  189. }
  190. }
  191. /**
  192. * ModelFactoryField: the representation of a Field (i.e. a property of a class)
  193. * @package ModelFactory
  194. */
  195. class MFField extends MFItem
  196. {
  197. }
  198. /**
  199. * ModelFactoryMethod: the representation of a Method (i.e. a method of a class)
  200. * @package ModelFactory
  201. */
  202. class MFMethod extends MFItem
  203. {
  204. }
  205. /**
  206. * ModelFactoryState: the representation of a state in the life cycle of the class
  207. * @package ModelFactory
  208. */
  209. class MFState extends MFItem
  210. {
  211. }
  212. /**
  213. * ModelFactoryRelation: the representation of a n:n relationship between two classes
  214. * @package ModelFactory
  215. */
  216. class MFRelation extends MFItem
  217. {
  218. }
  219. /**
  220. * ModelFactoryTransition: the representation of a transition between two states in the life cycle of the class
  221. * @package ModelFactory
  222. */
  223. class MFTransition extends MFItem
  224. {
  225. }
  226. /**
  227. * ModelFactory: the class that manages the in-memory representation of the XML MetaModel
  228. * @package ModelFactory
  229. */
  230. class ModelFactory
  231. {
  232. protected $sRootDir;
  233. protected $oDOMDocument;
  234. protected $oRoot;
  235. protected $oClasses;
  236. protected $oMenus;
  237. static protected $aLoadedClasses;
  238. static protected $aLoadedMenus;
  239. static protected $aWellKnownParents = array('DBObject', 'CMDBObject','cmdbAbstractObject');
  240. static protected $aLoadedModules;
  241. public function __construct($sRootDir)
  242. {
  243. $this->sRootDir = $sRootDir;
  244. $this->oDOMDocument = new DOMDocument('1.0', 'UTF-8');
  245. $this->oRoot = $this->oDOMDocument->CreateElement('itop_design');
  246. $this->oDOMDocument->AppendChild($this->oRoot);
  247. $this->oClasses = $this->oDOMDocument->CreateElement('classes');
  248. $this->oRoot->AppendChild($this->oClasses);
  249. $this->oMenus = $this->oDOMDocument->CreateElement('menus');
  250. $this->oRoot->AppendChild($this->oMenus);
  251. self::$aLoadedClasses = array();
  252. self::$aLoadedMenus = array();
  253. self::$aLoadedModules = array();
  254. }
  255. public function Dump($oNode = null)
  256. {
  257. if (is_null($oNode))
  258. {
  259. $oNode = $this->oMenus;
  260. }
  261. echo htmlentities($this->oDOMDocument->saveXML($oNode));
  262. }
  263. /**
  264. * Loads the definitions corresponding to the given Module
  265. * @param MFModule $oModule
  266. */
  267. public function LoadModule(MFModule $oModule)
  268. {
  269. $aDataModels = $oModule->GetDataModelFiles();
  270. $sModuleName = $oModule->GetName();
  271. $aClasses = array();
  272. self::$aLoadedModules[] = $oModule;
  273. foreach($aDataModels as $sXmlFile)
  274. {
  275. $oDocument = new DOMDocument('1.0', 'UTF-8');
  276. $oDocument->load($sXmlFile, LIBXML_NOBLANKS);
  277. $oXPath = new DOMXPath($oDocument);
  278. $oNodeList = $oXPath->query('//*');
  279. foreach($oNodeList as $oNode)
  280. {
  281. $oNode->SetAttribute('_source', $sXmlFile);
  282. }
  283. $oXPath = new DOMXPath($oDocument);
  284. $oNodeList = $oXPath->query('/itop_design/classes/class');
  285. foreach($oNodeList as $oNode)
  286. {
  287. if ($oNode->hasAttribute('parent'))
  288. {
  289. $sParentClass = $oNode->GetAttribute('parent');
  290. }
  291. else
  292. {
  293. $sParentClass = '';
  294. }
  295. $sClassName = $oNode->GetAttribute('name');
  296. $aClasses[$sClassName] = array('name' => $sClassName, 'parent' => $sParentClass, 'node' => $oNode);
  297. }
  298. // Menus - temporary for compiling ???
  299. $oNodeList = $oXPath->query('/itop_design/menus/menu');
  300. foreach($oNodeList as $oNode)
  301. {
  302. $this->AddMenu($oNode, $sModuleName);
  303. }
  304. }
  305. $index = 1;
  306. do
  307. {
  308. $bNothingLoaded = true;
  309. foreach($aClasses as $sClassName => $aClassData)
  310. {
  311. $sOperation = $aClassData['node']->getAttribute('_operation');
  312. switch($sOperation)
  313. {
  314. case 'added':
  315. case '':
  316. if (in_array($aClassData['parent'], self::$aWellKnownParents))
  317. {
  318. $this->AddClass($aClassData['node'], $sModuleName);
  319. unset($aClasses[$sClassName]);
  320. $bNothingLoaded = false;
  321. }
  322. else if ($this->ClassNameExists($aClassData['parent']))
  323. {
  324. $this->AddClass($aClassData['node'], $sModuleName);
  325. unset($aClasses[$sClassName]);
  326. $bNothingLoaded = false;
  327. }
  328. break;
  329. case 'removed':
  330. unset($aClasses[$sClassName]);
  331. $this->RemoveClass($sClassName);
  332. break;
  333. case 'modified':
  334. unset($aClasses[$sClassName]);
  335. $this->AlterClass($sClassName, $aClassData['node']);
  336. break;
  337. }
  338. }
  339. $index++;
  340. }
  341. while((count($aClasses)>0) && !$bNothingLoaded);
  342. // The remaining classes have no known parent, let's add them at the root
  343. foreach($aClasses as $sClassName => $aClassData)
  344. {
  345. $sOperation = $aClassData['node']->getAttribute('_operation');
  346. switch($sOperation)
  347. {
  348. case 'added':
  349. case '':
  350. // Add the class as a new root class
  351. $this->AddClass($aClassData['node'], $sModuleName);
  352. $oNewNode = $this->oDOMDocument->ImportNode($aClassData['node']);
  353. break;
  354. case 'removed':
  355. $this->RemoveClass($sClassName);
  356. break;
  357. case 'modified':
  358. //@@TODO Handle the modification of a class here
  359. $this->AlterClass($sClassName, $aClassData['node']);
  360. break;
  361. }
  362. }
  363. }
  364. function GetLoadedModules($bExcludeWorkspace = true)
  365. {
  366. if ($bExcludeWorkspace)
  367. {
  368. $aModules = array();
  369. foreach(self::$aLoadedModules as $oModule)
  370. {
  371. if (!$oModule instanceof MFWorkspace)
  372. {
  373. $aModules[] = $oModule;
  374. }
  375. }
  376. }
  377. else
  378. {
  379. $aModules = self::$aLoadedModules;
  380. }
  381. return $aModules;
  382. }
  383. function GetModule($sModuleName)
  384. {
  385. foreach(self::$aLoadedModules as $oModule)
  386. {
  387. if ($oModule->GetName() == $sModuleName) return $oModule;
  388. }
  389. return null;
  390. }
  391. /**
  392. * Check if the class specified by the given node already exists in the loaded DOM
  393. * @param DOMNode $oClassNode The node corresponding to the class to load
  394. * @throws Exception
  395. * @return bool True if the class exists, false otherwise
  396. */
  397. protected function ClassExists(DOMNode $oClassNode)
  398. {
  399. if ($oClassNode->hasAttribute('name'))
  400. {
  401. $sClassName = $oClassNode->GetAttribute('name');
  402. }
  403. else
  404. {
  405. throw new Exception('ModelFactory::AddClass: Cannot add a class with no name');
  406. }
  407. return (array_key_exists($sClassName, self::$aLoadedClasses));
  408. }
  409. /**
  410. * Check if the class specified by the given name already exists in the loaded DOM
  411. * @param string $sClassName The node corresponding to the class to load
  412. * @throws Exception
  413. * @return bool True if the class exists, false otherwise
  414. */
  415. protected function ClassNameExists($sClassName)
  416. {
  417. return (array_key_exists($sClassName, self::$aLoadedClasses));
  418. }
  419. /**
  420. * Add the given class to the DOM
  421. * @param DOMNode $oClassNode
  422. * @param string $sModuleName The name of the module in which this class is declared
  423. * @throws Exception
  424. */
  425. public function AddClass(DOMNode $oClassNode, $sModuleName)
  426. {
  427. if ($oClassNode->hasAttribute('name'))
  428. {
  429. $sClassName = $oClassNode->GetAttribute('name');
  430. }
  431. else
  432. {
  433. throw new Exception('ModelFactory::AddClass: Cannot add a class with no name');
  434. }
  435. if ($this->ClassExists($oClassNode))
  436. {
  437. throw new Exception("ModelFactory::AddClass: Cannot add the already existing class $sClassName");
  438. }
  439. $sParentClass = '';
  440. if ($oClassNode->hasAttribute('parent'))
  441. {
  442. $sParentClass = $oClassNode->GetAttribute('parent');
  443. }
  444. //echo "Adding class: $sClassName, parent: $sParentClass<br/>";
  445. if (!in_array($sParentClass, self::$aWellKnownParents) && $this->ClassNameExists($sParentClass))
  446. {
  447. // The class is a subclass of a class already loaded, add it under
  448. self::$aLoadedClasses[$sClassName] = $this->oDOMDocument->ImportNode($oClassNode, true /* bDeep */);
  449. self::$aLoadedClasses[$sClassName]->SetAttribute('_operation', 'added');
  450. if ($sModuleName != '')
  451. {
  452. self::$aLoadedClasses[$sClassName]->SetAttribute('_created_in', $sModuleName);
  453. }
  454. self::$aLoadedClasses[$sParentClass]->AppendChild(self::$aLoadedClasses[$sClassName]);
  455. $bNothingLoaded = false;
  456. }
  457. else if (in_array($sParentClass, self::$aWellKnownParents))
  458. {
  459. // Add the class as a new root class
  460. self::$aLoadedClasses[$sClassName] = $this->oDOMDocument->ImportNode($oClassNode, true /* bDeep */);
  461. self::$aLoadedClasses[$sClassName]->SetAttribute('_operation', 'added');
  462. if ($sModuleName != '')
  463. {
  464. self::$aLoadedClasses[$sClassName]->SetAttribute('_created_in', $sModuleName);
  465. }
  466. $this->oClasses->AppendChild(self::$aLoadedClasses[$sClassName]);
  467. }
  468. else
  469. {
  470. throw new Exception("ModelFactory::AddClass: Cannot add the class $sClassName, unknown parent class: $sParentClass");
  471. }
  472. }
  473. /**
  474. * Remove a class from the DOM
  475. * @param string $sClass
  476. * @throws Exception
  477. */
  478. public function RemoveClass($sClass)
  479. {
  480. if (!$this->ClassNameExists($sClass))
  481. {
  482. throw new Exception("ModelFactory::RemoveClass: Cannot remove the non existing class $sClass");
  483. }
  484. $oClassNode = self::$aLoadedClasses[$sClass];
  485. if ($oClassNode->getAttribute('_operation') == 'added')
  486. {
  487. $oClassNode->parentNode->RemoveChild($oClassNode);
  488. unset(self::$aLoadedClasses[$sClass]);
  489. }
  490. else
  491. {
  492. self::$aLoadedClasses[$sClass]->SetAttribute('_operation', 'removed');
  493. //TODO: also mark as removed the child classes
  494. }
  495. }
  496. /**
  497. * Modify a class within the DOM
  498. * @param string $sMenuId
  499. * @param DOMNode $oMenuNode
  500. * @throws Exception
  501. */
  502. public function AlterClass($sClassName, DOMNode $oClassNode)
  503. {
  504. $sOriginalName = $sClassName;
  505. if ($this->ClassNameExists($sClassName))
  506. {
  507. $oDestNode = self::$aLoadedClasses[$sClassName];
  508. }
  509. else
  510. {
  511. $sOriginalName = $oClassNode->getAttribute('_original_name');
  512. if ($this->ClassNameExists($sOriginalName))
  513. {
  514. // Class was renamed !
  515. $oDestNode = self::$aLoadedClasses[$sOriginalName];
  516. }
  517. else
  518. {
  519. throw new Exception("ModelFactory::AddClass: Cannot alter the non-existing class $sClassName / $sOriginalName");
  520. }
  521. }
  522. $this->_priv_AlterNode($oDestNode, $oClassNode);
  523. $sClassName = $oDestNode->getAttribute('name');
  524. if ($sOriginalName != $sClassName)
  525. {
  526. unset(self::$aLoadedClasses[$sOriginalName]);
  527. self::$aLoadedClasses[$sClassName] = $oDestNode;
  528. }
  529. $this->_priv_SetFlag($oDestNode, 'modified');
  530. }
  531. /**
  532. * Add the given menu to the DOM
  533. * @param DOMNode $oMenuNode
  534. * @param string $sModuleName The name of the module in which this class is declared
  535. * @throws Exception
  536. */
  537. public function AddMenu($oMenuNode, $sModuleName)
  538. {
  539. $sMenuId = $oMenuNode->GetAttribute('id');
  540. self::$aLoadedMenus[$sMenuId] = $this->oDOMDocument->ImportNode($oMenuNode, true /* bDeep */);
  541. self::$aLoadedMenus[$sMenuId]->SetAttribute('_operation', 'added');
  542. if ($sModuleName != '')
  543. {
  544. self::$aLoadedMenus[$sMenuId]->SetAttribute('_created_in', $sModuleName);
  545. }
  546. $this->oMenus->AppendChild(self::$aLoadedMenus[$sMenuId]);
  547. }
  548. /**
  549. * Remove a menu from the DOM
  550. * @param string $sMenuId
  551. * @throws Exception
  552. */
  553. public function RemoveMenu($sMenuId)
  554. {
  555. $oMenuNode = self::$aLoadedMenus[$sClass];
  556. if ($oMenuNode->getAttribute('_operation') == 'added')
  557. {
  558. $oMenuNode->parentNode->RemoveChild($oMenuNode);
  559. unset(self::$aLoadedMenus[$sMenuId]);
  560. }
  561. else
  562. {
  563. self::$aLoadedMenus[$sMenuId]->SetAttribute('_operation', 'removed');
  564. }
  565. }
  566. /**
  567. * Modify a menu within the DOM
  568. * @param string $sMenuId
  569. * @param DOMNode $oMenuNode
  570. * @throws Exception
  571. */
  572. public function AlterMenu($sMenuId, DOMNode $oMenuNode)
  573. {
  574. // Todo - implement... do we have to handle menu renaming ???
  575. }
  576. protected function _priv_AlterNode(DOMNode $oNode, DOMNode $oDeltaNode)
  577. {
  578. foreach ($oDeltaNode->attributes as $sName => $oAttrNode)
  579. {
  580. $sCurrentValue = $oNode->getAttribute($sName);
  581. $sNewValue = $oAttrNode->value;
  582. $oNode->setAttribute($sName, $oAttrNode->value);
  583. }
  584. $aSrcChildNodes = $oNode->childNodes;
  585. foreach($oDeltaNode->childNodes as $index => $oChildNode)
  586. {
  587. if (!$oChildNode instanceof DOMElement)
  588. {
  589. // Text or CData nodes are treated by position
  590. $sOperation = $oChildNode->parentNode->getAttribute('_operation');
  591. switch($sOperation)
  592. {
  593. case 'removed':
  594. // ???
  595. break;
  596. case 'modified':
  597. case 'replaced':
  598. case 'added':
  599. $oNewNode = $this->oDOMDocument->importNode($oChildNode);
  600. $oSrcChildNode = $aSrcChildNodes->item($index);
  601. if ($oSrcChildNode)
  602. {
  603. $oNode->replaceChild($oNewNode, $oSrcChildNode);
  604. }
  605. else
  606. {
  607. $oNode->appendChild($oNewNode);
  608. }
  609. break;
  610. case '':
  611. // Do nothing
  612. }
  613. }
  614. else
  615. {
  616. $sOperation = $oChildNode->getAttribute('_operation');
  617. $sPath = $oChildNode->tagName;
  618. $sName = $oChildNode->getAttribute('name');
  619. if ($sName != '')
  620. {
  621. $sPath .= "[@name='$sName']";
  622. }
  623. switch($sOperation)
  624. {
  625. case 'removed':
  626. $oToRemove = $this->_priv_GetNodes($sPath, $oNode)->item(0);
  627. if ($oToRemove != null)
  628. {
  629. $this->_priv_SetFlag($oToRemove, 'removed');
  630. }
  631. break;
  632. case 'modified':
  633. $oToModify = $this->_priv_GetNodes($sPath, $oNode)->item(0);
  634. if ($oToModify != null)
  635. {
  636. $this->_priv_AlterNode($oToModify, $oChildNode);
  637. }
  638. else
  639. {
  640. throw new Exception("Cannot modify the non-existing node '$sPath' in '".$oNode->getNodePath()."'");
  641. }
  642. break;
  643. case 'replaced':
  644. $oNewNode = $this->oDOMDocument->importNode($oChildNode, true); // Import the node and its child nodes
  645. $oToModify = $this->_priv_GetNodes($sPath, $oNode)->item(0);
  646. $oNode->replaceChild($oNewNode, $oToModify);
  647. break;
  648. case 'added':
  649. $oNewNode = $this->oDOMDocument->importNode($oChildNode);
  650. $oNode->appendChild($oNewNode);
  651. $this->_priv_SetFlag($oNewNode, 'added');
  652. break;
  653. case '':
  654. // Do nothing
  655. }
  656. }
  657. }
  658. }
  659. public function GetClassXMLTemplate($sName, $sIcon)
  660. {
  661. return
  662. <<<EOF
  663. <?xml version="1.0" encoding="utf-8"?>
  664. <class name="$sName" parent="" db_table="" category="" abstract="" key_type="autoincrement" db_key_field="id" db_final_class_field="finalclass">
  665. <properties>
  666. <comment/>
  667. <naming format=""><attributes/></naming>
  668. <reconciliation><attributes/></reconciliation>
  669. <display_template/>
  670. <icon>$sIcon</icon>
  671. </properties>
  672. <fields/>
  673. <methods/>
  674. <presentation>
  675. <details><items/></details>
  676. <search><items/></search>
  677. <list><items/></list>
  678. </presentation>
  679. </class>
  680. EOF
  681. ;
  682. }
  683. /**
  684. * List all classes from the DOM, for a given module
  685. * @param string $sModuleNale
  686. * @param bool $bFlattenLayers
  687. * @throws Exception
  688. */
  689. public function ListClasses($sModuleName, $bFlattenLayers = true)
  690. {
  691. $sXPath = "//class[@_created_in='$sModuleName']";
  692. if ($bFlattenLayers)
  693. {
  694. $sXPath = "//class[@_created_in='$sModuleName' and @_operation!='removed']";
  695. }
  696. return $this->_priv_GetNodes($sXPath);
  697. }
  698. /**
  699. * List all classes from the DOM, for a given module
  700. * @param string $sModuleNale
  701. * @param bool $bFlattenLayers
  702. * @throws Exception
  703. */
  704. public function ListAllClasses($bFlattenLayers = true)
  705. {
  706. $sXPath = "//class";
  707. if ($bFlattenLayers)
  708. {
  709. $sXPath = "//class[@_operation!='removed']";
  710. }
  711. return $this->_priv_GetNodes($sXPath);
  712. }
  713. public function GetClass($sClassName, $bFlattenLayers = true)
  714. {
  715. if (!$this->ClassNameExists($sClassName))
  716. {
  717. return null;
  718. }
  719. $oClassNode = self::$aLoadedClasses[$sClassName];
  720. if ($bFlattenLayers)
  721. {
  722. $sOperation = $oClassNode->getAttribute('_operation');
  723. if ($sOperation == 'removed')
  724. {
  725. $oClassNode = null;
  726. }
  727. }
  728. return $oClassNode;
  729. }
  730. public function GetChildClasses($oClassNode, $bFlattenLayers = true)
  731. {
  732. $sXPath = "class";
  733. if ($bFlattenLayers)
  734. {
  735. $sXPath = "class[(@_operation!='removed')]";
  736. }
  737. return $this->_priv_GetNodes($sXPath, $oClassNode);
  738. }
  739. public function GetField($sClassName, $sAttCode, $bFlattenLayers = true)
  740. {
  741. if (!$this->ClassNameExists($sClassName))
  742. {
  743. return null;
  744. }
  745. $oClassNode = self::$aLoadedClasses[$sClassName];
  746. if ($bFlattenLayers)
  747. {
  748. $sOperation = $oClassNode->getAttribute('_operation');
  749. if ($sOperation == 'removed')
  750. {
  751. $oClassNode = null;
  752. }
  753. }
  754. $sXPath = "fields/field[@name='$sAttCode']";
  755. if ($bFlattenLayers)
  756. {
  757. $sXPath = "fields/field[(@name='$sAttCode' and (not(@_operation) or @_operation!='removed'))]";
  758. }
  759. $oFieldNode = $this->_priv_GetNodes($sXPath, $oClassNode)->item(0);
  760. if (($oFieldNode == null) && ($oClassNode->getAttribute('parent') != ''))
  761. {
  762. return $this->GetField($oClassNode->getAttribute('parent'), $sAttCode, $bFlattenLayers);
  763. }
  764. return $oFieldNode;
  765. }
  766. /**
  767. * List all classes from the DOM
  768. * @param bool $bFlattenLayers
  769. * @throws Exception
  770. */
  771. public function ListFields(DOMNode $oClassNode, $bFlattenLayers = true)
  772. {
  773. $sXPath = "fields/field";
  774. if ($bFlattenLayers)
  775. {
  776. $sXPath = "fields/field[not(@_operation) or @_operation!='removed']";
  777. }
  778. return $this->_priv_GetNodes($sXPath, $oClassNode);
  779. }
  780. public function AddField(DOMNode $oClassNode, $sFieldCode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
  781. {
  782. $oNewField = $this->oDOMDocument->createElement('field');
  783. $oNewField->setAttribute('name', $sFieldCode);
  784. $this->_priv_AlterField($oNewField, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams);
  785. $oFields = $oClassNode->getElementsByTagName('fields')->item(0);
  786. $oFields->AppendChild($oNewField);
  787. $this->_priv_SetFlag($oNewField, 'added');
  788. }
  789. public function RemoveField(DOMNode $oClassNode, $sFieldCode)
  790. {
  791. $sXPath = "fields/field[@name='$sFieldCode']";
  792. $oFieldNodes = $this->_priv_GetNodes($sXPath, $oClassNode);
  793. if (is_object($oFieldNodes) && (is_object($oFieldNodes->item(0))))
  794. {
  795. $oFieldNode = $oFieldNodes->item(0);
  796. $sOpCode = $oFieldNode->getAttribute('_operation');
  797. if ($oFieldNode->getAttribute('_operation') == 'added')
  798. {
  799. $oFieldNode->parentNode->removeChild($oFieldNode);
  800. }
  801. else
  802. {
  803. $this->_priv_SetFlag($oFieldNode, 'removed');
  804. }
  805. }
  806. }
  807. public function AlterField(DOMNode $oClassNode, $sFieldCode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
  808. {
  809. $sXPath = "fields/field[@name='$sFieldCode']";
  810. $oFieldNodes = $this->_priv_GetNodes($sXPath, $oClassNode);
  811. if (is_object($oFieldNodes) && (is_object($oFieldNodes->item(0))))
  812. {
  813. $oFieldNode = $oFieldNodes->item(0);
  814. //@@TODO: if the field was 'added' => then let it as 'added'
  815. $sOpCode = $oFieldNode->getAttribute('_operation');
  816. switch($sOpCode)
  817. {
  818. case 'added':
  819. case 'modified':
  820. // added or modified, let it as it is
  821. break;
  822. default:
  823. $this->_priv_SetFlag($oFieldNodes->item(0), 'modified');
  824. }
  825. $this->_priv_AlterField($oFieldNodes->item(0), $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams);
  826. }
  827. }
  828. protected function _priv_AlterField(DOMNode $oFieldNode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
  829. {
  830. switch($sFieldType)
  831. {
  832. case 'Blob':
  833. case 'Boolean':
  834. case 'CaseLog':
  835. case 'Deadline':
  836. case 'Duration':
  837. case 'EmailAddress':
  838. case 'EncryptedString':
  839. case 'HTML':
  840. case 'IPAddress':
  841. case 'LongText':
  842. case 'OQL':
  843. case 'OneWayPassword':
  844. case 'Password':
  845. case 'Percentage':
  846. case 'String':
  847. case 'Text':
  848. case 'Text':
  849. case 'TemplateHTML':
  850. case 'TemplateString':
  851. case 'TemplateText':
  852. case 'URL':
  853. case 'Date':
  854. case 'DateTime':
  855. case 'Decimal':
  856. case 'Integer':
  857. break;
  858. case 'ExternalKey':
  859. $this->_priv_AddFieldAttribute($oFieldNode, 'target_class', $aExtraParams);
  860. // Fall through
  861. case 'HierarchicalKey':
  862. $this->_priv_AddFieldAttribute($oFieldNode, 'on_target_delete', $aExtraParams);
  863. $this->_priv_AddFieldAttribute($oFieldNode, 'filter', $aExtraParams);
  864. break;
  865. case 'ExternalField':
  866. $this->_priv_AddFieldAttribute($oFieldNode, 'extkey_attcode', $aExtraParams);
  867. $this->_priv_AddFieldAttribute($oFieldNode, 'target_attcode', $aExtraParams);
  868. break;
  869. case 'Enum':
  870. $this->_priv_SetFieldValues($oFieldNode, $aExtraParams);
  871. break;
  872. case 'LinkedSetIndirect':
  873. $this->_priv_AddFieldAttribute($oFieldNode, 'ext_key_to_remote', $aExtraParams);
  874. // Fall through
  875. case 'LinkedSet':
  876. $this->_priv_AddFieldAttribute($oFieldNode, 'linked_class', $aExtraParams);
  877. $this->_priv_AddFieldAttribute($oFieldNode, 'ext_key_to_me', $aExtraParams);
  878. $this->_priv_AddFieldAttribute($oFieldNode, 'count_min', $aExtraParams);
  879. $this->_priv_AddFieldAttribute($oFieldNode, 'count_max', $aExtraParams);
  880. break;
  881. default:
  882. throw(new Exception('Unsupported type of field: '.$sFieldType));
  883. }
  884. $this->_priv_SetFieldDependencies($oFieldNode, $aExtraParams);
  885. $oFieldNode->setAttribute('type', $sFieldType);
  886. $oFieldNode->setAttribute('sql', $sSQL);
  887. $oFieldNode->setAttribute('default_value', $defaultValue);
  888. $oFieldNode->setAttribute('is_null_alllowed', $bIsNullAllowed ? 'true' : 'false');
  889. }
  890. protected function _priv_AddFieldAttribute(DOMNode $oFieldNode, $sAttributeCode, $aExtraParams, $bMandatory = false)
  891. {
  892. $value = array_key_exists($sAttributeCode, $aExtraParams) ? $aExtraParams[$sAttributeCode] : '';
  893. if (($value == '') && (!$bMandatory)) return;
  894. $oFieldNode->setAttribute($sAttributeCode, $value);
  895. }
  896. protected function _priv_SetFieldDependencies($oFieldNode, $aExtraParams)
  897. {
  898. $aDeps = array_key_exists('dependencies', $aExtraParams) ? $aExtraParams['dependencies'] : '';
  899. $oDependencies = $oFieldNode->getElementsByTagName('dependencies')->item(0);
  900. // No dependencies before, and no dependencies to add, exit
  901. if (($oDependencies == null) && ($aDeps == '')) return;
  902. // Remove the previous dependencies
  903. $oFieldNode->removeChild($oDependencies);
  904. // If no dependencies, exit
  905. if ($aDeps == '') return;
  906. // Build the new list of dependencies
  907. $oDependencies = $this->oDOMDocument->createElement('dependencies');
  908. foreach($aDeps as $sAttCode)
  909. {
  910. $oDep = $this->oDOMDocument->createElement('attribute');
  911. $oDep->setAttribute('name', $sAttCode);
  912. $oDependencies->addChild($oDep);
  913. }
  914. $oFieldNode->addChild($oDependencies);
  915. }
  916. protected function _priv_SetFieldValues($oFieldNode, $aExtraParams)
  917. {
  918. $aVals = array_key_exists('values', $aExtraParams) ? $aExtraParams['values'] : '';
  919. $oValues = $oFieldNode->getElementsByTagName('values')->item(0);
  920. // No dependencies before, and no dependencies to add, exit
  921. if (($oValues == null) && ($aVals == '')) return;
  922. // Remove the previous dependencies
  923. $oFieldNode->removeChild($oValues);
  924. // If no dependencies, exit
  925. if ($aVals == '') return;
  926. // Build the new list of dependencies
  927. $oValues = $this->oDOMDocument->createElement('values');
  928. foreach($aVals as $sValue)
  929. {
  930. $oVal = $this->oDOMDocument->createElement('value', $sValue);
  931. $oValues->appendChild($oVal);
  932. }
  933. $oFieldNode->appendChild($oValues);
  934. }
  935. public function SetPresentation(DOMNode $oClassNode, $sPresentationCode, $aPresentation)
  936. {
  937. $oPresentation = $oClassNode->getElementsByTagName('presentation')->item(0);
  938. if (!is_object($oPresentation))
  939. {
  940. $oPresentation = $this->oDOMDocument->createElement('presentation');
  941. $oClassNode->appendChild($oPresentation);
  942. }
  943. $oZlist = $oPresentation->getElementsByTagName($sPresentationCode)->item(0);
  944. if (is_object($oZlist))
  945. {
  946. // Remove the previous Zlist
  947. $oPresentation->removeChild($oZlist);
  948. }
  949. // Create the ZList anew
  950. $oZlist = $this->oDOMDocument->createElement($sPresentationCode);
  951. $oPresentation->appendChild($oZlist);
  952. $this->AddZListItem($oZlist, $aPresentation);
  953. $this->_priv_SetFlag($oZlist, 'replaced');
  954. }
  955. protected function AddZListItem($oXMLNode, $value)
  956. {
  957. if (is_array($value))
  958. {
  959. $oXmlItems = $this->oDOMDocument->CreateElement('items');
  960. $oXMLNode->appendChild($oXmlItems);
  961. foreach($value as $key => $item)
  962. {
  963. $oXmlItem = $this->oDOMDocument->CreateElement('item');
  964. $oXmlItems->appendChild($oXmlItem);
  965. if (is_string($key))
  966. {
  967. $oXmlItem->SetAttribute('key', $key);
  968. }
  969. $this->AddZListItem($oXmlItem, $item);
  970. }
  971. }
  972. else
  973. {
  974. $oXmlText = $this->oDOMDocument->CreateTextNode((string) $value);
  975. $oXMLNode->appendChild($oXmlText);
  976. }
  977. }
  978. public function _priv_SetFlag($oNode, $sFlagValue)
  979. {
  980. $sPreviousFlag = $oNode->getAttribute('_operation');
  981. if ($sPreviousFlag == 'added')
  982. {
  983. // Do nothing ??
  984. }
  985. else
  986. {
  987. $oNode->setAttribute('_operation', $sFlagValue);
  988. }
  989. if ($oNode->tagName != 'class')
  990. {
  991. $this->_priv_SetFlag($oNode->parentNode, 'modified');
  992. }
  993. }
  994. /**
  995. * List all transitions from a given state
  996. * @param DOMNode $oStateNode The state
  997. * @param bool $bFlattenLayers
  998. * @throws Exception
  999. */
  1000. public function ListTransitions(DOMNode $oStateNode, $bFlattenLayers = true)
  1001. {
  1002. $sXPath = "transitions/transition";
  1003. if ($bFlattenLayers)
  1004. {
  1005. //$sXPath = "transitions/transition[@_operation!='removed']";
  1006. }
  1007. return $this->_priv_GetNodes($sXPath, $oStateNode);
  1008. }
  1009. /**
  1010. * List all states of a given class
  1011. * @param DOMNode $oClassNode The class
  1012. * @param bool $bFlattenLayers
  1013. * @throws Exception
  1014. */
  1015. public function ListStates(DOMNode $oClassNode, $bFlattenLayers = true)
  1016. {
  1017. $sXPath = "lifecycle/states/state";
  1018. if ($bFlattenLayers)
  1019. {
  1020. //$sXPath = "lifecycle/states/state[@_operation!='removed']";
  1021. }
  1022. return $this->_priv_GetNodes($sXPath, $oClassNode);
  1023. }
  1024. /**
  1025. * List Zlists from the DOM for a given class
  1026. * @param bool $bFlattenLayers
  1027. * @throws Exception
  1028. */
  1029. public function ListZLists(DOMNode $oClassNode, $bFlattenLayers = true)
  1030. {
  1031. // Not yet implemented !!!
  1032. return array();
  1033. }
  1034. /**
  1035. * List all menus from the DOM, for a given module
  1036. * @param string $sModuleName
  1037. * @param bool $bFlattenLayers
  1038. * @throws Exception
  1039. */
  1040. public function ListMenus($sModuleName, $bFlattenLayers = true)
  1041. {
  1042. $sXPath = "//menu[@_created_in='$sModuleName']";
  1043. if ($bFlattenLayers)
  1044. {
  1045. $sXPath = "//menu[@_created_in='$sModuleName' and @_operation!='removed']";
  1046. }
  1047. return $this->_priv_GetNodes($sXPath, $this->oMenus);
  1048. }
  1049. public function ApplyChanges()
  1050. {
  1051. $oNodes = $this->ListChanges();
  1052. foreach($oNodes as $oClassNode)
  1053. {
  1054. $sOperation = $oClassNode->GetAttribute('_operation');
  1055. switch($sOperation)
  1056. {
  1057. case 'added':
  1058. case 'modified':
  1059. // marked as added or modified, just reset the flag
  1060. $oClassNode->setAttribute('_operation', '');
  1061. break;
  1062. case 'removed':
  1063. // marked as deleted, let's remove the node from the tree
  1064. $oParent = $oClassNode->parentNode;
  1065. $sClass = $oClassNode->GetAttribute('name');
  1066. echo "Calling removeChild...<br/>";
  1067. $oParent->removeChild($oClassNode);
  1068. unset(self::$aLoadedClasses[$sClass]);
  1069. break;
  1070. }
  1071. }
  1072. }
  1073. public function ListChanges()
  1074. {
  1075. $sXPath = "//*[@_operation!='']";
  1076. return $this->_priv_GetNodes($sXPath);
  1077. }
  1078. /**
  1079. * Get the text/XML version of the delta
  1080. */
  1081. public function GetDelta()
  1082. {
  1083. $oDelta = new DOMDocument('1.0', 'UTF-8');
  1084. $oRootNode = $oDelta->createElement('itop_design');
  1085. $oDelta->appendChild($oRootNode);
  1086. $oClasses = $oDelta->createElement('classes');
  1087. $oRootNode->appendChild($oClasses);
  1088. $this->_priv_ImportModifiedChildren($oDelta, $oClasses, $this->oDOMDocument->firstChild);
  1089. //file_put_contents($sXMLDestPath, $oDelta->saveXML());
  1090. return $oDelta->saveXML();
  1091. }
  1092. protected function _priv_ImportModifiedChildren(DOMDocument $oDoc, DOMNode $oDestNode, DOMNode $oNode)
  1093. {
  1094. static $iDepth = 0;
  1095. $iDepth++;
  1096. foreach($oNode->childNodes as $oChildNode)
  1097. {
  1098. $sNodeName = $oChildNode->nodeName;
  1099. $sNodeValue = $oChildNode->nodeValue;
  1100. if (!$oChildNode instanceof DOMElement)
  1101. {
  1102. $sName = '$$';
  1103. $sOperation = $oChildNode->parentNode->getAttribute('_operation');
  1104. }
  1105. else
  1106. {
  1107. $sName = $oChildNode->getAttribute('name');;
  1108. $sOperation = $oChildNode->getAttribute('_operation');
  1109. }
  1110. echo str_repeat('+', $iDepth)." $sNodeName [$sName], operation: $sOperation\n";
  1111. switch($sOperation)
  1112. {
  1113. case 'removed':
  1114. $oDeletedNode = $oDoc->importNode($oChildNode->cloneNode(false)); // Copies all the node's attributes, but NOT the child nodes
  1115. $oDeletedNode->removeAttribute('_source');
  1116. if ($oDeletedNode->tagName == 'class')
  1117. {
  1118. // classes are always located under the root node
  1119. $oDoc->firstChild->appendChild($oDeletedNode);
  1120. }
  1121. else
  1122. {
  1123. $oDestNode->appendChild($oDeletedNode);
  1124. }
  1125. echo "<p>".str_repeat('+', $iDepth).$oChildNode->getAttribute('name')." was removed...</p>";
  1126. break;
  1127. case 'added':
  1128. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." was created...</p>";
  1129. $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes as well
  1130. if ($oChildNode instanceof DOMElement)
  1131. {
  1132. $oModifiedNode->removeAttribute('_source');
  1133. if ($oModifiedNode->tagName == 'class')
  1134. {
  1135. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." inserting under 'classes'...</p>";
  1136. // classes are always located under the root node
  1137. $oDoc->firstChild->appendChild($oModifiedNode);
  1138. // Process any subclasses: move them under the root of the document
  1139. $oSubclasses = $oModifiedNode->GetElementsByTagName('class');
  1140. $aSubClasses = array();
  1141. foreach($oSubclasses as $oSubclassNode)
  1142. {
  1143. $aSubClasses[] = $oSubclassNode;
  1144. }
  1145. for($index = count($aSubClasses)-1; $index >= 0; $index--)
  1146. {
  1147. $aSubClasses[$index]->parentNode->removeChild($aSubClasses[$index]);
  1148. $oDoc->firstChild->appendChild($aSubClasses[$index]);
  1149. }
  1150. }
  1151. else
  1152. {
  1153. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." inserting in the hierarchy...</p>";
  1154. $oDestNode->appendChild($oModifiedNode);
  1155. }
  1156. }
  1157. else
  1158. {
  1159. $oDestNode->appendChild($oModifiedNode);
  1160. }
  1161. break;
  1162. case 'replaced':
  1163. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." was replaced...</p>";
  1164. $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes as well
  1165. if ($oChildNode instanceof DOMElement)
  1166. {
  1167. $oModifiedNode->removeAttribute('_source');
  1168. }
  1169. $oDestNode->appendChild($oModifiedNode);
  1170. break;
  1171. case 'modified':
  1172. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." was modified...</p>";
  1173. if ($oChildNode instanceof DOMElement)
  1174. {
  1175. echo str_repeat('+', $iDepth)." Copying (NON recursively) the modified node\n";
  1176. $oModifiedNode = $oDoc->importNode($oChildNode, false); // Copies all the node's attributes, but NOT the child nodes
  1177. $oModifiedNode->removeAttribute('_source');
  1178. $this->_priv_ImportModifiedChildren($oDoc, $oModifiedNode, $oChildNode);
  1179. if ($oModifiedNode->tagName == 'class')
  1180. {
  1181. // classes are always located under the root node
  1182. $oDoc->firstChild->appendChild($oModifiedNode);
  1183. }
  1184. else
  1185. {
  1186. $oDestNode->appendChild($oModifiedNode);
  1187. }
  1188. }
  1189. else
  1190. {
  1191. echo str_repeat('+', $iDepth)." Copying (recursively) the modified node\n";
  1192. $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes
  1193. $oDestNode->appendChild($oModifiedNode);
  1194. }
  1195. break;
  1196. default:
  1197. // No change: do nothing
  1198. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." was NOT modified...</p>";
  1199. $oModifiedNode = $oDoc->importNode($oChildNode, true); // Importing the node for future recusrsion if needed
  1200. if ($oChildNode->tagName == 'class')
  1201. {
  1202. echo "<p>".str_repeat('+', $iDepth)."Checking if a subclass of ".$oChildNode->getAttribute('name')." was modified...</p>";
  1203. // classes are always located under the root node
  1204. $this->_priv_ImportModifiedChildren($oDoc, $oModifiedNode, $oChildNode);
  1205. }
  1206. }
  1207. }
  1208. $iDepth--;
  1209. }
  1210. /**
  1211. * Searches on disk in the root directory for module description files
  1212. * and returns an array of MFModules
  1213. * @return array Array of MFModules
  1214. */
  1215. public function FindModules($sSubDirectory = '')
  1216. {
  1217. $aAvailableModules = ModuleDiscovery::GetAvailableModules($this->sRootDir, $sSubDirectory);
  1218. $aResult = array();
  1219. foreach($aAvailableModules as $sId => $aModule)
  1220. {
  1221. $aResult[] = new MFModule($sId, $aModule['root_dir'], $aModule['label']);
  1222. }
  1223. return $aResult;
  1224. }
  1225. /**
  1226. * Produce the PHP files corresponding to the data model
  1227. * @param $bTestOnly bool
  1228. * @return array Array of errors, empty if no error
  1229. */
  1230. public function Compile($bTestOnly)
  1231. {
  1232. return array();
  1233. }
  1234. /**
  1235. * Extracts some nodes from the DOM
  1236. * @param string $sXPath A XPath expression
  1237. * @return DOMNodeList
  1238. */
  1239. public function _priv_GetNodes($sXPath, $oContextNode = null)
  1240. {
  1241. $oXPath = new DOMXPath($this->oDOMDocument);
  1242. if (is_null($oContextNode))
  1243. {
  1244. return $oXPath->query($sXPath);
  1245. }
  1246. else
  1247. {
  1248. return $oXPath->query($sXPath, $oContextNode);
  1249. }
  1250. }
  1251. /**
  1252. * Insert a new node in the DOM
  1253. * @param DOMNode $oNode
  1254. * @param DOMNode $oParentNode
  1255. */
  1256. public function _priv_AddNode(DOMNode $oNode, DOMNode $oParentNode)
  1257. {
  1258. }
  1259. /**
  1260. * Remove a node from the DOM
  1261. * @param DOMNode $oNode
  1262. * @param DOMNode $oParentNode
  1263. */
  1264. public function _priv_RemoveNode(DOMNode $oNode)
  1265. {
  1266. }
  1267. /**
  1268. * Add or modify an attribute of a node
  1269. * @param DOMNode $oNode
  1270. * @param string $sAttributeName
  1271. * @param mixed $atttribueValue
  1272. */
  1273. public function _priv_SetNodeAttribute(DOMNode $oNode, $sAttributeName, $atttribueValue)
  1274. {
  1275. }
  1276. /**
  1277. * Helper to browse the DOM -could be factorized in ModelFactory
  1278. * Returns the node directly under the given node, and that is supposed to be always present and unique
  1279. */
  1280. protected function GetUniqueElement($oDOMNode, $sTagName, $bMustExist = true)
  1281. {
  1282. $oNode = null;
  1283. foreach($oDOMNode->childNodes as $oChildNode)
  1284. {
  1285. if ($oChildNode->nodeName == $sTagName)
  1286. {
  1287. $oNode = $oChildNode;
  1288. break;
  1289. }
  1290. }
  1291. if ($bMustExist && is_null($oNode))
  1292. {
  1293. throw new DOMFormatException('Missing unique tag: '.$sTagName);
  1294. }
  1295. return $oNode;
  1296. }
  1297. /**
  1298. * Helper to browse the DOM -could be factorized in ModelFactory
  1299. * Returns the node directly under the given node, or null is missing
  1300. */
  1301. protected function GetOptionalElement($oDOMNode, $sTagName)
  1302. {
  1303. return $this->GetUniqueElement($oDOMNode, $sTagName, false);
  1304. }
  1305. /**
  1306. * Helper to browse the DOM -could be factorized in ModelFactory
  1307. * Returns the TEXT of the given node (possibly from several subnodes)
  1308. */
  1309. protected function GetNodeText($oNode)
  1310. {
  1311. $sText = '';
  1312. foreach($oNode->childNodes as $oChildNode)
  1313. {
  1314. if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection
  1315. {
  1316. $sText .= $oChildNode->wholeText;
  1317. }
  1318. }
  1319. return $sText;
  1320. }
  1321. /**
  1322. * Helper to browse the DOM -could be factorized in ModelFactory
  1323. * Assumes the given node to be either a text or
  1324. * <items>
  1325. * <item [key]="..."]>value<item>
  1326. * <item [key]="..."]>value<item>
  1327. * </items>
  1328. * where value can be the either a text or an array of items... recursively
  1329. * Returns a PHP array
  1330. */
  1331. public function GetNodeAsArrayOfItems($oNode)
  1332. {
  1333. $oItems = $this->GetOptionalElement($oNode, 'items');
  1334. if ($oItems)
  1335. {
  1336. $res = array();
  1337. foreach($oItems->childNodes as $oItem)
  1338. {
  1339. // When an attribute is missing
  1340. if ($oItem->hasAttribute('key'))
  1341. {
  1342. $key = $oItem->getAttribute('key');
  1343. $res[$key] = $this->GetNodeAsArrayOfItems($oItem);
  1344. }
  1345. else
  1346. {
  1347. $res[] = $this->GetNodeAsArrayOfItems($oItem);
  1348. }
  1349. }
  1350. }
  1351. else
  1352. {
  1353. $res = $this->GetNodeText($oNode);
  1354. }
  1355. return $res;
  1356. }
  1357. }