File indexing completed on 2024-04-21 16:36:33

0001 <?php
0002 
0003 /**
0004  * Flooer Framework
0005  *
0006  * LICENSE: BSD License (2 Clause)
0007  *
0008  * @category    Flooer
0009  * @package     Flooer_View
0010  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0011  * @copyright   Akira Ohgaki
0012  * @license     https://opensource.org/licenses/BSD-2-Clause  BSD License (2 Clause)
0013  * @link        https://github.com/akiraohgaki/flooer
0014  */
0015 
0016 /**
0017  * Usage
0018  *
0019  * $view = new Flooer_View();
0020  * $view->setBaseDir('./views');
0021  * $view->setFile('view.phtml');
0022  * $view->key = $value;
0023  * echo $view->render();
0024  */
0025 
0026 /**
0027  * View class
0028  *
0029  * @category    Flooer
0030  * @package     Flooer_View
0031  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0032  */
0033 class Flooer_View
0034 {
0035 
0036     /**
0037      * Configuration options
0038      *
0039      * @var     array
0040      */
0041     protected $_config = array(
0042         'baseDir' => null,
0043         'file' => null
0044     );
0045 
0046     /**
0047      * Constructor
0048      *
0049      * @param   array $config
0050      * @return  void
0051      */
0052     public function __construct(array $config = null)
0053     {
0054         if ($config) {
0055             $this->_config = $config + $this->_config;
0056         }
0057     }
0058 
0059     /**
0060      * Magic method to set a property
0061      *
0062      * @param   string $key
0063      * @param   mixed $value
0064      * @return  void
0065      */
0066     public function __set($key, $value)
0067     {
0068         if ($key[0] != '_') {
0069             $this->$key = $value;
0070             return;
0071         }
0072         trigger_error(
0073             "Setting protected or private property ($key) is not allowed",
0074             E_USER_NOTICE
0075         );
0076     }
0077 
0078     /**
0079      * Magic method to get a property
0080      *
0081      * @param   string $key
0082      * @return  mixed
0083      */
0084     public function __get($key)
0085     {
0086         if ($key[0] != '_' && isset($this->$key)) {
0087             return $this->$key;
0088         }
0089         return null;
0090     }
0091 
0092     /**
0093      * Magic method to check a property
0094      *
0095      * @param   string $key
0096      * @return  bool
0097      */
0098     public function __isset($key)
0099     {
0100         if ($key[0] != '_') {
0101             return isset($this->$key);
0102         }
0103         return false;
0104     }
0105 
0106     /**
0107      * Magic method to unset a property
0108      *
0109      * @param   string $key
0110      * @return  void
0111      */
0112     public function __unset($key)
0113     {
0114         if ($key[0] != '_') {
0115             unset($this->$key);
0116         }
0117     }
0118 
0119     /**
0120      * Render a view
0121      *
0122      * @param   string $filename
0123      * @return  string
0124      */
0125     public function render($filename = null)
0126     {
0127         if (!$filename && $this->_config['file']) {
0128             $filename = $this->_config['file'];
0129         }
0130         if ($this->_config['baseDir']) {
0131             $filename = $this->_config['baseDir'] . '/' . $filename;
0132         }
0133         if (is_file($filename)) {
0134             ob_start();
0135             include $filename;
0136             $content = ob_get_contents();
0137             ob_end_clean();
0138             return $content;
0139         }
0140         trigger_error(
0141             "View script file ($filename) not found",
0142             E_USER_NOTICE
0143         );
0144     }
0145 
0146     /**
0147      * Set the path of a base directory
0148      *
0149      * @param   string $path
0150      * @return  void
0151      */
0152     public function setBaseDir($path)
0153     {
0154         $this->_config['baseDir'] = $path;
0155     }
0156 
0157     /**
0158      * Get the path of a base directory
0159      *
0160      * @return  string
0161      */
0162     public function getBaseDir()
0163     {
0164         return $this->_config['baseDir'];
0165     }
0166 
0167     /**
0168      * Set the filename of a view script
0169      *
0170      * @param   string $filename
0171      * @return  void
0172      */
0173     public function setFile($filename)
0174     {
0175         $this->_config['file'] = $filename;
0176     }
0177 
0178     /**
0179      * Get the filename of a view script
0180      *
0181      * @return  string
0182      */
0183     public function getFile()
0184     {
0185         return $this->_config['file'];
0186     }
0187 
0188 }