Completed
Push — master ( 58f189...b5488f )
by Iurii
01:42
created

Twig::getDefaultFunctions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 70
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 9.1724
c 0
b 0
f 0
cc 2
eloc 41
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
     * Implements hook "library.list"
51
     * @param array $libraries
52
     */
53
    public function hookLibraryList(array &$libraries)
54
    {
55
        $libraries['twig'] = array(
56
            'name' => 'Twig',
57
            'description' => 'Twig is a template engine for PHP',
58
            'url' => 'https://github.com/twigphp/Twig',
59
            'download' => 'https://github.com/twigphp/Twig/archive/v1.33.0.zip',
60
            'type' => 'php',
61
            'module' => 'twig',
62
            'version_source' => array(
63
                'lines' => 100,
64
                'pattern' => '/.*VERSION.*(\\d+\\.+\\d+\\.+\\d+)/',
65
                'file' => 'vendor/twig/twig/lib/Twig/Environment.php'
66
            ),
67
            'files' => array(
68
                'vendor/autoload.php'
69
            )
70
        );
71
    }
72
73
    /**
74
     * Implements hook "route.list"
75
     * @param array $routes
76
     */
77
    public function hookRouteList(array &$routes)
78
    {
79
        // Module settings page
80
        $routes['admin/module/settings/twig'] = array(
81
            'access' => 'module_edit',
82
            'handlers' => array(
83
                'controller' => array('gplcart\\modules\\twig\\controllers\\Settings', 'editSettings')
84
            )
85
        );
86
    }
87
88
    /**
89
     * Implements hook "template.render"
90
     * @param string $template
91
     * @param array $data
92
     * @param null|string $rendered
93
     * @param \gplcart\core\Controller $object
94
     */
95
    public function hookTemplateRender($template, $data, &$rendered, $object)
96
    {
97
        $template .= '.twig';
98
99
        if (is_file($template)) {
100
            $rendered = $this->render($template, $data, $object);
101
        }
102
    }
103
104
    /**
105
     * Returns a TWIG instance for the given file directory
106
     * @param string $path
107
     * @param \gplcart\core\Controller $object
108
     */
109
    public function getTwigInstance($path, $object)
110
    {
111
        if (empty($this->twig)) {
112
            $this->library->load('twig');
113
            $this->settings = $this->config->module('twig');
114
        }
115
116
        if (isset($this->twig[$path])) {
117
            return $this->twig[$path];
118
        }
119
120
        $options = $this->settings;
121
122
        if (!empty($this->settings['cache'])) {
123
            $options['cache'] = __DIR__ . '/cache';
124
        }
125
126
        $twig = new \Twig_Environment(new \Twig_Loader_Filesystem($path), $options);
127
128
        if (!empty($options['debug'])) {
129
            $twig->addExtension(new \Twig_Extension_Debug());
130
        }
131
132
        foreach ($this->getDefaultFunctions($object) as $function) {
133
            $twig->addFunction($function);
134
        }
135
136
        return $this->twig[$path] = $twig;
137
    }
138
139
    /**
140
     * Renders a .twig template
141
     * @param string $template
142
     * @param array $data
143
     * @param \gplcart\core\Controller $object
144
     * @return string
145
     */
146
    public function render($template, $data, $object)
147
    {
148
        $parts = explode('/', $template);
149
        $file = array_pop($parts);
150
151
        $twig = $this->getTwigInstance(implode('/', $parts), $object);
152
153
        $controller_data = $object->getData();
154
        $merged = gplcart_array_merge($controller_data, $data);
155
156
        return $twig->loadTemplate($file)->render($merged);
157
    }
158
159
    /**
160
     * Adds custom functions and returns an array of Twig_SimpleFunction objects
161
     * @param \gplcart\core\Controller $object
162
     * @return array
163
     */
164
    protected function getDefaultFunctions($object)
165
    {
166
        $functions = array();
167
168
        $functions[] = new \Twig_SimpleFunction('error', function ($key = null, $has_error = null, $no_error = '') use ($object) {
169
            return $object->error($key, $has_error, $no_error);
170
        }, array('is_safe' => array('all')));
171
172
        $functions[] = new \Twig_SimpleFunction('text', function ($text, $arguments = array()) use ($object) {
173
            return $object->text($text, $arguments);
174
        }, array('is_safe' => array('all')));
175
176
        $functions[] = new \Twig_SimpleFunction('access', function ($permission) use ($object) {
177
            return $object->access($permission);
178
        });
179
180
        $functions[] = new \Twig_SimpleFunction('url', function ($path = '', array $query = array(), $absolute = false) use ($object) {
181
            return $object->url($path, $query, $absolute);
182
        });
183
184
        $functions[] = new \Twig_SimpleFunction('isSuperadmin', function ($user_id = null) use ($object) {
185
            return $object->isSuperadmin($user_id);
186
        });
187
188
        $functions[] = new \Twig_SimpleFunction('date', function ($timestamp = null, $full = true, $unix_format = '') use ($object) {
189
            return $object->date($timestamp, $full, $unix_format);
190
        });
191
192
        $functions[] = new \Twig_SimpleFunction('attributes', function ($attributes) use ($object) {
193
            return $object->attributes($attributes);
194
        }, array('is_safe' => array('all')));
195
196
        $functions[] = new \Twig_SimpleFunction('config', function ($key = null, $default = null) use ($object) {
197
            return $object->config($key, $default);
198
        });
199
200
        $functions[] = new \Twig_SimpleFunction('settings', function ($key = null, $default = null) use ($object) {
201
            return $object->settings($key, $default);
202
        });
203
204
        $functions[] = new \Twig_SimpleFunction('summary', function ($text, $xss = false, $filter = null) use ($object) {
205
            return $object->summary($text, $xss, $filter);
206
        }, array('is_safe' => array('all')));
207
208
        $functions[] = new \Twig_SimpleFunction('d', function ($key = null) use ($object) {
209
            if (function_exists('d')) {
210
                d($object->getData($key)); // Kint
211
            } else {
212
                print_r($object->getData($key), true);
213
            }
214
        });
215
216
        $functions[] = new \Twig_SimpleFunction('filter', function ($text, $filter = null) use ($object) {
217
            return $object->filter($text, $filter);
218
        }, array('is_safe' => array('all')));
219
220
        $functions[] = new \Twig_SimpleFunction('truncate', function ($string, $length = 100, $trimmarker = '...') use ($object) {
221
            return $object->truncate($string, $length, $trimmarker);
222
        });
223
224
        $functions[] = new \Twig_SimpleFunction('prop', function ($name) use ($object) {
225
            return $object->prop($name);
226
        });
227
228
        $functions[] = new \Twig_SimpleFunction('path', function ($path = null) use ($object) {
229
            return $object->path($path);
230
        });
231
232
        return $functions;
233
    }
234
235
    /**
236
     * Implements hook "module.enable.after"
237
     */
238
    public function hookModuleEnableAfter()
239
    {
240
        $this->library->clearCache();
241
    }
242
243
    /**
244
     * Implements hook "module.disable.after"
245
     */
246
    public function hookModuleDisableAfter()
247
    {
248
        $this->library->clearCache();
249
    }
250
251
    /**
252
     * Implements hook "module.install.after"
253
     */
254
    public function hookModuleInstallAfter()
255
    {
256
        $this->library->clearCache();
257
    }
258
259
    /**
260
     * Implements hook "module.uninstall.after"
261
     */
262
    public function hookModuleUninstallAfter()
263
    {
264
        $this->library->clearCache();
265
    }
266
267
}
268