Completed
Push — dev ( 48451b...8a2d43 )
by James Ekow Abaka
08:16
created

PhpEngine::strip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 ntentan\honam\engines\php\HelperFactory;
30
use ntentan\honam\engines\php\Janitor;
31
use ntentan\honam\TemplateEngine;
32
use ntentan\honam\engines\php\Variable;
33
use ntentan\utils\StringStream;
34
35
/**
36
 * The PHP engine is a template engine built into honam which uses raw PHP as
37
 * the template language. By virtue of this, the PHP engine can boast of high
38
 * performance. Since this engine uses PHP, it has access to all the language's
39
 * features. This could sometimes create a problem since some of these features
40
 * are not intended for templating use.
41
 */
42
class PhpEngine extends AbstractEngine
43
{
44
    private $helpersLoader;
45
    private $janitor;
46
47
    public function __construct(HelperFactory $helpersLoader, Janitor $janitor)
0 ignored issues
show
Unused Code introduced by
The parameter $janitor 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...
48
    {
49
        $this->helpersLoader = $helpersLoader;
50
    }
51
52
    function partial($template, $templateData = array())
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
53
    {
54
        return $this->templateRenderer->render($template, $templateData);
55
    }
56
57
    function unescape($item)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
58
    {
59
        if($item instanceof ntentan\honam\template_engines\php\Variable) {
0 ignored issues
show
Bug introduced by
The class ntentan\honam\engines\nt...te_engines\php\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
60
            return $item->unescape();
61
        } else {
62
            return $item;
63
        }
64
    }
65
66
    /**
67
     * Passes the data to be rendered to the template engine instance.
68
     */
69
    public function renderFromFileTemplate(string $filePath, array $data) : string
70
    {
71
        // Escape each variable by passing it through the variable class.
72
        // Users would have to unescape them by calling the escape method directly
73
        // on the variable.
74
        foreach ($data as $_key => $_value) {
75
            $$_key = Variable::initialize($_value);
76
        }
77
78
        // Expose helpers
79
        $helpers = $this->helpersLoader;
80
81
        // Start trapping the output buffer and include the PHP template for
82
        // execution.
83
        ob_start();
84
        try {
85
            include $filePath;
86
        } catch (\Exception $e) {
87
            ob_get_flush();
88
            throw $e;
89
        }
90
        return ob_get_clean();
91
    }
92
93
    /**
94
     * Passes a template string and data to be rendered to the template engine
95
     * instance.
96
     */
97
    public function renderFromStringTemplate(string $string, array $data) : string
98
    {
99
        StringStream::register();
100
        file_put_contents('string://template', $string);
101
        return $this->renderFromFileTemplate('string://template', $data);
102
    }
103
104
//    public function generate($templateVariables)
105
//    {
106
//        // Escape each variable by passing it through the variable class.
107
//        // Users would have to unescape them by calling the escape method directly
108
//        // on the variable.
109
//        foreach ($templateVariables as $_key => $_value) {
110
//            $$_key = php\Variable::initialize($_value);
111
//        }
112
//
113
//        // Expose helpers
114
//        $helpers = $this->getHelpersLoader();
115
//
116
//        // Start trapping the output buffer and include the PHP template for
117
//        // execution.
118
//        ob_start();
119
//        try {
120
//            include $this->template;
121
//        } catch (\Exception $e) {
122
//            ob_get_flush();
123
//            throw $e;
124
//        }
125
//        return ob_get_clean();
126
//    }
127
//
128
    /**
129
     * A utility function to strip the text of all HTML code. This function
130
     * removes all HTML tags instead of escaping them.
131
     *
132
     * @param string $text
133
     * @return string
134
     */
135
    public function strip($text) {
136
        return php\Janitor::cleanHtml($text, true);
137
    }
138
139
    /**
140
     * A utility function to cut long pieces of text into meaningful short
141
     * chunks. The function is very good in cases where you want to show just
142
     * a short preview snippet of a long text. The function cuts the long string
143
     * without cutting through words and appends some sort of ellipsis
144
     * terminator to the text.
145
     *
146
     * @param string $text The text to be truncated.
147
     * @param string $length The maximum lenght of the truncated string. Might
148
     *      return a shorter string if the lenght ends in the middle of a word.
149
     * @param string $terminator The ellipsis terminator to use for the text.
150
     * @return string
151
     */
152
    public function truncate($text, $length, $terminator = ' ...') {
153
        while (mb_substr($text, $length, 1) != ' ' && $length > 0) {
154
            $length--;
155
        }
156
        return mb_substr($text, 0, $length) . $terminator;
157
    }
158
//
159
//    protected function generateFromString($string, $data) {
160
//        \ntentan\utils\StringStream::register();
161
//        file_put_contents('string://template', $string);
162
//        $this->template = 'string://template';
163
//        return $this->generate($data);
164
//    }
165
//
166
    public function __destruct() {
167
        StringStream::unregister();
168
    }
169
170
}
171