Author: bshaffer
Date: 2010-05-14 17:56:10 +0200 (Fri, 14 May 2010)
New Revision: 29471

Modified:
   plugins/csSettingsPlugin/trunk/lib/BasecsSettings.class.php
Log:
adding custom cache handler

Modified: plugins/csSettingsPlugin/trunk/lib/BasecsSettings.class.php
===================================================================
--- plugins/csSettingsPlugin/trunk/lib/BasecsSettings.class.php 2010-05-14 
15:45:17 UTC (rev 29470)
+++ plugins/csSettingsPlugin/trunk/lib/BasecsSettings.class.php 2010-05-14 
15:56:10 UTC (rev 29471)
@@ -11,6 +11,8 @@
  */
 class BasecsSettings
 {
+  static  $cache = false;
+    
   static function getDefaultUploadPath()
   {
     return 'uploads/setting';
@@ -48,123 +50,107 @@
    * @access public
    * @return string
    */
-  static function get($setting)
+  static function get($setting, $default = null)
   {
     // Pull from cached settings array
-    $settingsArray = self::getSettingsArray();
-    if (isset($settingsArray[$setting]))
+    $cache = self::buildCache('settings_array');
+    
+    if($cache->has('settings_array_'.$setting))
     {
-      return $settingsArray[$setting];
+      return unserialize($cache->get('settings_array_'.$setting));
     }
 
     //Look in app.ymls for setting
-    return sfConfig::get('app_'.self::settingize($setting));
+    return sfConfig::get('app_'.self::settingize($setting), $default);
   }
-
-  /**
-   * getSetting
-  * pulls the csSetting object for a given setting
-   *
-   * @param string $setting
-   * @static
-   * @access public
-   * @return object csSetting
-   */
-  static function getSetting($setting)
+  
+  static function buildCache($type)
   {
-    $objArray = self::getAllSettings();
-    if (isset($objArray[$setting]))
-    {
-      $ret = new csSetting();
-      $ret->fromArray($objArray[$setting]);
-      return $ret;
-    }
+    $cache_handler = self::getCache();
 
-    return null;
-  }
-
-  /**
-   * getAllSettings
-   * Returns an array of all setting objects
-   * @static
-   * @access public
-   * @return array
-   */
-  static function getAllSettings()
-  {
-    $cachePath = 
sfConfig::get('sf_cache_dir').'/'.self::getCache('object_array');
-    if (!file_exists($cachePath))
+    if(!$cache_handler->get($type.'_cs_cache_is_built', false))
     {
-      $objArray = array();
       foreach (Doctrine::getTable('csSetting')->findAll() as $setting)
       {
-        $objArray[$setting['slug']] = $setting->toArray();
-        $objArray[$setting['name']] = $setting->toArray();
+        if($type == 'settings_array')
+        {
+          $cache_handler->set($type.'_'.$setting['slug'], 
serialize($setting->getValue()));
+          $cache_handler->set($type.'_'.$setting['name'], 
serialize($setting->getValue()));
+        }
+        elseif( $type == 'settings_object')
+        {
+          $cache_handler->set($type.'_'.$setting['slug'], 
serialize($setting->toArray()));
+          $cache_handler->set($type.'_'.$setting['name'], 
serialize($setting->toArray()));
+        }
       }
-
-      // Cache Settings
-      $serialized = serialize($objArray);
-      file_put_contents($cachePath, $serialized);
+      
+      $cache_handler->set($type.'_cs_cache_is_built', true);
     }
-    else
-    {
-      // Pull settings array
-      $objArray = unserialize(file_get_contents($cachePath));
-    }
-    return $objArray;
+    
+    return $cache_handler;
   }
 
   /**
-   * getAll
-   * Returns an array of settings
-   * (key: setting slug or name, value: setting value)
+   * getSetting
+   * pulls the csSetting object for a given setting
    *
+   * @param string $setting
    * @static
    * @access public
-   * @return void
+   * @return object csSetting
    */
-  static function getSettingsArray()
+  static function getSetting($setting)
   {
-    $cachePath = 
sfConfig::get('sf_cache_dir').'/'.self::getCache('settings_array');
-    if (!file_exists($cachePath))
+
+    if(strlen(trim($setting)) == 0)
     {
-      $settingsArray = array();
-      foreach (Doctrine::getTable('csSetting')->findAll() as $setting)
+      throw new sfException('[f6Settings::getSetting] invalid name');
+    }
+    
+    $cache = self::buildCache('settings_object');
+    
+    if ($cache->has('settings_object_'.$setting))
+    {
+      $ret = new csSetting();
+      $ret->fromArray(unserialize($cache->get('settings_object_'.$setting, 
serialize(array()))));
+      
+      $value = unserialize($cache->get('settings_object_'.$setting));
+      if(!is_array($value))
       {
-        $settingsArray[$setting['slug']] = $setting->getValue();
-        $settingsArray[$setting['name']] = $setting->getValue();
+        return null;
       }
-
-      // Cache Settings
-      $serialized = serialize($settingsArray);
-      file_put_contents($cachePath, $serialized);
+      
+      $ret->toArray($value);
+      $ret->assignIdentifier($value['id']);
+      
+      return $ret;
     }
-    else
-    {
-      // Pull settings array
-      $settingsArray = unserialize(file_get_contents($cachePath));
-    }
-    return $settingsArray;
+
+    return null;
   }
 
   static public function clearSettingsCache()
   {
-    if(is_array(self::getCache())){
-      foreach (self::getCache() as $cachedir)
-      {
-        $cachePath = sfConfig::get('sf_cache_dir').'/'.$cachedir;
-        if (file_exists($cachePath))
-        {
-          unlink($cachePath);
-        }
-      }
-    }
+    
+    self::getCache()->clean(); 
   }
 
-  static public function getCache($key = '')
+  static public function getCache()
   {
-    $cache = sfConfig::get('app_csSettingsPlugin_cachepaths');
-    return $key ? $cache[$key] : $cache;
+    if(!self::$cache)
+    {
+      $cache_settings = sfConfig::get('app_csSettingsPlugin_cache', array(
+        'class'   => 'sfNoCache',
+        'options' => array()
+      ));
+      
+      $class    = $cache_settings['class'];
+      $options  = $cache_settings['options'];
+            
+      self::$cache = new $class($options);
+    }
+    
+    return self::$cache;
   }
 
   static public function settingize($anystring)

-- 
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