Renamed Registry -> GenericRegistry to make it clear that this registry does
[core.git] / framework / main / classes / template / class_BaseTemplateEngine.php
index cb0712578ad38c5e73e86b80dbf27cd034d45340..01ff9a92ede901e7b3925676badebae3a48def26 100644 (file)
@@ -1,13 +1,19 @@
 <?php
 // Own namespace
-namespace CoreFramework\Template\Engine;
+namespace Org\Mxchange\CoreFramework\Template\Engine;
 
 // Import framework stuff
-use CoreFramework\Factory\ObjectFactory;
-use CoreFramework\Generic\EmptyVariableException;
-use CoreFramework\Manager\ManageableApplication;
-use CoreFramework\Object\BaseFrameworkSystem;
-use CoreFramework\Response\Responseable;
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
+use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
+use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Response\Responseable;
+
+// Import SPL stuff
+use \InvalidArgumentException;
+use \SplFileInfo;
 
 /**
  * A generic template engine
@@ -31,7 +37,7 @@ use CoreFramework\Response\Responseable;
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseTemplateEngine extends BaseFrameworkSystem {
+abstract class BaseTemplateEngine extends BaseFrameworkSystem {
        /**
         * The local path name where all templates and sub folders for special
         * templates are stored. We will internally determine the language plus
@@ -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);
@@ -1106,7 +1112,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @param       $variableName   The variable we are looking for
         * @param       $value                  The value we want to store in the variable
         * @return      void
-        * @throws      EmptyVariableException  If the variable name is left empty
+        * @throws      InvalidArgumentException        If the variable name is left empty
         */
        public final function assignVariable ($variableName, $value) {
                // Replace all dashes to underscores to match variables with configuration entries
@@ -1115,14 +1121,14 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // Empty variable found?
                if (empty($variableName)) {
                        // Throw an exception
-                       throw new EmptyVariableException(array($this, 'variableName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+                       throw new InvalidArgumentException('Parameter "variableName" is empty');
                } // END - if
 
                // First search for the variable if it was already added
                $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 "<?"
                                $evalLeft = substr($eval, 0, strpos($eval, $this->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: <pre>%s</pre>', $this->markupCode($eval, TRUE)), TRUE);
+                               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Failed eval() code: <pre>%s</pre>', $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=<pre>' . htmlentities($rawCode) . '</pre>');
                preg_match_all($this->regExpVarValue, $rawCode, $varMatches);
 
                // Compile all variables
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:<pre>' . print_r($varMatches, TRUE) . '</pre>');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:<pre>' . print_r($varMatches, true) . '</pre>');
                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;
        }