Completed
Push — master ( 02b122...2a8558 )
by James Ekow Abaka
02:43
created

Php   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 68
ccs 19
cts 19
cp 1
rs 10
c 2
b 0
f 0
wmc 9
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A strip() 0 3 1
A __destruct() 0 3 1
A generate() 0 22 3
A truncate() 0 6 3
A generateFromString() 0 6 1
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 40 and the first side effect is on line 31.

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
/*
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\template_engines;
28
29
use ntentan\honam\TemplateEngine;
30
31 1
require_once 'php/functions.php';
32
33
/**
34
 * The PHP engine is a template engine built into honam which uses raw PHP as
35
 * the template language. By virtue of this, the PHP engine can boast of high
36
 * performance. Since this engine uses PHP, it has access to all the language's
37
 * features. This could sometimes create a problem since some of these features
38
 * are not intended for templating use.
39
 */
40
class Php extends TemplateEngine {
41
42 31
    public function generate($templateVariables) {
43
        // Escape each variable by passing it through the variable class.
44
        // Users would have to unescape them by calling the escape method directly
45
        // on the variable.
46 31
        foreach ($templateVariables as $_key => $_value) {
47 25
            $$_key = php\Variable::initialize($_value);
48
        }
49
50
        // Expose helpers
51 31
        $helpers = $this->getHelpersLoader();
52
53
        // Start trapping the output buffer and include the PHP template for
54
        // execution.
55 31
        ob_start();
56
        try {
57 31
            include $this->template;
58 2
        } catch (\Exception $e) {
59 2
            ob_get_flush();
60 2
            throw $e;
61
        }
62 29
        return ob_get_clean();
63
    }
64
65
    /**
66
     * A utility function to strip the text of all HTML code. This function
67
     * removes all HTML tags instead of escaping them.
68
     * 
69
     * @param string $text
70
     * @return string
71
     */
72 1
    public function strip($text) {
73 1
        return php\Janitor::cleanHtml($text, true);
74
    }
75
76
    /**
77
     * A utility function to cut long pieces of text into meaningful short
78
     * chunks. The function is very good in cases where you want to show just
79
     * a short preview snippet of a long text. The function cuts the long string
80
     * without cutting through words and appends some sort of ellipsis
81
     * terminator to the text.
82
     * 
83
     * @param string $text The text to be truncated.
84
     * @param string $length The maximum lenght of the truncated string. Might 
85
     *      return a shorter string if the lenght ends in the middle of a word.
86
     * @param string $terminator The ellipsis terminator to use for the text.
87
     * @return string
88
     */
89 1
    public function truncate($text, $length, $terminator = ' ...') {
90 1
        while (mb_substr($text, $length, 1) != ' ' && $length > 0) {
91 1
            $length--;
92
        }
93 1
        return mb_substr($text, 0, $length) . $terminator;
94
    }
95
96
    protected function generateFromString($string, $data) {
97
        \ntentan\utils\StringStream::register();
98
        file_put_contents('string://template', $string);
99
        $this->template = 'string://template';
100
        return $this->generate($data);
101
    }
102
103 2
    public function __destruct() {
104 2
        \ntentan\utils\StringStream::unregister();
105 2
    }
106
107
}
108