Completed
Push — master ( d34ae1...d2cd4a )
by kill
10:24
created

Controller::show()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace puck;
3
4
class Controller
5
{
6
7
    protected $viewPath = '';
8
    private $twig;
9
    private $tVar = array();
10
    protected $title='';
11
    public function __construct()
12
    {
13
        $loader = new \Twig_Loader_Filesystem(BASE_PATH . $this->viewPath);
14
        $twig = new \Twig_Environment($loader, array(
15
            'debug' => DEBUG,
16
            'cache' => BASE_PATH . '/cache',
17
        ));
18
        $this->twig = $twig;
19
        $this->initTwigFilter();
20
        $this->initTwigFunction();
21
        $this->db = \MysqliDb::getInstance();
0 ignored issues
show
Bug introduced by
The property db does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
    private function initTwigFilter(){
24
        $filter = new \Twig_SimpleFilter('long2ip', 'long2ip');
25
        $this->twig->addFilter($filter);
26
    }
27
    private function initTwigFunction(){
28
        $function = new \Twig_SimpleFunction('I','I');
29
        $this->twig->addFunction($function);
30
    }
31
    protected function assign($name, $value = '')
32
    {
33
        if (is_array($name)) {
34
            $this->tVar = array_merge($this->tVar, $name);
35
        } else {
36
            $this->tVar[$name] = $value;
37
        }
38
    }
39
40
    protected function show($tmpPath = '')
41
    {
42
        if ($tmpPath == '') {
43
            if (defined("CONTROLLER_NAME") && defined("ACTION_NAME")) {
44
                $tmpPath = CONTROLLER_NAME . '/' . ACTION_NAME;
45
            } else {
46
                show_json($this->tVar);
47
            }
48
        }
49
        header('Content-Type:text/html; charset=utf-8');
50
        header('Cache-control: private'); // 页面缓存控制
51
        header('X-Powered-By:ViviAnAuthSystem');
52
        $this->assign('title',$this->title);
53
        echo $this->twig->render($tmpPath . '.' . TempExt, $this->tVar);
54
        die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method show() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
55
    }
56
}