From: Roland Häder Date: Wed, 2 Sep 2009 21:56:00 +0000 (+0000) Subject: MenuTemplateEngine heavily extended, but still in non-working state X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c27d9eeb693e0b8ee34d39ac28d771faee450b25;p=core.git MenuTemplateEngine heavily extended, but still in non-working state --- diff --git a/.gitattributes b/.gitattributes index 4f46c621..0185c481 100644 --- a/.gitattributes +++ b/.gitattributes @@ -552,6 +552,8 @@ templates/.htaccess -text templates/_compiled/.htaccess -text templates/images/.htaccess -text templates/images/_cache/.htaccess -text +templates/menus/.htaccess -text +templates/menus/_cache/.htaccess -text tests/ConfigTest.php -text tests/RegistryTest.php -text tests/RequestTest.php -text diff --git a/inc/classes/exceptions/controller/class_DefaultControllerException.php b/inc/classes/exceptions/controller/class_DefaultControllerException.php index 89a1a506..551c0c5d 100644 --- a/inc/classes/exceptions/controller/class_DefaultControllerException.php +++ b/inc/classes/exceptions/controller/class_DefaultControllerException.php @@ -38,7 +38,7 @@ class DefaultControllerException extends FrameworkException { ); // Set extra data - $this->setExtraData($resolverInstance->getClassName().':'.$this->getLine()); + $this->setExtraData($resolverInstance->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); diff --git a/inc/classes/exceptions/controller/class_InvalidControllerException.php b/inc/classes/exceptions/controller/class_InvalidControllerException.php index a8e1d96a..b637ae7a 100644 --- a/inc/classes/exceptions/controller/class_InvalidControllerException.php +++ b/inc/classes/exceptions/controller/class_InvalidControllerException.php @@ -39,7 +39,7 @@ class InvalidControllerException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); diff --git a/inc/classes/exceptions/crypto/class_EncryptInvalidLengthException.php b/inc/classes/exceptions/crypto/class_EncryptInvalidLengthException.php index 63c6c2e3..a6af83da 100644 --- a/inc/classes/exceptions/crypto/class_EncryptInvalidLengthException.php +++ b/inc/classes/exceptions/crypto/class_EncryptInvalidLengthException.php @@ -41,7 +41,7 @@ class EncryptInvalidLengthException extends FrameworkException { ); // Set extra data - $this->setExtraData($filterInstance->__toString().':'.$length); + $this->setExtraData($filterInstance->__toString() . ':' . $length); // Call parent exception constructor parent::__construct($message, $code); diff --git a/inc/classes/exceptions/main/class_InvalidCommandException.php b/inc/classes/exceptions/main/class_InvalidCommandException.php index a46bb95f..ea743a53 100644 --- a/inc/classes/exceptions/main/class_InvalidCommandException.php +++ b/inc/classes/exceptions/main/class_InvalidCommandException.php @@ -39,7 +39,7 @@ class InvalidCommandException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent constructor parent::__construct($message, $code); diff --git a/inc/classes/exceptions/state/class_InvalidStateException.php b/inc/classes/exceptions/state/class_InvalidStateException.php index 1aeefefb..f781c48c 100644 --- a/inc/classes/exceptions/state/class_InvalidStateException.php +++ b/inc/classes/exceptions/state/class_InvalidStateException.php @@ -39,7 +39,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); @@ -89,7 +89,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); @@ -139,7 +139,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); @@ -189,7 +189,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); @@ -239,7 +239,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); @@ -289,7 +289,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); @@ -339,7 +339,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); @@ -389,7 +389,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); @@ -439,7 +439,7 @@ class InvalidStateException extends FrameworkException { ); // Set extra data - $this->setExtraData($msgArray[0]->getClassName().':'.$this->getLine()); + $this->setExtraData($msgArray[0]->getClassName() . ':' . $this->getLine()); // Call parent contructor with message parent::__construct($message, $code); diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index f07ba33b..f297db74 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -88,6 +88,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { */ private $listInstance = null; + /** + * Instance of a menu + */ + private $menuInstance = null; + /** * The real class name */ @@ -1205,6 +1210,25 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { protected final function getListInstance () { return $this->listInstance; } + + /** + * 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 + */ + protected final function getMenuInstance () { + return $this->menuInstance; + } } // [EOF] diff --git a/inc/classes/main/menu/class_BaseMenu.php b/inc/classes/main/menu/class_BaseMenu.php index aedf6c28..8041813b 100644 --- a/inc/classes/main/menu/class_BaseMenu.php +++ b/inc/classes/main/menu/class_BaseMenu.php @@ -41,13 +41,23 @@ class BaseMenu extends BaseFrameworkSystem { */ public function renderMenu () { // Initialize the menu system by preparing it's template instance - $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance())); + $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this)); // Set it for later use $this->setTemplateInstance($templateInstance); - // Load the base (XML) template for generating menus - $this->getTemplateInstance()->loadMenuTemplate('base_menu'); + // Load the menu template for all + $this->getTemplateInstance()->loadMenuTemplate('generic_menu_entries'); + + // Get the 'page' from request instance + $page = $this->getApplicationInstance()->getRequestInstance()->getRequestElement('page'); + + // Load the menu template for this page + try { + $this->getTemplateInstance()->loadMenuTemplate($page . '_menu_entries'); + } catch (FileNotFoundException $e) { + // @TODO Should we log it here? We should, because it will be silently ignored. + } // Get the prepared content $menuContent = $this->getTemplateInstance()->getRawTemplateData(); diff --git a/inc/classes/main/template/image/class_ImageTemplateEngine.php b/inc/classes/main/template/image/class_ImageTemplateEngine.php index 13192647..19b83c77 100644 --- a/inc/classes/main/template/image/class_ImageTemplateEngine.php +++ b/inc/classes/main/template/image/class_ImageTemplateEngine.php @@ -199,7 +199,7 @@ class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTempl * @return void * @throws XmlNodeMismatchException If current main node mismatches the closing one */ - protected function endElement ($resource, $nodeName) { + public function endElement ($resource, $nodeName) { // Make all lower-case $nodeName = strtolower($nodeName); @@ -228,7 +228,7 @@ class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTempl * @return void * @todo Find something usefull with this! */ - protected function characterHandler ($resource, $characters) { + public function characterHandler ($resource, $characters) { // Trim all spaces away $characters = trim($characters); @@ -479,7 +479,7 @@ class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTempl $this->getGenericBasePath(), 'images/_cache', md5( - $this->imageInstance->getImageName().':'.$this->__toString().':'.$this->imageInstance->__toString() + $this->imageInstance->getImageName() . ':' . $this->__toString() . ':' . $this->imageInstance->__toString() ), $this->imageInstance->getImageType() ); diff --git a/inc/classes/main/template/menu/class_MenuTemplateEngine.php b/inc/classes/main/template/menu/class_MenuTemplateEngine.php index 9f179c0f..0dbd2cb8 100644 --- a/inc/classes/main/template/menu/class_MenuTemplateEngine.php +++ b/inc/classes/main/template/menu/class_MenuTemplateEngine.php @@ -22,6 +22,37 @@ * along with this program. If not, see . */ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate { + /** + * Main nodes in the XML tree ('menu' is ignored) + */ + private $mainNodes = array( + 'header', + 'entries', + 'footer', + ); + + /** + * Sub nodes in the XML tree + */ + private $subNodes = array( + 'title', + 'entry', + 'anchor', + 'design', + 'text', + 'advert', + ); + + /** + * Menu instance + */ + private $menuInstance = null; + + /** + * Current main node + */ + private $currMainNode = ''; + /** * Protected constructor * @@ -36,6 +67,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * Creates an instance of the class TemplateEngine and prepares it for usage * * @param $appInstance A manageable application + * @param $menuInstance A RenderableMenu instance * @return $tplInstance An instance of TemplateEngine * @throws BasePathIsEmptyException If the provided $templateBasePath is empty * @throws InvalidBasePathStringException If $templateBasePath is no string @@ -44,7 +76,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla * @throws BasePathReadProtectedException If $templateBasePath is * read-protected */ - public final static function createMenuTemplateEngine (ManageableApplication $appInstance) { + public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) { // Get a new instance $tplInstance = new MenuTemplateEngine(); @@ -87,6 +119,9 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla // Absolute output path for compiled templates $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path')); + // Set the menu instance + $tplInstance->setMenuInstance($menuInstance); + // Return the prepared instance return $tplInstance; } @@ -105,6 +140,154 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla // Load the special template $this->loadTemplate($template); } + + /** + * Getter for current main node + * + * @return $currMainNode Current main node + */ + public final function getCurrMainNode () { + return $this->currMainNode; + } + + /** + * Getter for main node array + * + * @return $mainNodes Array with valid main node names + */ + public final function getMainNodes () { + return $this->mainNodes; + } + + /** + * Getter for sub node array + * + * @return $subNodes Array with valid sub node names + */ + public final function getSubNodes () { + return $this->subNodes; + } + + /** + * Handles the start element of an XML resource + * + * @param $resource XML parser resource (currently ignored) + * @param $element The element we shall handle + * @param $attributes All attributes + * @return void + * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found + */ + public function startElement ($resource, $element, array $attributes) { + // Initial method name which will never be called... + $methodName = 'initMenu'; + + // Make the element name lower-case + $element = strtolower($element); + + // Is the element a main node? + /* DEBUG: */ echo "START: >".$element."<
\n"; + if (in_array($element, $this->getMainNodes())) { + // Okay, main node found! + $methodName = 'startMenu' . $this->convertToClassName($element); + + // Set it + $this->currMainNode = $element; + } elseif (in_array($element, $this->getSubNodes())) { + // Sub node found + $methodName = 'setMenuProperty' . $this->convertToClassName($element); + } elseif ($element != 'menu') { + // Invalid node name found + throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN); + } + + // Call method + /* DEBUG: */ echo "call: ".$methodName."
\n"; + call_user_func_array(array($this, $methodName), $attributes); + } + + /** + * Ends the main or sub node by sending out the gathered data + * + * @param $resource An XML resource pointer (currently ignored) + * @param $nodeName Name of the node we want to finish + * @return void + * @throws XmlNodeMismatchException If current main node mismatches the closing one + */ + public function endElement ($resource, $nodeName) { + // Make all lower-case + $nodeName = strtolower($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); + } elseif (in_array($nodeName, $this->getSubNodes())) { + // Silently ignore sub nodes + return; + } + + // Construct method name + $methodName = 'finish' . $this->convertToClassName($nodeName); + + // Call the corresponding method + /* DEBUG: */ echo "call: ".$methodName."
\n"; + call_user_func_array(array($this->getMenuInstance(), $methodName), array()); + } + + /** + * Currently not used + * + * @param $resource XML parser resource (currently ignored) + * @param $characters Characters to handle + * @return void + * @todo Find something usefull with this! + */ + public function characterHandler ($resource, $characters) { + // Trim all spaces away + $characters = trim($characters); + + // Is this string empty? + if (empty($characters)) { + // Then skip it silently + return false; + } // END - if + + // Unfinished work! + $this->partialStub("Handling extra characters is not yet supported!"); + } + + /** + * Intializes the menu + * + * @return void + * @todo Add cache creation here + */ + private function initMenu () { + // Unfinished work! + $this->partialStub('Please implement this method.'); + } + + /** + * Getter for menu cache file (FQFN) + * + * @return $fqfn 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', + md5( + $this->getMenuInstance()->getMenuName() . ':' . $this->__toString() . ':' . $this->getMenuInstance()->__toString() + ), + $this->getMenuInstance()->getMenuType() + ); + + // Return it + return $fqfn; + } } // [EOF] diff --git a/templates/menus/.htaccess b/templates/menus/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/templates/menus/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/templates/menus/_cache/.htaccess b/templates/menus/_cache/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/templates/menus/_cache/.htaccess @@ -0,0 +1 @@ +Deny from all