Просмотр исходного кода

Enhancement: PHP snippets inside the XML.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3520 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 10 лет назад
Родитель
Сommit
0a04b2f216
1 измененных файлов с 110 добавлено и 0 удалено
  1. 110 0
      setup/compiler.class.inc.php

+ 110 - 0
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;
 <<<EOF
 	}
 } // class $sMenuCreationClass
+
 EOF;
 			}
 
@@ -304,6 +321,18 @@ EOF;
 				$sCompiledCode .= $this->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 '<module>' tag.");
+				}
+			}
+			else if ($sPlacement === 'null')
+			{
+				throw new DOMFormatException("Invalid definition for snippet id='$sSnippetId'. Missing <placement> tag.");
+			}
+			else
+			{
+				throw new DOMFormatException("Invalid definition for snippet id='$sSnippetId'. Incorrect value '$sPlacement' for <placement> 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'));
+		}
+	}
 }
 
 ?>