Passed
Push — master ( 31299b...5b3941 )
by Mikael
01:30
created

ViewHelperFunctions.php ➔ showEnvironment()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 6
nop 1
dl 0
loc 21
ccs 0
cts 17
cp 0
crap 56
rs 7.551
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\View;
4
5
#use \Anax\View\View2 as View;
6
use \Anax\View\View2;
7
8
/**
9
 * Define helper functions to include before processing the view template.
10
 * The functions here are exposed to the view and can be used in the view.
11
 */
12
13
/**
14
 * Shortcut to create an url for a static asset.
15
 *
16
 * @param string $url url to use when creating the url.
17
 *
18
 * @return string as resulting url.
19
 */
20
function asset($url = "")
21
{
22
    global $di;
23
    return $di->get("url")->asset($url);
24
}
25
26
27
28
/**
29
 * Shortcut to create an url for routing in the framework.
30
 *
31
 * @param null|string $url url to use when creating the url.
32
 *
33
 * @return string as resulting url.
34
 */
35
function url($url = "")
36
{
37
    global $di;
38
    return $di->get("url")->create($url);
39
}
40
41
42
43
/**
44
 * Render a view with an optional data set of variables.
45
 *
46
 * @param string $template the template file, or array
47
 * @param array  $data     variables to make available to the
48
 *                         view, default is empty
49
 *
50
 * @return void
51
 */
52
function renderView($template, $data = [])
53
{
54
    global $di;
55
    $view = new View2();
56
    $template = $di->get("view")->getTemplateFile($template);
57
    $view->set($template, $data);
58
    $view->render($this->app);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
59
}
60
61
62
63
/**
64
 * Check if the region in the view container has views to render.
65
 *
66
 * @param string $region to check
67
 *
68
 * @return boolean true or false
69
 */
70
function regionHasContent($region)
71
{
72
    global $di;
73
    return $di->get("view")->hasContent($region);
74
}
75
76
77
78
/**
79
 * Render views, from the view container, in the region.
80
 *
81
 * @param string $region to render in
82
 *
83
 * @return boolean true or false
84
 */
85
function renderRegion($region)
86
{
87
    global $di;
88
    return $di->get("view")->render($region);
89
}
90
91
92
93
/**
94
 * Create a class attribute from a string or array.
95
 *
96
 * @param string|array $args variable amount of classlists.
97
 *
98
 * @return string as complete class attribute
99
 */
100 View Code Duplication
function classList(...$args)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
{
102
    $classes = [];
103
104
    foreach ($args as $arg) {
105
        if (empty($arg)) {
106
            continue;
107
        } elseif (is_string($arg)) {
108
            $arg = explode(" ", $arg);
109
        }
110
        $classes = array_merge($classes, $arg);
111
    }
112
113
    return "class=\"" . implode(" ", $classes) . "\"";
114
}
115
116
117
118
/**
119
 * Get current url, without querystring.
120
 *
121
 * @return string as resulting url.
122
 */
123
function currentUrl()
124
{
125
    global $di;
126
    return $di->get("request")->getCurrentUrl(false);
127
}
128
129
130
131
/**
132
 * Get current route.
133
 *
134
 * @return string as resulting route.
135
 */
136
function currentRoute()
137
{
138
    global $di;
139
    return $di->get("request")->getRoute();
140
}
141
142
143
144
/**
145
 * Show variables/functions that are currently defined and can
146
 * be used within the view. Call the function with get_defined_vars()
147
 * as the parameter.
148
 *
149
 * @param array $variables should be the retiurned value from
150
 *                         get_defined_vars()
151
 *
152
 * @return string showing variables.
153
 */
154
function showEnvironment($variables)
155
{
156
    $all = array_keys($variables);
157
    sort($all);
158
    $res = "<pre>\nVariables (var_dump them for more information):\n";
159
    foreach ($all as $var) {
160
        $variable = $variables[$var];
161
        $res .= "  $var (" . gettype($variable) . ")";
162
        if (is_integer($variable) || is_double($variable) ) {
163
            $res .= ": $variable";
164
        } elseif (is_string($variable)) {
165
            $res .= ": \"$variable\"";
166
        } elseif (is_bool($variable)) {
167
            $res .= ": " . ( $variable ? "true" : "false" );
168
        }
169
        $res .= "\n";
170
    }
171
    $res .= "</pre>";
172
173
    return $res;
174
}
175