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

SVN:trunk[4844]
This commit is contained in:
Denis Flaven
2017-07-21 13:55:22 +00:00
parent be4c1e2c1c
commit 61d3a4e48f
2 changed files with 130 additions and 1 deletions

View File

@@ -1489,4 +1489,102 @@ class utils
'}';
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;
}
}

View File

@@ -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)
{
$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)
{
$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
//
@@ -204,18 +222,30 @@ class MFCompiler
$sModuleVersion = $oModule->GetVersion();
$sModuleRootDir = $oModule->GetRootDir();
$iStart = strlen(realpath(APPROOT));
if ($sModuleRootDir != '')
{
$sModuleRootDir = realpath($sModuleRootDir);
$sRelativeDir = basename($sModuleRootDir);
if ($bUseSymbolicLinks)
{
$sRealRelativeDir = substr(realpath($sModuleRootDir), $iStart);
}
else
{
$sRealRelativeDir = substr(realpath($sFinalTargetDir.'/'.$sRelativeDir), $iStart);
}
// Push the other module files
SetupUtils::copydir($sModuleRootDir, $sTempTargetDir.'/'.$sRelativeDir, $bUseSymbolicLinks);
}
else
{
$sRelativeDir = '';
$sRealRelativeDir = '';
}
$aModulesInfo[$sModuleName] = array('root_dir' => $sRealRelativeDir, 'version' => $sModuleVersion);
$sCompiledCode = '';
$oConstants = $this->oFactory->ListConstants($sModuleName);
@@ -550,6 +580,7 @@ EOF
$sPHPFileContent .= "\nMetaModel::IncludeModule('".basename($sFinalTargetDir)."/core/main.php');\n";
$sPHPFileContent .= implode("\n", $aDataModelFiles);
$sPHPFileContent .= implode("\n", $aWebservicesFiles);
$sPHPFileContent .= "\nfunction GetModulesInfo()\n{\nreturn ".var_export($aModulesInfo, true).";\n}\n";
file_put_contents($sPHPFile, $sPHPFileContent);
} // DoCompile()