Completed
Push — master ( 6a4085...f391ee )
by James Ekow Abaka
09:32
created

Php::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 39 and the first side effect is on line 30.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 * Ntentan Framework
4
 * Copyright (c) 2010-2015 James Ekow Abaka Ainooson
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 * 
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 * 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
24
 */
25
26
namespace ntentan\honam\template_engines;
27
28
use ntentan\honam\TemplateEngine;
29
30
require_once 'php/functions.php';
31
32
/**
33
 * The PHP engine is a template engine built into honam which uses raw PHP as
34
 * the template language. By virtue of this, the PHP engine can boast of high
35
 * performance. Since this engine uses PHP, it has access to all the language's
36
 * features. This could sometimes create a problem since some of these features
37
 * are not intended for templating use.
38
 */
39
class Php extends TemplateEngine
40
{
41
    private $stringStreamRegistered = false;
0 ignored issues
show
Unused Code introduced by
The property $stringStreamRegistered is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
    
43
    public function generate($templateVariables)
44
    {
45
        // Escape each variable by passing it through the variable class.
46
        // Users would have to unescape them by calling the escape method directly
47
        // on the variable.
48
        foreach($templateVariables as $key => $value)
49
        {
50
            $$key = php\Variable::initialize($value);
51
        }
52
53
        // Expose helpers
54
        $helpers = $this->getHelpersLoader();   
55
56
        // Start trapping the output buffer and include the PHP template for
57
        // execution.
58
        ob_start();
59
        try{
60
            include $this->template;
61
        }
62
        catch(\Exception $e)
63
        {
64
            ob_get_flush();
65
            throw $e;
66
        }
67
        return ob_get_clean();
68
    }
69
70
    /**
71
     * A utility function to strip the text of all HTML code. This function
72
     * removes all HTML tags instead of escaping them.
73
     * 
74
     * @param string $text
75
     * @return string
76
     */
77
    public function strip($text)
78
    {
79
        return php\Janitor::cleanHtml($text, true);
80
    }
81
82
    /**
83
     * A utility function to cut long pieces of text into meaningful short
84
     * chunks. The function is very good in cases where you want to show just
85
     * a short preview snippet of a long text. The function cuts the long string
86
     * without cutting through words and appends some sort of ellipsis
87
     * terminator to the text.
88
     * 
89
     * @param string $text The text to be truncated.
90
     * @param string $length The maximum lenght of the truncated string. Might 
91
     *      return a shorter string if the lenght ends in the middle of a word.
92
     * @param string $terminator The ellipsis terminator to use for the text.
93
     * @return string
94
     */
95
    public function truncate($text, $length, $terminator = ' ...')
96
    {
97
        while(mb_substr($text, $length, 1) != ' ' && $length > 0)
98
        {
99
            $length--;
100
        }
101
        return mb_substr($text, 0, $length) . $terminator;
102
    }
103
104
    protected function generateFromString($string, $data)
105
    {
106
        \ntentan\utils\StringStream::register();
107
        file_put_contents('string://template', $string);
108
        $this->template = 'string://template';
109
        return $this->generate($data);
110
    }
111
    
112
    public function __destruct()
113
    {
114
        \ntentan\utils\StringStream::unregister();
115
    }
116
}
117
118