Completed
Push — master ( ca7867...23e15e )
by Iurii
01:54
created

Twig::getDefaultFunctions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 90
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 8.5454
c 0
b 0
f 0
cc 1
eloc 51
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @package Twig
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\twig;
11
12
use gplcart\core\Module,
13
    gplcart\core\Library;
14
15
/**
16
 * Main class for Twig module
17
 */
18
class Twig extends Module
19
{
20
21
    /**
22
     * Library class instance
23
     * @var \gplcart\core\Library
24
     */
25
    protected $library;
26
27
    /**
28
     * An array of TWIG instances keyed by file directory
29
     * @var array
30
     */
31
    protected $twig = array();
32
33
    /**
34
     * An array of module settings
35
     * @var array
36
     */
37
    protected $settings = array();
38
39
    /**
40
     * @param Library $library
41
     */
42
    public function __construct(Library $library)
43
    {
44
        parent::__construct();
45
46
        $this->library = $library;
47
    }
48
49
    /**
50
     * Module info
51
     * @return array
52
     */
53
    public function info()
54
    {
55
        return array(
56
            'name' => 'Twig',
57
            'version' => GC_VERSION,
58
            'description' => 'A GPL Cart module that allows to render .twig templates',
59
            'author' => 'Iurii Makukh ',
60
            'core' => '1.x',
61
            'license' => 'GPL-3.0+',
62
            'configure' => 'admin/module/settings/twig',
63
            'settings' => array(
64
                'cache' => true,
65
                'debug' => false,
66
                'auto_reload' => false,
67
                'strict_variables' => false
68
            ),
69
        );
70
    }
71
72
    /**
73
     * Implements hook "library.list"
74
     * @param array $libraries
75
     */
76
    public function hookLibraryList(array &$libraries)
77
    {
78
        $libraries['twig'] = array(
79
            'name' => 'Twig',
80
            'description' => 'Twig is a template engine for PHP',
81
            'url' => 'https://github.com/twigphp/Twig',
82
            'download' => 'https://github.com/twigphp/Twig/archive/v1.33.0.zip',
83
            'type' => 'php',
84
            'module' => 'twig',
85
            'version_source' => array(
86
                'lines' => 100,
87
                'pattern' => '/.*VERSION.*(\\d+\\.+\\d+\\.+\\d+)/',
88
                'file' => 'vendor/twig/twig/lib/Twig/Environment.php'
89
            ),
90
            'files' => array(
91
                'vendor/autoload.php'
92
            )
93
        );
94
    }
95
96
    /**
97
     * Implements hook "route.list"
98
     * @param array $routes
99
     */
100
    public function hookRouteList(array &$routes)
101
    {
102
        // Module settings page
103
        $routes['admin/module/settings/twig'] = array(
104
            'access' => 'module_edit',
105
            'handlers' => array(
106
                'controller' => array('gplcart\\modules\\twig\\controllers\\Settings', 'editSettings')
107
            )
108
        );
109
    }
110
111
    /**
112
     * Implements hook "template.render"
113
     * @param string $template
114
     * @param array $data
115
     * @param null|string $rendered
116
     * @param \gplcart\core\Controller $object
117
     */
118
    public function hookTemplateRender($template, $data, &$rendered, $object)
119
    {
120
        $template .= '.twig';
121
122
        if (is_file($template)) {
123
124
            if (empty($this->twig)) {
125
                $this->library->load('twig');
126
                $this->settings = $this->config->module('twig');
127
            }
128
129
            $rendered = $this->render($template, $data, $object);
130
        }
131
    }
132
133
    /**
134
     * Returns a TWIG instance for the given file directory
135
     * @param string $path
136
     * @param \gplcart\core\Controller $object
137
     */
138
    public function getTwigInstance($path, $object)
139
    {
140
        if (isset($this->twig[$path])) {
141
            return $this->twig[$path];
142
        }
143
144
        $options = $this->settings;
145
146
        if (!empty($this->settings['cache'])) {
147
            $options['cache'] = __DIR__ . '/cache';
148
        }
149
150
        $twig = new \Twig_Environment(new \Twig_Loader_Filesystem($path), $options);
151
152
        if (!empty($options['debug'])) {
153
            $twig->addExtension(new \Twig_Extension_Debug());
154
        }
155
156
        foreach ($this->getDefaultFunctions($object) as $function) {
157
            $twig->addFunction($function);
158
        }
159
160
        return $this->twig[$path] = $twig;
161
    }
162
163
    /**
164
     * Renders a .twig template
165
     * @param string $template
166
     * @param array $data
167
     * @param \gplcart\core\Controller $object
168
     * @return string
169
     */
170
    public function render($template, $data, $object)
171
    {
172
        $parts = explode('/', $template);
173
        $file = array_pop($parts);
174
175
        $twig = $this->getTwigInstance(implode('/', $parts), $object);
176
177
        $controller_data = $object->getData();
178
        $merged = gplcart_array_merge($controller_data, $data);
179
180
        return $twig->loadTemplate($file)->render($merged);
181
    }
182
183
    /**
184
     * Adds custom functions and returns an array of Twig_SimpleFunction objects
185
     * @param \gplcart\core\Controller $object
186
     * @return array
187
     */
188
    protected function getDefaultFunctions($object)
189
    {
190
        $functions = array();
191
192
        $functions[] = new \Twig_SimpleFunction('error', function ($key = null, $has_error = null, $no_error = '') use ($object) {
193
            return $object->error($key, $has_error, $no_error);
194
        }, array('is_safe' => array('all')));
195
196
        $functions[] = new \Twig_SimpleFunction('text', function ($text, $arguments = array()) use ($object) {
197
            return $object->text($text, $arguments);
198
        }, array('is_safe' => array('all')));
199
200
        $functions[] = new \Twig_SimpleFunction('access', function ($permission) use ($object) {
201
            return $object->access($permission);
202
        });
203
204
        $functions[] = new \Twig_SimpleFunction('url', function ($path = '', array $query = array(), $absolute = false) use ($object) {
205
            return $object->url($path, $query, $absolute);
206
        });
207
208
        $functions[] = new \Twig_SimpleFunction('isSuperadmin', function ($user_id = null) use ($object) {
209
            return $object->isSuperadmin($user_id);
210
        });
211
212
        $functions[] = new \Twig_SimpleFunction('date', function ($timestamp = null, $full = true, $unix_format = '') use ($object) {
213
            return $object->date($timestamp, $full, $unix_format);
214
        });
215
216
        $functions[] = new \Twig_SimpleFunction('attributes', function ($attributes) use ($object) {
217
            return $object->attributes($attributes);
218
        }, array('is_safe' => array('all')));
219
220
        $functions[] = new \Twig_SimpleFunction('config', function ($key = null, $default = null) use ($object) {
221
            return $object->config($key, $default);
222
        });
223
224
        $functions[] = new \Twig_SimpleFunction('settings', function ($key = null, $default = null) use ($object) {
225
            return $object->settings($key, $default);
226
        });
227
228
        $functions[] = new \Twig_SimpleFunction('summary', function ($text, $xss = false, $filter = null) use ($object) {
229
            return $object->summary($text, $xss, $filter);
230
        }, array('is_safe' => array('all')));
231
232
        $functions[] = new \Twig_SimpleFunction('user', function ($item = null) use ($object) {
233
            return $object->user($item);
234
        });
235
236
        $functions[] = new \Twig_SimpleFunction('store', function ($item = null) use ($object) {
237
            return $object->store($item);
238
        });
239
240
        $functions[] = new \Twig_SimpleFunction('d', function ($key = null) use ($object) {
241
            d($object->getData($key));
242
        });
243
244
        $functions[] = new \Twig_SimpleFunction('xss', function ($text, $filter = null) use ($object) {
245
            return $object->xss($text, $filter);
246
        }, array('is_safe' => array('all')));
247
248
        $functions[] = new \Twig_SimpleFunction('truncate', function ($string, $length = 100, $trimmarker = '...') use ($object) {
249
            return $object->truncate($string, $length, $trimmarker);
250
        });
251
252
        $functions[] = new \Twig_SimpleFunction('cart', function ($key = null) use ($object) {
253
            return $object->cart($key);
254
        });
255
256
        $functions[] = new \Twig_SimpleFunction('compare', function ($key = null) use ($object) {
257
            return $object->compare($key);
258
        });
259
260
        $functions[] = new \Twig_SimpleFunction('wishlist', function ($key = null) use ($object) {
261
            return $object->wishlist($key);
262
        });
263
264
        $functions[] = new \Twig_SimpleFunction('menu', function (array $options = array()) use ($object) {
265
            return $object->menu($options);
266
        }, array('is_safe' => array('all')));
267
268
        $functions[] = new \Twig_SimpleFunction('prop', function ($name) use ($object) {
269
            return $object->prop($name);
270
        });
271
272
        $functions[] = new \Twig_SimpleFunction('path', function ($path = null) use ($object) {
273
            return $object->path($path);
274
        });
275
276
        return $functions;
277
    }
278
279
    /**
280
     * Implements hook "module.enable.after"
281
     */
282
    public function hookModuleEnableAfter()
283
    {
284
        $this->library->clearCache();
285
    }
286
287
    /**
288
     * Implements hook "module.disable.after"
289
     */
290
    public function hookModuleDisableAfter()
291
    {
292
        $this->library->clearCache();
293
    }
294
295
    /**
296
     * Implements hook "module.install.after"
297
     */
298
    public function hookModuleInstallAfter()
299
    {
300
        $this->library->clearCache();
301
    }
302
303
    /**
304
     * Implements hook "module.uninstall.after"
305
     */
306
    public function hookModuleUninstallAfter()
307
    {
308
        $this->library->clearCache();
309
    }
310
311
}
312