PHP::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * View\PHP
5
 *
6
 * Core\View PHP templates bridge.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.com
11
 */
12
13
namespace View;
14
15
class PHP implements Adapter {
16
17
  const EXTENSION = '.php';
18
19
  protected static $templatePath,
20
                   $globals = [];
21
22
  public function __construct($path=null,$options=[]){
23
      self::$templatePath = ($path ? rtrim($path,'/') : __DIR__) . '/';
24
  }
25
26
  public static function exists($path){
27
      return is_file(self::$templatePath . $path . static::EXTENSION);
28
  }
29
30
  public static function addGlobal($key,$val){
31
    self::$globals[$key] = $val;
32
  }
33
34
  public static function addGlobals(array $defs){
35
    foreach ((array)$defs as $key=>$val) {
36
        self::$globals[$key] = $val;
37
    }
38
  }
39
40
  public function render($template, $data=[]){
41
      $template_path = self::$templatePath . trim($template,'/') . static::EXTENSION;
42
      $sandbox = function() use ($template_path){
43
          ob_start();
44
          include($template_path);
45
          $__buffer__ = ob_get_contents();
46
          ob_end_clean();
47
          return $__buffer__;
48
      };
49
      return call_user_func($sandbox->bindTo(new PHPContext(
50
          array_merge(self::$globals, $data),
51
          self::$templatePath
52
      )));
53
  }
54
}
55
56
class PHPContext {
57
  protected $data = [];
58
59
  public function __construct($data=[], $path=null){
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
      $this->data = $data;
61
  }
62
63
  public function partial($template, $vars=[]){
64
      return \View::from($template,array_merge($this->data,$vars));
65
  }
66
67
  public function __isset($n){ return true; }
68
69
  public function __unset($n){}
70
71
  public function __get($n){
72
    return empty($this->data[$n]) ? '' : $this->data[$n];
73
  }
74
}
75