X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Ftemplate%2Fclass_BaseTemplateEngine.php;h=5ca3c6ad2b7f38a3b2b90c7f5f94483ce79d120e;hp=cb0712578ad38c5e73e86b80dbf27cd034d45340;hb=b9bfbe86c031c9d83c3670602906df191a33ba2a;hpb=4e95c4e90f08f67f43591eaaa0c006f923d8bacf diff --git a/framework/main/classes/template/class_BaseTemplateEngine.php b/framework/main/classes/template/class_BaseTemplateEngine.php index cb071257..5ca3c6ad 100644 --- a/framework/main/classes/template/class_BaseTemplateEngine.php +++ b/framework/main/classes/template/class_BaseTemplateEngine.php @@ -3,12 +3,18 @@ namespace CoreFramework\Template\Engine; // Import framework stuff +use CoreFramework\Bootstrap\FrameworkBootstrap; +use CoreFramework\EntryPoint\ApplicationEntryPoint; use CoreFramework\Factory\ObjectFactory; +use CoreFramework\Filesystem\FileNotFoundException; use CoreFramework\Generic\EmptyVariableException; use CoreFramework\Manager\ManageableApplication; use CoreFramework\Object\BaseFrameworkSystem; use CoreFramework\Response\Responseable; +// Import SPL stuff +use \SplFileInfo; + /** * A generic template engine * @@ -75,9 +81,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem { private $compiledData = ''; /** - * The last loaded template's FQFN for debugging the engine + * The last loaded template's file instance (SplFileInfo) */ - private $lastTemplate = ''; + private $lastTemplate = NULL; /** * The variable stack for the templates @@ -149,12 +155,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * Language support is enabled by default */ - private $languageSupport = TRUE; + private $languageSupport = true; /** * XML compacting is disabled by default */ - private $xmlCompacting = FALSE; + private $xmlCompacting = false; // Exception codes for the template engine const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED = 0x110; @@ -184,14 +190,14 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * * @param $variableName The variable we are looking for * @param $variableGroup Optional variable group to look in - * @return $index FALSE means not found, >=0 means found on a specific index + * @return $index false means not found, >=0 means found on a specific index */ private function getVariableIndex ($variableName, $variableGroup = NULL) { // Replace all dashes to underscores to match variables with configuration entries $variableName = trim(self::convertDashesToUnderscores($variableName)); // First everything is not found - $found = FALSE; + $found = false; // If the stack is NULL, use the current group if (is_null($variableGroup)) { @@ -279,7 +285,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $found = $this->getVariableIndex($variableName, $variableGroup); // Is the variable found? - if ($found !== FALSE) { + if ($found !== false) { // Read it $content = $this->getVariableValue($variableGroup, $found); } // END - if @@ -330,13 +336,13 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * @param $add Whether add this group * @return void */ - public function setVariableGroup ($groupName, $add = TRUE) { + public function setVariableGroup ($groupName, $add = true) { // Set group name //* DEBIG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.': currGroup=' . $groupName); $this->currGroup = $groupName; // Skip group 'general' - if (($groupName != 'general') && ($add === TRUE)) { + if (($groupName != 'general') && ($add === true)) { $this->variableGroups[$groupName] = 'OK'; } // END - if } @@ -394,7 +400,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $index = $this->getVariableIndex($variableName); // Is the variable set? - if ($index === FALSE) { + if ($index === false) { // Unset variables cannot be modified throw new NoVariableException(array($this, $variableName, $value), self::EXCEPTION_VARIABLE_IS_MISSING); } // END - if @@ -433,7 +439,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $index = $this->getVariableIndex($variableName); // Is the variable set? - if ($index === FALSE) { + if ($index === false) { // Is the stack there? if (!isset($this->varStack[$variableGroup])) { // Then initialize it here @@ -491,17 +497,17 @@ class BaseTemplateEngine extends BaseFrameworkSystem { } /** - * Setter for the last loaded template's FQFN + * Setter for the last loaded template's file instance * * @param $template The last loaded template * @return void */ - private final function setLastTemplate ($template) { - $this->lastTemplate = (string) $template; + private final function setLastTemplate (SplFileInfo $fileInstance) { + $this->lastTemplate = $fileInstance; } /** - * Getter for the last loaded template's FQFN + * Getter for the last loaded template's file instance * * @return $template The last loaded template */ @@ -669,12 +675,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * Private loader for all template types * - * @param $template The template we shall load + * @param $templateName The template we shall load * @param $extOther An other extension to use * @return void * @throws FileNotFoundException If the template was not found */ - protected function loadTemplate ($template, $extOther = '') { + protected function loadTemplate ($templateName, $extOther = '') { // Get extension for the template if empty if (empty($extOther)) { // None provided, so get the raw one @@ -689,19 +695,19 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * now entirely done by php_intl. These old thing with language-based * template paths comes from an older time. */ - $fqfn = sprintf('%s%s%s%s/%s%s', - $this->getConfigInstance()->getConfigEntry('application_base_path'), + $fileInstance = new SplFileInfo(sprintf('%s%s%s%s%s%s', $this->getTemplateBasePath(), $this->getGenericBasePath(), $this->getTemplateType(), - (string) $template, + DIRECTORY_SEPARATOR, + (string) $templateName, $ext - ); + )); // First try this try { // Load the raw template data - $this->loadRawTemplateData($fqfn); + $this->loadRawTemplateData($fileInstance); } catch (FileNotFoundException $e) { // If we shall load a code-template we need to switch the file extension if (($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('html_template_type')) && (empty($extOther))) { @@ -709,10 +715,10 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $ext = $this->getCodeTemplateExtension(); // Try it again... - $this->loadTemplate($template, $ext); + $this->loadTemplate($templateName, $ext); } else { // Throw it again - throw new FileNotFoundException($fqfn, self::EXCEPTION_FILE_NOT_FOUND); + throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_NOT_FOUND); } } @@ -721,21 +727,21 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * A private loader for raw template names * - * @param $fqfn The full-qualified file name for a template + * @param $fileInstance An instance of a SplFileInfo class * @return void */ - private function loadRawTemplateData ($fqfn) { + private function loadRawTemplateData (SplFileInfo $fileInstance) { // Some debug code to look on the file which is being loaded - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: FQFN=' . $fqfn); + //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: fileInstance=' . $fileInstance); // Load the raw template - $rawTemplateData = $this->getFileIoInstance()->loadFileContents($fqfn); + $rawTemplateData = $this->getFileIoInstance()->loadFileContents($fileInstance); // Store the template's contents into this class $this->setRawTemplateData($rawTemplateData); - // Remember the template's FQFN - $this->setLastTemplate($fqfn); + // Remember the template's file instance + $this->setLastTemplate($fileInstance); } /** @@ -778,7 +784,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { preg_match_all('/\$(\w+)(\[(\w+)\])?/', $rawData, $variableMatches); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawData(' . strlen($rawData) . ')=' . $rawData . ',variableMatches=' . print_r($variableMatches, TRUE)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawData(' . strlen($rawData) . ')=' . $rawData . ',variableMatches=' . print_r($variableMatches, true)); // Did we find some variables? if ((is_array($variableMatches)) && (count($variableMatches) == 4) && (count($variableMatches[0]) > 0)) { @@ -830,7 +836,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:template=' . $template); // Template not found, but maybe variable assigned? - if ($this->getVariableIndex($template) !== FALSE) { + if ($this->getVariableIndex($template) !== false) { // Use that content here $this->loadedRawData[$template] = $this->readVariable($template); @@ -908,7 +914,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $foundIndex = array_search($template, $templateMatches[1]); // Lookup the matching template replacement - if (($foundIndex !== FALSE) && (isset($templateMatches[0][$foundIndex]))) { + if (($foundIndex !== false) && (isset($templateMatches[0][$foundIndex]))) { // Get the current raw template $rawData = $this->getRawTemplateData(); @@ -984,7 +990,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $this->assignVariable($var, $varMatches[3][$key]); } elseif (!empty($varMatches[2][$key])) { // @TODO Non-string found so we need some deeper analysis... - ApplicationEntryPoint::app_exit('Deeper analysis not yet implemented!'); + ApplicationEntryPoint::exitApplication('Deeper analysis not yet implemented!'); } } // END - foreach } @@ -1017,10 +1023,10 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $foundIndex = array_search($template, $templateMatches[1]); // Lookup the matching variable data - if (($foundIndex !== FALSE) && (isset($templateMatches[3][$foundIndex]))) { + if (($foundIndex !== false) && (isset($templateMatches[3][$foundIndex]))) { // Split it up with another reg. exp. into variable=value pairs preg_match_all($this->regExpVarValue, $templateMatches[3][$foundIndex], $varMatches); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varMatches=' . print_r($varMatches, TRUE)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varMatches=' . print_r($varMatches, true)); // Assign all variables $this->assignAllVariables($varMatches); @@ -1122,7 +1128,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $index = $this->getVariableIndex($variableName); // Was it found? - if ($index === FALSE) { + if ($index === false) { // Add it to the stack //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:ADD: ' . $variableName . '[' . gettype($value) . ']=' . $value); $this->addVariable($variableName, $value); @@ -1145,7 +1151,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $index = $this->getVariableIndex($variableName, $variableGroup); // Was it found? - if ($index !== FALSE) { + if ($index !== false) { // Remove this variable //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:UNSET: variableGroup=' . $variableGroup . ',variableName=' . $variableName . ',index=' . $index); $this->unsetVariableStackOffset($index, $variableGroup); @@ -1232,7 +1238,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public function loadCodeTemplate ($template) { // Set template type - $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type')); + $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type')); // Load the special template $this->loadTemplate($template); @@ -1271,7 +1277,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Iterate through all general variables foreach ($this->getVarStack('general') as $index => $currVariable) { // Compile the value - $value = $this->compileRawCode($this->readVariable($currVariable['name']), TRUE); + $value = $this->compileRawCode($this->readVariable($currVariable['name']), true); // Debug message //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: name=' . $currVariable['name'] . ',value=' . $value); @@ -1328,7 +1334,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { ); // This loop does remove the backslashes (\) in PHP parameters - while (strpos($eval, $this->codeBegin) !== FALSE) { + while (strpos($eval, $this->codeBegin) !== false) { // Get left part before "codeBegin)); @@ -1362,7 +1368,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Goes something wrong? if ((!isset($result)) || (empty($result))) { // Output eval command - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Failed eval() code:
%s
', $this->markupCode($eval, TRUE)), TRUE); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Failed eval() code:
%s
', $this->markupCode($eval, true)), true); // Output backtrace here $this->debugBackTrace(); @@ -1391,12 +1397,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public function compileTemplate () { // Get code type to make things shorter - $codeType = $this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type'); + $codeType = $this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type'); // We will only work with template type "code" from configuration if (substr($this->getTemplateType(), 0, strlen($codeType)) != $codeType) { // Abort here - throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED); + throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED); } // END - if // Get the raw data. @@ -1412,7 +1418,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { preg_match_all($this->regExpCodeTags, $rawData, $templateMatches); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:templateMatches=' . print_r($templateMatches , TRUE)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:templateMatches=' . print_r($templateMatches , true)); // Analyze the matches array if ((is_array($templateMatches)) && (count($templateMatches) == 4) && (count($templateMatches[0]) > 0)) { @@ -1476,13 +1482,13 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * @param $setMatchAsCode Sets $match if readVariable() returns empty result * @return $rawCode Compile code with inserted variable value */ - public function compileRawCode ($rawCode, $setMatchAsCode=FALSE) { + public function compileRawCode ($rawCode, $setMatchAsCode=false) { // Find the variables //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawCode=
' . htmlentities($rawCode) . '
'); preg_match_all($this->regExpVarValue, $rawCode, $varMatches); // Compile all variables - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:
' . print_r($varMatches, TRUE) . '
'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:
' . print_r($varMatches, true) . '
'); foreach ($varMatches[0] as $match) { // Add variable tags around it $varCode = '{?' . $match . '?}'; @@ -1491,12 +1497,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varCode=' . $varCode); // Is the variable found in code? (safes some calls) - if (strpos($rawCode, $varCode) !== FALSE) { + if (strpos($rawCode, $varCode) !== false) { // Debug message //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',rawCode[' . gettype($rawCode) . ']=' . $rawCode); // Use $match as new value or $value from read variable? - if ($setMatchAsCode === TRUE) { + if ($setMatchAsCode === true) { // Insert match $rawCode = str_replace($varCode, $match, $rawCode); } else { @@ -1575,7 +1581,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * @param $languageSupport New language support setting * @return void */ - public final function enableLanguageSupport ($languageSupport = TRUE) { + public final function enableLanguageSupport ($languageSupport = true) { $this->languageSupport = (bool) $languageSupport; } @@ -1594,7 +1600,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * @param $xmlCompacting New XML compacting setting * @return void */ - public final function enableXmlCompacting ($xmlCompacting = TRUE) { + public final function enableXmlCompacting ($xmlCompacting = true) { $this->xmlCompacting = (bool) $xmlCompacting; }