Advertisement
stixlink

framework-view

Oct 27th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by Stixlink.
  4.  * E-mail: stixlink@gmail.com
  5.  * Skype: stixlink
  6.  * Date: 17.01.16
  7.  * Time: 23:32
  8.  */
  9.  
  10. namespace core;
  11.  
  12.  
  13. class View {
  14.     public $titlePage;
  15.  
  16.     private $_layouts = "main";
  17.     private $_controller;
  18.     private $_defaultDir = "views";
  19.     private $_layoutsDir = "layouts";
  20.     private $_extensionsFile = "php";
  21.  
  22.     public function __construct($controller) {
  23.  
  24.         $this->_controller = $controller;
  25.     }
  26.  
  27.     /**
  28.      *
  29.      * @return mixed
  30.      */
  31.     public function getController() {
  32.  
  33.         return $this->_controller;
  34.     }
  35.  
  36.     public function render($view, $params, $inLayout = true, $return = false) {
  37.  
  38.         $path = $this->getViewPath($view);
  39.         $content = $this->getViewContent($path, $params);
  40.         if ($inLayout) {
  41.             $content = $this->getViewContent($this->getLayoutsPath(),
  42.                 [
  43.                     'content' => $content,
  44.                     'titlePage' => $this->titlePage
  45.                 ]
  46.             );
  47.         }
  48.         if ($return) {
  49.             return $content;
  50.         } else {
  51.             echo $content;
  52.             exit();
  53.         }
  54.     }
  55.  
  56.     /**
  57.      * @param $view
  58.      *
  59.      * @return string
  60.      * @throws \Exception
  61.      */
  62.     public function getViewPath($view) {
  63.  
  64.         $path = BASE_PATH . DS . APP_DIR . DS . $this->_defaultDir . DS . ($this->_controller->getId() . DS . $view . "." . $this->_extensionsFile);
  65.         if (file_exists($path)) {
  66.             return $path;
  67.         }
  68.         throw new  \Exception('Not exist view file "' . $path . '"');
  69.     }
  70.  
  71.     /**
  72.      * @param      $pathViewFile
  73.      * @param null $data
  74.      * @param bool $return
  75.      *
  76.      * @return string
  77.      */
  78.     protected function getViewContent($pathViewFile, $data = null, $return = true) {
  79.  
  80.         if (is_array($data)) {
  81.             extract($data, EXTR_PREFIX_SAME, 'data');
  82.         }
  83.         if ($return) {
  84.             ob_start();
  85.             ob_implicit_flush(false);
  86.             require($pathViewFile);
  87.  
  88.             return ob_get_clean();
  89.         } else {
  90.             require($pathViewFile);
  91.         }
  92.     }
  93.  
  94.     public function getLayoutsPath() {
  95.  
  96.         $path = BASE_PATH . DS . APP_DIR . DS . $this->_defaultDir . DS . $this->_layoutsDir . DS . $this->getLayoutsName() . "." . $this->_extensionsFile;
  97.         if (file_exists($path)) {
  98.             return $path;
  99.         }
  100.         throw new  \Exception('Not exist layout file "' . $path . '"');
  101.     }
  102.  
  103.     /**
  104.      * @return string
  105.      */
  106.     public function getLayoutsName() {
  107.  
  108.         return $this->_layouts;
  109.     }
  110.  
  111.     /**
  112.      * @param $value
  113.      */
  114.     public function setLayoutsName($value) {
  115.  
  116.         $this->_layouts = $value;
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement