X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Ftemplate%2Fmenu%2Fclass_MenuTemplateEngine.php;h=943b6a85c95f199e60254671337904145038001b;hb=ef7a7e55c59c9e887e6bb09c8c02b8126309f716;hp=6663f8adc10423319eff05841d35810a7ccedca2;hpb=6e0f0121a45058e64816ef6d1c621ba4218c1095;p=core.git diff --git a/framework/main/classes/template/menu/class_MenuTemplateEngine.php b/framework/main/classes/template/menu/class_MenuTemplateEngine.php index 6663f8ad..943b6a85 100644 --- a/framework/main/classes/template/menu/class_MenuTemplateEngine.php +++ b/framework/main/classes/template/menu/class_MenuTemplateEngine.php @@ -1,19 +1,29 @@ * @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 * @@ -31,17 +41,20 @@ use CoreFramework\Template\CompileableTemplate; * along with this program. If not, see . */ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate { + // Load traits + use StackableTrait; + /** * 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', @@ -64,12 +77,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 @@ -77,12 +90,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', @@ -92,32 +105,32 @@ 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 $dependencyContent = array(); + private $menuInstance = NULL; /** * Protected constructor @@ -134,10 +147,8 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * * @param $menuInstance A RenderableMenu instance * @return $templateInstance An instance of TemplateEngine - * @throws BasePathIsEmptyException If the provided $templateBasePath is empty - * @throws InvalidBasePathStringException If $templateBasePath is no string - * @throws BasePathIsNoDirectoryException If $templateBasePath is no - * directory or not found + * @throws UnexpectedValueException If the found $templateBasePath is empty or not a string + * @throws InvalidDirectoryException If $templateBasePath is no directory or not found * @throws BasePathReadProtectedException If $templateBasePath is * read-protected */ @@ -146,21 +157,21 @@ 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->getRequestInstance()->getRequestElement('app') . '/'; + $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/'; // Is the base path valid? if (empty($templateBasePath)) { // Base path is empty - throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); } elseif (!is_string($templateBasePath)) { // Is not a string - throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING); + throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING); } elseif (!is_dir($templateBasePath)) { // Is not a path - throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME); + throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME); } elseif (!is_readable($templateBasePath)) { // Is not readable throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH); @@ -170,11 +181,14 @@ 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($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')); + $templateInstance->setCompileOutputPath(sprintf('%s%s/', + $templateBasePath, + FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path') + )); // Set the menu instance $templateInstance->setMenuInstance($menuInstance); @@ -189,6 +203,25 @@ 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; + } + /** * Load a specified menu template into the engine * @@ -196,9 +229,9 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * located in 'menu' by default * @return void */ - public function loadMenuTemplate ($template) { + public function loadMenuTemplate (string $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); @@ -219,8 +252,8 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * @param $element Element name to set as current main node * @return $currMainNode Current main node */ - private final function setCurrMainNode ($element) { - $this->curr['main_node'] = (string) $element; + private final function setCurrMainNode (string $element) { + $this->curr['main_node'] = $element; } /** @@ -250,7 +283,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * @return void * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found */ - public function startElement ($resource, $element, array $attributes) { + public function startElement ($resource, string $element, array $attributes) { // Initial method name which will never be called... $methodName = 'initMenu'; @@ -261,16 +294,16 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla //* DEBUG: */ echo "START: >".$element."<
\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 @@ -286,23 +319,20 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * @return void * @throws XmlNodeMismatchException If current main node mismatches the closing one */ - public function finishElement ($resource, $nodeName) { - // Make all lower-case - $nodeName = strtolower($nodeName); - + public function finishElement ($resource, string $nodeName) { // Does this match with current main node? //* DEBUG: */ echo "END: >".$nodeName."<
\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); - } // END - if + throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), Parseable::EXCEPTION_XML_NODE_MISMATCH); + } // Construct method name - $methodName = 'finish' . self::convertToClassName($nodeName); + $methodName = 'finish' . StringUtils::convertToClassName($nodeName); // Call the corresponding method //* DEBUG: */ echo "call: ".$methodName."
\n"; - call_user_func_array(array($this, $methodName), array()); + call_user_func_array(array($this, $methodName), []); } /** @@ -313,7 +343,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * @return void * @todo Find something useful with this! */ - public function characterHandler ($resource, $characters) { + public function characterHandler ($resource, string $characters) { // Trim all spaces away $characters = trim($characters); @@ -321,7 +351,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla if (empty($characters)) { // Then skip it silently return; - } // END - if + } // Assign the found characters to variable and use the last entry from // stack as the name @@ -335,7 +365,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * @param $templateDependency A template to load to satisfy dependencies * @return void */ - private function handleTemplateDependency ($node, $templateDependency) { + private function handleTemplateDependency (string $node, string $templateDependency) { // Is the template dependency set? if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) { // Get a temporay menu template instance @@ -349,7 +379,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla // Save the parsed raw content in our dependency array $this->dependencyContent[$node] = $templateInstance->getRawTemplateData(); - } // END - if + } } /** @@ -359,9 +389,9 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * @return void * @todo Add cache creation here */ - private function initMenu ($templateDependency = '') { + private function initMenu (string $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); @@ -774,11 +804,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) { @@ -789,18 +816,18 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla if ($variableName == 'anchor-href') { // Expand variable with URL then $variableValue = '{?base_url?}/' . $variableValue; - } // END - if + } // ... into the instance - $templateInstance->assignVariable($variableName, $variableValue); - } // END - foreach + $this->getTemplateInstance()->assignVariable($variableName, $variableValue); + } // 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(); } /** @@ -813,11 +840,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) { @@ -825,21 +849,21 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla $variableValue = $this->readVariable($variableName); // ... into the instance - $templateInstance->assignVariable($variableName, $variableValue); - } // END - foreach + $this->getTemplateInstance()->assignVariable($variableName, $variableValue); + } // 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 = []; } /** @@ -852,33 +876,35 @@ 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 () { - // Get the FQFN ready - $fqfn = sprintf('%s%s%s/%s.%s', - $this->getConfigInstance()->getConfigEntry('base_path'), - $this->getGenericBasePath(), - 'menus/_cache', + public function getMenuCacheFile () { + // Get the application instance from registry + $applicationInstance = GenericRegistry::getRegistry()->getInstance('application'); + + // 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() . ':' . $this->__toString() . ':' . $this->getMenuInstance()->__toString() ), $this->getMenuInstance()->getMenuType() - ); + )); // Return it - return $fqfn; + return $fileInstance; } }