Преглед на файлове

(FAF for module developers): utilities to easily know the name/version/dir/URL of the current module and write module independent code !!

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@4844 a333f486-631f-4898-b8df-5754b55c2be0
dflaven преди 7 години
родител
ревизия
94fda8cd41
променени са 2 файла, в които са добавени 130 реда и са изтрити 1 реда
  1. 98 0
      application/utils.inc.php
  2. 32 1
      setup/compiler.class.inc.php

+ 98 - 0
application/utils.inc.php

@@ -1489,4 +1489,102 @@ class utils
 				'}';
 				'}';
 		return $sUUID;
 		return $sUUID;
 	}
 	}
+
+	/**
+	 * Returns the name of the module containing the file where the call to this function is made
+	 * or an empty string if no such module is found (or not called within a module file)
+	 * @param number $iCallDepth The depth of the module in the callstack. Zero when called directly from within the module
+	 * @return string
+	 */
+	static public function GetCurrentModuleName($iCallDepth = 0)
+	{
+		$sCurrentModuleName = '';
+		$aCallStack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+		$sCallerFile = realpath($aCallStack[$iCallDepth]['file']);
+		
+		foreach(GetModulesInfo() as $sModuleName => $aInfo)
+		{
+			if ($aInfo['root_dir'] !== '')
+			{
+				$sRootDir = realpath(APPROOT.$aInfo['root_dir']);
+				
+				if(substr($sCallerFile, 0, strlen($sRootDir)) === $sRootDir)
+				{
+					$sCurrentModuleName = $sModuleName;
+					break;
+				}
+			}
+		}
+		return $sCurrentModuleName;
+	}
+	
+	/**
+	 * Returns the relative (to APPROOT) path of the root directory of the module containing the file where the call to this function is made
+	 * or an empty string if no such module is found (or not called within a module file)
+	 * @param number $iCallDepth The depth of the module in the callstack. Zero when called directly from within the module
+	 * @return string
+	 */
+	static public function GetCurrentModuleDir($iCallDepth)
+	{
+		$sCurrentModuleDir = '';
+		$aCallStack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+		$sCallerFile = realpath($aCallStack[$iCallDepth]['file']);
+	
+		foreach(GetModulesInfo() as $sModuleName => $aInfo)
+		{
+			if ($aInfo['root_dir'] !== '')
+			{
+				$sRootDir = realpath(APPROOT.$aInfo['root_dir']);
+	
+				if(substr($sCallerFile, 0, strlen($sRootDir)) === $sRootDir)
+				{
+					$sCurrentModuleDir = basename($sRootDir);
+					break;
+				}
+			}
+		}
+		return $sCurrentModuleDir;
+	}
+	
+	/**
+	 * Returns the base URL for all files in the current module from which this method is called
+	 * or an empty string if no such module is found (or not called within a module file)
+	 * @return string
+	 */
+	static public function GetCurrentModuleUrl()
+	{
+		$sDir = static::GetCurrentModuleDir(1);
+		if ( $sDir !== '')
+		{
+			return static::GetAbsoluteUrlModulesRoot().'/'.$sDir;
+		}
+		return '';
+	}
+	
+	/**
+	 * Get the value of a given setting for the current module
+	 * @param string $sProperty The name of the property to retrieve
+	 * @param mixed $defaultvalue
+	 * @return mixed
+	 */
+	static public function GetCurrentModuleSetting($sProperty, $defaultvalue = null)
+	{
+		$sModuleName = static::GetCurrentModuleName(1);
+		return MetaModel::GetModuleSetting($sModuleName, $sProperty, $defaultvalue);
+	}
+	
+	/**
+	 * Get the compiled version of a given module, as it was seen by the compiler
+	 * @param string $sModuleName
+	 * @return string|NULL
+	 */
+	static public function GetCompiledModuleVersion($sModuleName)
+	{
+		$aModulesInfo = GetModulesInfo();
+		if (array_key_exists($sModuleName, $aModulesInfo))
+		{
+			return $aModulesInfo[$sModuleName]['version'];
+		}
+		return null;
+	}
 }
 }

+ 32 - 1
setup/compiler.class.inc.php

@@ -88,6 +88,14 @@ class MFCompiler
 	}
 	}
 	
 	
 
 
+	/**
+	 * Compile the data model into PHP files and data structures
+	 * @param string $sTargetDir The target directory where to put the resulting files
+	 * @param Page $oP For some output...
+	 * @param bool $bUseSymbolicLinks
+	 * @throws Exception
+	 * @return void
+	 */
 	public function Compile($sTargetDir, $oP = null, $bUseSymbolicLinks = false)
 	public function Compile($sTargetDir, $oP = null, $bUseSymbolicLinks = false)
 	{
 	{
 		$sFinalTargetDir = $sTargetDir;
 		$sFinalTargetDir = $sTargetDir;
@@ -142,9 +150,19 @@ class MFCompiler
 	}
 	}
 	
 	
 
 
+	/**
+	 * Perform the actual "Compilation" of all modules
+	 * @param string $sTempTargetDir
+	 * @param string $sFinalTargetDir
+	 * @param Page $oP
+	 * @param bool $bUseSymbolicLinks
+	 * @throws Exception
+	 * @return string void
+	 */
 	protected function DoCompile($sTempTargetDir, $sFinalTargetDir, $oP = null, $bUseSymbolicLinks = false)
 	protected function DoCompile($sTempTargetDir, $sFinalTargetDir, $oP = null, $bUseSymbolicLinks = false)
 	{
 	{
 		$aAllClasses = array(); // flat list of classes
 		$aAllClasses = array(); // flat list of classes
+		$aModulesInfo = array(); // Hash array of module_name => array('version' => string, 'root_dir' => string)
 
 
 		// Determine the target modules for the MENUS
 		// Determine the target modules for the MENUS
 		//
 		//
@@ -204,18 +222,30 @@ class MFCompiler
 			$sModuleVersion = $oModule->GetVersion();
 			$sModuleVersion = $oModule->GetVersion();
 		
 		
 			$sModuleRootDir = $oModule->GetRootDir();
 			$sModuleRootDir = $oModule->GetRootDir();
+			$iStart = strlen(realpath(APPROOT));
 			if ($sModuleRootDir != '')
 			if ($sModuleRootDir != '')
 			{
 			{
 				$sModuleRootDir = realpath($sModuleRootDir);
 				$sModuleRootDir = realpath($sModuleRootDir);
 				$sRelativeDir = basename($sModuleRootDir);
 				$sRelativeDir = basename($sModuleRootDir);
+				if ($bUseSymbolicLinks)
+				{
+					$sRealRelativeDir = substr(realpath($sModuleRootDir), $iStart);
+				}
+				else
+				{
+					$sRealRelativeDir = substr(realpath($sFinalTargetDir.'/'.$sRelativeDir), $iStart);
+				}
+
 				// Push the other module files
 				// Push the other module files
 				SetupUtils::copydir($sModuleRootDir, $sTempTargetDir.'/'.$sRelativeDir, $bUseSymbolicLinks);
 				SetupUtils::copydir($sModuleRootDir, $sTempTargetDir.'/'.$sRelativeDir, $bUseSymbolicLinks);
 			}
 			}
 			else
 			else
 			{
 			{
 				$sRelativeDir = '';
 				$sRelativeDir = '';
+				$sRealRelativeDir = '';
 			}
 			}
-
+			$aModulesInfo[$sModuleName] = array('root_dir' => $sRealRelativeDir, 'version' => $sModuleVersion);
+				
 			$sCompiledCode = '';
 			$sCompiledCode = '';
 
 
 			$oConstants = $this->oFactory->ListConstants($sModuleName);
 			$oConstants = $this->oFactory->ListConstants($sModuleName);
@@ -550,6 +580,7 @@ EOF
 		$sPHPFileContent .= "\nMetaModel::IncludeModule('".basename($sFinalTargetDir)."/core/main.php');\n";
 		$sPHPFileContent .= "\nMetaModel::IncludeModule('".basename($sFinalTargetDir)."/core/main.php');\n";
 		$sPHPFileContent .= implode("\n", $aDataModelFiles);
 		$sPHPFileContent .= implode("\n", $aDataModelFiles);
 		$sPHPFileContent .= implode("\n", $aWebservicesFiles);
 		$sPHPFileContent .= implode("\n", $aWebservicesFiles);
+		$sPHPFileContent .= "\nfunction GetModulesInfo()\n{\nreturn ".var_export($aModulesInfo, true).";\n}\n";
 		file_put_contents($sPHPFile, $sPHPFileContent);
 		file_put_contents($sPHPFile, $sPHPFileContent);
 		
 		
 	} // DoCompile()
 	} // DoCompile()