123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563 |
- <?php
- // Copyright (C) 2011 Combodo SARL
- //
- /**
- * ModelFactory: in-memory manipulation of the XML MetaModel
- *
- * @author Erwan Taloc <erwan.taloc@combodo.com>
- * @author Romain Quetiez <romain.quetiez@combodo.com>
- * @author Denis Flaven <denis.flaven@combodo.com>
- * @license Combodo Private
- */
- require_once(APPROOT.'setup/moduleinstaller.class.inc.php');
- /**
- * ModelFactoryItem: an item managed by the ModuleFactory
- * @package ModelFactory
- */
- abstract class MFItem
- {
- public function __construct($sName, $sValue)
- {
- parent::__construct($sName, $sValue);
- }
-
- /**
- * List the source files for this item
- */
- public function ListSources()
- {
-
- }
- /**
- * List the rights/restrictions for this item
- */
- public function ListRights()
- {
-
- }
- }
- /**
- * ModelFactoryModule: the representation of a Module (i.e. element that can be selected during the setup)
- * @package ModelFactory
- */
- class MFModule extends MFItem
- {
- protected $sId;
- protected $sName;
- protected $sVersion;
- protected $sRootDir;
- protected $sLabel;
- protected $aDataModels;
-
- public function __construct($sId, $sRootDir, $sLabel)
- {
- $this->sId = $sId;
-
- list($this->sName, $this->sVersion) = ModuleDiscovery::GetModuleName($sId);
- if (strlen($this->sVersion) == 0)
- {
- $this->sVersion = '1.0.0';
- }
- $this->sRootDir = $sRootDir;
- $this->sLabel = $sLabel;
- $this->aDataModels = array();
-
- // Scan the module's root directory to find the datamodel(*).xml files
- if ($hDir = opendir($sRootDir))
- {
- // This is the correct way to loop over the directory. (according to the documentation)
- while (($sFile = readdir($hDir)) !== false)
- {
- if (preg_match('/^datamodel(.*)\.xml$/i', $sFile, $aMatches))
- {
- $this->aDataModels[] = $this->sRootDir.'/'.$aMatches[0];
- }
- }
- closedir($hDir);
- }
- }
-
-
- public function GetId()
- {
- return $this->sId;
- }
-
- public function GetName()
- {
- return $this->sName;
- }
- public function GetVersion()
- {
- return $this->sVersion;
- }
- public function GetLabel()
- {
- return $this->sLabel;
- }
-
- public function GetRootDir()
- {
- return $this->sRootDir;
- }
- public function GetModuleDir()
- {
- return basename($this->sRootDir);
- }
- public function GetDataModelFiles()
- {
- return $this->aDataModels;
- }
-
- /**
- * List all classes in this module
- */
- public function ListClasses()
- {
- return array();
- }
- }
- class MFWorkspace extends MFModule
- {
- public function __construct($sRootDir)
- {
- $this->sId = 'itop-workspace';
-
- $this->sName = 'workspace';
- $this->sVersion = '1.0';
- $this->sRootDir = $sRootDir;
- $this->sLabel = 'Workspace';
- $this->aDataModels = array();
- $this->aDataModels[] = $this->GetWorkspacePath();
- }
- public function GetWorkspacePath()
- {
- return $this->sRootDir.'/workspace.xml';
- }
-
- public function GetName()
- {
- return ''; // The workspace itself has no name so that objects created inside it retain their original module's name
- }
- }
- /**
- * ModelFactoryClass: the representation of a Class (i.e. a PHP class)
- * @package ModelFactory
- */
- class MFClass extends MFItem
- {
- /**
- * List all fields of this class
- */
- public function ListFields()
- {
- return array();
- }
-
- /**
- * List all methods of this class
- */
- public function ListMethods()
- {
- return array();
- }
-
- /**
- * Whether or not the class has a lifecycle
- * @return bool
- */
- public function HasLifeCycle()
- {
- return true; //TODO implement
- }
-
- /**
- * Returns the code of the attribute used to store the lifecycle state
- * @return string
- */
- public function GetLifeCycleAttCode()
- {
- if ($this->HasLifeCycle())
- {
-
- }
- return '';
- }
-
- /**
- * List all states of this class
- */
- public function ListStates()
- {
- return array();
- }
- /**
- * List all relations of this class
- */
- public function ListRelations()
- {
- return array();
- }
- /**
- * List all transitions of this class
- */
- public function ListTransitions()
- {
- return array();
- }
- }
- /**
- * ModelFactoryField: the representation of a Field (i.e. a property of a class)
- * @package ModelFactory
- */
- class MFField extends MFItem
- {
- }
- /**
- * ModelFactoryMethod: the representation of a Method (i.e. a method of a class)
- * @package ModelFactory
- */
- class MFMethod extends MFItem
- {
- }
- /**
- * ModelFactoryState: the representation of a state in the life cycle of the class
- * @package ModelFactory
- */
- class MFState extends MFItem
- {
- }
- /**
- * ModelFactoryRelation: the representation of a n:n relationship between two classes
- * @package ModelFactory
- */
- class MFRelation extends MFItem
- {
- }
- /**
- * ModelFactoryTransition: the representation of a transition between two states in the life cycle of the class
- * @package ModelFactory
- */
- class MFTransition extends MFItem
- {
- }
- /**
- * ModelFactory: the class that manages the in-memory representation of the XML MetaModel
- * @package ModelFactory
- */
- class ModelFactory
- {
- protected $sRootDir;
- protected $oDOMDocument;
- protected $oRoot;
- protected $oClasses;
- protected $oMenus;
- static protected $aLoadedClasses;
- static protected $aLoadedMenus;
- static protected $aWellKnownParents = array('DBObject', 'CMDBObject','cmdbAbstractObject');
- static protected $aLoadedModules;
- static protected $aLoadErrors;
-
- public function __construct($sRootDir)
- {
- $this->sRootDir = $sRootDir;
- $this->oDOMDocument = new DOMDocument('1.0', 'UTF-8');
- $this->oDOMDocument->registerNodeClass('DOMElement', 'MFDOMElement');
- $this->oRoot = $this->oDOMDocument->CreateElement('itop_design');
- $this->oDOMDocument->AppendChild($this->oRoot);
- $this->oClasses = $this->oDOMDocument->CreateElement('classes');
- $this->oRoot->AppendChild($this->oClasses);
- $this->oMenus = $this->oDOMDocument->CreateElement('menus');
- $this->oRoot->AppendChild($this->oMenus);
- self::$aLoadedClasses = array();
- self::$aLoadedMenus = array();
- self::$aLoadedModules = array();
- self::$aLoadErrors = array();
- libxml_use_internal_errors(true);
- }
-
- public function Dump($oNode = null)
- {
- if (is_null($oNode))
- {
- $oNode = $this->oMenus;
- }
- echo htmlentities($this->oDOMDocument->saveXML($oNode));
- }
- /**
- * Loads the definitions corresponding to the given Module
- * @param MFModule $oModule
- */
- public function LoadModule(MFModule $oModule)
- {
- $aDataModels = $oModule->GetDataModelFiles();
- $sModuleName = $oModule->GetName();
- $aClasses = array();
- self::$aLoadedModules[] = $oModule;
- foreach($aDataModels as $sXmlFile)
- {
- $oDocument = new DOMDocument('1.0', 'UTF-8');
- $oDocument->registerNodeClass('DOMElement', 'MFDOMElement');
- libxml_clear_errors();
- $oDocument->load($sXmlFile, LIBXML_NOBLANKS);
- $bValidated = $oDocument->schemaValidate(APPROOT.'setup/itop_design.xsd');
- $aErrors = libxml_get_errors();
- if (count($aErrors) > 0)
- {
- self::$aLoadErrors[$sModuleName] = $aErrors;
- return;
- }
- $oXPath = new DOMXPath($oDocument);
- $oNodeList = $oXPath->query('//*');
- foreach($oNodeList as $oNode)
- {
- $oNode->SetAttribute('_source', $sXmlFile);
- }
- $oXPath = new DOMXPath($oDocument);
- $oNodeList = $oXPath->query('/itop_design/classes/class');
- foreach($oNodeList as $oNode)
- {
- $sParentClass = $oNode->GetUniqueElement('properties')->GetChildText('parent', '');
- $sClassName = $oNode->GetAttribute('id');
- $aClasses[$sClassName] = array('name' => $sClassName, 'parent' => $sParentClass, 'node' => $oNode);
- }
- // Menus - temporary for compiling ???
- $oNodeList = $oXPath->query('/itop_design/menus/menu');
- foreach($oNodeList as $oNode)
- {
- $this->AddMenu($oNode, $sModuleName);
- }
- }
-
- $index = 1;
- do
- {
- $bNothingLoaded = true;
- foreach($aClasses as $sClassName => $aClassData)
- {
- $sOperation = $aClassData['node']->getAttribute('_operation');
- switch($sOperation)
- {
- case 'added':
- case '':
- if (in_array($aClassData['parent'], self::$aWellKnownParents))
- {
- $this->AddClass($aClassData['node'], $sModuleName);
- unset($aClasses[$sClassName]);
- $bNothingLoaded = false;
- }
- else if ($this->ClassNameExists($aClassData['parent']))
- {
- $this->AddClass($aClassData['node'], $sModuleName);
- unset($aClasses[$sClassName]);
- $bNothingLoaded = false;
- }
- break;
-
- case 'removed':
- unset($aClasses[$sClassName]);
- $this->RemoveClass($sClassName);
- break;
-
- case 'modified':
- unset($aClasses[$sClassName]);
- $this->AlterClass($sClassName, $aClassData['node']);
- break;
- }
- }
- $index++;
- }
- while((count($aClasses)>0) && !$bNothingLoaded);
- // The remaining classes have no known parent, let's add them at the root
- foreach($aClasses as $sClassName => $aClassData)
- {
- $sOperation = $aClassData['node']->getAttribute('_operation');
- switch($sOperation)
- {
- case 'added':
- case '':
- // Add the class as a new root class
- $this->AddClass($aClassData['node'], $sModuleName);
- $oNewNode = $this->oDOMDocument->ImportNode($aClassData['node']);
- break;
-
- case 'removed':
- $this->RemoveClass($sClassName);
- break;
-
- case 'modified':
- //@@TODO Handle the modification of a class here
- $this->AlterClass($sClassName, $aClassData['node']);
- break;
- }
- }
- }
- /**
- * XML load errors (XML format and validation)
- */
- function HasLoadErrors()
- {
- return (count(self::$aLoadErrors) > 0);
- }
- function GetLoadErrors()
- {
- return self::$aLoadErrors;
- }
- function GetLoadedModules($bExcludeWorkspace = true)
- {
- if ($bExcludeWorkspace)
- {
- $aModules = array();
- foreach(self::$aLoadedModules as $oModule)
- {
- if (!$oModule instanceof MFWorkspace)
- {
- $aModules[] = $oModule;
- }
- }
- }
- else
- {
- $aModules = self::$aLoadedModules;
- }
- return $aModules;
- }
-
-
- function GetModule($sModuleName)
- {
- foreach(self::$aLoadedModules as $oModule)
- {
- if ($oModule->GetName() == $sModuleName) return $oModule;
- }
- return null;
- }
-
-
- /**
- * Check if the class specified by the given node already exists in the loaded DOM
- * @param DOMNode $oClassNode The node corresponding to the class to load
- * @throws Exception
- * @return bool True if the class exists, false otherwise
- */
- protected function ClassExists(DOMNode $oClassNode)
- {
- if ($oClassNode->hasAttribute('id'))
- {
- $sClassName = $oClassNode->GetAttribute('id');
- }
- else
- {
- throw new Exception('ModelFactory::AddClass: Cannot add a class with no name');
- }
-
- return (array_key_exists($sClassName, self::$aLoadedClasses));
- }
-
- /**
- * Check if the class specified by the given name already exists in the loaded DOM
- * @param string $sClassName The node corresponding to the class to load
- * @throws Exception
- * @return bool True if the class exists, false otherwise
- */
- protected function ClassNameExists($sClassName)
- {
- return (array_key_exists($sClassName, self::$aLoadedClasses));
- }
- /**
- * Add the given class to the DOM
- * @param DOMNode $oClassNode
- * @param string $sModuleName The name of the module in which this class is declared
- * @throws Exception
- */
- public function AddClass(DOMNode $oClassNode, $sModuleName)
- {
- if ($oClassNode->hasAttribute('id'))
- {
- $sClassName = $oClassNode->GetAttribute('id');
- }
- else
- {
- throw new Exception('ModelFactory::AddClass: Cannot add a class with no name');
- }
- if ($this->ClassExists($oClassNode))
- {
- throw new Exception("ModelFactory::AddClass: Cannot add the already existing class $sClassName");
- }
-
- $sParentClass = $oClassNode->GetUniqueElement('properties')->GetChildText('parent', '');
-
- //echo "Adding class: $sClassName, parent: $sParentClass<br/>";
- if (!in_array($sParentClass, self::$aWellKnownParents) && $this->ClassNameExists($sParentClass))
- {
- // The class is a subclass of a class already loaded, add it under
- self::$aLoadedClasses[$sClassName] = $this->oDOMDocument->ImportNode($oClassNode, true /* bDeep */);
- self::$aLoadedClasses[$sClassName]->SetAttribute('_operation', 'added');
- if ($sModuleName != '')
- {
- self::$aLoadedClasses[$sClassName]->SetAttribute('_created_in', $sModuleName);
- }
- self::$aLoadedClasses[$sParentClass]->AppendChild(self::$aLoadedClasses[$sClassName]);
- $bNothingLoaded = false;
- }
- else if (in_array($sParentClass, self::$aWellKnownParents))
- {
- // Add the class as a new root class
- self::$aLoadedClasses[$sClassName] = $this->oDOMDocument->ImportNode($oClassNode, true /* bDeep */);
- self::$aLoadedClasses[$sClassName]->SetAttribute('_operation', 'added');
- if ($sModuleName != '')
- {
- self::$aLoadedClasses[$sClassName]->SetAttribute('_created_in', $sModuleName);
- }
- $this->oClasses->AppendChild(self::$aLoadedClasses[$sClassName]);
- }
- else
- {
- throw new Exception("ModelFactory::AddClass: Cannot add the class $sClassName, unknown parent class: $sParentClass (loaded classes: ".implode(', ', array_keys(self::$aLoadedClasses)).")");
- }
- }
-
- /**
- * Remove a class from the DOM
- * @param string $sClass
- * @throws Exception
- */
- public function RemoveClass($sClass)
- {
- if (!$this->ClassNameExists($sClass))
- {
- throw new Exception("ModelFactory::RemoveClass: Cannot remove the non existing class $sClass");
- }
- $oClassNode = self::$aLoadedClasses[$sClass];
- if ($oClassNode->getAttribute('_operation') == 'added')
- {
- $oClassNode->parentNode->RemoveChild($oClassNode);
- unset(self::$aLoadedClasses[$sClass]);
- }
- else
- {
- self::$aLoadedClasses[$sClass]->SetAttribute('_operation', 'removed');
- //TODO: also mark as removed the child classes
- }
-
- }
- /**
- * Modify a class within the DOM
- * @param string $sMenuId
- * @param DOMNode $oMenuNode
- * @throws Exception
- */
- public function AlterClass($sClassName, DOMNode $oClassNode)
- {
- $sOriginalName = $sClassName;
- if ($this->ClassNameExists($sClassName))
- {
- $oDestNode = self::$aLoadedClasses[$sClassName];
- }
- else
- {
- $sOriginalName = $oClassNode->getAttribute('_original_name');
- if ($this->ClassNameExists($sOriginalName))
- {
- // Class was renamed !
- $oDestNode = self::$aLoadedClasses[$sOriginalName];
- }
- else
- {
- throw new Exception("ModelFactory::AddClass: Cannot alter the non-existing class $sClassName / $sOriginalName");
- }
- }
- $this->_priv_AlterNode($oDestNode, $oClassNode);
- $sClassName = $oDestNode->getAttribute('id');
- if ($sOriginalName != $sClassName)
- {
- unset(self::$aLoadedClasses[$sOriginalName]);
- self::$aLoadedClasses[$sClassName] = $oDestNode;
- }
- $this->_priv_SetFlag($oDestNode, 'modified');
- }
- /**
- * Add the given menu to the DOM
- * @param DOMNode $oMenuNode
- * @param string $sModuleName The name of the module in which this class is declared
- * @throws Exception
- */
- public function AddMenu($oMenuNode, $sModuleName)
- {
- $sMenuId = $oMenuNode->GetAttribute('id');
- self::$aLoadedMenus[$sMenuId] = $this->oDOMDocument->ImportNode($oMenuNode, true /* bDeep */);
- self::$aLoadedMenus[$sMenuId]->SetAttribute('_operation', 'added');
- if ($sModuleName != '')
- {
- self::$aLoadedMenus[$sMenuId]->SetAttribute('_created_in', $sModuleName);
- }
- $this->oMenus->AppendChild(self::$aLoadedMenus[$sMenuId]);
- }
-
- /**
- * Remove a menu from the DOM
- * @param string $sMenuId
- * @throws Exception
- */
- public function RemoveMenu($sMenuId)
- {
- $oMenuNode = self::$aLoadedMenus[$sClass];
- if ($oMenuNode->getAttribute('_operation') == 'added')
- {
- $oMenuNode->parentNode->RemoveChild($oMenuNode);
- unset(self::$aLoadedMenus[$sMenuId]);
- }
- else
- {
- self::$aLoadedMenus[$sMenuId]->SetAttribute('_operation', 'removed');
- }
-
- }
- /**
- * Modify a menu within the DOM
- * @param string $sMenuId
- * @param DOMNode $oMenuNode
- * @throws Exception
- */
- public function AlterMenu($sMenuId, DOMNode $oMenuNode)
- {
- // Todo - implement... do we have to handle menu renaming ???
- }
- protected function _priv_AlterNode(DOMNode $oNode, DOMNode $oDeltaNode)
- {
- foreach ($oDeltaNode->attributes as $sName => $oAttrNode)
- {
- $sCurrentValue = $oNode->getAttribute($sName);
- $sNewValue = $oAttrNode->value;
- $oNode->setAttribute($sName, $oAttrNode->value);
- }
-
- $aSrcChildNodes = $oNode->childNodes;
- foreach($oDeltaNode->childNodes as $index => $oChildNode)
- {
- if (!$oChildNode instanceof DOMElement)
- {
- // Text or CData nodes are treated by position
- $sOperation = $oChildNode->parentNode->getAttribute('_operation');
- switch($sOperation)
- {
- case 'removed':
- // ???
- break;
-
- case 'modified':
- case 'replaced':
- case 'added':
- $oNewNode = $this->oDOMDocument->importNode($oChildNode);
- $oSrcChildNode = $aSrcChildNodes->item($index);
- if ($oSrcChildNode)
- {
- $oNode->replaceChild($oNewNode, $oSrcChildNode);
- }
- else
- {
- $oNode->appendChild($oNewNode);
- }
-
- break;
-
- case '':
- // Do nothing
- }
- }
- else
- {
- $sOperation = $oChildNode->getAttribute('_operation');
- $sPath = $oChildNode->tagName;
- $sName = $oChildNode->getAttribute('id');
- if ($sName != '')
- {
- $sPath .= "[@id='$sName']";
- }
- switch($sOperation)
- {
- case 'removed':
- $oToRemove = $this->_priv_GetNodes($sPath, $oNode)->item(0);
- if ($oToRemove != null)
- {
- $this->_priv_SetFlag($oToRemove, 'removed');
- }
- break;
-
- case 'modified':
- $oToModify = $this->_priv_GetNodes($sPath, $oNode)->item(0);
- if ($oToModify != null)
- {
- $this->_priv_AlterNode($oToModify, $oChildNode);
- }
- else
- {
- throw new Exception("Cannot modify the non-existing node '$sPath' in '".$oNode->getNodePath()."'");
- }
- break;
-
- case 'replaced':
- $oNewNode = $this->oDOMDocument->importNode($oChildNode, true); // Import the node and its child nodes
- $oToModify = $this->_priv_GetNodes($sPath, $oNode)->item(0);
- $oNode->replaceChild($oNewNode, $oToModify);
- break;
-
- case 'added':
- $oNewNode = $this->oDOMDocument->importNode($oChildNode);
- $oNode->appendChild($oNewNode);
- $this->_priv_SetFlag($oNewNode, 'added');
- break;
-
- case '':
- // Do nothing
- }
- }
- }
- }
-
- public function GetClassXMLTemplate($sName, $sIcon)
- {
- return
- <<<EOF
- <?xml version="1.0" encoding="utf-8"?>
- <class id="$sName">
- <comment/>
- <properties>
- </properties>
- <naming format=""><attributes/></naming>
- <reconciliation><attributes/></reconciliation>
- <display_template/>
- <icon>$sIcon</icon>
- </properties>
- <fields/>
- <lifecycle/>
- <methods/>
- <presentation>
- <details><items/></details>
- <search><items/></search>
- <list><items/></list>
- </presentation>
- </class>
- EOF
- ;
- }
- /**
- * List all classes from the DOM, for a given module
- * @param string $sModuleNale
- * @param bool $bFlattenLayers
- * @throws Exception
- */
- public function ListClasses($sModuleName, $bFlattenLayers = true)
- {
- $sXPath = "//class[@_created_in='$sModuleName']";
- if ($bFlattenLayers)
- {
- $sXPath = "//class[@_created_in='$sModuleName' and @_operation!='removed']";
- }
- return $this->_priv_GetNodes($sXPath);
- }
-
- /**
- * List all classes from the DOM, for a given module
- * @param string $sModuleNale
- * @param bool $bFlattenLayers
- * @throws Exception
- */
- public function ListAllClasses($bFlattenLayers = true)
- {
- $sXPath = "//class";
- if ($bFlattenLayers)
- {
- $sXPath = "//class[@_operation!='removed']";
- }
- return $this->_priv_GetNodes($sXPath);
- }
-
- public function GetClass($sClassName, $bFlattenLayers = true)
- {
- if (!$this->ClassNameExists($sClassName))
- {
- return null;
- }
- $oClassNode = self::$aLoadedClasses[$sClassName];
- if ($bFlattenLayers)
- {
- $sOperation = $oClassNode->getAttribute('_operation');
- if ($sOperation == 'removed')
- {
- $oClassNode = null;
- }
- }
- return $oClassNode;
- }
-
- public function GetChildClasses($oClassNode, $bFlattenLayers = true)
- {
- $sXPath = "class";
- if ($bFlattenLayers)
- {
- $sXPath = "class[(@_operation!='removed')]";
- }
- return $this->_priv_GetNodes($sXPath, $oClassNode);
- }
-
-
- public function GetField($sClassName, $sAttCode, $bFlattenLayers = true)
- {
- if (!$this->ClassNameExists($sClassName))
- {
- return null;
- }
- $oClassNode = self::$aLoadedClasses[$sClassName];
- if ($bFlattenLayers)
- {
- $sOperation = $oClassNode->getAttribute('_operation');
- if ($sOperation == 'removed')
- {
- $oClassNode = null;
- }
- }
- $sXPath = "fields/field[@id='$sAttCode']";
- if ($bFlattenLayers)
- {
- $sXPath = "fields/field[(@id='$sAttCode' and (not(@_operation) or @_operation!='removed'))]";
- }
- $oFieldNode = $this->_priv_GetNodes($sXPath, $oClassNode)->item(0);
- if (($oFieldNode == null) && ($sParentClass = $oClassNode->GetUniqueElement('properties')->GetChildText('parent')))
- {
- return $this->GetField($sParentClass, $sAttCode, $bFlattenLayers);
- }
- return $oFieldNode;
- }
-
- /**
- * List all classes from the DOM
- * @param bool $bFlattenLayers
- * @throws Exception
- */
- public function ListFields(DOMNode $oClassNode, $bFlattenLayers = true)
- {
- $sXPath = "fields/field";
- if ($bFlattenLayers)
- {
- $sXPath = "fields/field[not(@_operation) or @_operation!='removed']";
- }
- return $this->_priv_GetNodes($sXPath, $oClassNode);
- }
-
- public function AddField(DOMNode $oClassNode, $sFieldCode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
- {
- $oNewField = $this->oDOMDocument->createElement('field');
- $oNewField->setAttribute('id', $sFieldCode);
- $this->_priv_AlterField($oNewField, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams);
- $oFields = $oClassNode->getElementsByTagName('fields')->item(0);
- $oFields->AppendChild($oNewField);
- $this->_priv_SetFlag($oNewField, 'added');
- }
-
- public function RemoveField(DOMNode $oClassNode, $sFieldCode)
- {
- $sXPath = "fields/field[@id='$sFieldCode']";
- $oFieldNodes = $this->_priv_GetNodes($sXPath, $oClassNode);
- if (is_object($oFieldNodes) && (is_object($oFieldNodes->item(0))))
- {
- $oFieldNode = $oFieldNodes->item(0);
- $sOpCode = $oFieldNode->getAttribute('_operation');
- if ($oFieldNode->getAttribute('_operation') == 'added')
- {
- $oFieldNode->parentNode->removeChild($oFieldNode);
- }
- else
- {
- $this->_priv_SetFlag($oFieldNode, 'removed');
- }
- }
- }
-
- public function AlterField(DOMNode $oClassNode, $sFieldCode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
- {
- $sXPath = "fields/field[@id='$sFieldCode']";
- $oFieldNodes = $this->_priv_GetNodes($sXPath, $oClassNode);
- if (is_object($oFieldNodes) && (is_object($oFieldNodes->item(0))))
- {
- $oFieldNode = $oFieldNodes->item(0);
- //@@TODO: if the field was 'added' => then let it as 'added'
- $sOpCode = $oFieldNode->getAttribute('_operation');
- switch($sOpCode)
- {
- case 'added':
- case 'modified':
- // added or modified, let it as it is
- break;
-
- default:
- $this->_priv_SetFlag($oFieldNodes->item(0), 'modified');
- }
- $this->_priv_AlterField($oFieldNodes->item(0), $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams);
- }
- }
- protected function _priv_AlterField(DOMNode $oFieldNode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
- {
- switch($sFieldType)
- {
- case 'Blob':
- case 'Boolean':
- case 'CaseLog':
- case 'Deadline':
- case 'Duration':
- case 'EmailAddress':
- case 'EncryptedString':
- case 'HTML':
- case 'IPAddress':
- case 'LongText':
- case 'OQL':
- case 'OneWayPassword':
- case 'Password':
- case 'Percentage':
- case 'String':
- case 'Text':
- case 'Text':
- case 'TemplateHTML':
- case 'TemplateString':
- case 'TemplateText':
- case 'URL':
- case 'Date':
- case 'DateTime':
- case 'Decimal':
- case 'Integer':
- break;
-
- case 'ExternalKey':
- $this->_priv_AddFieldAttribute($oFieldNode, 'target_class', $aExtraParams);
- // Fall through
- case 'HierarchicalKey':
- $this->_priv_AddFieldAttribute($oFieldNode, 'on_target_delete', $aExtraParams);
- $this->_priv_AddFieldAttribute($oFieldNode, 'filter', $aExtraParams);
- break;
- case 'ExternalField':
- $this->_priv_AddFieldAttribute($oFieldNode, 'extkey_attcode', $aExtraParams);
- $this->_priv_AddFieldAttribute($oFieldNode, 'target_attcode', $aExtraParams);
- break;
-
- case 'Enum':
- $this->_priv_SetFieldValues($oFieldNode, $aExtraParams);
- break;
-
- case 'LinkedSetIndirect':
- $this->_priv_AddFieldAttribute($oFieldNode, 'ext_key_to_remote', $aExtraParams);
- // Fall through
- case 'LinkedSet':
- $this->_priv_AddFieldAttribute($oFieldNode, 'linked_class', $aExtraParams);
- $this->_priv_AddFieldAttribute($oFieldNode, 'ext_key_to_me', $aExtraParams);
- $this->_priv_AddFieldAttribute($oFieldNode, 'count_min', $aExtraParams);
- $this->_priv_AddFieldAttribute($oFieldNode, 'count_max', $aExtraParams);
- break;
-
- default:
- throw(new Exception('Unsupported type of field: '.$sFieldType));
- }
- $this->_priv_SetFieldDependencies($oFieldNode, $aExtraParams);
- $oFieldNode->setAttribute('type', $sFieldType);
- $oFieldNode->setAttribute('sql', $sSQL);
- $oFieldNode->setAttribute('default_value', $defaultValue);
- $oFieldNode->setAttribute('is_null_alllowed', $bIsNullAllowed ? 'true' : 'false');
- }
-
- protected function _priv_AddFieldAttribute(DOMNode $oFieldNode, $sAttributeCode, $aExtraParams, $bMandatory = false)
- {
- $value = array_key_exists($sAttributeCode, $aExtraParams) ? $aExtraParams[$sAttributeCode] : '';
- if (($value == '') && (!$bMandatory)) return;
- $oFieldNode->setAttribute($sAttributeCode, $value);
- }
-
- protected function _priv_SetFieldDependencies($oFieldNode, $aExtraParams)
- {
- $aDeps = array_key_exists('dependencies', $aExtraParams) ? $aExtraParams['dependencies'] : '';
- $oDependencies = $oFieldNode->getElementsByTagName('dependencies')->item(0);
- // No dependencies before, and no dependencies to add, exit
- if (($oDependencies == null) && ($aDeps == '')) return;
-
- // Remove the previous dependencies
- $oFieldNode->removeChild($oDependencies);
- // If no dependencies, exit
- if ($aDeps == '') return;
- // Build the new list of dependencies
- $oDependencies = $this->oDOMDocument->createElement('dependencies');
- foreach($aDeps as $sAttCode)
- {
- $oDep = $this->oDOMDocument->createElement('attribute');
- $oDep->setAttribute('id', $sAttCode);
- $oDependencies->addChild($oDep);
- }
- $oFieldNode->addChild($oDependencies);
- }
-
- protected function _priv_SetFieldValues($oFieldNode, $aExtraParams)
- {
- $aVals = array_key_exists('values', $aExtraParams) ? $aExtraParams['values'] : '';
- $oValues = $oFieldNode->getElementsByTagName('values')->item(0);
- // No dependencies before, and no dependencies to add, exit
- if (($oValues == null) && ($aVals == '')) return;
-
- // Remove the previous dependencies
- $oFieldNode->removeChild($oValues);
- // If no dependencies, exit
- if ($aVals == '') return;
- // Build the new list of dependencies
- $oValues = $this->oDOMDocument->createElement('values');
- foreach($aVals as $sValue)
- {
- $oVal = $this->oDOMDocument->createElement('value', $sValue);
- $oValues->appendChild($oVal);
- }
- $oFieldNode->appendChild($oValues);
- }
- public function SetPresentation(DOMNode $oClassNode, $sPresentationCode, $aPresentation)
- {
- $oPresentation = $oClassNode->getElementsByTagName('presentation')->item(0);
- if (!is_object($oPresentation))
- {
- $oPresentation = $this->oDOMDocument->createElement('presentation');
- $oClassNode->appendChild($oPresentation);
- }
- $oZlist = $oPresentation->getElementsByTagName($sPresentationCode)->item(0);
- if (is_object($oZlist))
- {
- // Remove the previous Zlist
- $oPresentation->removeChild($oZlist);
- }
- // Create the ZList anew
- $oZlist = $this->oDOMDocument->createElement($sPresentationCode);
- $oPresentation->appendChild($oZlist);
- $this->AddZListItem($oZlist, $aPresentation);
- $this->_priv_SetFlag($oZlist, 'replaced');
- }
- protected function AddZListItem($oXMLNode, $value)
- {
- if (is_array($value))
- {
- $oXmlItems = $this->oDOMDocument->CreateElement('items');
- $oXMLNode->appendChild($oXmlItems);
-
- foreach($value as $key => $item)
- {
- $oXmlItem = $this->oDOMDocument->CreateElement('item');
- $oXmlItems->appendChild($oXmlItem);
-
- if (is_string($key))
- {
- $oXmlItem->SetAttribute('key', $key);
- }
- $this->AddZListItem($oXmlItem, $item);
- }
- }
- else
- {
- $oXmlText = $this->oDOMDocument->CreateTextNode((string) $value);
- $oXMLNode->appendChild($oXmlText);
- }
- }
-
- public function _priv_SetFlag($oNode, $sFlagValue)
- {
- $sPreviousFlag = $oNode->getAttribute('_operation');
- if ($sPreviousFlag == 'added')
- {
- // Do nothing ??
- }
- else
- {
- $oNode->setAttribute('_operation', $sFlagValue);
- }
- if ($oNode->tagName != 'class')
- {
- $this->_priv_SetFlag($oNode->parentNode, 'modified');
- }
- }
- /**
- * List all transitions from a given state
- * @param DOMNode $oStateNode The state
- * @param bool $bFlattenLayers
- * @throws Exception
- */
- public function ListTransitions(DOMNode $oStateNode, $bFlattenLayers = true)
- {
- $sXPath = "transitions/transition";
- if ($bFlattenLayers)
- {
- //$sXPath = "transitions/transition[@_operation!='removed']";
- }
- return $this->_priv_GetNodes($sXPath, $oStateNode);
- }
-
- /**
- * List all states of a given class
- * @param DOMNode $oClassNode The class
- * @param bool $bFlattenLayers
- * @throws Exception
- */
- public function ListStates(DOMNode $oClassNode, $bFlattenLayers = true)
- {
- $sXPath = "lifecycle/states/state";
- if ($bFlattenLayers)
- {
- //$sXPath = "lifecycle/states/state[@_operation!='removed']";
- }
- return $this->_priv_GetNodes($sXPath, $oClassNode);
- }
-
- /**
- * List Zlists from the DOM for a given class
- * @param bool $bFlattenLayers
- * @throws Exception
- */
- public function ListZLists(DOMNode $oClassNode, $bFlattenLayers = true)
- {
- // Not yet implemented !!!
- return array();
- }
-
- /**
- * List all menus from the DOM, for a given module
- * @param string $sModuleName
- * @param bool $bFlattenLayers
- * @throws Exception
- */
- public function ListMenus($sModuleName, $bFlattenLayers = true)
- {
- $sXPath = "//menu[@_created_in='$sModuleName']";
- if ($bFlattenLayers)
- {
- $sXPath = "//menu[@_created_in='$sModuleName' and @_operation!='removed']";
- }
- return $this->_priv_GetNodes($sXPath, $this->oMenus);
- }
-
- /**
- * Get a menu, given its is id
- * @param string $sModuleName
- * @param bool $bFlattenLayers
- * @throws Exception
- */
- public function GetMenu($sMenuId, $bFlattenLayers = true)
- {
- if (!array_key_exists($sMenuId, self::$aLoadedMenus))
- {
- return null;
- }
- $oMenuNode = self::$aLoadedMenus[$sMenuId];
- if ($bFlattenLayers)
- {
- $sOperation = $oMenuNode->getAttribute('_operation');
- if ($sOperation == 'removed')
- {
- $oMenuNode = null;
- }
- }
- return $oMenuNode;
- }
-
- public function ApplyChanges()
- {
- $oNodes = $this->ListChanges();
- foreach($oNodes as $oClassNode)
- {
- $sOperation = $oClassNode->GetAttribute('_operation');
- switch($sOperation)
- {
- case 'added':
- case 'modified':
- // marked as added or modified, just reset the flag
- $oClassNode->setAttribute('_operation', '');
- break;
-
- case 'removed':
- // marked as deleted, let's remove the node from the tree
- $oParent = $oClassNode->parentNode;
- $sClass = $oClassNode->GetAttribute('id');
- echo "Calling removeChild...<br/>";
- $oParent->removeChild($oClassNode);
- unset(self::$aLoadedClasses[$sClass]);
- break;
- }
- }
- }
-
- public function ListChanges()
- {
- $sXPath = "//*[@_operation!='']";
- return $this->_priv_GetNodes($sXPath);
- }
-
- /**
- * Get the text/XML version of the delta
- */
- public function GetDelta()
- {
- $oDelta = new DOMDocument('1.0', 'UTF-8');
- $oDelta->registerNodeClass('DOMElement', 'MFDOMElement');
- $oRootNode = $oDelta->createElement('itop_design');
- $oDelta->appendChild($oRootNode);
- $oClasses = $oDelta->createElement('classes');
- $oRootNode->appendChild($oClasses);
-
- $this->_priv_ImportModifiedChildren($oDelta, $oClasses, $this->oDOMDocument->firstChild);
- //file_put_contents($sXMLDestPath, $oDelta->saveXML());
- return $oDelta->saveXML();
- }
-
-
- protected function _priv_ImportModifiedChildren(DOMDocument $oDoc, DOMNode $oDestNode, DOMNode $oNode)
- {
- static $iDepth = 0;
- $iDepth++;
- foreach($oNode->childNodes as $oChildNode)
- {
- $sNodeName = $oChildNode->nodeName;
- $sNodeValue = $oChildNode->nodeValue;
- if (!$oChildNode instanceof DOMElement)
- {
- $sName = '$$';
- $sOperation = $oChildNode->parentNode->getAttribute('_operation');
- }
- else
- {
- $sName = $oChildNode->getAttribute('id');;
- $sOperation = $oChildNode->getAttribute('_operation');
- }
-
- echo str_repeat('+', $iDepth)." $sNodeName [$sName], operation: $sOperation\n";
-
- switch($sOperation)
- {
- case 'removed':
- $oDeletedNode = $oDoc->importNode($oChildNode->cloneNode(false)); // Copies all the node's attributes, but NOT the child nodes
- $oDeletedNode->removeAttribute('_source');
- if ($oDeletedNode->tagName == 'class')
- {
- // classes are always located under the root node
- $oDoc->firstChild->appendChild($oDeletedNode);
- }
- else
- {
- $oDestNode->appendChild($oDeletedNode);
- }
- echo "<p>".str_repeat('+', $iDepth).$oChildNode->getAttribute('id')." was removed...</p>";
- break;
- case 'added':
- echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('id')." was created...</p>";
- $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes as well
- if ($oChildNode instanceof DOMElement)
- {
- $oModifiedNode->removeAttribute('_source');
- if ($oModifiedNode->tagName == 'class')
- {
- echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('id')." inserting under 'classes'...</p>";
- // classes are always located under the root node
- $oDoc->firstChild->appendChild($oModifiedNode);
-
- // Process any subclasses: move them under the root of the document
- $oSubclasses = $oModifiedNode->GetElementsByTagName('class');
- $aSubClasses = array();
- foreach($oSubclasses as $oSubclassNode)
- {
- $aSubClasses[] = $oSubclassNode;
- }
- for($index = count($aSubClasses)-1; $index >= 0; $index--)
- {
- $aSubClasses[$index]->parentNode->removeChild($aSubClasses[$index]);
- $oDoc->firstChild->appendChild($aSubClasses[$index]);
- }
- }
- else
- {
- echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('id')." inserting in the hierarchy...</p>";
- $oDestNode->appendChild($oModifiedNode);
- }
- }
- else
- {
- $oDestNode->appendChild($oModifiedNode);
- }
- break;
-
- case 'replaced':
- echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('id')." was replaced...</p>";
- $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes as well
- if ($oChildNode instanceof DOMElement)
- {
- $oModifiedNode->removeAttribute('_source');
- }
- $oDestNode->appendChild($oModifiedNode);
- break;
-
- case 'modified':
- echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('id')." was modified...</p>";
- if ($oChildNode instanceof DOMElement)
- {
- echo str_repeat('+', $iDepth)." Copying (NON recursively) the modified node\n";
- $oModifiedNode = $oDoc->importNode($oChildNode, false); // Copies all the node's attributes, but NOT the child nodes
- $oModifiedNode->removeAttribute('_source');
- $this->_priv_ImportModifiedChildren($oDoc, $oModifiedNode, $oChildNode);
- if ($oModifiedNode->tagName == 'class')
- {
- // classes are always located under the root node
- $oDoc->firstChild->appendChild($oModifiedNode);
- }
- else
- {
- $oDestNode->appendChild($oModifiedNode);
- }
- }
- else
- {
- echo str_repeat('+', $iDepth)." Copying (recursively) the modified node\n";
- $oModifiedNode = $oDoc->importNode($oChildNode, true); // Copies all the node's attributes, and the child nodes
- $oDestNode->appendChild($oModifiedNode);
- }
- break;
-
- default:
- // No change: do nothing
- echo "<p>".str_repeat('+', $iDepth).$oChildNode->tagName.':'.$oChildNode->getAttribute('id')." was NOT modified...</p>";
- $oModifiedNode = $oDoc->importNode($oChildNode, true); // Importing the node for future recusrsion if needed
- if ($oChildNode->tagName == 'class')
- {
- echo "<p>".str_repeat('+', $iDepth)."Checking if a subclass of ".$oChildNode->getAttribute('id')." was modified...</p>";
- // classes are always located under the root node
- $this->_priv_ImportModifiedChildren($oDoc, $oModifiedNode, $oChildNode);
- }
- }
- }
- $iDepth--;
- }
- /**
- * Searches on disk in the root directory for module description files
- * and returns an array of MFModules
- * @return array Array of MFModules
- */
- public function FindModules($sSubDirectory = '')
- {
- $aAvailableModules = ModuleDiscovery::GetAvailableModules($this->sRootDir, $sSubDirectory);
- $aResult = array();
- foreach($aAvailableModules as $sId => $aModule)
- {
- $aResult[] = new MFModule($sId, $aModule['root_dir'], $aModule['label']);
- }
- return $aResult;
- }
-
- /**
- * Produce the PHP files corresponding to the data model
- * @param $bTestOnly bool
- * @return array Array of errors, empty if no error
- */
- public function Compile($bTestOnly)
- {
- return array();
- }
-
- /**
- * Extracts some nodes from the DOM
- * @param string $sXPath A XPath expression
- * @return DOMNodeList
- */
- public function _priv_GetNodes($sXPath, $oContextNode = null)
- {
- $oXPath = new DOMXPath($this->oDOMDocument);
-
- if (is_null($oContextNode))
- {
- return $oXPath->query($sXPath);
- }
- else
- {
- return $oXPath->query($sXPath, $oContextNode);
- }
- }
-
- /**
- * Insert a new node in the DOM
- * @param DOMNode $oNode
- * @param DOMNode $oParentNode
- */
- public function _priv_AddNode(DOMNode $oNode, DOMNode $oParentNode)
- {
- }
-
- /**
- * Remove a node from the DOM
- * @param DOMNode $oNode
- * @param DOMNode $oParentNode
- */
- public function _priv_RemoveNode(DOMNode $oNode)
- {
- }
-
- /**
- * Add or modify an attribute of a node
- * @param DOMNode $oNode
- * @param string $sAttributeName
- * @param mixed $atttribueValue
- */
- public function _priv_SetNodeAttribute(DOMNode $oNode, $sAttributeName, $atttribueValue)
- {
- }
- }
- /**
- * MFDOMElement: helper to read the information from the DOM
- * @package ModelFactory
- */
- class MFDOMElement extends DOMElement
- {
- /**
- * Returns the node directly under the given node
- */
- public function GetUniqueElement($sTagName, $bMustExist = true)
- {
- $oNode = null;
- foreach($this->childNodes as $oChildNode)
- {
- if ($oChildNode->nodeName == $sTagName)
- {
- $oNode = $oChildNode;
- break;
- }
- }
- if ($bMustExist && is_null($oNode))
- {
- throw new DOMFormatException('Missing unique tag: '.$sTagName);
- }
- return $oNode;
- }
-
- /**
- * Returns the node directly under the current node, or null if missing
- */
- public function GetOptionalElement($sTagName)
- {
- return $this->GetUniqueElement($sTagName, false);
- }
-
-
- /**
- * Returns the TEXT of the current node (possibly from several subnodes)
- */
- public function GetText($sDefault = null)
- {
- $sText = null;
- foreach($this->childNodes as $oChildNode)
- {
- if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection
- {
- if (is_null($sText)) $sText = '';
- $sText .= $oChildNode->wholeText;
- }
- }
- if (is_null($sText))
- {
- return $sDefault;
- }
- else
- {
- return $sText;
- }
- }
-
- /**
- * Get the TEXT value from the child node
- */
- public function GetChildText($sTagName, $sDefault = null)
- {
- $sRet = $sDefault;
- if ($oChild = $this->GetOptionalElement($sTagName))
- {
- $sRet = $oChild->GetText($sDefault);
- }
- return $sRet;
- }
- /**
- * Assumes the current node to be either a text or
- * <items>
- * <item [key]="..."]>value<item>
- * <item [key]="..."]>value<item>
- * </items>
- * where value can be the either a text or an array of items... recursively
- * Returns a PHP array
- */
- public function GetNodeAsArrayOfItems()
- {
- $oItems = $this->GetOptionalElement('items');
- if ($oItems)
- {
- $res = array();
- foreach($oItems->childNodes as $oItem)
- {
- // When an attribute is missing
- if ($oItem->hasAttribute('id'))
- {
- $key = $oItem->getAttribute('id');
- $res[$key] = $oItem->GetNodeAsArrayOfItems();
- }
- else
- {
- $res[] = $oItem->GetNodeAsArrayOfItems();
- }
- }
- }
- else
- {
- $res = $this->GetText();
- }
- return $res;
- }
- }
|