compiler.class.inc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <?php
  2. // Copyright (C) 2011 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. class DOMFormatException extends Exception
  17. {
  18. }
  19. class CompilerEchoPage
  20. {
  21. public function p($s)
  22. {
  23. echo "<p>\n$s</p>\n";
  24. }
  25. public function add($s)
  26. {
  27. //echo $s;
  28. }
  29. public function output()
  30. {
  31. }
  32. }
  33. /**
  34. * Compiler class
  35. */
  36. class MFCompiler
  37. {
  38. protected $oFactory;
  39. protected $sSourceDir;
  40. public function __construct($oModelFactory, $sSourceDir)
  41. {
  42. $this->oFactory = $oModelFactory;
  43. $this->sSourceDir = $sSourceDir;
  44. }
  45. public function Compile($sTargetDir, $oP = null)
  46. {
  47. if (is_null($oP))
  48. {
  49. $oP = new CompilerEchoPage();
  50. }
  51. $aResultFiles = array();
  52. $aModules = $this->oFactory->GetLoadedModules();
  53. foreach($aModules as $foo => $oModule)
  54. {
  55. $sModuleName = $oModule->GetName();
  56. $sModuleVersion = $oModule->GetVersion();
  57. $sModuleRootDir = realpath($oModule->GetRootDir());
  58. $sRelativeDir = substr($sModuleRootDir, strlen($this->sSourceDir));
  59. // Push the other module files
  60. $this->CopyDirectory($sModuleRootDir, $sTargetDir.$sRelativeDir);
  61. $oClasses = $this->oFactory->ListClasses($sModuleName);
  62. $iClassCount = $oClasses->length;
  63. if ($iClassCount == 0)
  64. {
  65. $oP->p("Found module without classes declared: $sModuleName");
  66. }
  67. else
  68. {
  69. $sResultFile = $sTargetDir.$sRelativeDir.'/model.'.$sModuleName.'.php';
  70. if (is_file($sResultFile))
  71. {
  72. $oP->p("Updating <a href=\"#$sModuleName\">$sResultFile</a> for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
  73. }
  74. else
  75. {
  76. $sResultDir = dirname($sResultFile);
  77. if (!is_dir($sResultDir))
  78. {
  79. $oP->p("Creating directory $sResultDir");
  80. mkdir($sResultDir, 0777, true);
  81. }
  82. $oP->p("Creating <a href=\"#$sModuleName\">$sResultFile</a> for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
  83. }
  84. // Compile the module into a single file
  85. //
  86. $sId = $sModuleName;
  87. $aResultFiles[$sId] = $sResultFile;
  88. $sCurrDate = date(DATE_ISO8601);
  89. $sAuthor = 'Combodo compiler';
  90. $sLicence = 'http://www.opensource.org/licenses/gpl-3.0.html LGPL';
  91. $sFileHeader =
  92. <<<EOF
  93. <?php
  94. //
  95. // File generated by ... on the $sCurrDate
  96. // Please do not edit manually
  97. //
  98. //
  99. // Copyright (C) 2010 Combodo SARL
  100. //
  101. // ben on met quoi ici ?
  102. // Signé: Romain
  103. //
  104. // This program is free software; you can redistribute it and/or modify
  105. // it under the terms of the GNU General Public License as published by
  106. // the Free Software Foundation; version 3 of the License.
  107. //
  108. // This program is distributed in the hope that it will be useful,
  109. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  110. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  111. // GNU General Public License for more details.
  112. //
  113. // You should have received a copy of the GNU General Public License
  114. // along with this program; if not, write to the Free Software
  115. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  116. /**
  117. * Persistent classes for $sModuleName (version $sModuleVersion)
  118. *
  119. * @author $sAuthor
  120. * @license $sLicence
  121. */
  122. EOF;
  123. file_put_contents($sResultFile, $sFileHeader);
  124. foreach($oClasses as $oClass)
  125. {
  126. $sClass = $oClass->getAttribute("name");
  127. try
  128. {
  129. $this->CompileClass($oClass, $sResultFile, $oP);
  130. }
  131. catch (ssDOMFormatException $e)
  132. {
  133. $sClass = $oClass->getAttribute("name");
  134. throw new Exception("Failed to process class '$sClass', from '$sModuleRootDir': ".$e->getMessage());
  135. }
  136. }
  137. }
  138. }
  139. if (count($aResultFiles))
  140. {
  141. $oP->add('<h2>Files</h2>');
  142. foreach ($aResultFiles as $sModuleName => $sFile)
  143. {
  144. $oP->add('<h3>'.$sFile.'</h3>');
  145. $oP->add('<a name="'.$sModuleName.'"/><div style="border:1;">');
  146. $oP->add(highlight_file($sFile, true));
  147. $oP->add('</div style="">');
  148. }
  149. }
  150. $oP->output();
  151. }
  152. /**
  153. * Helper to copy the module files to the exploitation environment
  154. * Returns true if successfull
  155. */
  156. protected function CopyDirectory($sSource, $sDest)
  157. {
  158. if (is_dir($sSource))
  159. {
  160. if (!is_dir($sDest))
  161. {
  162. mkdir($sDest);
  163. }
  164. $aFiles = scandir($sSource);
  165. if(sizeof($aFiles) > 0 )
  166. {
  167. foreach($aFiles as $sFile)
  168. {
  169. if ($sFile == '.' || $sFile == '..' || $sFile == '.svn')
  170. {
  171. // Skip
  172. continue;
  173. }
  174. if (is_dir($sSource.'/'.$sFile))
  175. {
  176. $this->CopyDirectory($sSource.'/'.$sFile, $sDest.'/'.$sFile);
  177. }
  178. else
  179. {
  180. copy($sSource.'/'.$sFile, $sDest.'/'.$sFile);
  181. }
  182. }
  183. }
  184. return true;
  185. }
  186. elseif (is_file($sSource))
  187. {
  188. return copy($sSource, $sDest);
  189. }
  190. else
  191. {
  192. return false;
  193. }
  194. }
  195. /**
  196. * Helper to browse the DOM -could be factorized in ModelFactory
  197. * Returns the node directly under the given node, and that is supposed to be always present and unique
  198. */
  199. protected function GetUniqueElement($oDOMNode, $sTagName, $bMustExist = true)
  200. {
  201. $oNode = null;
  202. foreach($oDOMNode->childNodes as $oChildNode)
  203. {
  204. if ($oChildNode->nodeName == $sTagName)
  205. {
  206. $oNode = $oChildNode;
  207. break;
  208. }
  209. }
  210. if ($bMustExist && is_null($oNode))
  211. {
  212. throw new DOMFormatException('Missing unique tag: '.$sTagName);
  213. }
  214. return $oNode;
  215. }
  216. /**
  217. * Helper to browse the DOM -could be factorized in ModelFactory
  218. * Returns the node directly under the given node, or null is missing
  219. */
  220. protected function GetOptionalElement($oDOMNode, $sTagName)
  221. {
  222. return $this->GetUniqueElement($oDOMNode, $sTagName, false);
  223. }
  224. /**
  225. * Helper to browse the DOM -could be factorized in ModelFactory
  226. * Returns the TEXT of the given node (possibly from several subnodes)
  227. */
  228. protected function GetNodeText($oNode)
  229. {
  230. $sText = '';
  231. foreach($oNode->childNodes as $oChildNode)
  232. {
  233. if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection
  234. {
  235. $sText .= $oChildNode->wholeText;
  236. }
  237. }
  238. return $sText;
  239. }
  240. /**
  241. * Helper to browse the DOM -could be factorized in ModelFactory
  242. * Assumes the given node to be either a text or
  243. * <items>
  244. * <item [key]="..."]>value<item>
  245. * <item [key]="..."]>value<item>
  246. * </items>
  247. * where value can be the either a text or an array of items... recursively
  248. * Returns a PHP array
  249. */
  250. protected function GetNodeAsArrayOfItems($oNode)
  251. {
  252. $oItems = $this->GetOptionalElement($oNode, 'items');
  253. if ($oItems)
  254. {
  255. $res = array();
  256. foreach($oItems->childNodes as $oItem)
  257. {
  258. // When an attribute is msising
  259. if ($oItem->hasAttribute('key'))
  260. {
  261. $key = $oItem->getAttribute('key');
  262. $res[$key] = $this->GetNodeAsArrayOfItems($oItem);
  263. }
  264. else
  265. {
  266. $res[] = $this->GetNodeAsArrayOfItems($oItem);
  267. }
  268. }
  269. }
  270. else
  271. {
  272. $res = $this->GetNodeText($oNode);
  273. }
  274. return $res;
  275. }
  276. /**
  277. * Helper to format the flags for an attribute, in a given state
  278. * @param object $oAttNode DOM node containing the information to build the flags
  279. * Returns string PHP flags, based on the OPT_ATT_ constants, or empty (meaning 0, can be omitted)
  280. */
  281. protected function FlagsToPHP($oAttNode)
  282. {
  283. static $aNodeAttributeToFlag = array(
  284. 'mandatory' => 'OPT_ATT_MANDATORY',
  285. 'read_only' => 'OPT_ATT_READONLY',
  286. 'must_prompt' => 'OPT_ATT_MUSTPROMPT',
  287. 'must_change' => 'OPT_ATT_MUSTCHANGE',
  288. 'hidden' => 'OPT_ATT_HIDDEN',
  289. );
  290. $aFlags = array();
  291. foreach ($aNodeAttributeToFlag as $sNodeAttribute => $sFlag)
  292. {
  293. $bFlag = ($oAttNode->GetAttribute($sNodeAttribute) == '1');
  294. if ($bFlag)
  295. {
  296. $aFlags[] = $sFlag;
  297. }
  298. }
  299. $sRes = implode(' | ', $aFlags);
  300. return $sRes;
  301. }
  302. protected function CompileClass($oClass, $sResFile, $oP)
  303. {
  304. $sClass = $oClass->getAttribute('name');
  305. $oProperties = $this->GetUniqueElement($oClass, 'properties');
  306. // Class caracteristics
  307. //
  308. $aClassParams = array();
  309. $aClassParams['category'] = "'".$oClass->getAttribute('category')."'";
  310. $aClassParams['key_type'] = "'autoincrement'";
  311. $oNaming = $this->GetUniqueElement($oProperties, 'naming');
  312. $oNameAttributes = $this->GetUniqueElement($oNaming, 'attributes');
  313. $oAttributes = $oNameAttributes->getElementsByTagName('attribute');
  314. $aNameAttCodes = array();
  315. foreach($oAttributes as $oAttribute)
  316. {
  317. $aNameAttCodes[] = $oAttribute->getAttribute('name');
  318. }
  319. if (count($aNameAttCodes) > 1)
  320. {
  321. // New style...
  322. $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
  323. }
  324. elseif (count($aNameAttCodes) == 1)
  325. {
  326. // New style...
  327. $sNameAttCode = "'$aNameAttCodes[0]'";
  328. }
  329. else
  330. {
  331. $sNameAttCode = "''";
  332. }
  333. $aClassParams['name_attcode'] = $sNameAttCode;
  334. $oLifecycle = $this->GetOptionalElement($oClass, 'lifecycle');
  335. if ($oLifecycle)
  336. {
  337. $sStateAttCode = $oLifecycle->getAttribute('attribute');
  338. }
  339. else
  340. {
  341. $sStateAttCode = "";
  342. }
  343. $aClassParams['state_attcode'] = "'$sStateAttCode'";
  344. $oReconciliation = $this->GetUniqueElement($oProperties, 'reconciliation');
  345. $oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
  346. $aReconcAttCodes = array();
  347. foreach($oReconcAttributes as $oAttribute)
  348. {
  349. $aReconcAttCodes[] = $oAttribute->getAttribute('name');
  350. }
  351. $sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
  352. $aClassParams['reconc_keys'] = $sReconcKeys;
  353. $aClassParams['db_table'] = "'".$oClass->getAttribute('db_table')."'";
  354. $aClassParams['db_key_field'] = "'".$oClass->getAttribute('db_key_field')."'";
  355. $aClassParams['db_finalclass_field'] = "'".$oClass->getAttribute('db_final_class_field')."'";
  356. $oDisplayTemplate = $this->GetOptionalElement($oProperties, 'display_template');
  357. if ($oDisplayTemplate)
  358. {
  359. $sDisplayTemplate = $oDisplayTemplate->textContent;
  360. $aClassParams['display_template'] = "'$sDisplayTemplate'";
  361. }
  362. $oIcon = $this->GetOptionalElement($oProperties, 'icon');
  363. if ($oIcon)
  364. {
  365. $sIcon = $oIcon->textContent;
  366. $aClassParams['icon'] = "'$sIcon'";
  367. }
  368. // Finalize class params declaration
  369. //
  370. $aClassParamsPHP = array();
  371. foreach($aClassParams as $sKey => $sPHPValue)
  372. {
  373. $aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
  374. }
  375. $sClassParams = implode("\n", $aClassParamsPHP);
  376. // Comment on top of the class declaration
  377. //
  378. $oComment = $this->GetOptionalElement($oProperties, 'comment');
  379. if ($oComment)
  380. {
  381. $sCodeComment = $oComment->textContent;
  382. }
  383. else
  384. {
  385. $sCodeComment = '';
  386. }
  387. // Fields
  388. //
  389. $sAttributes = '';
  390. foreach($this->oFactory->ListFields($oClass) as $oField)
  391. {
  392. // $oField
  393. $sAttCode = $oField->getAttribute('name');
  394. $sAttType = 'Attribute'.$oField->getAttribute('type');
  395. $aDependencies = array();
  396. $oDependencies = $this->GetOptionalElement($oField, 'dependencies');
  397. if (!is_null($oDependencies))
  398. {
  399. $oDepNodes = $oDependencies->getElementsByTagName('attribute');
  400. foreach($oDepNodes as $oDepAttribute)
  401. {
  402. $aDependencies[] = "'".$oDepAttribute->getAttribute('name')."'";
  403. }
  404. }
  405. $sDependencies = 'array('.implode(', ', $aDependencies).')';
  406. $aParameters = array();
  407. if ($sAttType == 'AttributeLinkedSetIndirect')
  408. {
  409. $aParameters['linked_class'] = "'".$oField->getAttribute('linked_class')."'";
  410. $aParameters['ext_key_to_me'] = "'".$oField->getAttribute('ext_key_to_me')."'";
  411. $aParameters['ext_key_to_remote'] = "'".$oField->getAttribute('ext_key_to_remote')."'";
  412. // todo - utile ?
  413. $aParameters['allowed_values'] = 'null';
  414. $aParameters['count_min'] = $oField->getAttribute('count_min');
  415. $aParameters['count_max'] = $oField->getAttribute('count_max');
  416. $aParameters['depends_on'] = $sDependencies;
  417. }
  418. elseif ($sAttType == 'AttributeLinkedSet')
  419. {
  420. $aParameters['linked_class'] = "'".$oField->getAttribute('linked_class')."'";
  421. $aParameters['ext_key_to_me'] = "'".$oField->getAttribute('ext_key_to_me')."'";
  422. // todo - utile ?
  423. $aParameters['allowed_values'] = 'null';
  424. $aParameters['count_min'] = $oField->getAttribute('count_min');
  425. $aParameters['count_max'] = $oField->getAttribute('count_max');
  426. $aParameters['depends_on'] = $sDependencies;
  427. }
  428. elseif ($sAttType == 'AttributeExternalKey')
  429. {
  430. $aParameters['targetclass'] = "'".$oField->getAttribute('target_class')."'";
  431. // todo = v�rifier l'utilit�
  432. $aParameters['jointype'] = 'null';
  433. if (($sOql = $oField->getAttribute('filter')) != '')
  434. {
  435. $sEscapedOql = addslashes($sOql);
  436. $aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
  437. }
  438. else
  439. {
  440. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  441. }
  442. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  443. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  444. $aParameters['on_target_delete'] = $oField->getAttribute('on_target_delete');
  445. $aParameters['depends_on'] = $sDependencies;
  446. }
  447. elseif ($sAttType == 'AttributeHierarchicalKey')
  448. {
  449. if (($sOql = $oField->getAttribute('filter')) != '')
  450. {
  451. $sEscapedOql = addslashes($sOql);
  452. $aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
  453. }
  454. else
  455. {
  456. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  457. }
  458. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  459. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  460. $aParameters['on_target_delete'] = $oField->getAttribute('on_target_delete');
  461. $aParameters['depends_on'] = $sDependencies;
  462. }
  463. elseif ($sAttType == 'AttributeExternalField')
  464. {
  465. $aParameters['allowed_values'] = 'null';
  466. $aParameters['extkey_attcode'] = "'".$oField->getAttribute('extkey_attcode')."'";
  467. $aParameters['target_attcode'] = "'".$oField->getAttribute('target_attcode')."'";
  468. }
  469. elseif ($sAttType == 'AttributeURL')
  470. {
  471. $aParameters['target'] = "'".$oField->getAttribute('target')."'";
  472. $aParameters['allowed_values'] = 'null';
  473. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  474. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  475. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  476. $aParameters['depends_on'] = $sDependencies;
  477. }
  478. elseif ($sAttType == 'AttributeEnum')
  479. {
  480. $oValues = $this->GetUniqueElement($oField, 'values');
  481. $oValueNodes = $oValues->getElementsByTagName('value');
  482. $aValues = array();
  483. foreach($oValueNodes as $oValue)
  484. {
  485. // new style... $aValues[] = "'".addslashes($oValue->textContent)."'";
  486. $aValues[] = $oValue->textContent;
  487. }
  488. // new style... $sValues = 'array('.implode(', ', $aValues).')';
  489. $sValues = '"'.implode(',', $aValues).'"';
  490. $aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
  491. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  492. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  493. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  494. $aParameters['depends_on'] = $sDependencies;
  495. }
  496. elseif ($sAttType == 'AttributeBlob')
  497. {
  498. $aParameters['depends_on'] = $sDependencies;
  499. }
  500. else
  501. {
  502. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  503. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  504. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  505. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  506. $aParameters['depends_on'] = $sDependencies;
  507. if ($sValidationPattern = $oField->getAttribute('validation_pattern'))
  508. {
  509. $aParameters['validation_pattern'] = '"'.addslashes($sValidationPattern).'"';
  510. }
  511. }
  512. $aParams = array();
  513. foreach($aParameters as $sKey => $sValue)
  514. {
  515. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  516. }
  517. $sParams = implode(', ', $aParams);
  518. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  519. }
  520. // Lifecycle
  521. //
  522. $sLifecycle = '';
  523. if ($oLifecycle)
  524. {
  525. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  526. $sLifecycle .= "\t\t//\n";
  527. $oStimuli = $this->GetUniqueElement($oLifecycle, 'stimuli');
  528. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  529. {
  530. $sStimulus = $oStimulus->getAttribute('name');
  531. $sStimulusClass = $oStimulus->getAttribute('type');
  532. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  533. }
  534. $oStates = $this->GetUniqueElement($oLifecycle, 'states');
  535. foreach ($oStates->getElementsByTagName('state') as $oState)
  536. {
  537. $sState = $oState->getAttribute('name');
  538. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  539. $sLifecycle .= " \"".$sState."\",\n";
  540. $sLifecycle .= " array(\n";
  541. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  542. $sLifecycle .= " \"attribute_list\" => array(\n";
  543. $oFlags = $this->GetUniqueElement($oState, 'flags');
  544. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  545. {
  546. $sFlags = $this->FlagsToPHP($oAttributeNode);
  547. if (strlen($sFlags) > 0)
  548. {
  549. $sAttCode = $oAttributeNode->GetAttribute('name');
  550. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  551. }
  552. }
  553. $sLifecycle .= " ),\n";
  554. $sLifecycle .= " )\n";
  555. $sLifecycle .= " );\n";
  556. $oTransitions = $this->GetUniqueElement($oState, 'transitions');
  557. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  558. {
  559. $sStimulus = $oTransition->getAttribute('stimulus');
  560. $sTargetState = $oTransition->getAttribute('target');
  561. $oActions = $this->GetUniqueElement($oTransition, 'actions');
  562. $aVerbs = array();
  563. foreach ($oActions->getElementsByTagName('action') as $oAction)
  564. {
  565. $sVerb = $oAction->getAttribute('verb');
  566. $aVerbs[] = "'$sVerb'";
  567. }
  568. $sActions = implode(', ', $aVerbs);
  569. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  570. }
  571. }
  572. }
  573. // ZLists
  574. //
  575. $aListRef = array(
  576. 'details' => 'details',
  577. 'standard_search' => 'search',
  578. 'list' => 'list'
  579. );
  580. $oPresentation = $this->GetUniqueElement($oClass, 'presentation');
  581. $sZlists = '';
  582. foreach ($aListRef as $sListCode => $sListTag)
  583. {
  584. $oListNode = $this->GetUniqueElement($oPresentation, $sListTag);
  585. $aAttributes = $this->GetNodeAsArrayOfItems($oListNode);
  586. $sZAttributes = var_export($aAttributes, true);
  587. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  588. }
  589. // Methods
  590. $sMethods = "";
  591. $oMethods = $this->GetUniqueElement($oClass, 'methods');
  592. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  593. {
  594. $sMethodCode = $this->GetNodeText($oMethod);
  595. $oMethodComment = $this->GetOptionalElement($oMethod, 'comment');
  596. if ($oMethodComment)
  597. {
  598. $sMethods .= "\n\t".$oMethodComment->textContent."\n".$sMethodCode."\n";
  599. }
  600. else
  601. {
  602. $sMethods .= "\n\n".$sMethodCode."\n";
  603. }
  604. }
  605. // Let's make the whole class declaration
  606. //
  607. $sPHP = "\n\n$sCodeComment\n";
  608. if ($oClass->getAttribute('abstract') == 'true')
  609. {
  610. $sPHP .= 'abstract class '.$oClass->getAttribute('name');
  611. }
  612. else
  613. {
  614. $sPHP .= 'class '.$oClass->getAttribute('name');
  615. }
  616. $sPHP .= " extends ".$oClass->getAttribute('parent')."\n";
  617. $sPHP .=
  618. <<<EOF
  619. {
  620. public static function Init()
  621. {
  622. \$aParams = array
  623. (
  624. $sClassParams
  625. );
  626. MetaModel::Init_Params(\$aParams);
  627. MetaModel::Init_InheritAttributes();
  628. $sAttributes
  629. $sLifecycle
  630. $sZlists
  631. }
  632. $sMethods
  633. }
  634. EOF;
  635. file_put_contents($sResFile, $sPHP, FILE_APPEND);
  636. }
  637. }
  638. ?>