Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2.  
  3. namespace core;
  4.  
  5. abstract class Controller{
  6.    
  7.     public $pageLayout = '/layout/main';
  8.     public $pageTitle = '';
  9.     public $pageMeta = [];
  10.     public $pageParam = [];
  11.    
  12.     public function run($action, $params){
  13.         $vars = [];
  14.         $ref = new \ReflectionMethod($this, $action);
  15.         foreach($ref->getParameters() as $param){
  16.             $name=$param->getName();
  17.             if(isset($params[$name]))
  18.             {
  19.                 if($param->isArray())
  20.                     $vars[]=is_array($params[$name]) ? $params[$name] : array($params[$name]);
  21.                 elseif(!is_array($params[$name]))
  22.                     $vars[]=$params[$name];
  23.             }
  24.             elseif($param->isDefaultValueAvailable())
  25.                 $vars[]=$param->getDefaultValue();
  26.         }
  27.         call_user_func_array([$this, $action], $vars);
  28.     }
  29.    
  30.     public function render($file, $vars = null, $return = false){
  31.         $content = $this->renderPartial($file, $vars, true);
  32.         $this->renderPartial($this->pageLayout, ['content'=>$content]);
  33.     }
  34.  
  35.     private $_viewFolder;
  36.     public function renderPartial($file, $vars = null, $return = false){
  37.         if(str_start($file, '/')){
  38.             // Если указана папка то запоминаем её
  39.             $a = explode('/', trim($file, '/'));
  40.             $oldfolder = $this->_viewFolder;
  41.             $this->_viewFolder = $a[0];
  42.             $file = DIR_VIEWS.$file.'.php';
  43.             $output = $this->renderInternal($file, $vars, $return);
  44.             $this->_viewFolder = $oldfolder;
  45.         }else{
  46.             if(!$this->_viewFolder){
  47.                 // По умолчанию папка по имени контролера
  48.                 $class = get_called_class();
  49.                 $this->_viewFolder = strtolower(substr($class, 11, strlen($class) - 21));
  50.             }
  51.             $file = $this->_viewFolder.'/'.$file;
  52.             $file = DIR_VIEWS.$file.'.php';
  53.             $output = $this->renderInternal($file, $vars, $return);
  54.         }
  55.         return $output;
  56.     }
  57.    
  58.     private function renderInternal($_file, $_vars = null, $_return = false){
  59.         if(is_array($_vars) && !empty($_vars))
  60.             extract($_vars);
  61.         extract(['this' => $this]);
  62.         if($_return){
  63.             ob_start();
  64.             ob_implicit_flush(false);
  65.             require($_file);
  66.             return ob_get_clean();
  67.         }else{
  68.             require($_file);
  69.             return true;
  70.         }
  71.     }
  72.    
  73.     public function renderJson($data = []){
  74.         header('Content-Type: application/json');
  75.         echo json_encode($data);
  76.         $this->end();
  77.     }
  78.  
  79.     public function renderJsonError($err_code, $msg = '', $data = []){
  80.         header('Content-Type: application/json');
  81.         header($_SERVER['SERVER_PROTOCOL'].' '.$err_code.' '.$msg, true, $err_code);
  82.         echo json_encode($data);
  83.         $this->end();
  84.     }
  85.  
  86.     public function redirect($url){
  87.         header('Location: '.$url);
  88.         $this->end();
  89.     }
  90.    
  91.     public function end(){
  92.         App::instance()->end();
  93.     }
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement