Dotclear

Changeset 1128:9e868a6c21c3


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.

Location:
inc
Files:
2 added
5 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?> 
  • inc/core/class.dc.core.php

    r1099 r1128  
    9797          $this->addFormater('wiki', array($this,'wikiTransform')); 
    9898           
    99           $this->loadTemplateEnvironment(); 
     99          $this->tpl = new dcTemplate(DC_TPL_CACHE,'$core->tpl',$this); 
    100100     } 
    101101      
     
    119119           
    120120          return new $c($this); 
    121      } 
    122       
    123      /** 
    124      Create template environment (Twig_Environment instance) 
    125       
    126      default-templates path must be added from admin|public/prepend.php with: 
    127      $core->tpl->getLoader()->addPath('PATH_TO/default-templates'); 
    128      Selected theme path must be added with: 
    129      $core->tpl->getLoader()->prependPath('PATH_TO/MY_THEME'); 
    130      */ 
    131      protected function loadTemplateEnvironment() 
    132      { 
    133           # If cache dir is writable, use it. 
    134           $cache_dir = path::real(DC_TPL_CACHE.'/twtpl',false); 
    135           if (!is_dir($cache_dir)) { 
    136                try { 
    137                     files::makeDir($cache_dir); 
    138                } catch (Exception $e) { 
    139                     $cache_dir = false; 
    140                } 
    141           } 
    142            
    143           $this->tpl = new Twig_Environment( 
    144                new Twig_Loader_Filesystem(dirname(__FILE__).'/../swf'), 
    145                array( 
    146                     'auto_reload' => true, 
    147                     'autoescape' => false, 
    148                     'base_template_class' => 'Twig_Template', 
    149                     'cache' => $cache_dir,  
    150                     'charset' => 'UTF-8', 
    151                     'debug' => DC_DEBUG, 
    152                     'optimizations' => -1, 
    153                     'strict_variables' => 0 //DC_DEBUG // Please fix undefined variables! 
    154                ) 
    155           ); 
    156           $this->tpl->addExtension(new dcFormExtension($this)); 
    157           $this->tpl->addExtension(new dcTabExtension($this)); 
    158121     } 
    159122      
  • inc/prepend.php

    r1106 r1128  
    4646$__autoload['dcWorkspace']              = dirname(__FILE__).'/core/class.dc.workspace.php'; 
    4747$__autoload['dcPrefs']                  = dirname(__FILE__).'/core/class.dc.prefs.php'; 
    48 $__autoload['dcTwigPage']               = dirname(__FILE__).'/core/class.dc.twig.page.php'; 
     48//$__autoload['dcTwigPage']             = dirname(__FILE__).'/core/class.dc.twig.page.php'; 
     49$__autoload['dcTemplate']               = dirname(__FILE__).'/core/class.dc.template.php'; 
     50$__autoload['dcContext']           = dirname(__FILE__).'/core/class.dc.context.php'; 
    4951 
    5052$__autoload['rsExtPost']                = dirname(__FILE__).'/core/class.dc.rs.extensions.php'; 
     
    6264$__autoload['adminUserList']            = dirname(__FILE__).'/admin/lib.pager.php'; 
    6365 
    64 $__autoload['dcTemplate']               = dirname(__FILE__).'/public/class.dc.template.php'; 
    65 $__autoload['context']                  = dirname(__FILE__).'/public/lib.tpl.context.php'; 
     66$__autoload['dcPublicContext']                    = dirname(__FILE__).'/public/class.dc.publiccontext.php'; 
     67//$__autoload['dcTemplate']             = dirname(__FILE__).'/public/class.dc.template.php'; 
     68//$__autoload['context']                = dirname(__FILE__).'/public/lib.tpl.context.php'; 
    6669$__autoload['dcUrlHandlers']            = dirname(__FILE__).'/public/lib.urlhandlers.php'; 
    6770$__autoload['dcForm']              = dirname(__FILE__).'/admin/class.dc.form.php'; 
  • inc/public/lib.urlhandlers.php

    r779 r1128  
    8686               $_ctx->nb_entry_per_page = $core->blog->settings->system->nb_post_per_page; 
    8787          } 
     88           
     89          // Break public template here for now  
     90          // just to check if template engine is well loaded. 
     91          $core->tpl->display($tpl.'.twig'); 
     92          // To be continued... 
    8893           
    8994          $tpl_file = $core->tpl->getFilePath($tpl); 
  • inc/public/prepend.php

    r921 r1128  
    5555} catch (Exception $e) {} 
    5656 
     57# Add public default templates path 
     58$core->tpl->getLoader()->addPath(dirname(__FILE__).'/default-templates'); 
     59# Set public context 
     60$_ctx = new dcPublicContext($core); 
     61$core->tpl->addExtension($_ctx); 
     62 
     63/* 
    5764# Creating template context 
    5865$_ctx = new context(); 
     
    6471          ,640); 
    6572} 
    66  
     73*/ 
    6774# Loading locales 
    6875$_lang = $core->blog->settings->system->lang; 
     
    120127 
    121128# --BEHAVIOR-- publicPrepend 
    122 $core->callBehavior('publicPrepend',$core); 
     129$core->callBehavior('publicPrepend',$core,$_ctx); 
    123130 
    124131# Prepare the HTTP cache thing 
     
    127134$mod_ts[] = $core->blog->upddt; 
    128135 
     136 
     137# Add parent theme path 
     138if ($__parent_theme && is_dir($core->blog->themes_path.'/'.$__parent_theme.'/tpl')) { 
     139     $core->tpl->getLoader()->addPath($core->blog->themes_path.'/'.$__parent_theme.'/tpl'); 
     140} 
     141# Add theme path at the begining of path list 
     142if (is_dir($core->blog->themes_path.'/'.$__theme.'/tpl')) { 
     143     $core->tpl->getLoader()->prependPath($core->blog->themes_path.'/'.$__theme.'/tpl'); 
     144} 
     145/* 
    129146$__theme_tpl_path = array( 
    130147     $core->blog->themes_path.'/'.$__theme.'/tpl' 
     
    138155     dirname(__FILE__).'/default-templates', 
    139156     $core->tpl->getPath()); 
    140  
     157*/ 
    141158$core->url->mode = $core->blog->settings->system->url_scan; 
    142159 
Note: See TracChangeset for help on using the changeset viewer.

Sites map