Development

Changeset 28485

You must first sign up to be able to contribute.

Changeset 28485

Show
Ignore:
Timestamp:
03/11/10 16:59:26 (3 years ago)
Author:
rodchyn
Message:

Add storage class

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php

    r28482 r28485  
    1212 */ 
    1313 
    14 class sfUserOnline 
     14class sfUserOnline extends myUser 
    1515{ 
    16     /** 
    17      * Static private variable to store memcache connection 
    18      * 
    19      * @var object Memcache connection resource 
    20      */ 
    21     private static $mem = null; 
     16    protected $statusHolder = null; 
     17    protected $userUniqueId = null; 
    2218     
    23     private static function connect(
     19    public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array()
    2420    { 
    25         if (!class_exists('Memcache')) { 
    26             throw new Exception("No memcache extension loaded"); 
    27         } 
    28         if (is_null(self::$mem)) { 
    29             $memcache = new Memcache; 
    30             $result = $memcache->connect(sfConfig::get('app_memcache_host', '127.0.0.1'), sfConfig::get('app_memcache_port', '11211')); 
    31             if(!$result) { 
    32                 throw new Exception("Can't connect to memcache server"); 
     21        // initialize parent 
     22        parent::initialize($dispatcher, $storage, $options); 
     23 
     24        $options = array_merge(array( 
     25            'online_status_class' => 'onlineMemcacheStorage', 
     26            'user_unique_method'  => 'getId' 
     27        ), $options); 
     28         
     29        $this->userUniqueId = call_user_func(array($this, $options['user_unique_method'])); 
     30        $this->statusHolder = new $options['online_status_class'];         
     31        $this->statusHolder->initialize($dispatcher, $storage, $options); 
     32        $this->refreshStatus(); 
     33         
     34    } 
     35     
     36     
     37    public function refreshStatus() 
     38    { 
     39        if($status = $this->getStatus()) { 
     40            if (!$this->statusHolder->replace($this->userUniqueId, $status)) { 
     41                $this->statusHolder->set($this->userUniqueId, $status); 
    3342            } 
    34             self::$mem = $memcache; 
    35         } 
    36         return self::$mem; 
    37     } 
    38     private static function getIdWithPrefix($user_id) 
    39     { 
    40         return sfConfig::get('app_memcache_online_users_prefix' . $user_id, $user_id); 
    41     } 
    42     public static function refreshStatus($user_id) 
    43     { 
    44         $user_id = self::getIdWithPrefix($user_id); 
    45         $memcache = self::connect(); 
    46         if ($memcache) { 
    47             if (!$memcache->replace($user_id, 1, false, sfConfig::get('app_memcache_online_time', '600'))) { 
    48                 $memcache->set($user_id, 1, false, sfConfig::get('app_memcache_online_time', '600')); 
    49             } 
     43            $this->dispatcher->notify(new sfEvent($this, 'user.change_status', array('status' => $status))); 
     44        } else { 
     45            return false; 
    5046        } 
    5147    } 
    52     public static function setOffline($user_id
     48    public function setOffline(
    5349    { 
    54         $user_id = self::getIdWithPrefix($user_id); 
    55         $memcache = self::connect(); 
    56         if ($memcache) { 
    57             $memcache->delete($user_id); 
    58         } 
     50        return $this->statusHolder->delete($this->userUniqueId); 
    5951    } 
    60     public static function isOnline($user_id
     52    public function isOnline(
    6153    { 
    62         $user_id = self::getIdWithPrefix($user_id); 
    63         $memcache = self::connect(); 
    64         if ($memcache) { 
    65             return (bool)$memcache->get($user_id); 
    66         } 
    67         return false; 
     54        return (bool)self::getStatus(); 
    6855    } 
    69     public static function getPlainStatus($user_id) 
     56     
     57    public function setStatus($status) 
    7058    { 
    71         $user_id = self::getIdWithPrefix($user_id); 
    72         sfLoader::loadHelpers(array('I18N')); 
    73         $memcache = self::connect(); 
    74         if ($memcache) { 
    75             return self::isOnline($user_id) ? __('Online') : __('Offline'); 
    76         } else { 
    77             return ''; 
    78         } 
     59        return $this->statusHolder->set($this->userUniqueId, $status); 
    7960    } 
    80     public static function getStatus($user_id) 
     61     
     62    public function getStatus() 
    8163    { 
    82         $user_id = self::getIdWithPrefix($user_id); 
    83         sfLoader::loadHelpers(array('I18N')); 
    84         $memcache = self::connect(); 
    85         if ($memcache) { 
    86             return self::isOnline($user_id) ? '<span class="user_status status_online">' . __('Online') . '</span>' : '<span class="user_status status_offline">' . __('Offline') . '</span>'; 
    87         } else { 
    88             return ''; 
    89         } 
     64        return $this->statusHolder->get($this->userUniqueId); 
     65    } 
     66     
     67    public function getId() 
     68    { 
     69        throw new Exception("Replace this method to get user unique id"); 
    9070    } 
    9171}