Dotclear


Ignore:
Timestamp:
03/25/13 04:31:18 (12 years ago)
Author:
JcDenis
Branch:
twig
Message:

Yet another more flexible method to create template environment and context.

File:
1 edited

Legend:

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

    r1091 r1128  
    1313 
    1414/** 
    15 @ingroup DC_CORE 
    16 @brief Template extension for admin context 
    17  
    18 This extends template environment with tools required in admin context. 
    19 */ 
    20 class dcAdminContext extends Twig_Extension 
     15 * @ingroup DC_CORE 
     16 * @brief Template extension for admin context. 
     17 *  
     18 * This extends template environment with tools required in admin context. 
     19 */ 
     20class dcAdminContext extends dcContext 
    2121{ 
    22      protected $core; 
    23      protected $globals = array(); 
    24      protected $protected_globals = array(); 
    25      protected $memory = array(); 
    26       
    2722     public function __construct($core) 
    2823     { 
    29           $this->core = $core; 
    30            
    31           # Globals editable via context 
    32           $this->globals = array(); 
    33            
    34           # Globals not editable via context 
    35           $this->protected_globals = array( 
    36                'messages' => array( 
    37                     'static' => array(), 
    38                     'lists' => array(), 
    39                     'alert' => '', 
    40                     'errors' => array() 
    41                ), 
    42                 
     24          parent::__construct($core); 
     25           
     26          $this->protected_globals = array_merge($this->protected_globals,array( 
    4327               'page_title'   => array(), 
    4428               'page_global'  => false, 
     
    4731               'theme_url'    => DC_ADMIN_URL.'index.php?tf=', 
    4832               'plugin_url'   => DC_ADMIN_URL.'index.php?pf=', 
    49                 
    50                'version'           => DC_VERSION, 
    51                'vendor_name'  => DC_VENDOR_NAME, 
    52                 
    53                'safe_mode'    => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'], 
    54                'debug_mode'   => DC_DEBUG 
    55           ); 
    56      } 
    57       
    58      /** 
    59      Prevent call crash from template on method that return this class 
    60      */ 
    61      public function __toString() 
    62      { 
    63           return ''; 
    64      } 
    65       
    66      /** 
    67      Test a global variable 
    68       
    69      @param string $name Name of the variable to test 
    70      @return boolean 
    71      */ 
    72      public function __isset($name) 
    73      { 
    74           return isset($this->globals[$name]); 
    75      } 
    76       
    77      /** 
    78      Add a global variable 
    79       
    80      @param string $name Name of the variable 
    81      @param mixed $value Value of the variable 
    82      */ 
    83      public function __set($name,$value) 
    84      { 
    85 /* 
    86           # Overload protect 
    87           if ($value === null && isset($this->globals[$name])) { 
    88                unset($this->globals[$name]); 
    89           } 
    90           elseif (!isset($this->globals[$name])) { 
    91                throw new Exception('Modification of overloaded globals has no effect'); 
    92           } 
    93 //*/ 
    94           $this->globals[$name] = $value; 
    95      } 
    96       
    97      /** 
    98      Get a global variable 
    99       
    100      @param string $name Name of the variable 
    101      @return mixed Value of the variable or null 
    102      */ 
    103      public function __get($name) 
    104      { 
    105           return isset($this->globals[$name]) ? $this->globals[$name] : null; 
    106      } 
    107       
    108      /** 
    109      Returns a list of filters to add to the existing list. 
    110       
    111      @return array An array of filters 
    112      */ 
    113      public function getFilters() 
    114      { 
    115           return array( 
    116                'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html'))) 
    117           ); 
    118      } 
    119       
    120      /** 
    121      Returns a list of functions to add to the existing list. 
    122       
    123      @return array An array of functions 
    124      */ 
    125      public function getFunctions() 
    126      { 
    127           return array( 
    128                '__'           => new Twig_Function_Function("__", array('is_safe' => array('html'))), 
    129                'debug_info' => new Twig_Function_Method($this, 'getDebugInfo', array('is_safe' => array('html'))), 
    130                'memorize' => new Twig_Function_Method($this, 'setMemory', array('is_safe' => array('html'))), 
    131                'memorized' => new Twig_Function_Method($this, 'getMemory', array('is_safe' => array('html'))) 
    132           ); 
    133      } 
    134       
    135      /** 
    136      Returns a list of global variables to add to the existing list. 
    137       
    138      This merges overloaded variables with defined variables. 
    139       
    140      @return array An array of global variables 
    141      */ 
     33          )); 
     34     } 
     35      
     36     /** 
     37      * Returns a list of global variables to add to the existing list. 
     38      * 
     39      * This merges overloaded variables with defined variables. 
     40      *  
     41      * @return array An array of global variables 
     42      */ 
    14243     public function getGlobals() 
    14344     { 
     
    16465      
    16566     /** 
    166      Returns the name of the extension. 
    167       
    168      @return string The extension name 
    169      */ 
    170      public function getName() 
    171      { 
    172           return 'AdminContext'; 
    173      } 
    174       
    175       
    176      /** 
    177      Add an informational message 
    178       
    179      @param string $message A message 
    180      @return object self 
    181      */ 
    182      public function setSafeMode($safe_mode) 
    183      { 
    184           $this->protected_globals['safe_mode'] = (boolean) $safe_mode; 
    185           return $this; 
    186      } 
    187       
    188      /** 
    189      Add an informational message 
    190       
    191      @param string $message A message 
    192      @return object self 
    193      */ 
    194      public function addMessageStatic($message) 
    195      { 
    196           $this->protected_globals['messages']['static'][] = $message; 
    197           return $this; 
    198      } 
    199       
    200      /** 
    201      Add a list of informational messages 
    202       
    203      @param string $message A title 
    204      @param array $message A list of messages 
    205      @return object self 
    206      */ 
    207      public function addMessagesList($title,$messages) 
    208      { 
    209           $this->protected_globals['messages']['lists'][$title] = $messages; 
    210           return $this; 
    211      } 
    212       
    213      /** 
    214      Set an important message 
    215       
    216      @param string $message A message 
    217      @return object self 
    218      */ 
    219      public function setAlert($message) 
    220      { 
    221           $this->protected_globals['messages']['alert'] = $message; 
    222           return $this; 
    223      } 
    224       
    225      /** 
    226      Add an error message 
    227       
    228      @param string Error message 
    229      @return object self 
    230      */ 
    231      public function addError($error) 
    232      { 
    233           $this->protected_globals['messages']['errors'][] = $error; 
    234           return $this; 
    235      } 
    236       
    237      /** 
    238      Check if there is an error message 
    239       
    240      @return boolean 
    241      */ 
    242      public function hasError() 
    243      { 
    244           return !empty($this->protected_globals['messages']['errors']); 
    245      } 
    246       
    247      /** 
    248      Fill the page title 
    249       
    250      $title can be:  
    251      a string for page title part or  
    252      TRUE to add blog name at the begining of title or 
    253      NULL to empty/reset title 
    254       
    255      @param mixed $title A title part 
    256      @param boolean $url Link of the title part 
    257      @return object self 
    258      */ 
     67      * Fill the page title. 
     68      * 
     69      * $title can be:  
     70      * - a string for page title part or  
     71      * - TRUE to add blog name at the begining of title or 
     72      * - NULL to empty/reset title 
     73      * 
     74      * @param mixed $title A title part 
     75      * @param boolean $url Link of the title part 
     76      * @return object self 
     77      */ 
    25978     public function fillPageTitle($title,$url='') 
    26079     { 
     
    27695      
    27796     /** 
    278      Check if a page title is set 
    279      */ 
     97      * Check if a page title is set 
     98      */ 
    28099     public function hasPageTitle() 
    281100     { 
     
    284103      
    285104     /** 
    286      Get list of blogs 
    287      */ 
     105      * Get list of blogs 
     106      */ 
    288107     protected function getBlogs() 
    289108     { 
     
    321140      
    322141     /** 
    323      Get current blog information 
    324      */ 
     142      * Get current blog information 
     143      */ 
    325144     protected function getCurrentBlog() 
    326145     { 
     
    346165      
    347166     /** 
    348      Get current user information 
    349      */ 
     167      * Get current user information 
     168      */ 
    350169     protected function getCurrentUser() 
    351170     { 
     
    395214      
    396215     /** 
    397      Get sidebar menus 
    398      */ 
     216      * Get sidebar menus 
     217      */ 
    399218     protected function getMenus() 
    400219     { 
     
    416235          } 
    417236     } 
    418       
    419      /** 
    420      Get an array of debug/dev infos 
    421      */ 
    422      public function getDebugInfo() 
    423      { 
    424           if (!DC_DEBUG) { 
    425                return array(); 
    426           } 
    427            
    428           $di = array( 
    429                'global_vars' => implode(', ',array_keys($GLOBALS)), 
    430                'memory' => array( 
    431                     'usage' => memory_get_usage(), 
    432                     'size' => files::size(memory_get_usage()) 
    433                ), 
    434                'xdebug' => array() 
    435           ); 
    436            
    437           if (function_exists('xdebug_get_profiler_filename')) { 
    438            
    439                $url = http::getSelfURI(); 
    440                $url .= strpos($url,'?') === false ? '?' : '&'; 
    441                $url .= 'XDEBUG_PROFILE'; 
    442                 
    443                $di['xdebug'] = array( 
    444                     'elapse_time' => xdebug_time_index(), 
    445                     'profiler_file' => xdebug_get_profiler_filename(), 
    446                     'profiler_url' =>  $url 
    447                ); 
    448                 
    449                /* xdebug configuration: 
    450                zend_extension = /.../xdebug.so 
    451                xdebug.auto_trace = On 
    452                xdebug.trace_format = 0 
    453                xdebug.trace_options = 1 
    454                xdebug.show_mem_delta = On 
    455                xdebug.profiler_enable = 0 
    456                xdebug.profiler_enable_trigger = 1 
    457                xdebug.profiler_output_dir = /tmp 
    458                xdebug.profiler_append = 0 
    459                xdebug.profiler_output_name = timestamp 
    460                */ 
    461           } 
    462            
    463           return $di; 
    464      } 
    465       
    466      /** 
    467      Add a value in a namespace memory 
    468       
    469      This help keep variable when recalling Twig macros 
    470       
    471      @param string $ns A namespace 
    472      @param string $str A value to memorize in this namespace 
    473      */ 
    474      public function setMemory($ns,$str) 
    475      { 
    476           if (!array_key_exists($ns,$this->memory) || !in_array($str,$this->memory[$ns])) { 
    477                $this->memory[$ns][] = $str; 
    478           } 
    479      } 
    480       
    481      /** 
    482      Check if a value is previously memorized in a namespace 
    483       
    484      @param string $ns A namespace 
    485      @param string $str A value to search in this namespace 
    486      @return array True if exists 
    487      */ 
    488      public function getMemory($ns,$str) 
    489      { 
    490           return array_key_exists($ns,$this->memory) && in_array($str,$this->memory[$ns]); 
    491      } 
    492237} 
    493238?> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map