PluginEval::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Functions
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.2
13
 * @date      2017-01-06
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Core;
20
use Dwoo\Plugin;
21
use Dwoo\Template\Str as TemplateString;
22
23
/**
24
 * Evaluates the given string as if it was a template.
25
 * Although this plugin is kind of optimized and will
26
 * not recompile your string each time, it is still not
27
 * a good practice to use it. If you want to have templates
28
 * stored in a database or something you should probably use
29
 * the Dwoo\Template\Str class or make another class that
30
 * extends it
31
 * <pre>
32
 *  * var : the string to use as a template
33
 *  * assign : if set, the output of the template will be saved in this variable instead of being output
34
 * </pre>
35
 * This software is provided 'as-is', without any express or implied warranty.
36
 * In no event will the authors be held liable for any damages arising from the use of this software.
37
 */
38
class PluginEval extends Plugin
39
{
40
    /**
41
     * @param string $var
42
     * @param null   $assign
43
     *
44
     * @return string
45
     */
46
    public function process($var, $assign = null)
47
    {
48
        if ($var == '') {
49
            return '';
50
        }
51
52
        $tpl   = new TemplateString($var);
53
        $clone = clone $this->core;
54
        $out   = $clone->get($tpl, $this->core->readVar('_parent'));
55
56
        if ($assign !== null) {
57
            $this->core->assignInScope($out, $assign);
58
        } else {
59
            return $out;
60
        }
61
    }
62
}