compiler.class.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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, $sRelativeDir, $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. if ($oDOMNode->hasChildNodes())
  203. {
  204. foreach($oDOMNode->childNodes as $oChildNode)
  205. {
  206. if ($oChildNode->nodeName == $sTagName)
  207. {
  208. $oNode = $oChildNode;
  209. break;
  210. }
  211. }
  212. }
  213. if ($bMustExist && is_null($oNode))
  214. {
  215. throw new DOMFormatException('Missing unique tag: '.$sTagName);
  216. }
  217. return $oNode;
  218. }
  219. /**
  220. * Helper to browse the DOM -could be factorized in ModelFactory
  221. * Returns the node directly under the given node, or null is missing
  222. */
  223. protected function GetOptionalElement($oDOMNode, $sTagName)
  224. {
  225. return $this->GetUniqueElement($oDOMNode, $sTagName, false);
  226. }
  227. /**
  228. * Helper to browse the DOM -could be factorized in ModelFactory
  229. * Returns the TEXT of the given node (possibly from several subnodes)
  230. */
  231. protected function GetNodeText($oNode)
  232. {
  233. $sText = '';
  234. if ($oNode->hasChildNodes())
  235. {
  236. foreach($oNode->childNodes as $oChildNode)
  237. {
  238. if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection
  239. {
  240. $sText .= $oChildNode->wholeText;
  241. }
  242. }
  243. }
  244. return $sText;
  245. }
  246. /**
  247. * Helper to browse the DOM -could be factorized in ModelFactory
  248. * Assumes the given node to be either a text or
  249. * <items>
  250. * <item [key]="..."]>value<item>
  251. * <item [key]="..."]>value<item>
  252. * </items>
  253. * where value can be the either a text or an array of items... recursively
  254. * Returns a PHP array
  255. */
  256. protected function GetNodeAsArrayOfItems($oNode)
  257. {
  258. $oItems = $this->GetOptionalElement($oNode, 'items');
  259. if ($oItems)
  260. {
  261. $res = array();
  262. if ($oItems->hasChildNodes())
  263. {
  264. foreach($oItems->childNodes as $oItem)
  265. {
  266. // When an attribute is msising
  267. if ($oItem->hasAttributes() && $oItem->hasAttribute('key'))
  268. {
  269. $key = $oItem->getAttribute('key');
  270. $res[$key] = $this->GetNodeAsArrayOfItems($oItem);
  271. }
  272. else
  273. {
  274. $res[] = $this->GetNodeAsArrayOfItems($oItem);
  275. }
  276. }
  277. }
  278. }
  279. else
  280. {
  281. $res = $this->GetNodeText($oNode);
  282. }
  283. return $res;
  284. }
  285. /**
  286. * Helper to format the flags for an attribute, in a given state
  287. * @param object $oAttNode DOM node containing the information to build the flags
  288. * Returns string PHP flags, based on the OPT_ATT_ constants, or empty (meaning 0, can be omitted)
  289. */
  290. protected function FlagsToPHP($oAttNode)
  291. {
  292. static $aNodeAttributeToFlag = array(
  293. 'mandatory' => 'OPT_ATT_MANDATORY',
  294. 'read_only' => 'OPT_ATT_READONLY',
  295. 'must_prompt' => 'OPT_ATT_MUSTPROMPT',
  296. 'must_change' => 'OPT_ATT_MUSTCHANGE',
  297. 'hidden' => 'OPT_ATT_HIDDEN',
  298. );
  299. $aFlags = array();
  300. foreach ($aNodeAttributeToFlag as $sNodeAttribute => $sFlag)
  301. {
  302. $bFlag = ($oAttNode->GetAttribute($sNodeAttribute) == '1');
  303. if ($bFlag)
  304. {
  305. $aFlags[] = $sFlag;
  306. }
  307. }
  308. $sRes = implode(' | ', $aFlags);
  309. return $sRes;
  310. }
  311. protected function CompileClass($oClass, $sResFile, $sModuleRelativeDir, $oP)
  312. {
  313. $sClass = $oClass->getAttribute('name');
  314. $oProperties = $this->GetUniqueElement($oClass, 'properties');
  315. // Class caracteristics
  316. //
  317. $aClassParams = array();
  318. $aClassParams['category'] = "'".$oClass->getAttribute('category')."'";
  319. $aClassParams['key_type'] = "'autoincrement'";
  320. $oNaming = $this->GetUniqueElement($oProperties, 'naming');
  321. $oNameAttributes = $this->GetUniqueElement($oNaming, 'attributes');
  322. $oAttributes = $oNameAttributes->getElementsByTagName('attribute');
  323. $aNameAttCodes = array();
  324. foreach($oAttributes as $oAttribute)
  325. {
  326. $aNameAttCodes[] = $oAttribute->getAttribute('name');
  327. }
  328. if (count($aNameAttCodes) > 1)
  329. {
  330. // New style...
  331. $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
  332. }
  333. elseif (count($aNameAttCodes) == 1)
  334. {
  335. // New style...
  336. $sNameAttCode = "'$aNameAttCodes[0]'";
  337. }
  338. else
  339. {
  340. $sNameAttCode = "''";
  341. }
  342. $aClassParams['name_attcode'] = $sNameAttCode;
  343. $oLifecycle = $this->GetOptionalElement($oClass, 'lifecycle');
  344. if ($oLifecycle)
  345. {
  346. $sStateAttCode = $oLifecycle->getAttribute('attribute');
  347. }
  348. else
  349. {
  350. $sStateAttCode = "";
  351. }
  352. $aClassParams['state_attcode'] = "'$sStateAttCode'";
  353. $oReconciliation = $this->GetUniqueElement($oProperties, 'reconciliation');
  354. $oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
  355. $aReconcAttCodes = array();
  356. foreach($oReconcAttributes as $oAttribute)
  357. {
  358. $aReconcAttCodes[] = $oAttribute->getAttribute('name');
  359. }
  360. $sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
  361. $aClassParams['reconc_keys'] = $sReconcKeys;
  362. $aClassParams['db_table'] = "'".$oClass->getAttribute('db_table')."'";
  363. $aClassParams['db_key_field'] = "'".$oClass->getAttribute('db_key_field')."'";
  364. $aClassParams['db_finalclass_field'] = "'".$oClass->getAttribute('db_final_class_field')."'";
  365. $oDisplayTemplate = $this->GetOptionalElement($oProperties, 'display_template');
  366. if ($oDisplayTemplate)
  367. {
  368. $sDisplayTemplate = $sModuleRelativeDir.'/'.$oDisplayTemplate->textContent;
  369. $aClassParams['display_template'] = "utils::GetAbsoluteUrlModulesRoot().'$sDisplayTemplate'";
  370. }
  371. $oIcon = $this->GetOptionalElement($oProperties, 'icon');
  372. if ($oIcon)
  373. {
  374. $sIcon = $sModuleRelativeDir.'/'.$oIcon->textContent;
  375. $aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
  376. }
  377. $oOrder = $this->GetOptionalElement($oProperties, 'order');
  378. if ($oOrder)
  379. {
  380. $oColumnsNode = $this->GetUniqueElement($oOrder, 'columns');
  381. $oColumns = $oColumnsNode->getElementsByTagName('column');
  382. $aSortColumns = array();
  383. foreach($oColumns as $oColumn)
  384. {
  385. $aSortColumns[] = "'".$oColumn->getAttribute('name')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false');
  386. }
  387. if (count($aSortColumns) > 0)
  388. {
  389. $aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")";
  390. }
  391. }
  392. // Finalize class params declaration
  393. //
  394. $aClassParamsPHP = array();
  395. foreach($aClassParams as $sKey => $sPHPValue)
  396. {
  397. $aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
  398. }
  399. $sClassParams = implode("\n", $aClassParamsPHP);
  400. // Comment on top of the class declaration
  401. //
  402. $oComment = $this->GetOptionalElement($oProperties, 'comment');
  403. if ($oComment)
  404. {
  405. $sCodeComment = $oComment->textContent;
  406. }
  407. else
  408. {
  409. $sCodeComment = '';
  410. }
  411. // Fields
  412. //
  413. $sAttributes = '';
  414. foreach($this->oFactory->ListFields($oClass) as $oField)
  415. {
  416. // $oField
  417. $sAttCode = $oField->getAttribute('name');
  418. $sAttType = 'Attribute'.$oField->getAttribute('type');
  419. $aDependencies = array();
  420. $oDependencies = $this->GetOptionalElement($oField, 'dependencies');
  421. if (!is_null($oDependencies))
  422. {
  423. $oDepNodes = $oDependencies->getElementsByTagName('attribute');
  424. foreach($oDepNodes as $oDepAttribute)
  425. {
  426. $aDependencies[] = "'".$oDepAttribute->getAttribute('name')."'";
  427. }
  428. }
  429. $sDependencies = 'array('.implode(', ', $aDependencies).')';
  430. $aParameters = array();
  431. if ($sAttType == 'AttributeLinkedSetIndirect')
  432. {
  433. $aParameters['linked_class'] = "'".$oField->getAttribute('linked_class')."'";
  434. $aParameters['ext_key_to_me'] = "'".$oField->getAttribute('ext_key_to_me')."'";
  435. $aParameters['ext_key_to_remote'] = "'".$oField->getAttribute('ext_key_to_remote')."'";
  436. // todo - utile ?
  437. $aParameters['allowed_values'] = 'null';
  438. $aParameters['count_min'] = $oField->getAttribute('count_min');
  439. $aParameters['count_max'] = $oField->getAttribute('count_max');
  440. $aParameters['depends_on'] = $sDependencies;
  441. }
  442. elseif ($sAttType == 'AttributeLinkedSet')
  443. {
  444. $aParameters['linked_class'] = "'".$oField->getAttribute('linked_class')."'";
  445. $aParameters['ext_key_to_me'] = "'".$oField->getAttribute('ext_key_to_me')."'";
  446. // todo - utile ?
  447. $aParameters['allowed_values'] = 'null';
  448. $aParameters['count_min'] = $oField->getAttribute('count_min');
  449. $aParameters['count_max'] = $oField->getAttribute('count_max');
  450. $aParameters['depends_on'] = $sDependencies;
  451. }
  452. elseif ($sAttType == 'AttributeExternalKey')
  453. {
  454. $aParameters['targetclass'] = "'".$oField->getAttribute('target_class')."'";
  455. // todo = v�rifier l'utilit�
  456. $aParameters['jointype'] = 'null';
  457. if (($sOql = $oField->getAttribute('filter')) != '')
  458. {
  459. $sEscapedOql = addslashes($sOql);
  460. $aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
  461. }
  462. else
  463. {
  464. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  465. }
  466. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  467. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  468. $aParameters['on_target_delete'] = $oField->getAttribute('on_target_delete');
  469. $aParameters['depends_on'] = $sDependencies;
  470. }
  471. elseif ($sAttType == 'AttributeHierarchicalKey')
  472. {
  473. if (($sOql = $oField->getAttribute('filter')) != '')
  474. {
  475. $sEscapedOql = addslashes($sOql);
  476. $aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
  477. }
  478. else
  479. {
  480. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  481. }
  482. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  483. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  484. $aParameters['on_target_delete'] = $oField->getAttribute('on_target_delete');
  485. $aParameters['depends_on'] = $sDependencies;
  486. }
  487. elseif ($sAttType == 'AttributeExternalField')
  488. {
  489. $aParameters['allowed_values'] = 'null';
  490. $aParameters['extkey_attcode'] = "'".$oField->getAttribute('extkey_attcode')."'";
  491. $aParameters['target_attcode'] = "'".$oField->getAttribute('target_attcode')."'";
  492. }
  493. elseif ($sAttType == 'AttributeURL')
  494. {
  495. $aParameters['target'] = "'".$oField->getAttribute('target')."'";
  496. $aParameters['allowed_values'] = 'null';
  497. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  498. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  499. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  500. $aParameters['depends_on'] = $sDependencies;
  501. }
  502. elseif ($sAttType == 'AttributeEnum')
  503. {
  504. $oValues = $this->GetUniqueElement($oField, 'values');
  505. $oValueNodes = $oValues->getElementsByTagName('value');
  506. $aValues = array();
  507. foreach($oValueNodes as $oValue)
  508. {
  509. // new style... $aValues[] = "'".addslashes($oValue->textContent)."'";
  510. $aValues[] = $oValue->textContent;
  511. }
  512. // new style... $sValues = 'array('.implode(', ', $aValues).')';
  513. $sValues = '"'.implode(',', $aValues).'"';
  514. $aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
  515. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  516. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  517. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  518. $aParameters['depends_on'] = $sDependencies;
  519. }
  520. elseif ($sAttType == 'AttributeBlob')
  521. {
  522. $aParameters['depends_on'] = $sDependencies;
  523. }
  524. else
  525. {
  526. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  527. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  528. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  529. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  530. $aParameters['depends_on'] = $sDependencies;
  531. if ($sValidationPattern = $oField->getAttribute('validation_pattern'))
  532. {
  533. $aParameters['validation_pattern'] = '"'.addslashes($sValidationPattern).'"';
  534. }
  535. }
  536. $aParams = array();
  537. foreach($aParameters as $sKey => $sValue)
  538. {
  539. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  540. }
  541. $sParams = implode(', ', $aParams);
  542. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  543. }
  544. // Lifecycle
  545. //
  546. $sLifecycle = '';
  547. if ($oLifecycle)
  548. {
  549. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  550. $sLifecycle .= "\t\t//\n";
  551. $oStimuli = $this->GetUniqueElement($oLifecycle, 'stimuli');
  552. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  553. {
  554. $sStimulus = $oStimulus->getAttribute('name');
  555. $sStimulusClass = $oStimulus->getAttribute('type');
  556. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  557. }
  558. $oStates = $this->GetUniqueElement($oLifecycle, 'states');
  559. foreach ($oStates->getElementsByTagName('state') as $oState)
  560. {
  561. $sState = $oState->getAttribute('name');
  562. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  563. $sLifecycle .= " \"".$sState."\",\n";
  564. $sLifecycle .= " array(\n";
  565. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  566. $sLifecycle .= " \"attribute_list\" => array(\n";
  567. $oFlags = $this->GetUniqueElement($oState, 'flags');
  568. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  569. {
  570. $sFlags = $this->FlagsToPHP($oAttributeNode);
  571. if (strlen($sFlags) > 0)
  572. {
  573. $sAttCode = $oAttributeNode->GetAttribute('name');
  574. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  575. }
  576. }
  577. $sLifecycle .= " ),\n";
  578. $sLifecycle .= " )\n";
  579. $sLifecycle .= " );\n";
  580. $oTransitions = $this->GetUniqueElement($oState, 'transitions');
  581. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  582. {
  583. $sStimulus = $oTransition->getAttribute('stimulus');
  584. $sTargetState = $oTransition->getAttribute('target');
  585. $oActions = $this->GetUniqueElement($oTransition, 'actions');
  586. $aVerbs = array();
  587. foreach ($oActions->getElementsByTagName('action') as $oAction)
  588. {
  589. $sVerb = $oAction->getAttribute('verb');
  590. $aVerbs[] = "'$sVerb'";
  591. }
  592. $sActions = implode(', ', $aVerbs);
  593. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  594. }
  595. }
  596. }
  597. // ZLists
  598. //
  599. $aListRef = array(
  600. 'details' => 'details',
  601. 'standard_search' => 'search',
  602. 'list' => 'list'
  603. );
  604. $oPresentation = $this->GetUniqueElement($oClass, 'presentation');
  605. $sZlists = '';
  606. foreach ($aListRef as $sListCode => $sListTag)
  607. {
  608. $oListNode = $this->GetUniqueElement($oPresentation, $sListTag);
  609. $aAttributes = $this->GetNodeAsArrayOfItems($oListNode);
  610. $sZAttributes = var_export($aAttributes, true);
  611. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  612. }
  613. // Methods
  614. $sMethods = "";
  615. $oMethods = $this->GetUniqueElement($oClass, 'methods');
  616. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  617. {
  618. $sMethodCode = $this->GetNodeText($oMethod);
  619. $oMethodComment = $this->GetOptionalElement($oMethod, 'comment');
  620. if ($oMethodComment)
  621. {
  622. $sMethods .= "\n\t".$oMethodComment->textContent."\n".$sMethodCode."\n";
  623. }
  624. else
  625. {
  626. $sMethods .= "\n\n".$sMethodCode."\n";
  627. }
  628. }
  629. // Let's make the whole class declaration
  630. //
  631. $sPHP = "\n\n$sCodeComment\n";
  632. if ($oClass->getAttribute('abstract') == 'true')
  633. {
  634. $sPHP .= 'abstract class '.$oClass->getAttribute('name');
  635. }
  636. else
  637. {
  638. $sPHP .= 'class '.$oClass->getAttribute('name');
  639. }
  640. $sPHP .= " extends ".$oClass->getAttribute('parent')."\n";
  641. $sPHP .=
  642. <<<EOF
  643. {
  644. public static function Init()
  645. {
  646. \$aParams = array
  647. (
  648. $sClassParams
  649. );
  650. MetaModel::Init_Params(\$aParams);
  651. MetaModel::Init_InheritAttributes();
  652. $sAttributes
  653. $sLifecycle
  654. $sZlists
  655. }
  656. $sMethods
  657. }
  658. EOF;
  659. file_put_contents($sResFile, $sPHP, FILE_APPEND);
  660. }
  661. }
  662. ?>