From 2ba3ab305704fa810f5b927e723f12ef2e151df1 Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Fri, 27 Mar 2015 17:16:40 +0000 Subject: [PATCH] Enhancement: PHP snippets inside the XML. SVN:trunk[3520] --- setup/compiler.class.inc.php | 110 +++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/setup/compiler.class.inc.php b/setup/compiler.class.inc.php index d3c203c26d..e20551e82c 100644 --- a/setup/compiler.class.inc.php +++ b/setup/compiler.class.inc.php @@ -33,6 +33,7 @@ class MFCompiler protected $aRootClasses; protected $aLog; protected $sMainPHPCode; // Code that goes into core/main.php + protected $aSnippets; public function __construct($oModelFactory) { @@ -43,6 +44,7 @@ class MFCompiler $this->sMainPHPCode .= "/**\n"; $this->sMainPHPCode .= " * This file was automatically generated by the compiler on ".date('Y-m-d H:i:s')." -- DO NOT EDIT\n"; $this->sMainPHPCode .= " */\n"; + $this->aSnippets = array(); } protected function Log($sText) @@ -151,6 +153,8 @@ class MFCompiler $this->Log("Root class (with child classes): ".$oClass->getAttribute('id')); $this->aRootClasses[$oClass->getAttribute('id')] = $oClass; } + + $this->LoadSnippets(); // Compile, module by module // @@ -179,6 +183,18 @@ class MFCompiler $sCompiledCode .= $this->CompileConstant($oConstant)."\n"; } } + + if (array_key_exists($sModuleName, $this->aSnippets)) + { + foreach( $this->aSnippets[$sModuleName]['before'] as $aSnippet) + { + $sCompiledCode .= "\n"; + $sCompiledCode .= "/**\n"; + $sCompiledCode .= " * Snippet: {$aSnippet['snippet_id']}\n"; + $sCompiledCode .= " */\n"; + $sCompiledCode .= $aSnippet['content']."\n"; + } + } $oClasses = $this->oFactory->ListClasses($sModuleName); $iClassCount = $oClasses->length; @@ -294,6 +310,7 @@ EOF; <<CompileUserRights($oUserRightsNode); } + if (array_key_exists($sModuleName, $this->aSnippets)) + { + foreach( $this->aSnippets[$sModuleName]['after'] as $aSnippet) + { + $sCompiledCode .= "\n"; + $sCompiledCode .= "/**\n"; + $sCompiledCode .= " * Snippet: {$aSnippet['snippet_id']}\n"; + $sCompiledCode .= " */\n"; + $sCompiledCode .= $aSnippet['content']."\n"; + } + } + // Create (overwrite if existing) the compiled file // if (strlen($sCompiledCode) > 0) @@ -382,6 +411,18 @@ EOF; // $oBrandingNode = $this->oFactory->GetNodes('branding')->item(0); $this->CompileBranding($oBrandingNode, $sTempTargetDir, $sFinalTargetDir); + + if (array_key_exists('_core_', $this->aSnippets)) + { + foreach( $this->aSnippets['_core_']['before'] as $aSnippet) + { + $this->sMainPHPCode .= "\n"; + $this->sMainPHPCode .= "/**\n"; + $this->sMainPHPCode .= " * Snippet: {$aSnippet['snippet_id']}\n"; + $this->sMainPHPCode .= " */\n"; + $this->sMainPHPCode .= $aSnippet['content']."\n"; + } + } // Compile the portals $oPortalsNode = $this->oFactory->GetNodes('/itop_design/portals')->item(0); @@ -391,6 +432,18 @@ EOF; $oParametersNode = $this->oFactory->GetNodes('/itop_design/module_parameters')->item(0); $this->CompileParameters($oParametersNode, $sTempTargetDir, $sFinalTargetDir); + if (array_key_exists('_core_', $this->aSnippets)) + { + foreach( $this->aSnippets['_core_']['after'] as $aSnippet) + { + $this->sMainPHPCode .= "\n"; + $this->sMainPHPCode .= "/**\n"; + $this->sMainPHPCode .= " * Snippet: {$aSnippet['snippet_id']}\n"; + $this->sMainPHPCode .= " */\n"; + $this->sMainPHPCode .= $aSnippet['content']."\n"; + } + } + // Write core/main.php SetupUtils::builddir($sTempTargetDir.'/core'); $sPHPFile = $sTempTargetDir.'/core/main.php'; @@ -2117,6 +2170,63 @@ EOF; $this->sMainPHPCode .= "}\n"; } } + + protected function LoadSnippets() + { + $oSnippets = $this->oFactory->GetNodes('/itop_design/snippets/snippet'); + foreach($oSnippets as $oSnippet) + { + $sSnippetId = $oSnippet->getAttribute('id'); + $sPlacement = $oSnippet->GetChildText('placement', null); + if ($sPlacement == 'core') + { + $sModuleId = '_core_'; + } + else if ($sPlacement == 'module') + { + $sModuleId = $oSnippet->GetChildText('module', null); + if ($sModuleId == null) + { + throw new DOMFormatException("Invalid definition for snippet id='$sSnippetId' with placement=module. Missing '' tag."); + } + } + else if ($sPlacement === 'null') + { + throw new DOMFormatException("Invalid definition for snippet id='$sSnippetId'. Missing tag."); + } + else + { + throw new DOMFormatException("Invalid definition for snippet id='$sSnippetId'. Incorrect value '$sPlacement' for tag. The allowed values are either 'core' or 'module'."); + } + if (!array_key_exists($sModuleId, $this->aSnippets)) + { + $this->aSnippets[$sModuleId] = array('before' => array(), 'after' => array()); + } + + $fOrder = (float) $oSnippet->GetChildText('rank', 0); + $sContent = $oSnippet->GetChildText('content', ''); + if ($fOrder < 0) + { + $this->aSnippets[$sModuleId]['before'][] = array( + 'rank' => $fOrder, + 'content' => $sContent, + 'snippet_id' => $sSnippetId, + ); + } + else + { + $this->aSnippets[$sModuleId]['after'][] = array( + 'rank' => $fOrder, + 'content' => $sContent, + 'snippet_id' => $sSnippetId, + ); + } + } + foreach($this->aSnippets as $sModuleId => $void) + { + uasort($this->aSnippets[$sModuleId]['before'], array(get_class($this), 'SortOnRank')); + } + } } ?>