]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/template/menu/class_MenuTemplateEngine.php
Continued:
[core.git] / framework / main / classes / template / menu / class_MenuTemplateEngine.php
index 316f1518b8b4f41f011ff397950581d1ce4613b7..777412745014f0538be020d2209812c03b37bbf9 100644 (file)
@@ -1,15 +1,20 @@
 <?php
 // Own namespace
-namespace CoreFramework\Template\Engine;
+namespace Org\Mxchange\CoreFramework\Template\Engine;
 
 // Import framework stuff
-use CoreFramework\Factory\ObjectFactory;
-use CoreFramework\Filesystem\InvalidDirectoryException;
-use CoreFramework\Parser\Xml\XmlParser;
-use CoreFramework\Registry\Registry;
-use CoreFramework\Template\CompileableTemplate;
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
+use Org\Mxchange\CoreFramework\Menu\RenderableMenu;
+use Org\Mxchange\CoreFramework\Parser\Parseable;
+use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
+use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
+use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
+use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
 
 // Import SPL stuff
+use \SplFileInfo;
 use \UnexpectedValueException;
 
 /**
@@ -17,7 +22,7 @@ use \UnexpectedValueException;
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -38,14 +43,14 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
        /**
         * Main nodes in the XML tree ('menu' is ignored)
         */
-       private $mainNodes = array(
+       private $mainNodes = [
                'block-list',
-       );
+       ];
 
        /**
         * Sub nodes in the XML tree
         */
-       private $subNodes = array(
+       private $subNodes = [
                'entry-list',
                'entry',
                'entry-id',
@@ -68,12 +73,12 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                'anchor-text',
                'anchor-title',
                'anchor-href',
-       );
+       ];
 
        /**
         * Variables for a menu entry
         */
-       private $menuEntryVariables = array(
+       private $menuEntryVariables = [
                // List entry
                'entry_id',
                // Anchor
@@ -81,12 +86,12 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                'anchor-text',
                'anchor-title',
                'anchor-href',
-       );
+       ];
 
        /**
         * Variables for a menu block
         */
-       private $menuBlockVariables = array(
+       private $menuBlockVariables = [
                // Title
                'title_id',
                'title_class',
@@ -96,32 +101,37 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                'footer_id',
                'footer_class',
                'footer_text',
-       );
+       ];
 
        /**
         * Rendered menu entries
         */
-       private $menuEntries = array();
+       private $menuEntries = [];
 
        /**
         * Rendered menu blocks
         */
-       private $menuBlocks = array();
+       private $menuBlocks = [];
 
        /**
-        * Menu instance
+        * Current main node
         */
-       private $menuInstance = NULL;
+       private $curr = [];
 
        /**
-        * Current main node
+        * Content from dependency
         */
-       private $curr = array();
+       private $dependencyContent = [];
 
        /**
-        * Content from dependency
+        * Instance of a menu
+        */
+       private $menuInstance = NULL;
+
+       /**
+        * Template engine instance
         */
-       private $dependencyContent = array();
+       private $templateInstance = NULL;
 
        /**
         * Protected constructor
@@ -148,10 +158,10 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                $templateInstance = new MenuTemplateEngine();
 
                // Get the application instance from registry
-               $applicationInstance = Registry::getRegistry()->getInstance('app');
+               $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
 
                // Determine base path
-               $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
+               $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
 
                // Is the base path valid?
                if (empty($templateBasePath)) {
@@ -172,13 +182,13 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                $templateInstance->setTemplateBasePath($templateBasePath);
 
                // Set template extensions
-               $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
-               $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
+               $templateInstance->setRawTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('raw_template_extension'));
+               $templateInstance->setCodeTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('menu_template_extension'));
 
                // Absolute output path for compiled templates
                $templateInstance->setCompileOutputPath(sprintf('%s%s/',
                        $templateBasePath,
-                       $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
+                       FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path')
                ));
 
                // Set the menu instance
@@ -194,6 +204,44 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                return $templateInstance;
        }
 
+       /**
+        * Setter for the menu instance
+        *
+        * @param       $menuInstance   A RenderableMenu instance
+        * @return      void
+        */
+       protected final function setMenuInstance (RenderableMenu $menuInstance) {
+               $this->menuInstance = $menuInstance;
+       }
+
+       /**
+        * Getter for the menu instance
+        *
+        * @return      $menuInstance   A RenderableMenu instance
+        */
+       private final function getMenuInstance () {
+               return $this->menuInstance;
+       }
+
+       /**
+        * Setter for template engine instances
+        *
+        * @param       $templateInstance       An instance of a CompileableTemplate class
+        * @return      void
+        */
+       protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
+               $this->templateInstance = $templateInstance;
+       }
+
+       /**
+        * Getter for template engine instances
+        *
+        * @return      $templateInstance       An instance of a CompileableTemplate class
+        */
+       public final function getTemplateInstance () {
+               return $this->templateInstance;
+       }
+
        /**
         * Load a specified menu template into the engine
         *
@@ -203,7 +251,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
         */
        public function loadMenuTemplate ($template) {
                // Set template type
-               $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
+               $this->setTemplateType(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('menu_template_type'));
 
                // Load the special template
                $this->loadTemplate($template);
@@ -266,16 +314,16 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
                if (in_array($element, $this->getMainNodes())) {
                        // Okay, main node found!
-                       $methodName = 'start' . self::convertToClassName($element);
+                       $methodName = 'start' . StringUtils::convertToClassName($element);
 
                        // Set it
                        $this->setCurrMainNode($element);
                } elseif (in_array($element, $this->getSubNodes())) {
                        // Sub node found
-                       $methodName = 'start' . self::convertToClassName($element);
+                       $methodName = 'start' . StringUtils::convertToClassName($element);
                } elseif ($element != 'menu') {
                        // Invalid node name found
-                       throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
+                       throw new InvalidXmlNodeException(array($this, $element, $attributes), Parseable::EXCEPTION_XML_NODE_UNKNOWN);
                }
 
                // Call method
@@ -299,15 +347,15 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
                if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
                        // Did not match!
-                       throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
+                       throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), Parseable::EXCEPTION_XML_NODE_MISMATCH);
                } // END - if
 
                // Construct method name
-               $methodName = 'finish' . self::convertToClassName($nodeName);
+               $methodName = 'finish' . StringUtils::convertToClassName($nodeName);
 
                // Call the corresponding method
                //* DEBUG: */ echo "call: ".$methodName."<br />\n";
-               call_user_func_array(array($this, $methodName), array());
+               call_user_func_array(array($this, $methodName), []);
        }
 
        /**
@@ -366,7 +414,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
         */
        private function initMenu ($templateDependency = '') {
                // Get web template engine
-               $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array($this->getApplicationInstance())));
+               $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array(GenericRegistry::getRegistry()->getInstance('application'))));
 
                // Handle the dependency template
                $this->handleTemplateDependency('menu', $templateDependency);
@@ -779,11 +827,8 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
         * @return      void
         */
        private function renderMenuEntry () {
-               // Prepare template engine
-               $templateInstance = $this->prepareTemplateInstance();
-
                // Load menu entry template
-               $templateInstance->loadCodeTemplate('menu_entry');
+               $this->getTemplateInstance()->loadCodeTemplate('menu_entry');
 
                // Copy all variables over to it
                foreach ($this->menuEntryVariables as $variableName) {
@@ -797,15 +842,15 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                        } // END - if
 
                        // ... into the instance
-                       $templateInstance->assignVariable($variableName, $variableValue);
+                       $this->getTemplateInstance()->assignVariable($variableName, $variableValue);
                } // END - foreach
 
                // Compile template + variables
-               $templateInstance->compileTemplate();
-               $templateInstance->compileVariables();
+               $this->getTemplateInstance()->compileTemplate();
+               $this->getTemplateInstance()->compileVariables();
 
                // Remember it here
-               $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
+               $this->menuEntries[$this->readVariable('entry_id')] = $this->getTemplateInstance()->getRawTemplateData();
        }
 
        /**
@@ -818,11 +863,8 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                // Init block content
                $blockContent = implode('', $this->menuEntries);
 
-               // Prepare template engine
-               $templateInstance = $this->prepareTemplateInstance();
-
                // Load menu entry template
-               $templateInstance->loadCodeTemplate('menu_block');
+               $this->getTemplateInstance()->loadCodeTemplate('menu_block');
 
                // Copy all variables over to it
                foreach ($this->menuBlockVariables as $variableName) {
@@ -830,21 +872,21 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                        $variableValue = $this->readVariable($variableName);
 
                        // ... into the instance
-                       $templateInstance->assignVariable($variableName, $variableValue);
+                       $this->getTemplateInstance()->assignVariable($variableName, $variableValue);
                } // END - foreach
 
                // Assign block content
-               $templateInstance->assignVariable('block_content', $blockContent);
+               $this->getTemplateInstance()->assignVariable('block_content', $blockContent);
 
                // Compile template + variables
-               $templateInstance->compileTemplate();
-               $templateInstance->compileVariables();
+               $this->getTemplateInstance()->compileTemplate();
+               $this->getTemplateInstance()->compileVariables();
 
                // Remember it here
-               array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
+               array_push($this->menuBlocks, $this->getTemplateInstance()->getRawTemplateData());
 
                // Reset rendered menu entries array
-               $this->menuEntries = array();
+               $this->menuEntries = [];
        }
 
        /**
@@ -857,24 +899,24 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                $menuContent = implode('', $this->menuBlocks);
 
                // Clean variable
-               $this->menuBlocks = array();
+               $this->menuBlocks = [];
 
                // And return it
                return $menuContent;
        }
 
        /**
-        * Getter for menu cache file (FQFN)
+        * Getter for menu cache file instance
         *
-        * @return      $fqfn   Full-qualified file name of the menu cache
+        * @return      $fileInstance   Full-qualified file name of the menu cache
         */
-       public function getMenuCacheFqfn () {
+       public function getMenuCacheFile () {
                // Get the application instance from registry
-               $applicationInstance = Registry::getRegistry()->getInstance('app');
+               $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
 
-               // Get the FQFN ready
-               $fqfn = sprintf('%s%smenus/_cache/%s.%s',
-                       $this->getConfigInstance()->getConfigEntry('application_base_path'),
+               // Get the file instance ready
+               $fileInstance = new SplFileInfo(sprintf('%s%smenus/_cache/%s.%s',
+                       FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path'),
                        $applicationInstance->getAppShortName(),
                        md5(
                                $this->getMenuInstance()->getMenuName() . ':' .
@@ -882,10 +924,10 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                                $this->getMenuInstance()->__toString()
                        ),
                        $this->getMenuInstance()->getMenuType()
-               );
+               ));
 
                // Return it
-               return $fqfn;
+               return $fileInstance;
        }
 
 }