mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
704 lines
20 KiB
PHP
704 lines
20 KiB
PHP
<?php
|
||
// Copyright (C) 2011 Combodo SARL
|
||
//
|
||
// This program is free software; you can redistribute it and/or modify
|
||
// it under the terms of the GNU General Public License as published by
|
||
// the Free Software Foundation; version 3 of the License.
|
||
//
|
||
// This program is distributed in the hope that it will be useful,
|
||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
// GNU General Public License for more details.
|
||
//
|
||
// You should have received a copy of the GNU General Public License
|
||
// along with this program; if not, write to the Free Software
|
||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
||
class DOMFormatException extends Exception
|
||
{
|
||
}
|
||
|
||
class CompilerEchoPage
|
||
{
|
||
public function p($s)
|
||
{
|
||
echo "<p>\n$s</p>\n";
|
||
}
|
||
|
||
public function add($s)
|
||
{
|
||
//echo $s;
|
||
}
|
||
|
||
public function output()
|
||
{
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* Compiler class
|
||
*/
|
||
class MFCompiler
|
||
{
|
||
protected $oFactory;
|
||
protected $sSourceDir;
|
||
|
||
public function __construct($oModelFactory, $sSourceDir)
|
||
{
|
||
$this->oFactory = $oModelFactory;
|
||
$this->sSourceDir = $sSourceDir;
|
||
}
|
||
|
||
public function Compile($sTargetDir, $oP = null)
|
||
{
|
||
if (is_null($oP))
|
||
{
|
||
$oP = new CompilerEchoPage();
|
||
}
|
||
|
||
$aResultFiles = array();
|
||
|
||
$aModules = $this->oFactory->GetLoadedModules();
|
||
foreach($aModules as $foo => $oModule)
|
||
{
|
||
$sModuleName = $oModule->GetName();
|
||
$sModuleVersion = $oModule->GetVersion();
|
||
|
||
$sModuleRootDir = realpath($oModule->GetRootDir());
|
||
$sRelativeDir = substr($sModuleRootDir, strlen($this->sSourceDir));
|
||
|
||
// Push the other module files
|
||
$this->CopyDirectory($sModuleRootDir, $sTargetDir.$sRelativeDir);
|
||
|
||
$oClasses = $this->oFactory->ListClasses($sModuleName);
|
||
$iClassCount = $oClasses->length;
|
||
if ($iClassCount == 0)
|
||
{
|
||
$oP->p("Found module without classes declared: $sModuleName");
|
||
}
|
||
else
|
||
{
|
||
$sResultFile = $sTargetDir.$sRelativeDir.'/model.'.$sModuleName.'.php';
|
||
if (is_file($sResultFile))
|
||
{
|
||
$oP->p("Updating <a href=\"#$sModuleName\">$sResultFile</a> for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
|
||
}
|
||
else
|
||
{
|
||
$sResultDir = dirname($sResultFile);
|
||
if (!is_dir($sResultDir))
|
||
{
|
||
$oP->p("Creating directory $sResultDir");
|
||
mkdir($sResultDir, 0777, true);
|
||
}
|
||
$oP->p("Creating <a href=\"#$sModuleName\">$sResultFile</a> for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
|
||
}
|
||
|
||
// Compile the module into a single file
|
||
//
|
||
$sId = $sModuleName;
|
||
$aResultFiles[$sId] = $sResultFile;
|
||
$sCurrDate = date(DATE_ISO8601);
|
||
$sAuthor = 'Combodo compiler';
|
||
$sLicence = 'http://www.opensource.org/licenses/gpl-3.0.html LGPL';
|
||
$sFileHeader =
|
||
<<<EOF
|
||
<?php
|
||
//
|
||
// File generated by ... on the $sCurrDate
|
||
// Please do not edit manually
|
||
//
|
||
//
|
||
// Copyright (C) 2010 Combodo SARL
|
||
//
|
||
// ben on met quoi ici ?
|
||
// Signé: Romain
|
||
//
|
||
// This program is free software; you can redistribute it and/or modify
|
||
// it under the terms of the GNU General Public License as published by
|
||
// the Free Software Foundation; version 3 of the License.
|
||
//
|
||
// This program is distributed in the hope that it will be useful,
|
||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
// GNU General Public License for more details.
|
||
//
|
||
// You should have received a copy of the GNU General Public License
|
||
// along with this program; if not, write to the Free Software
|
||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
||
/**
|
||
* Persistent classes for $sModuleName (version $sModuleVersion)
|
||
*
|
||
* @author $sAuthor
|
||
* @license $sLicence
|
||
*/
|
||
|
||
EOF;
|
||
|
||
file_put_contents($sResultFile, $sFileHeader);
|
||
|
||
foreach($oClasses as $oClass)
|
||
{
|
||
$sClass = $oClass->getAttribute("name");
|
||
try
|
||
{
|
||
$this->CompileClass($oClass, $sResultFile, $oP);
|
||
}
|
||
catch (ssDOMFormatException $e)
|
||
{
|
||
$sClass = $oClass->getAttribute("name");
|
||
throw new Exception("Failed to process class '$sClass', from '$sModuleRootDir': ".$e->getMessage());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (count($aResultFiles))
|
||
{
|
||
$oP->add('<h2>Files</h2>');
|
||
foreach ($aResultFiles as $sModuleName => $sFile)
|
||
{
|
||
$oP->add('<h3>'.$sFile.'</h3>');
|
||
$oP->add('<a name="'.$sModuleName.'"/><div style="border:1;">');
|
||
$oP->add(highlight_file($sFile, true));
|
||
$oP->add('</div style="">');
|
||
}
|
||
}
|
||
|
||
$oP->output();
|
||
}
|
||
|
||
/**
|
||
* Helper to copy the module files to the exploitation environment
|
||
* Returns true if successfull
|
||
*/
|
||
protected function CopyDirectory($sSource, $sDest)
|
||
{
|
||
if (is_dir($sSource))
|
||
{
|
||
if (!is_dir($sDest))
|
||
{
|
||
mkdir($sDest);
|
||
}
|
||
$aFiles = scandir($sSource);
|
||
if(sizeof($aFiles) > 0 )
|
||
{
|
||
foreach($aFiles as $sFile)
|
||
{
|
||
if ($sFile == '.' || $sFile == '..' || $sFile == '.svn')
|
||
{
|
||
// Skip
|
||
continue;
|
||
}
|
||
|
||
if (is_dir($sSource.'/'.$sFile))
|
||
{
|
||
$this->CopyDirectory($sSource.'/'.$sFile, $sDest.'/'.$sFile);
|
||
}
|
||
else
|
||
{
|
||
copy($sSource.'/'.$sFile, $sDest.'/'.$sFile);
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
elseif (is_file($sSource))
|
||
{
|
||
return copy($sSource, $sDest);
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* Helper to browse the DOM -could be factorized in ModelFactory
|
||
* Returns the node directly under the given node, and that is supposed to be always present and unique
|
||
*/
|
||
protected function GetUniqueElement($oDOMNode, $sTagName, $bMustExist = true)
|
||
{
|
||
$oNode = null;
|
||
foreach($oDOMNode->childNodes as $oChildNode)
|
||
{
|
||
if ($oChildNode->nodeName == $sTagName)
|
||
{
|
||
$oNode = $oChildNode;
|
||
break;
|
||
}
|
||
}
|
||
if ($bMustExist && is_null($oNode))
|
||
{
|
||
throw new DOMFormatException('Missing unique tag: '.$sTagName);
|
||
}
|
||
return $oNode;
|
||
}
|
||
|
||
/**
|
||
* Helper to browse the DOM -could be factorized in ModelFactory
|
||
* Returns the node directly under the given node, or null is missing
|
||
*/
|
||
protected function GetOptionalElement($oDOMNode, $sTagName)
|
||
{
|
||
return $this->GetUniqueElement($oDOMNode, $sTagName, false);
|
||
}
|
||
|
||
|
||
/**
|
||
* Helper to browse the DOM -could be factorized in ModelFactory
|
||
* Returns the TEXT of the given node (possibly from several subnodes)
|
||
*/
|
||
protected function GetNodeText($oNode)
|
||
{
|
||
$sText = '';
|
||
foreach($oNode->childNodes as $oChildNode)
|
||
{
|
||
if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection
|
||
{
|
||
$sText .= $oChildNode->wholeText;
|
||
}
|
||
}
|
||
return $sText;
|
||
}
|
||
|
||
/**
|
||
* Helper to browse the DOM -could be factorized in ModelFactory
|
||
* Assumes the given 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
|
||
*/
|
||
protected function GetNodeAsArrayOfItems($oNode)
|
||
{
|
||
$oItems = $this->GetOptionalElement($oNode, 'items');
|
||
if ($oItems)
|
||
{
|
||
$res = array();
|
||
foreach($oItems->childNodes as $oItem)
|
||
{
|
||
// When an attribute is msising
|
||
if ($oItem->hasAttribute('key'))
|
||
{
|
||
$key = $oItem->getAttribute('key');
|
||
$res[$key] = $this->GetNodeAsArrayOfItems($oItem);
|
||
}
|
||
else
|
||
{
|
||
$res[] = $this->GetNodeAsArrayOfItems($oItem);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
$res = $this->GetNodeText($oNode);
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* Helper to format the flags for an attribute, in a given state
|
||
* @param object $oAttNode DOM node containing the information to build the flags
|
||
* Returns string PHP flags, based on the OPT_ATT_ constants, or empty (meaning 0, can be omitted)
|
||
*/
|
||
protected function FlagsToPHP($oAttNode)
|
||
{
|
||
static $aNodeAttributeToFlag = array(
|
||
'mandatory' => 'OPT_ATT_MANDATORY',
|
||
'read_only' => 'OPT_ATT_READONLY',
|
||
'must_prompt' => 'OPT_ATT_MUSTPROMPT',
|
||
'must_change' => 'OPT_ATT_MUSTCHANGE',
|
||
'hidden' => 'OPT_ATT_HIDDEN',
|
||
);
|
||
|
||
$aFlags = array();
|
||
foreach ($aNodeAttributeToFlag as $sNodeAttribute => $sFlag)
|
||
{
|
||
$bFlag = ($oAttNode->GetAttribute($sNodeAttribute) == '1');
|
||
if ($bFlag)
|
||
{
|
||
$aFlags[] = $sFlag;
|
||
}
|
||
}
|
||
$sRes = implode(' | ', $aFlags);
|
||
return $sRes;
|
||
}
|
||
|
||
|
||
protected function CompileClass($oClass, $sResFile, $oP)
|
||
{
|
||
$sClass = $oClass->getAttribute('name');
|
||
$oProperties = $this->GetUniqueElement($oClass, 'properties');
|
||
|
||
// Class caracteristics
|
||
//
|
||
$aClassParams = array();
|
||
$aClassParams['category'] = "'".$oClass->getAttribute('category')."'";
|
||
$aClassParams['key_type'] = "'autoincrement'";
|
||
|
||
$oNaming = $this->GetUniqueElement($oProperties, 'naming');
|
||
$oNameAttributes = $this->GetUniqueElement($oNaming, 'attributes');
|
||
$oAttributes = $oNameAttributes->getElementsByTagName('attribute');
|
||
$aNameAttCodes = array();
|
||
foreach($oAttributes as $oAttribute)
|
||
{
|
||
$aNameAttCodes[] = $oAttribute->getAttribute('name');
|
||
}
|
||
if (count($aNameAttCodes) > 1)
|
||
{
|
||
// New style...
|
||
$sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
|
||
}
|
||
elseif (count($aNameAttCodes) == 1)
|
||
{
|
||
// New style...
|
||
$sNameAttCode = "'$aNameAttCodes[0]'";
|
||
}
|
||
else
|
||
{
|
||
$sNameAttCode = "''";
|
||
}
|
||
$aClassParams['name_attcode'] = $sNameAttCode;
|
||
|
||
$oLifecycle = $this->GetOptionalElement($oClass, 'lifecycle');
|
||
if ($oLifecycle)
|
||
{
|
||
$sStateAttCode = $oLifecycle->getAttribute('attribute');
|
||
}
|
||
else
|
||
{
|
||
$sStateAttCode = "";
|
||
}
|
||
$aClassParams['state_attcode'] = "'$sStateAttCode'";
|
||
|
||
$oReconciliation = $this->GetUniqueElement($oProperties, 'reconciliation');
|
||
$oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
|
||
$aReconcAttCodes = array();
|
||
foreach($oReconcAttributes as $oAttribute)
|
||
{
|
||
$aReconcAttCodes[] = $oAttribute->getAttribute('name');
|
||
}
|
||
$sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
|
||
$aClassParams['reconc_keys'] = $sReconcKeys;
|
||
|
||
$aClassParams['db_table'] = "'".$oClass->getAttribute('db_table')."'";
|
||
$aClassParams['db_key_field'] = "'".$oClass->getAttribute('db_key_field')."'";
|
||
$aClassParams['db_finalclass_field'] = "'".$oClass->getAttribute('db_final_class_field')."'";
|
||
|
||
$oDisplayTemplate = $this->GetOptionalElement($oProperties, 'display_template');
|
||
if ($oDisplayTemplate)
|
||
{
|
||
$sDisplayTemplate = $oDisplayTemplate->textContent;
|
||
$aClassParams['display_template'] = "'$sDisplayTemplate'";
|
||
}
|
||
|
||
$oIcon = $this->GetOptionalElement($oProperties, 'icon');
|
||
if ($oIcon)
|
||
{
|
||
$sIcon = $oIcon->textContent;
|
||
$aClassParams['icon'] = "'$sIcon'";
|
||
}
|
||
|
||
// Finalize class params declaration
|
||
//
|
||
$aClassParamsPHP = array();
|
||
foreach($aClassParams as $sKey => $sPHPValue)
|
||
{
|
||
$aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
|
||
}
|
||
$sClassParams = implode("\n", $aClassParamsPHP);
|
||
|
||
// Comment on top of the class declaration
|
||
//
|
||
$oComment = $this->GetOptionalElement($oProperties, 'comment');
|
||
if ($oComment)
|
||
{
|
||
$sCodeComment = $oComment->textContent;
|
||
}
|
||
else
|
||
{
|
||
$sCodeComment = '';
|
||
}
|
||
|
||
// Fields
|
||
//
|
||
$sAttributes = '';
|
||
foreach($this->oFactory->ListFields($oClass) as $oField)
|
||
{
|
||
// $oField
|
||
$sAttCode = $oField->getAttribute('name');
|
||
$sAttType = 'Attribute'.$oField->getAttribute('type');
|
||
|
||
$aDependencies = array();
|
||
$oDependencies = $this->GetOptionalElement($oField, 'dependencies');
|
||
if (!is_null($oDependencies))
|
||
{
|
||
$oDepNodes = $oDependencies->getElementsByTagName('attribute');
|
||
foreach($oDepNodes as $oDepAttribute)
|
||
{
|
||
$aDependencies[] = "'".$oDepAttribute->getAttribute('name')."'";
|
||
}
|
||
}
|
||
$sDependencies = 'array('.implode(', ', $aDependencies).')';
|
||
|
||
$aParameters = array();
|
||
|
||
if ($sAttType == 'AttributeLinkedSetIndirect')
|
||
{
|
||
$aParameters['linked_class'] = "'".$oField->getAttribute('linked_class')."'";
|
||
$aParameters['ext_key_to_me'] = "'".$oField->getAttribute('ext_key_to_me')."'";
|
||
$aParameters['ext_key_to_remote'] = "'".$oField->getAttribute('ext_key_to_remote')."'";
|
||
// todo - utile ?
|
||
$aParameters['allowed_values'] = 'null';
|
||
$aParameters['count_min'] = $oField->getAttribute('count_min');
|
||
$aParameters['count_max'] = $oField->getAttribute('count_max');
|
||
$aParameters['depends_on'] = $sDependencies;
|
||
}
|
||
elseif ($sAttType == 'AttributeLinkedSet')
|
||
{
|
||
$aParameters['linked_class'] = "'".$oField->getAttribute('linked_class')."'";
|
||
$aParameters['ext_key_to_me'] = "'".$oField->getAttribute('ext_key_to_me')."'";
|
||
// todo - utile ?
|
||
$aParameters['allowed_values'] = 'null';
|
||
$aParameters['count_min'] = $oField->getAttribute('count_min');
|
||
$aParameters['count_max'] = $oField->getAttribute('count_max');
|
||
$aParameters['depends_on'] = $sDependencies;
|
||
}
|
||
elseif ($sAttType == 'AttributeExternalKey')
|
||
{
|
||
$aParameters['targetclass'] = "'".$oField->getAttribute('target_class')."'";
|
||
// todo = v<>rifier l'utilit<EFBFBD>
|
||
$aParameters['jointype'] = 'null';
|
||
if (($sOql = $oField->getAttribute('filter')) != '')
|
||
{
|
||
$sEscapedOql = addslashes($sOql);
|
||
$aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
|
||
}
|
||
else
|
||
{
|
||
$aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
|
||
}
|
||
$aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
|
||
$aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
|
||
$aParameters['on_target_delete'] = $oField->getAttribute('on_target_delete');
|
||
$aParameters['depends_on'] = $sDependencies;
|
||
}
|
||
elseif ($sAttType == 'AttributeHierarchicalKey')
|
||
{
|
||
if (($sOql = $oField->getAttribute('filter')) != '')
|
||
{
|
||
$sEscapedOql = addslashes($sOql);
|
||
$aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
|
||
}
|
||
else
|
||
{
|
||
$aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
|
||
}
|
||
$aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
|
||
$aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
|
||
$aParameters['on_target_delete'] = $oField->getAttribute('on_target_delete');
|
||
$aParameters['depends_on'] = $sDependencies;
|
||
}
|
||
elseif ($sAttType == 'AttributeExternalField')
|
||
{
|
||
$aParameters['allowed_values'] = 'null';
|
||
$aParameters['extkey_attcode'] = "'".$oField->getAttribute('extkey_attcode')."'";
|
||
$aParameters['target_attcode'] = "'".$oField->getAttribute('target_attcode')."'";
|
||
}
|
||
elseif ($sAttType == 'AttributeURL')
|
||
{
|
||
$aParameters['target'] = "'".$oField->getAttribute('target')."'";
|
||
$aParameters['allowed_values'] = 'null';
|
||
$aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
|
||
$aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
|
||
$aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
|
||
$aParameters['depends_on'] = $sDependencies;
|
||
}
|
||
elseif ($sAttType == 'AttributeEnum')
|
||
{
|
||
$oValues = $this->GetUniqueElement($oField, 'values');
|
||
$oValueNodes = $oValues->getElementsByTagName('value');
|
||
$aValues = array();
|
||
foreach($oValueNodes as $oValue)
|
||
{
|
||
// new style... $aValues[] = "'".addslashes($oValue->textContent)."'";
|
||
$aValues[] = $oValue->textContent;
|
||
}
|
||
// new style... $sValues = 'array('.implode(', ', $aValues).')';
|
||
$sValues = '"'.implode(',', $aValues).'"';
|
||
$aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
|
||
$aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
|
||
$aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
|
||
$aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
|
||
$aParameters['depends_on'] = $sDependencies;
|
||
}
|
||
elseif ($sAttType == 'AttributeBlob')
|
||
{
|
||
$aParameters['depends_on'] = $sDependencies;
|
||
}
|
||
else
|
||
{
|
||
$aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
|
||
$aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
|
||
$aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
|
||
$aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
|
||
$aParameters['depends_on'] = $sDependencies;
|
||
|
||
if ($sValidationPattern = $oField->getAttribute('validation_pattern'))
|
||
{
|
||
$aParameters['validation_pattern'] = '"'.addslashes($sValidationPattern).'"';
|
||
}
|
||
}
|
||
|
||
$aParams = array();
|
||
foreach($aParameters as $sKey => $sValue)
|
||
{
|
||
$aParams[] = '"'.$sKey.'"=>'.$sValue;
|
||
}
|
||
$sParams = implode(', ', $aParams);
|
||
$sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
|
||
}
|
||
|
||
// Lifecycle
|
||
//
|
||
$sLifecycle = '';
|
||
if ($oLifecycle)
|
||
{
|
||
$sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
|
||
$sLifecycle .= "\t\t//\n";
|
||
|
||
$oStimuli = $this->GetUniqueElement($oLifecycle, 'stimuli');
|
||
foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
|
||
{
|
||
$sStimulus = $oStimulus->getAttribute('name');
|
||
$sStimulusClass = $oStimulus->getAttribute('type');
|
||
|
||
$sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
|
||
}
|
||
|
||
$oStates = $this->GetUniqueElement($oLifecycle, 'states');
|
||
foreach ($oStates->getElementsByTagName('state') as $oState)
|
||
{
|
||
$sState = $oState->getAttribute('name');
|
||
|
||
$sLifecycle .= " MetaModel::Init_DefineState(\n";
|
||
$sLifecycle .= " \"".$sState."\",\n";
|
||
$sLifecycle .= " array(\n";
|
||
$sLifecycle .= " \"attribute_inherit\" => '',\n";
|
||
$sLifecycle .= " \"attribute_list\" => array(\n";
|
||
|
||
$oFlags = $this->GetUniqueElement($oState, 'flags');
|
||
foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
|
||
{
|
||
$sFlags = $this->FlagsToPHP($oAttributeNode);
|
||
if (strlen($sFlags) > 0)
|
||
{
|
||
$sAttCode = $oAttributeNode->GetAttribute('name');
|
||
$sLifecycle .= " '$sAttCode' => $sFlags,\n";
|
||
}
|
||
}
|
||
|
||
$sLifecycle .= " ),\n";
|
||
$sLifecycle .= " )\n";
|
||
$sLifecycle .= " );\n";
|
||
|
||
$oTransitions = $this->GetUniqueElement($oState, 'transitions');
|
||
foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
|
||
{
|
||
$sStimulus = $oTransition->getAttribute('stimulus');
|
||
$sTargetState = $oTransition->getAttribute('target');
|
||
|
||
$oActions = $this->GetUniqueElement($oTransition, 'actions');
|
||
$aVerbs = array();
|
||
foreach ($oActions->getElementsByTagName('action') as $oAction)
|
||
{
|
||
$sVerb = $oAction->getAttribute('verb');
|
||
$aVerbs[] = "'$sVerb'";
|
||
}
|
||
$sActions = implode(', ', $aVerbs);
|
||
$sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
|
||
}
|
||
}
|
||
}
|
||
|
||
// ZLists
|
||
//
|
||
$aListRef = array(
|
||
'details' => 'details',
|
||
'standard_search' => 'search',
|
||
'list' => 'list'
|
||
);
|
||
|
||
$oPresentation = $this->GetUniqueElement($oClass, 'presentation');
|
||
$sZlists = '';
|
||
foreach ($aListRef as $sListCode => $sListTag)
|
||
{
|
||
$oListNode = $this->GetUniqueElement($oPresentation, $sListTag);
|
||
$aAttributes = $this->GetNodeAsArrayOfItems($oListNode);
|
||
|
||
$sZAttributes = var_export($aAttributes, true);
|
||
$sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
|
||
}
|
||
|
||
// Methods
|
||
$sMethods = "";
|
||
$oMethods = $this->GetUniqueElement($oClass, 'methods');
|
||
foreach($oMethods->getElementsByTagName('method') as $oMethod)
|
||
{
|
||
$sMethodCode = $this->GetNodeText($oMethod);
|
||
$oMethodComment = $this->GetOptionalElement($oMethod, 'comment');
|
||
if ($oMethodComment)
|
||
{
|
||
$sMethods .= "\n\t".$oMethodComment->textContent."\n".$sMethodCode."\n";
|
||
}
|
||
else
|
||
{
|
||
$sMethods .= "\n\n".$sMethodCode."\n";
|
||
}
|
||
}
|
||
|
||
// Let's make the whole class declaration
|
||
//
|
||
$sPHP = "\n\n$sCodeComment\n";
|
||
if ($oClass->getAttribute('abstract') == 'true')
|
||
{
|
||
$sPHP .= 'abstract class '.$oClass->getAttribute('name');
|
||
}
|
||
else
|
||
{
|
||
$sPHP .= 'class '.$oClass->getAttribute('name');
|
||
}
|
||
$sPHP .= " extends ".$oClass->getAttribute('parent')."\n";
|
||
$sPHP .=
|
||
<<<EOF
|
||
{
|
||
public static function Init()
|
||
{
|
||
\$aParams = array
|
||
(
|
||
$sClassParams
|
||
);
|
||
MetaModel::Init_Params(\$aParams);
|
||
MetaModel::Init_InheritAttributes();
|
||
$sAttributes
|
||
$sLifecycle
|
||
$sZlists
|
||
}
|
||
|
||
$sMethods
|
||
}
|
||
EOF;
|
||
file_put_contents($sResFile, $sPHP, FILE_APPEND);
|
||
}
|
||
}
|
||
|
||
?>
|