MenuTemplateEngine heavily extended, but still in non-working state
authorRoland Häder <roland@mxchange.org>
Wed, 2 Sep 2009 21:56:00 +0000 (21:56 +0000)
committerRoland Häder <roland@mxchange.org>
Wed, 2 Sep 2009 21:56:00 +0000 (21:56 +0000)
12 files changed:
.gitattributes
inc/classes/exceptions/controller/class_DefaultControllerException.php
inc/classes/exceptions/controller/class_InvalidControllerException.php
inc/classes/exceptions/crypto/class_EncryptInvalidLengthException.php
inc/classes/exceptions/main/class_InvalidCommandException.php
inc/classes/exceptions/state/class_InvalidStateException.php
inc/classes/main/class_BaseFrameworkSystem.php
inc/classes/main/menu/class_BaseMenu.php
inc/classes/main/template/image/class_ImageTemplateEngine.php
inc/classes/main/template/menu/class_MenuTemplateEngine.php
templates/menus/.htaccess [new file with mode: 0644]
templates/menus/_cache/.htaccess [new file with mode: 0644]

index 4f46c62..0185c48 100644 (file)
@@ -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
index 89a1a50..551c0c5 100644 (file)
@@ -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);
index a8e1d96..b637ae7 100644 (file)
@@ -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);
index 63c6c2e..a6af83d 100644 (file)
@@ -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);
index a46bb95..ea743a5 100644 (file)
@@ -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);
index 1aeefef..f781c48 100644 (file)
@@ -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);
index f07ba33..f297db7 100644 (file)
@@ -89,6 +89,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        private $listInstance = null;
 
        /**
+        * Instance of a menu
+        */
+       private $menuInstance = null;
+
+       /**
         * The real class name
         */
        private $realClass      = 'BaseFrameworkSystem';
@@ -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]
index aedf6c2..8041813 100644 (file)
@@ -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();
index 1319264..19b83c7 100644 (file)
@@ -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()
                );
index 9f179c0..0dbd2cb 100644 (file)
  */
 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
         *
         * @return      void
@@ -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: &gt;".$element."&lt;<br />\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."<br />\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: &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);
+               } 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."<br />\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 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/templates/menus/_cache/.htaccess b/templates/menus/_cache/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all