Dotclear

Changeset 1151:088d6e96adb4


Ignore:
Timestamp:
04/02/13 13:02:27 (12 years ago)
Author:
Dsls <dsls@…>
Branch:
twig
Message:

Add proxy class for context

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/class.dc.admincontext.php

    r1147 r1151  
    1212if (!defined('DC_RC_PATH')) { return; } 
    1313 
     14 
     15class dcProxy { 
     16     protected $object; 
     17     protected $attributes; 
     18     protected $methods; 
     19     protected $default; 
     20     protected $denyfirst; 
     21 
     22    /** 
     23     * valuesToArray - converts a list of strings to an array having these strings as keys. 
     24     *  
     25     * @param mixed $val the list to convert. 
     26     * @access protected 
     27     * @return mixed Value The resulting array 
     28     */ 
     29     protected function valuesToArray($val) { 
     30          $arr = array(); 
     31          foreach ($val as $k) { 
     32               $arr[$k]=true; 
     33          } 
     34          return $arr; 
     35     } 
     36 
     37     protected function isAllowed ($name,$list) { 
     38          if ($this->denyfirst) { 
     39               return isset($list[$name]); 
     40          } else { 
     41               return !isset($list[$name]); 
     42          } 
     43     } 
     44 
     45     public function __construct($object,$rights,$default='',$denyfirst=true) { 
     46          $this->object = $object; 
     47          $this->attributes = array(); 
     48          $this->methods = array(); 
     49          $this->denyfirst = $denyfirst; 
     50          if (isset($rights['attr'])) { 
     51               $this->attributes = $this->valuesToArray($rights['attr']); 
     52          } 
     53          if (isset($rights['methods'])) { 
     54               $this->methods = $this->valuesToArray($rights['methods']); 
     55          } 
     56     } 
     57 
     58     public function __get($name) { 
     59          if ($this->isAllowed($name,$this->attributes)) { 
     60               return $this->object->$name; 
     61          } else { 
     62               return $this->default; 
     63          } 
     64     } 
     65 
     66     public function __call($name,$args) { 
     67          if ($this->isAllowed($name,$this->methods) && 
     68               is_callable(array($this->object,$name))) { 
     69               return call_user_func_array(array($this->object,$name),$args); 
     70          } else { 
     71               return $this->default; 
     72          } 
     73 
     74     } 
     75} 
     76 
     77class dcArrayProxy extends dcProxy implements ArrayAccess { 
     78     public function offsetExists ($offset) { 
     79          return (isset($this->value[$offset])); 
     80     } 
     81     public function offsetGet ($offset) { 
     82          return new ProxyValue($this->object[$offset],$this->rights); 
     83     } 
     84     public function offsetSet ($offset ,$value ) { 
     85          // Do nothing, we are read only 
     86     } 
     87     public function offsetUnset ($offset) { 
     88          // Do nothing, we are read only 
     89     } 
     90} 
     91 
     92 
    1493/** 
    1594@ingroup DC_CORE 
     
    297376               while ($rs_blogs->fetch()) { 
    298377                    $blogs[$rs_blogs->blog_id] = $rs_blogs->blog_name.' - '.$rs_blogs->blog_url; 
    299                     $this->protected_globals['blogs'][$rs_blogs->blog_id] = array( 
    300                          'id'      => $rs_blogs->blog_id, 
    301                          'name'    => $rs_blogs->blog_name, 
    302                          'desc'    => $rs_blogs->blog_desc, 
    303                          'url'     => $rs_blogs->blog_url, 
    304                          'creadt'  => $rs_blogs->blog_creadt, 
    305                          'upddt'   => $rs_blogs->blog_upddt 
    306                     ); 
     378                    $this->protected_globals['blogs'][$rs_blogs->blog_id] =  
     379                    new dcArrayProxy($rs_blogs, array( 
     380                         'blog_id','blog_name','blog_desc','blog_url','blog_creadt','blog_upddt')); 
    307381               } 
    308382          } 
     
    326400     { 
    327401          $this->protected_globals['current_blog'] = $this->core->auth->blog_count ? 
    328                array( 
    329                     'id'      => $this->core->blog->id, 
    330                     'name'    => $this->core->blog->name, 
    331                     'desc'    => $this->core->blog->desc, 
    332                     'url'     => $this->core->blog->url, 
    333                     'host'    => $this->core->blog->host, 
    334                     'creadt'  => $this->core->blog->creadt, 
    335                     'upddt'   => $this->core->blog->upddt 
    336                ) : array( 
     402               new dcProxy($this->core->blog,array( 
     403                    'id','name','desc','url','host','creadt','upddt' 
     404               )) : array( 
    337405                    'id'      => '', 
    338406                    'name'    => '', 
Note: See TracChangeset for help on using the changeset viewer.

Sites map