PhpEngine   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 118
rs 10
c 2
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A truncate() 0 6 3
A renderFromStringTemplate() 0 11 2
A __construct() 0 5 1
A partial() 0 3 1
A __destruct() 0 3 1
A renderFromFileTemplate() 0 22 3
A strip() 0 3 1
A unescape() 0 6 2
1
<?php
2
3
/*
4
 * Ntentan Framework
5
 * Copyright (c) 2010-2015 James Ekow Abaka Ainooson
6
 * 
7
 * Permission is hereby granted, free of charge, to any person obtaining
8
 * a copy of this software and associated documentation files (the
9
 * "Software"), to deal in the Software without restriction, including
10
 * without limitation the rights to use, copy, modify, merge, publish,
11
 * distribute, sublicense, and/or sell copies of the Software, and to
12
 * permit persons to whom the Software is furnished to do so, subject to
13
 * the following conditions:
14
 * 
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 * 
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
25
 */
26
27
namespace ntentan\honam\engines;
28
29
use Exception;
30
use ntentan\honam\engines\php\HelperVariable;
31
use ntentan\honam\engines\php\Janitor;
32
use ntentan\honam\engines\php\Variable;
33
use ntentan\honam\TemplateRenderer;
34
use ntentan\utils\StringStream;
35
36
/**
37
 * The PHP engine is a template engine built into honam which uses raw PHP as
38
 * the template language. By virtue of this, the PHP engine can boast of high
39
 * performance. Since this engine uses PHP, it has access to all the language's
40
 * features. This could sometimes create a problem since some of these features
41
 * are not intended for templating use.
42
 */
43
class PhpEngine extends AbstractEngine
44
{
45
    /**
46
     * @var HelperVariable
47
     */
48
    private $helperVariable;
49
50
    /**
51
     * @var Janitor
52
     */
53
    private $janitor;
54
    private $templateRenderer;
55
56
    public function __construct(TemplateRenderer $templateRenderer, HelperVariable $helperVariable, Janitor $janitor)
57
    {
58
        $this->helperVariable = $helperVariable;
59
        $this->janitor = $janitor;
60
        $this->templateRenderer = $templateRenderer;
61
    }
62
63
    public function partial($template, $templateData = array())
64
    {
65
        return $this->templateRenderer->render($template, $templateData);
66
    }
67
68
    /**
69
     * @param mixed $item
70
     * @return mixed
71
     */
72
    public function unescape($item)
73
    {
74
        if($item instanceof Variable) {
75
            return $item->unescape();
76
        } else {
77
            return $item;
78
        }
79
    }
80
81
    /**
82
     * Passes the data to be rendered to the template engine instance.
83
     */
84
    public function renderFromFileTemplate(string $_filePath, array $_data) : string
85
    {
86
        // Escape each variable by passing it through the variable class.
87
        // Users would have to unescape them by calling the escape method directly
88
        // on the variable.
89
        foreach ($_data as $_key => $_value) {
90
            $$_key = Variable::initialize($_value, $this->janitor);
91
        }
92
93
        // Expose helpers
94
        $helpers = $this->helperVariable;
95
96
        // Start trapping the output buffer and include the PHP template for
97
        // execution.
98
        ob_start();
99
        try {
100
            include $_filePath;
101
        } catch (Exception $e) {
102
            ob_get_flush();
103
            throw $e;
104
        }
105
        return ob_get_clean();
106
    }
107
108
    /**
109
     * Passes a template string and data to be rendered to the template engine
110
     * instance.
111
     */
112
    public function renderFromStringTemplate(string $string, array $data) : string
113
    {
114
        foreach ($data as $_key => $_value) {
115
            $$_key = Variable::initialize($_value, $this->janitor);
116
        }
117
        // Expose helpers
118
        $helpers = $this->helperVariable;
119
        
120
        ob_start();
121
        eval(" ?> $string");
122
        return ob_get_clean();
123
    }
124
125
    /**
126
     * A utility function to strip the text of all HTML code. This function
127
     * removes all HTML tags instead of escaping them.
128
     *
129
     * @param string $text
130
     * @return string
131
     */
132
    public function strip($text)
133
    {
134
        return $this->janitor->cleanHtml($text, true);
135
    }
136
137
    /**
138
     * A utility function to cut long pieces of text into meaningful short
139
     * chunks. The function is very good in cases where you want to show just
140
     * a short preview snippet of a long text. The function cuts the long string
141
     * without cutting through words and appends some sort of ellipsis
142
     * terminator to the text.
143
     *
144
     * @param string $text The text to be truncated.
145
     * @param integer $length The maximum lenght of the truncated string. Might
146
     *      return a shorter string if the lenght ends in the middle of a word.
147
     * @param string $terminator The ellipsis terminator to 6use for the text.
148
     * @return string
149
     */
150
    public function truncate($text, $length, $terminator = ' ...')
151
    {
152
        while (mb_substr($text, $length, 1) != ' ' && $length > 0) {
153
            $length--;
154
        }
155
        return mb_substr($text, 0, $length) . $terminator;
156
    }
157
158
    public function __destruct()
159
    {
160
        StringStream::unregister();
161
    }
162
163
}
164