Axiom (v1.2.0)

A lightweight PHP framework
H:/Workspace/php-axiom/www/php-axiom/libraries/axiom/axViewManager.class.php
Go to the documentation of this file.
00001 <?php
00017 class axViewManager {
00018     
00023     protected $_viewPaths;
00024     
00029     protected $_outputCallback;
00030     
00035     protected $_layout;
00036     
00041     protected $_defaultFormat;
00042     
00047     protected $_layoutVars;
00048     
00056     public function __construct ($layout, $view_paths = null, $default_format = 'html', array $layout_vars = array()) {
00057         $this->_viewPaths      = (array)$view_paths;
00058         $this->_outputCallback = false;
00059         $this->_layoutVars     = $layout_vars;
00060         $this->_layout         = $layout ? $layout : false;
00061         $this->_defaultFormat  = $default_format;
00062     }
00063     
00071     public function add ($view) {
00072         if (!$this->_viewPaths[] = realpath($view));
00073             throw new axMissingFileException($view);
00074         
00075         return $this;
00076     }
00077     
00101     public function load () {
00102         $args = func_get_args();
00103         
00104         if (!count($args)) {
00105             throw new InvalidArgumentException("No argument provided");
00106         }
00107         elseif (count($args) == 1 && $args[0] instanceof axResponse) {
00108             
00114             $response = $args[0];
00115             $view     = $response->getView();
00116             $section  = $response->getViewSection();
00117             $format   = $response->getFormat() ? $response->getFormat() : $this->_defaultFormat;
00118             $vars     = $response->getVars();
00119             
00121             
00122             if (!$response->layoutState()) {
00123                 $layout = false;
00124             }
00125             elseif ($response->getLayout() !== null && $response->getLayout() != $this->_layout) {
00126                 $layout = $response->getLayout();
00127             }
00128             else {
00129                 $layout = $this->_layout;
00130             }
00131             
00132             if (!is_file($view_path = $this->_findView($section, $view, $format)))
00133                 throw new RuntimeException("Unable to find view {$section}/{$view} with {$format} format");
00134         }
00135         elseif ((count($args) == 2 && is_array($args[1])) || count($args) == 1) {
00136             
00142             list($view_path,$vars,$layout,$format) = $args + array('', array(), $this->_layout, $this->_defaultFormat);
00143             
00144             if (!is_file($view_path))
00145                 throw new RuntimeException("Unable to find view {$view_path}");
00146         }
00147         else {
00148             
00154             list($section,$view,$format,$vars,$layout) = $args + array(
00155                 '', '', $this->_defaultFormat, array(),$this->_layout
00156             );
00157             
00158             if (!is_file($view_path = $this->_findView($section, $view, $format)))
00159                 throw new RuntimeException("Unable to find view {$section}/{$view} with {$format} format");
00160         }
00161         
00162         if ($layout && !is_file($layout) && !$layout = $this->_findLayout($layout, $format))
00163             throw new RuntimeException("Unable to find layout {$layout} with format {$format}");
00164         
00165         if (!$this->setOutputFormat($format))
00166             throw new RuntimeException("Unable to set format to {$format}");
00167         
00169         
00170         if (!${'view_content'} = $this->_loadView($view_path, $vars))
00171             throw new RuntimeException("Unable to load view {$view_path}");
00172             
00173         if ($layout) {
00174             return $this->_loadLayout($layout, $vars + array('view_content' => ${'view_content'}));
00175         }
00176         else {
00177             return ${'view_content'};
00178         }
00179     }
00180     
00186     public function setOutputCallback ($callback) {
00187         if (!is_callable($this->_outputCallback = $callback))
00188             return false;
00189         
00190         return $this;
00191     }
00192     
00198     public function setOutputFormat ($format) {
00199         switch (strtolower($format)) {
00200             case 'html': header('Content-Type: text/html; charset=UTF-8'); break;
00201             case 'json': header('Content-Type: application/json; charset=UTF-8'); break;
00202             case 'csv' : header('Content-Type: application/csv; charset=UTF-8'); break;
00203             case 'xml' : header('Content-Type: text/xml; charset=UTF-8'); break;
00204             case 'text': header('Content-Type: text/plain; charset=UTF-8'); break;
00205             default: return false;
00206         }
00207         
00208         return $this;
00209     }
00210     
00218     public function setLayout ($layout, $format = null) {
00219         $this->_layout = $layout;
00220         return $this;
00221     }
00222     
00228     public function getVar ($name) {
00229         return isset($this->_layoutVars[$name]) ? $this->_layoutVars[$name] : null;
00230     }
00231     
00238     public function setVar ($name, $value) {
00239         $this->_layoutVars[$name] = $value;
00240         return $this;
00241     }
00242     
00249     public function addAll (array $vars, $method = self::MERGE_VARS) {
00250         switch ($method) {
00251             case self::MERGE_VARS:
00252                 $this->_layoutVars = array_merge($this->_layoutVars, $vars);
00253                 break;
00254             case self::ADD_VARS:
00255                 $this->_layoutVars += $vars;
00256                 break;
00257             default:
00258                 return false;
00259         }
00260         return $this;
00261     }
00262     
00270     public function __get ($key) {
00271         return $this->getVar($key);
00272     }
00273     
00282     public function __set ($key, $value) {
00283         $this->setVar($key, $value);
00284     }
00285     
00292     public function __isset ($key) {
00293         return isset($this->_layoutVars[$key]);
00294     }
00295     
00302     public function __unset ($key) {
00303         unset($this->_layoutVars[$key]);
00304     }
00305     
00315     protected function _findLayout ($layout, $format) {
00316         if (!$format || !$layout)
00317             return false;
00318             
00319         $format = strtolower($format);
00320         
00321         foreach ($this->_viewPaths as $view_path) {
00322             if (is_file($path = $view_path . "/layouts/{$layout}.{$format}.php"))
00323                 return $path;
00324         }
00325         return false;
00326     }
00327     
00338     protected function _findView ($section, $view, $format) {
00339         if (!$section || !$view || !$format)
00340             return false;
00341         
00342         $format = strtolower($format);
00343             
00344         foreach ($this->_viewPaths as $view_path) {
00345             if (is_file($path = $view_path . "/{$section}/{$view}.{$format}.php"))
00346                 return $path;
00347         }
00348         return false;
00349     }
00350     
00357     protected function _loadView ($__path, array $__vars = array()) {
00358         if (!ob_start())
00359             return false;
00360         
00361         extract($this->_layoutVars, EXTR_PREFIX_ALL, 'layout');
00362         extract($__vars);
00363         
00364         if (!include $__path)
00365             return false;
00366         
00367         return ob_get_clean();
00368     }
00369     
00376     protected function _loadLayout ($__path, array $__vars = array()) {
00377         if (!ob_start())
00378             return false;
00379             
00380         extract($this->_layoutVars, EXTR_PREFIX_ALL, 'layout');
00381         extract($__vars);
00382         
00383         if (!include $__path)
00384             return false;
00385         
00386         ${'layout_content'} = ob_get_clean();
00387         return ${'layout_content'};
00388     }
00389     
00395     const MERGE_VARS = "merge";
00396     const ADD_VARS   = "add";
00397 }
 All Data Structures Files Functions Variables