modelfactory.class.inc.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. /**
  1050. * Get a menu, given its is id
  1051. * @param string $sModuleName
  1052. * @param bool $bFlattenLayers
  1053. * @throws Exception
  1054. */
  1055. public function GetMenu($sMenuId, $bFlattenLayers = true)
  1056. {
  1057. if (!array_key_exists($sMenuId, self::$aLoadedMenus))
  1058. {
  1059. return null;
  1060. }
  1061. $oMenuNode = self::$aLoadedMenus[$sMenuId];
  1062. if ($bFlattenLayers)
  1063. {
  1064. $sOperation = $oMenuNode->getAttribute('_operation');
  1065. if ($sOperation == 'removed')
  1066. {
  1067. $oMenuNode = null;
  1068. }
  1069. }
  1070. return $oMenuNode;
  1071. }
  1072. public function ApplyChanges()
  1073. {
  1074. $oNodes = $this->ListChanges();
  1075. foreach($oNodes as $oClassNode)
  1076. {
  1077. $sOperation = $oClassNode->GetAttribute('_operation');
  1078. switch($sOperation)
  1079. {
  1080. case 'added':
  1081. case 'modified':
  1082. // marked as added or modified, just reset the flag
  1083. $oClassNode->setAttribute('_operation', '');
  1084. break;
  1085. case 'removed':
  1086. // marked as deleted, let's remove the node from the tree
  1087. $oParent = $oClassNode->parentNode;
  1088. $sClass = $oClassNode->GetAttribute('name');
  1089. echo "Calling removeChild...<br/>";
  1090. $oParent->removeChild($oClassNode);
  1091. unset(self::$aLoadedClasses[$sClass]);
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. public function ListChanges()
  1097. {
  1098. $sXPath = "//*[@_operation!='']";
  1099. return $this->_priv_GetNodes($sXPath);
  1100. }
  1101. /**
  1102. * Get the text/XML version of the delta
  1103. */
  1104. public function GetDelta()
  1105. {
  1106. $oDelta = new DOMDocument('1.0', 'UTF-8');
  1107. $oRootNode = $oDelta->createElement('itop_design');
  1108. $oDelta->appendChild($oRootNode);
  1109. $oClasses = $oDelta->createElement('classes');
  1110. $oRootNode->appendChild($oClasses);
  1111. $this->_priv_ImportModifiedChildren($oDelta, $oClasses, $this->oDOMDocument->firstChild);
  1112. //file_put_contents($sXMLDestPath, $oDelta->saveXML());
  1113. return $oDelta->saveXML();
  1114. }
  1115. protected function _priv_ImportModifiedChildren(DOMDocument $oDoc, DOMNode $oDestNode, DOMNode $oNode)
  1116. {
  1117. static $iDepth = 0;
  1118. $iDepth++;
  1119. foreach($oNode->childNodes as $oChildNode)
  1120. {
  1121. $sNodeName = $oChildNode->nodeName;
  1122. $sNodeValue = $oChildNode->nodeValue;
  1123. if (!$oChildNode instanceof DOMElement)
  1124. {
  1125. $sName = '$$';
  1126. $sOperation = $oChildNode->parentNode->getAttribute('_operation');
  1127. }
  1128. else
  1129. {
  1130. $sName = $oChildNode->getAttribute('name');;
  1131. $sOperation = $oChildNode->getAttribute('_operation');
  1132. }
  1133. echo str_repeat('+', $iDepth)." $sNodeName [$sName], operation: $sOperation\n";
  1134. switch($sOperation)
  1135. {
  1136. case 'removed':
  1137. $oDeletedNode = $oDoc->importNode($oChildNode->cloneNode(false)); // Copies all the node's attributes, but NOT the child nodes
  1138. $oDeletedNode->removeAttribute('_source');
  1139. if ($oDeletedNode->tagName == 'class')
  1140. {
  1141. // classes are always located under the root node
  1142. $oDoc->firstChild->appendChild($oDeletedNode);
  1143. }
  1144. else
  1145. {
  1146. $oDestNode->appendChild($oDeletedNode);
  1147. }
  1148. echo "<p>".str_repeat('+', $iDepth).$oChildNode->getAttribute('name')." was removed...</p>";
  1149. break;
  1150. case 'added':
  1151. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." was created...</p>";
  1152. $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes as well
  1153. if ($oChildNode instanceof DOMElement)
  1154. {
  1155. $oModifiedNode->removeAttribute('_source');
  1156. if ($oModifiedNode->tagName == 'class')
  1157. {
  1158. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." inserting under 'classes'...</p>";
  1159. // classes are always located under the root node
  1160. $oDoc->firstChild->appendChild($oModifiedNode);
  1161. // Process any subclasses: move them under the root of the document
  1162. $oSubclasses = $oModifiedNode->GetElementsByTagName('class');
  1163. $aSubClasses = array();
  1164. foreach($oSubclasses as $oSubclassNode)
  1165. {
  1166. $aSubClasses[] = $oSubclassNode;
  1167. }
  1168. for($index = count($aSubClasses)-1; $index >= 0; $index--)
  1169. {
  1170. $aSubClasses[$index]->parentNode->removeChild($aSubClasses[$index]);
  1171. $oDoc->firstChild->appendChild($aSubClasses[$index]);
  1172. }
  1173. }
  1174. else
  1175. {
  1176. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." inserting in the hierarchy...</p>";
  1177. $oDestNode->appendChild($oModifiedNode);
  1178. }
  1179. }
  1180. else
  1181. {
  1182. $oDestNode->appendChild($oModifiedNode);
  1183. }
  1184. break;
  1185. case 'replaced':
  1186. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." was replaced...</p>";
  1187. $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes as well
  1188. if ($oChildNode instanceof DOMElement)
  1189. {
  1190. $oModifiedNode->removeAttribute('_source');
  1191. }
  1192. $oDestNode->appendChild($oModifiedNode);
  1193. break;
  1194. case 'modified':
  1195. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." was modified...</p>";
  1196. if ($oChildNode instanceof DOMElement)
  1197. {
  1198. echo str_repeat('+', $iDepth)." Copying (NON recursively) the modified node\n";
  1199. $oModifiedNode = $oDoc->importNode($oChildNode, false); // Copies all the node's attributes, but NOT the child nodes
  1200. $oModifiedNode->removeAttribute('_source');
  1201. $this->_priv_ImportModifiedChildren($oDoc, $oModifiedNode, $oChildNode);
  1202. if ($oModifiedNode->tagName == 'class')
  1203. {
  1204. // classes are always located under the root node
  1205. $oDoc->firstChild->appendChild($oModifiedNode);
  1206. }
  1207. else
  1208. {
  1209. $oDestNode->appendChild($oModifiedNode);
  1210. }
  1211. }
  1212. else
  1213. {
  1214. echo str_repeat('+', $iDepth)." Copying (recursively) the modified node\n";
  1215. $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes
  1216. $oDestNode->appendChild($oModifiedNode);
  1217. }
  1218. break;
  1219. default:
  1220. // No change: do nothing
  1221. echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('name')." was NOT modified...</p>";
  1222. $oModifiedNode = $oDoc->importNode($oChildNode, true); // Importing the node for future recusrsion if needed
  1223. if ($oChildNode->tagName == 'class')
  1224. {
  1225. echo "<p>".str_repeat('+', $iDepth)."Checking if a subclass of ".$oChildNode->getAttribute('name')." was modified...</p>";
  1226. // classes are always located under the root node
  1227. $this->_priv_ImportModifiedChildren($oDoc, $oModifiedNode, $oChildNode);
  1228. }
  1229. }
  1230. }
  1231. $iDepth--;
  1232. }
  1233. /**
  1234. * Searches on disk in the root directory for module description files
  1235. * and returns an array of MFModules
  1236. * @return array Array of MFModules
  1237. */
  1238. public function FindModules($sSubDirectory = '')
  1239. {
  1240. $aAvailableModules = ModuleDiscovery::GetAvailableModules($this->sRootDir, $sSubDirectory);
  1241. $aResult = array();
  1242. foreach($aAvailableModules as $sId => $aModule)
  1243. {
  1244. $aResult[] = new MFModule($sId, $aModule['root_dir'], $aModule['label']);
  1245. }
  1246. return $aResult;
  1247. }
  1248. /**
  1249. * Produce the PHP files corresponding to the data model
  1250. * @param $bTestOnly bool
  1251. * @return array Array of errors, empty if no error
  1252. */
  1253. public function Compile($bTestOnly)
  1254. {
  1255. return array();
  1256. }
  1257. /**
  1258. * Extracts some nodes from the DOM
  1259. * @param string $sXPath A XPath expression
  1260. * @return DOMNodeList
  1261. */
  1262. public function _priv_GetNodes($sXPath, $oContextNode = null)
  1263. {
  1264. $oXPath = new DOMXPath($this->oDOMDocument);
  1265. if (is_null($oContextNode))
  1266. {
  1267. return $oXPath->query($sXPath);
  1268. }
  1269. else
  1270. {
  1271. return $oXPath->query($sXPath, $oContextNode);
  1272. }
  1273. }
  1274. /**
  1275. * Insert a new node in the DOM
  1276. * @param DOMNode $oNode
  1277. * @param DOMNode $oParentNode
  1278. */
  1279. public function _priv_AddNode(DOMNode $oNode, DOMNode $oParentNode)
  1280. {
  1281. }
  1282. /**
  1283. * Remove a node from the DOM
  1284. * @param DOMNode $oNode
  1285. * @param DOMNode $oParentNode
  1286. */
  1287. public function _priv_RemoveNode(DOMNode $oNode)
  1288. {
  1289. }
  1290. /**
  1291. * Add or modify an attribute of a node
  1292. * @param DOMNode $oNode
  1293. * @param string $sAttributeName
  1294. * @param mixed $atttribueValue
  1295. */
  1296. public function _priv_SetNodeAttribute(DOMNode $oNode, $sAttributeName, $atttribueValue)
  1297. {
  1298. }
  1299. /**
  1300. * Helper to browse the DOM -could be factorized in ModelFactory
  1301. * Returns the node directly under the given node, and that is supposed to be always present and unique
  1302. */
  1303. protected function GetUniqueElement($oDOMNode, $sTagName, $bMustExist = true)
  1304. {
  1305. $oNode = null;
  1306. foreach($oDOMNode->childNodes as $oChildNode)
  1307. {
  1308. if ($oChildNode->nodeName == $sTagName)
  1309. {
  1310. $oNode = $oChildNode;
  1311. break;
  1312. }
  1313. }
  1314. if ($bMustExist && is_null($oNode))
  1315. {
  1316. throw new DOMFormatException('Missing unique tag: '.$sTagName);
  1317. }
  1318. return $oNode;
  1319. }
  1320. /**
  1321. * Helper to browse the DOM -could be factorized in ModelFactory
  1322. * Returns the node directly under the given node, or null is missing
  1323. */
  1324. protected function GetOptionalElement($oDOMNode, $sTagName)
  1325. {
  1326. return $this->GetUniqueElement($oDOMNode, $sTagName, false);
  1327. }
  1328. /**
  1329. * Helper to browse the DOM -could be factorized in ModelFactory
  1330. * Returns the TEXT of the given node (possibly from several subnodes)
  1331. */
  1332. protected function GetNodeText($oNode)
  1333. {
  1334. $sText = '';
  1335. foreach($oNode->childNodes as $oChildNode)
  1336. {
  1337. if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection
  1338. {
  1339. $sText .= $oChildNode->wholeText;
  1340. }
  1341. }
  1342. return $sText;
  1343. }
  1344. /**
  1345. * Helper to browse the DOM -could be factorized in ModelFactory
  1346. * Assumes the given node to be either a text or
  1347. * <items>
  1348. * <item [key]="..."]>value<item>
  1349. * <item [key]="..."]>value<item>
  1350. * </items>
  1351. * where value can be the either a text or an array of items... recursively
  1352. * Returns a PHP array
  1353. */
  1354. public function GetNodeAsArrayOfItems($oNode)
  1355. {
  1356. $oItems = $this->GetOptionalElement($oNode, 'items');
  1357. if ($oItems)
  1358. {
  1359. $res = array();
  1360. foreach($oItems->childNodes as $oItem)
  1361. {
  1362. // When an attribute is missing
  1363. if ($oItem->hasAttribute('key'))
  1364. {
  1365. $key = $oItem->getAttribute('key');
  1366. $res[$key] = $this->GetNodeAsArrayOfItems($oItem);
  1367. }
  1368. else
  1369. {
  1370. $res[] = $this->GetNodeAsArrayOfItems($oItem);
  1371. }
  1372. }
  1373. }
  1374. else
  1375. {
  1376. $res = $this->GetNodeText($oNode);
  1377. }
  1378. return $res;
  1379. }
  1380. }