Advertisement
nsetyo

Tes

May 25th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2. /**
  3.  * -----------------------------------------------------------------------------
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * @author     Setyo Nugroho < email@nsetyo.net >
  8.  * @copyright  2014 Setyo Nugroho
  9.  * @license    The MIT License (MIT), see LISENSI.txt
  10.  * @link       https://github.com/nsetyo/tulang
  11.  *
  12.  * -----------------------------------------------------------------------------
  13.  */
  14. namespace Bones;
  15.  
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Bones\Helpers\Url;
  18.  
  19. /**
  20.  * -----------------------------------------------------------------------------
  21.  * Framework output control
  22.  *
  23.  * -----------------------------------------------------------------------------
  24.  */
  25. class View
  26. {
  27.     protected $assets_dir;
  28.     protected $assets_url;
  29.     protected $variables = array();
  30.     /**
  31.      * -------------------------------------------------------------------------
  32.      * Construct
  33.      * @param string $assets_dir Assets directory path
  34.      * @param string $assets_url Assets base url
  35.      *
  36.      * -------------------------------------------------------------------------
  37.      */
  38.     public function __construct($assets_dir, $assets_url)
  39.     {
  40.         $this->assets_dir = realpath($assets_dir);
  41.         $this->assets_url = Url::getBaseUrl() . trim($assets_url, "/");
  42.     }
  43.  
  44.     /**
  45.      * -------------------------------------------------------------------------
  46.      * Store variable
  47.      * @param array $variables Variable to store
  48.      *
  49.      * -------------------------------------------------------------------------
  50.      */
  51.     public function setVar(array $variables)
  52.     {
  53.         $this->variables = array_merge($this->variables, $variables);
  54.     }
  55.  
  56.     /**
  57.      * -------------------------------------------------------------------------
  58.      * Render output with setted variable
  59.      * @param  string   $template Template filename without extention
  60.      * @param  integer  $status   The response status code
  61.      * @return Response Response instance
  62.      *
  63.      * -------------------------------------------------------------------------
  64.      */
  65.     public function render($template, $status = 200)
  66.     {
  67.         ob_start();
  68.         extract($this->variables);
  69.  
  70.         $content_buf = require $this->assets_dir . '/' . $template . '.php';
  71.         $content_var = ob_get_clean();
  72.         $response    = new Response($content_var, (int) $status);
  73.  
  74.         return $response;
  75.     }
  76.  
  77.     /**
  78.      * -------------------------------------------------------------------------
  79.      * Return the assets file url
  80.      * @param  string $assets_file The assets file
  81.      * @return string The assets file URL
  82.      *
  83.      * -------------------------------------------------------------------------
  84.      */
  85.     public function assetsUrl($assets_file)
  86.     {  
  87.         $assets_file = "/" . ltrim($assets_file, "/");
  88.         return $this->assets_url . $assets_file;
  89.     }
  90.  
  91.     /**
  92.      * -------------------------------------------------------------------------
  93.      * Alias of assetsUrl
  94.      * @param  string $assets_file The assets file
  95.      * @return string The assets file URL
  96.      *
  97.      * -------------------------------------------------------------------------
  98.      */
  99.     public function assets($assets_url)
  100.     {
  101.         return $this->assetsUrl($assets_url);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement