Passed
Push — master ( f54e31...ad3cee )
by Thierry
09:31 queued 04:37
created

Engine   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A pagination() 0 4 1
A _render() 0 19 2
A render() 0 27 4
A addNamespace() 0 13 3
1
<?php
2
3
/**
4
 * Template.php - Template engine
5
 *
6
 * Generate templates with template vars.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Utils\Template;
16
17
use stdClass;
18
19
class Engine
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Engine
Loading history...
20
{
21
    /**
22
     * The namespaces
23
     *
24
     * @var array   $aNamespaces
25
     */
26
    protected $aNamespaces;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
27
28
    /**
29
     * The constructor
30
     *
31
     * @param   string      $sTemplateDir       The template directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 6 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 7 found
Loading history...
32
     */
33
    public function __construct($sTemplateDir)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
34
    {
35
        $sTemplateDir = rtrim(trim($sTemplateDir), '/\\');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
Consider using a different name than the parameter $sTemplateDir. This often makes code more readable.
Loading history...
36
        $this->aNamespaces = [
37
            'jaxon' => [
38
                'directory' => $sTemplateDir . DIRECTORY_SEPARATOR,
39
                'extension' => '.php',
40
            ],
41
            'pagination' => [
42
                'directory' => $sTemplateDir . DIRECTORY_SEPARATOR . 'pagination' . DIRECTORY_SEPARATOR,
43
                'extension' => '.php',
44
            ],
45
        ];
46
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
47
48
    /**
49
     * Add a namespace to the template system
50
     *
51
     * @param string        $sNamespace         The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
52
     * @param string        $sDirectory         The namespace directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
53
     * @param string        $sExtension         The extension to append to template names
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
54
     *
55
     * @return void
56
     */
57
    public function addNamespace($sNamespace, $sDirectory, $sExtension = '')
58
    {
59
        // The 'jaxon' key cannot be overriden
60
        if($sNamespace == 'jaxon' || $sNamespace == 'pagination')
61
        {
62
            return;
63
        }
64
        // Save the namespace
65
        $this->aNamespaces[$sNamespace] = [
66
            'directory' => rtrim(trim($sDirectory), "/\\") . DIRECTORY_SEPARATOR,
67
            'extension' => $sExtension,
68
        ];
69
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
70
71
    /**
72
     * Set a new directory for pagination templates
73
     *
74
     * @param string        $sDirectory             The directory path
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
75
     *
76
     * @return void
77
     */
78
    public function pagination($sDirectory)
79
    {
80
        $this->aNamespaces['pagination']['directory'] = $sDirectory;
81
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
82
83
    /**
84
     * Render a template
85
     *
86
     * @param string        $sPath                The path to the template
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 16 found
Loading history...
87
     * @param array         $aVars                The template vars
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 16 found
Loading history...
88
     *
89
     * @return string
90
     */
91
    private function _render($sPath, array $aVars)
92
    {
93
        // Make the template vars available as attributes
94
        $xData = new stdClass();
95
        foreach($aVars as $sName => $xValue)
96
        {
97
            $sName = (string)$sName;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
98
            $xData->$sName = $xValue;
99
        }
100
        // Render the template
101
        $cRenderer = function() use($sPath) {
102
            ob_start();
103
            include($sPath);
104
            return ob_get_clean();
105
        };
106
        // Call the closure in the context of the $xData object.
107
        // So the keyworg '$this' in the template will refer to the $xData object.
108
        return \call_user_func($cRenderer->bindTo($xData));
109
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
110
111
    /**
112
     * Render a template
113
     *
114
     * @param string        $sTemplate            The name of template to be rendered
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
115
     * @param array         $aVars                The template vars
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 16 found
Loading history...
116
     *
117
     * @return string
118
     */
119
    public function render($sTemplate, array $aVars = [])
120
    {
121
        $sTemplate = trim($sTemplate);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $sTemplate. This often makes code more readable.
Loading history...
122
        // Get the namespace name
123
        $sNamespace = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
124
        $iSeparatorPosition = strrpos($sTemplate, '::');
125
        if($iSeparatorPosition !== false)
126
        {
127
            $sNamespace = substr($sTemplate, 0, $iSeparatorPosition);
128
            $sTemplate = substr($sTemplate, $iSeparatorPosition + 2);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
Consider using a different name than the parameter $sTemplate. This often makes code more readable.
Loading history...
129
        }
130
        // The default namespace is 'jaxon'
131
        if(!($sNamespace = trim($sNamespace)))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
132
        {
133
            $sNamespace = 'jaxon';
134
        }
135
        // Check if the namespace is defined
136
        if(!key_exists($sNamespace, $this->aNamespaces))
137
        {
138
            return '';
139
        }
140
        $aNamespace = $this->aNamespaces[$sNamespace];
141
        // Get the template path
142
        $sTemplatePath = $aNamespace['directory'] . $sTemplate . $aNamespace['extension'];
143
        // Render the template
144
        return $this->_render($sTemplatePath, $aVars);
145
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
146
}
147