Author: rodchyn
Date: 2010-03-11 16:59:26 +0100 (Thu, 11 Mar 2010)
New Revision: 28485

Added:
   plugins/sfUserOnlinePlugin/lib/storage/
   plugins/sfUserOnlinePlugin/lib/storage/onlineMemcacheStorage.class.php
   plugins/sfUserOnlinePlugin/lib/storage/onlineStorage.class.php
Modified:
   plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php
Log:
Add storage class

Modified: plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php
===================================================================
--- plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php       2010-03-11 
14:42:27 UTC (rev 28484)
+++ plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php       2010-03-11 
15:59:26 UTC (rev 28485)
@@ -11,82 +11,62 @@
  *
  */
 
-class sfUserOnline
+class sfUserOnline extends myUser
 {
-    /**
-     * Static private variable to store memcache connection
-     *
-     * @var object Memcache connection resource
-     */
-    private static $mem = null;
+    protected $statusHolder = null;
+    protected $userUniqueId = null;
     
-    private static function connect()
+    public function initialize(sfEventDispatcher $dispatcher, sfStorage 
$storage, $options = array())
     {
-        if (!class_exists('Memcache')) {
-            throw new Exception("No memcache extension loaded");
-        }
-        if (is_null(self::$mem)) {
-            $memcache = new Memcache;
-            $result = $memcache->connect(sfConfig::get('app_memcache_host', 
'127.0.0.1'), sfConfig::get('app_memcache_port', '11211'));
-            if(!$result) {
-                throw new Exception("Can't connect to memcache server");
-            }
-            self::$mem = $memcache;
-        }
-        return self::$mem;
+        // initialize parent
+        parent::initialize($dispatcher, $storage, $options);
+
+        $options = array_merge(array(
+            'online_status_class' => 'onlineMemcacheStorage',
+            'user_unique_method'  => 'getId'
+        ), $options);
+        
+        $this->userUniqueId = call_user_func(array($this, 
$options['user_unique_method']));
+        $this->statusHolder = new $options['online_status_class'];        
+        $this->statusHolder->initialize($dispatcher, $storage, $options);
+        $this->refreshStatus();
+        
     }
-    private static function getIdWithPrefix($user_id)
+    
+    
+    public function refreshStatus()
     {
-        return sfConfig::get('app_memcache_online_users_prefix' . $user_id, 
$user_id);
-    }
-    public static function refreshStatus($user_id)
-    {
-        $user_id = self::getIdWithPrefix($user_id);
-        $memcache = self::connect();
-        if ($memcache) {
-            if (!$memcache->replace($user_id, 1, false, 
sfConfig::get('app_memcache_online_time', '600'))) {
-                $memcache->set($user_id, 1, false, 
sfConfig::get('app_memcache_online_time', '600'));
+        if($status = $this->getStatus()) {
+            if (!$this->statusHolder->replace($this->userUniqueId, $status)) {
+                $this->statusHolder->set($this->userUniqueId, $status);
             }
+            $this->dispatcher->notify(new sfEvent($this, 'user.change_status', 
array('status' => $status)));
+        } else {
+            return false;
         }
     }
-    public static function setOffline($user_id)
+    public function setOffline()
     {
-        $user_id = self::getIdWithPrefix($user_id);
-        $memcache = self::connect();
-        if ($memcache) {
-            $memcache->delete($user_id);
-        }
+        return $this->statusHolder->delete($this->userUniqueId);
     }
-    public static function isOnline($user_id)
+    public function isOnline()
     {
-        $user_id = self::getIdWithPrefix($user_id);
-        $memcache = self::connect();
-        if ($memcache) {
-            return (bool)$memcache->get($user_id);
-        }
-        return false;
+        return (bool)self::getStatus();
     }
-    public static function getPlainStatus($user_id)
+    
+    public function setStatus($status)
     {
-        $user_id = self::getIdWithPrefix($user_id);
-        sfLoader::loadHelpers(array('I18N'));
-        $memcache = self::connect();
-        if ($memcache) {
-            return self::isOnline($user_id) ? __('Online') : __('Offline');
-        } else {
-            return '';
-        }
+        return $this->statusHolder->set($this->userUniqueId, $status);
     }
-    public static function getStatus($user_id)
+    
+    public function getStatus()
     {
-        $user_id = self::getIdWithPrefix($user_id);
-        sfLoader::loadHelpers(array('I18N'));
-        $memcache = self::connect();
-        if ($memcache) {
-            return self::isOnline($user_id) ? '<span class="user_status 
status_online">' . __('Online') . '</span>' : '<span class="user_status 
status_offline">' . __('Offline') . '</span>';
-        } else {
-            return '';
-        }
+        return $this->statusHolder->get($this->userUniqueId);
     }
+    
+    public function getId()
+    {
+        throw new Exception("Replace this method to get user unique id");
+    }
 }
 ?>
\ No newline at end of file

Added: plugins/sfUserOnlinePlugin/lib/storage/onlineMemcacheStorage.class.php
===================================================================
--- plugins/sfUserOnlinePlugin/lib/storage/onlineMemcacheStorage.class.php      
                        (rev 0)
+++ plugins/sfUserOnlinePlugin/lib/storage/onlineMemcacheStorage.class.php      
2010-03-11 15:59:26 UTC (rev 28485)
@@ -0,0 +1,61 @@
+<?php
+
+class onlineMemcacheStorage extends onlineStorage
+{
+    private $resource = null;
+    private $lifetime = null;
+    private $prefix   = null;
+    
+    public function initialize(sfEventDispatcher $dispatcher, sfStorage 
$storage, $options = array())
+    {        
+        $options = array_merge(array(
+            'memcache_host'   => '127.0.0.1',
+            'memcache_port'   => '11211',
+            'status_lifetime' => '600', // ten minutes
+            'memcache_prefix' => 'ustatus_' // memcache prefix
+        ), $options);
+        
+        $this->prefix   = $options['memcache_prefix'];
+        $this->lifetime = $options['status_lifetime'];
+        
+        if (!class_exists('Memcache')) {
+            throw new Exception("No memcache extension loaded");
+        }
+        
+        $this->resource = new Memcache();
+        if(!$this->resource->connect($options['memcache_host'], 
$options['memcache_port'])) {
+            throw new Exception("Can't connect to memcache server");
+        }
+        
+    }
+    
+    public function set($name, $value)
+    {
+        $this->resource->set($this->getNameWithPrefix($name), $value, false, 
$this->lifetime);
+    }
+  
+    public function get($name)
+    {
+        return $this->resource->get($this->getNameWithPrefix($name));
+    }
+    
+    public function has($name)
+    {
+        return (bool)$this->resource->get($this->getNameWithPrefix($name));
+    }
+    
+    public function delete($name)
+    {
+        return $this->resource->delete($this->getNameWithPrefix($name));
+    }
+    
+    public function replace($name, $value)
+    {
+        $this->resource->replace($this->getNameWithPrefix($name), $value, 
false, $this->lifetime);
+    }
+    
+    private function getNameWithPrefix($name)
+    {
+        return $this->prefix . $name;
+    }
+}
\ No newline at end of file

Added: plugins/sfUserOnlinePlugin/lib/storage/onlineStorage.class.php
===================================================================
--- plugins/sfUserOnlinePlugin/lib/storage/onlineStorage.class.php              
                (rev 0)
+++ plugins/sfUserOnlinePlugin/lib/storage/onlineStorage.class.php      
2010-03-11 15:59:26 UTC (rev 28485)
@@ -0,0 +1,28 @@
+<?php
+
+abstract class onlineStorage
+{
+
+  /**
+   * Initializes this Storage instance.
+   *
+   * @param sfContext A sfContext instance
+   * @param array   An associative array of initialization parameters
+   *
+   * @return boolean true, if initialization completes successfully, otherwise 
false
+   *
+   * @throws <b>sfInitializationException</b> If an error occurs while 
initializing this sfStorage
+   */
+  abstract function initialize(sfEventDispatcher $dispatcher, sfStorage 
$storage, $options = array());
+   
+  abstract function set($name, $value);
+  
+  abstract function get($name);
+  
+  abstract function has($name);
+  
+  abstract function delete($name);
+  
+  abstract function replace($name, $value);
+  
+}
\ No newline at end of file

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