Engine::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Defines all possible template engine use cases
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Template
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\template;
17
18
/**
19
 * Defines all possible template engine use cases
20
 *
21
 * @category  Core
22
 * @package   Template
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
abstract class Engine
30
{
31
    /**
32
     * Name of active theme
33
     **/
34
    private static $_theme = '';
35
36
    /**
37
     * Parses only boxes in an array
38
     *
39
     * @param array  $boxes   Boxes as an array
40
     * @param string $content The content to put into the theme file
41
     *
42
     * @return array
43
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
44
45
    public static function boxes(array $boxes, $content)
46
    {
47
        $add   = [];
48
        $nodiv = ['nodiv' => true];
49
50
        //  Skip all parts except boxes
51
        foreach ($boxes AS $part) {
52
53
            $box       = $part['name'];
54
            $add[$box] = \csphere\core\template\CMD_Parse::box($part, $nodiv);
55
        }
56
57
        // Get page placeholders from hooks and append content
58
        $data             = \csphere\core\template\Hooks::export();
59
        $data['content'] .= $content;
60
61
        // Add boxes to data
62
        $data['boxes'] = $add;
63
64
        return $data;
65
    }
66
67
    /**
68
     * Search for template files in theme and plugin
69
     *
70
     * @param string $plugin   Name of the plugin
71
     * @param string $template Template file name without file ending
72
     *
73
     * @return array
74
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
75
76
    public static function source($plugin, $template)
77
    {
78
        // Get theme if it is not known yet
79
        if (self::$_theme == '') {
80
81
            $locator      = \csphere\core\service\Locator::get();
82
            $view         = $locator->load('view');
83
            self::$_theme = $view->getOption('theme');
84
        }
85
86
        // Check if theme overwrites the template file
87
        $target = new \csphere\core\themes\Checks(self::$_theme);
88
        $target->setTemplate($template, $plugin);
89
90
        $check = $target->existance();
91
92
        // Get template from plugin otherwise
93
        if ($check === false) {
94
95
            $target = new \csphere\core\plugins\Checks($plugin);
96
            $target->setTemplate($template);
97
        }
98
99
        // Load template file and fetch file content
100
        $file = $target->result();
101
        $file = file_get_contents($file);
102
103
        // Split template file content
104
        $tpl = \csphere\core\template\Prepare::template($file, $plugin);
105
106
        return $tpl;
107
    }
108
109
    /**
110
     * Parses a template file
111
     *
112
     * @param array $array Template file (part) as an array
113
     * @param array $data  Array with data to use in the template
114
     *
115
     * @return string
116
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
117
118
    public static function parse(array $array, array $data)
119
    {
120
        // Fill templates with their data
121
        try {
122
            $string = \csphere\core\template\Parse::template($array, $data);
123
124
        } catch (\Exception $exception) {
125
126
            // String must be a string in every case
127
            $string = '';
128
129
            // Continue to not cause further problems
130
            $cont = new \csphere\core\Errors\Controller($exception, true);
131
132
            unset($cont);
133
        }
134
135
        return $string;
136
    }
137
}
138