Author: Jonathan.Wage
Date: 2010-01-17 20:58:26 +0100 (Sun, 17 Jan 2010)
New Revision: 26779

Modified:
   plugins/sfSympalPlugin/trunk/config/app.yml
   plugins/sfSympalPlugin/trunk/lib/cache/sfSympalCache.class.php
   plugins/sfSympalPlugin/trunk/lib/core/sfSympalConfiguration.class.php
   plugins/sfSympalPlugin/trunk/lib/helper/SympalMenuHelper.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/modules/sympal_admin/lib/Basesympal_adminActions.class.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenu.class.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSite.class.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSiteManager.class.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/modules/sympal_menu_items/lib/Basesympal_menu_itemsActions.class.php
   plugins/sfSympalPlugin/trunk/test/unit/MenuTest.php
Log:
[1.4][sfSympalPlugin][1.0] Adding menu cache functionality


Modified: plugins/sfSympalPlugin/trunk/config/app.yml
===================================================================
--- plugins/sfSympalPlugin/trunk/config/app.yml 2010-01-17 19:17:53 UTC (rev 
26778)
+++ plugins/sfSympalPlugin/trunk/config/app.yml 2010-01-17 19:58:26 UTC (rev 
26779)
@@ -149,6 +149,11 @@
       with_layout: false
       lifetime: 3600
 
+    # Configure Sympal menu caching. Enabling this feature will cache the 
built menu objects avoiding
+    # database queries for menus
+    menu_cache:
+      enabled: true
+
     # Configure and customize the available content slot types
     content_slot_types:
       #MySlotType:

Modified: plugins/sfSympalPlugin/trunk/lib/cache/sfSympalCache.class.php
===================================================================
--- plugins/sfSympalPlugin/trunk/lib/cache/sfSympalCache.class.php      
2010-01-17 19:17:53 UTC (rev 26778)
+++ plugins/sfSympalPlugin/trunk/lib/cache/sfSympalCache.class.php      
2010-01-17 19:58:26 UTC (rev 26779)
@@ -210,6 +210,11 @@
     return $this->_cacheDriver->set($key, serialize($data), $lifeTime);
   }
 
+  public function has($key)
+  {
+    return $this->_cacheDriver->has($key);
+  }
+
   public function get($key)
   {
     return unserialize($this->_cacheDriver->get($key));

Modified: plugins/sfSympalPlugin/trunk/lib/core/sfSympalConfiguration.class.php
===================================================================
--- plugins/sfSympalPlugin/trunk/lib/core/sfSympalConfiguration.class.php       
2010-01-17 19:17:53 UTC (rev 26778)
+++ plugins/sfSympalPlugin/trunk/lib/core/sfSympalConfiguration.class.php       
2010-01-17 19:58:26 UTC (rev 26779)
@@ -7,6 +7,7 @@
     $_projectConfiguration,
     $_sympalContext,
     $_symfonyContext,
+    $_doctrineManager,
     $_bootstrap,
     $_plugins = array(),
     $_modules = array(),
@@ -16,6 +17,7 @@
   {
     $this->_dispatcher = $dispatcher;
     $this->_projectConfiguration = $projectConfiguration;
+    $this->_doctrineManager = Doctrine_Manager::getInstance();
 
     $this->_initializeSymfonyConfig();
 
@@ -53,10 +55,10 @@
 
     $this->_dispatcher->connect('task.cache.clear', array($this, 
'listenToTaskCacheClear'));
 
-    
Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_HYDRATE_OVERWRITE,
 false);
-    
Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_TABLE_CLASS, 
'sfSympalDoctrineTable');
-    
Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_QUERY_CLASS, 
'sfSympalDoctrineQuery');
-    
Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_COLLECTION_CLASS,
 'sfSympalDoctrineCollection');
+    
$this->_doctrineManager->setAttribute(Doctrine_Core::ATTR_HYDRATE_OVERWRITE, 
false);
+    $this->_doctrineManager->setAttribute(Doctrine_Core::ATTR_TABLE_CLASS, 
'sfSympalDoctrineTable');
+    $this->_doctrineManager->setAttribute(Doctrine_Core::ATTR_QUERY_CLASS, 
'sfSympalDoctrineQuery');
+    
$this->_doctrineManager->setAttribute(Doctrine_Core::ATTR_COLLECTION_CLASS, 
'sfSympalDoctrineCollection');
   }
 
   public function listenToTaskCacheClear(sfEvent $event)
@@ -120,6 +122,11 @@
     return $parameters;
   }
 
+  public function getDoctrineManager()
+  {
+    return $this->_doctrineManager;
+  }
+
   public function getCache()
   {
     return $this->_cache;

Modified: plugins/sfSympalPlugin/trunk/lib/helper/SympalMenuHelper.php
===================================================================
--- plugins/sfSympalPlugin/trunk/lib/helper/SympalMenuHelper.php        
2010-01-17 19:17:53 UTC (rev 26778)
+++ plugins/sfSympalPlugin/trunk/lib/helper/SympalMenuHelper.php        
2010-01-17 19:58:26 UTC (rev 26779)
@@ -7,7 +7,7 @@
  * @param bool $showChildren Whether or not it should show the children when 
rendering
  * @param string $class The menu class to return an instance of
  */
-function get_sympal_menu($name, $showChildren = true, $class = null)
+function get_sympal_menu($name, $showChildren = null, $class = null)
 {
   return sfSympalMenuSiteManager::getMenu($name, $showChildren, $class);
 }
@@ -21,7 +21,7 @@
  * @param string $split Whether to return a 2nd menu item with the remaining 
menu items in it
  * @return mixed Either one sfSympalMenu instance of an array with 2 
sfSympalMenu instances
  */
-function get_sympal_split_menus($name, $showChildren = true, $max = null, 
$split = false)
+function get_sympal_split_menus($name, $showChildren = null, $max = null, 
$split = false)
 {
   $menu = sfSympalMenuSiteManager::getMenu($name, $showChildren);
   if ($menu)

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/modules/sympal_admin/lib/Basesympal_adminActions.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/modules/sympal_admin/lib/Basesympal_adminActions.class.php
     2010-01-17 19:17:53 UTC (rev 26778)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/modules/sympal_admin/lib/Basesympal_adminActions.class.php
     2010-01-17 19:58:26 UTC (rev 26779)
@@ -58,6 +58,9 @@
         }
       }
     }
+
+    $this->clearCache();
+
     return sfView::NONE;
   }
 

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenu.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenu.class.php
 2010-01-17 19:17:53 UTC (rev 26778)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenu.class.php
 2010-01-17 19:58:26 UTC (rev 26779)
@@ -15,6 +15,7 @@
     $_requiresNoAuth   = null,
     $_showChildren     = true,
     $_current          = false,
+    $_currentObject    = null,
     $_options          = array(),
     $_children         = array(),
     $_credentials      = array();
@@ -449,13 +450,18 @@
     return $this->_current;
   }
 
+  public function setCurrentObject(sfSympalMenu $currentObject)
+  {
+    $this->_currentObject = $currentObject;
+  }
+
   public function isCurrentAncestor()
   {
-    $menuItem = sfSympalContext::getInstance()->getCurrentMenuItem();
-    if ($menuItem){
-      while ($menuItem->getLevel() != 0)
+    if ($currentObject = $this->_currentObject)
+    {
+      while ($currentObject->getLevel() != 0)
       {
-        if ($this->getRoute() == $menuItem->getRoute() && $this->getLabel() == 
$menuItem->getLabel())
+        if ($this->getRoute() == $currentObject->getRoute() && 
$this->getLabel() == $currentObject->getLabel())
         {
           $ret = true;
 
@@ -463,7 +469,7 @@
         }
 
         $ret = false;
-        $menuItem = $menuItem->getNode()->getParent();
+        $currentObject = $currentObject->getParent();
       }
     }
     else

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSite.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSite.class.php
     2010-01-17 19:17:53 UTC (rev 26778)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSite.class.php
     2010-01-17 19:58:26 UTC (rev 26779)
@@ -7,7 +7,7 @@
 
   public function findMenuItem(sfSympalMenuItem $menuItem)
   {
-    if ($this->_menuItem->id == $menuItem->id)
+    if ($this->_menuItem['id'] == $menuItem->id)
     {
       return $this;
     }
@@ -27,7 +27,7 @@
     $obj = $this;
 
     do {
-       $path[] = __($obj->getMenuItem()->getLabel());
+       $path[] = __($obj->_menuItem['label']);
     } while ($obj = $obj->getParent());
 
     return implode(' > ', array_reverse($path));
@@ -40,7 +40,7 @@
 
     if ($subItem)
     {
-      if ($subItem instanceof sfSympalContent && $this->_menuItem->_content_id 
== $subItem->id)
+      if ($subItem instanceof sfSympalContent && 
$this->_menuItem['content_id'] == $subItem->id)
       {
         $subItem = array();
       }
@@ -61,9 +61,8 @@
     }
 
     do {
-      $menuItem = $obj->getMenuItem();
-      $label = __($menuItem->getLabel());
-       $breadcrumbs[$label] = $menuItem->getItemRoute();
+      $label = __($obj->getLabel());
+       $breadcrumbs[$label] = $obj->getRoute();
     } while ($obj = $obj->getParent());
 
     return count($breadcrumbs) > 1 ? array_reverse($breadcrumbs):array();
@@ -74,14 +73,11 @@
     return 
sfSympalMenuBreadcrumbs::generate($this->getBreadcrumbsArray($subItem));
   }
 
-  public function getMenuItem()
-  {
-    return $this->_menuItem;
-  }
-
   public function setMenuItem(sfSympalMenuItem $menuItem)
   {
-    $this->_menuItem = $menuItem;
+    $this->_route = $menuItem->getItemRoute();
+    $this->_menuItem = $menuItem->toArray(false);
+    unset($this->_menuItem['__children']);
 
     $this->requiresAuth($menuItem->requires_auth);
     $this->requiresNoAuth($menuItem->requires_no_auth);
@@ -102,7 +98,7 @@
     $obj = $this;
 
     do {
-       if ($obj->getMenuItem()->getLevel() == 1)
+       if ($obj->getLevel() == 1)
        {
          return $obj;
        }
@@ -137,21 +133,10 @@
   public function isCurrentAncestor()
   {
     $menuItem = sfSympalContext::getInstance()->getCurrentMenuItem();
-    if ($menuItem) {
-
-      $current = $this;
-      $children = $current->getChildren();
-      do {
-        foreach ($children as $child)
-        {
-          if ($child->getMenuItem() == $menuItem)
-          {
-            return true;
-          } else {
-            $current = $child;
-          }
-        }
-      } while ($children = $current->getChildren());
+    if ($menuItem && $this->_menuItem)
+    {
+      $this->_currentObject = $this->findMenuItem($menuItem);
+      return parent::isCurrentAncestor();
     }
 
     return false;

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSiteManager.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSiteManager.class.php
      2010-01-17 19:17:53 UTC (rev 26778)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSiteManager.class.php
      2010-01-17 19:58:26 UTC (rev 26779)
@@ -12,11 +12,6 @@
 
   protected static $_instance;
 
-  public function __construct()
-  {
-    $this->initialize();
-  }
-
   public static function getInstance()
   {
     if (!self::$_instance)
@@ -49,26 +44,67 @@
     $this->initialize();
   }
 
-  public static function getMenu($name, $showChildren = true, $class = null)
+  public static function getMenu($name, $showChildren = null, $class = null)
   {
     return self::getInstance()->_getMenu($name, $showChildren, $class);
   }
 
-  protected function _getMenu($name, $showChildren = true, $class = null)
+  protected function _getMenu($name, $showChildren = null, $class = null)
   {
-    $key = md5($name.var_export($showChildren, true).$class);
-    if (isset($this->_menus[$key]))
+    if (!$name)
     {
-      return $this->_menus[$key];
+      return false;
     }
 
-    if (!$name)
+    $cacheKey = 'SYMPAL_MENU_'.md5($name.var_export($showChildren, 
true).$class);
+    if (isset($this->_menus[$cacheKey]))
     {
-      return false;
+      return $this->_menus[$cacheKey];
     }
 
+    if ($menuCacheEnabled = sfSympalConfig::get('menu_cache', 'enabled', true))
+    {
+      $cache = sfSympalConfiguration::getActive()->getCache();
+      if ($cache->has($cacheKey))
+      {
+        $menu = $cache->get($cacheKey);
+        $this->_menus[$cacheKey] = $menu;
+
+        if ($showChildren !== null)
+        {
+          $menu->callRecursively('showChildren', $showChildren);
+        }
+
+        return $menu;
+      }
+    }
+
     $this->initialize();
 
+    $menu = $this->_buildMenu($name, $class);
+
+    if ($menu)
+    {
+      if ($showChildren !== null)
+      {
+        $menu->callRecursively('showChildren', $showChildren);
+      }
+
+      $this->_menus[$cacheKey] = $menu;
+
+      if ($menuCacheEnabled)
+      {
+        $cache->set($cacheKey, $menu);
+      }
+
+      return $menu;
+    } else {
+      return false;
+    }
+  }
+
+  protected function _buildMenu($name, $class)
+  {
     if ($name instanceof sfSympalMenuItem)
     {
       $menuItem = $name;
@@ -96,21 +132,10 @@
 
     if (isset($menuItem))
     {
-      $return = 
$menu->getMenuItemSubMenu($menu->findMenuItem($menuItem)->getTopLevelParent()->getMenuItem());
+      return 
$menu->getMenuItemSubMenu($menu->findMenuItem($menuItem)->getTopLevelParent()->getMenuItem());
     } else {
-      $return = $menu;
+      return $menu;
     }
-
-    if ($return)
-    {
-      $return->callRecursively('showChildren', $showChildren);
-
-      $this->_menus[$key] = $return;
-
-      return $return;
-    } else {
-      return false;
-    }
   }
 
   public static function split($menu, $max, $split = false)

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/modules/sympal_menu_items/lib/Basesympal_menu_itemsActions.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/modules/sympal_menu_items/lib/Basesympal_menu_itemsActions.class.php
    2010-01-17 19:17:53 UTC (rev 26778)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/modules/sympal_menu_items/lib/Basesympal_menu_itemsActions.class.php
    2010-01-17 19:58:26 UTC (rev 26779)
@@ -26,6 +26,8 @@
 
       $tree = $form->save();
 
+      $this->clearCache();
+
       $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', 
array('object' => $tree)));
 
       if ($request->hasParameter('_save_and_add'))

Modified: plugins/sfSympalPlugin/trunk/test/unit/MenuTest.php
===================================================================
--- plugins/sfSympalPlugin/trunk/test/unit/MenuTest.php 2010-01-17 19:17:53 UTC 
(rev 26778)
+++ plugins/sfSympalPlugin/trunk/test/unit/MenuTest.php 2010-01-17 19:58:26 UTC 
(rev 26779)
@@ -154,7 +154,7 @@
 $t->is($menuItem->getContent()->getHeaderTitle(), 'Home', 'Test 
sfSympalMenuItem::getHeaderTitle()');
 $t->is($menuItem->getLabel(), 'Home', 'Test sfSympalMenuItem::getLabel()');
 $t->is($menuItem->getItemRoute(), '@sympal_content_home', 'Test 
sfSympalMenuItem::getItemRoute()');
-$t->is($menuItem->getBreadcrumbs()->getPathAsString(), '', 'Test 
sfSympalBreadcrumbs::getPathAsString()');
+$t->is($menuItem->getBreadcrumbs()->getPathAsString(), 'primary / Home', 'Test 
sfSympalBreadcrumbs::getPathAsString()');
 
 $menuManager = sfSympalMenuSiteManager::getInstance();
 $menuManager->clear();

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.


Reply via email to