PluginTemplate::preProcessing()   A
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.1768
c 0
b 0
f 0
cc 5
nc 10
nop 5
1
<?php
2
/**
3
 * Copyright (c) 2013-2016
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Blocks
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2016 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.0
13
 * @date      2016-09-20
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Blocks;
18
19
use Dwoo\Core;
20
use Dwoo\Compiler;
21
use Dwoo\Block\Plugin as BlockPlugin;
22
use Dwoo\ICompilable\Block as ICompilableBlock;
23
use Dwoo\Compilation\Exception as CompilationException;
24
25
/**
26
 * Defines a sub-template that can then be called (even recursively) with the defined arguments
27
 * <pre>
28
 *  * name : template name
29
 *  * rest : list of arguments and optional default values
30
 * </pre>
31
 * This software is provided 'as-is', without any express or implied warranty.
32
 * In no event will the authors be held liable for any damages arising from the use of this software.
33
 */
34
class PluginTemplate extends BlockPlugin implements ICompilableBlock
35
{
36
    /**
37
     * @param Compiler $compiler
38
     * @param array    $params
39
     * @param string   $prepend
40
     * @param string   $append
41
     * @param string   $type
42
     *
43
     * @return string
44
     * @throws CompilationException
45
     */
46
    public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
47
    {
48
        $params       = $compiler->getCompiledParams($params);
49
        $parsedParams = array();
50
        if (!isset($params['*'])) {
51
            $params['*'] = array();
52
        }
53
        foreach ($params['*'] as $param => $defValue) {
54
            if (is_numeric($param)) {
55
                $param    = $defValue;
56
                $defValue = null;
57
            }
58
            $param = trim($param, '\'"');
59
            if (!preg_match('#^[a-z0-9_]+$#i', $param)) {
60
                throw new CompilationException($compiler, 'Function : parameter names must contain only A-Z, 0-9 or _');
61
            }
62
            $parsedParams[$param] = $defValue;
63
        }
64
        $params['name'] = substr($params['name'], 1, - 1);
65
        $params['*']    = $parsedParams;
66
        $params['uuid'] = uniqid();
67
        $compiler->addTemplatePlugin($params['name'], $parsedParams, $params['uuid']);
68
        $currentBlock           = &$compiler->getCurrentBlock();
69
        $currentBlock['params'] = $params;
70
71
        return '';
72
    }
73
74
    /**
75
     * @param Compiler $compiler
76
     * @param array    $params
77
     * @param string   $prepend
78
     * @param string   $append
79
     * @param string   $content
80
     *
81
     * @return string|void
82
     */
83
    public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
84
    {
85
        $paramstr = 'Dwoo\Core $dwoo';
86
        $init     = 'static $_callCnt = 0;' . "\n" . '$dwoo->scope[\' ' . $params['uuid'] . '\'.$_callCnt] = array();' . "\n" . '$_scope = $dwoo->setScope(array(\' ' . $params['uuid'] . '\'.($_callCnt++)));' . "\n";
87
        $cleanup  = '/* -- template end output */ $dwoo->setScope($_scope, true);';
88
        foreach ($params['*'] as $param => $defValue) {
89
            if ($defValue === null) {
90
                $paramstr .= ', $' . $param;
91
            } else {
92
                $paramstr .= ', $' . $param . ' = ' . $defValue;
93
            }
94
            $init .= '$dwoo->scope[\'' . $param . '\'] = $' . $param . ";\n";
95
        }
96
        $init .= '/* -- template start output */';
97
98
        $funcName = 'Plugin' . Core::toCamelCase($params['name']) . Core::toCamelCase($params['uuid']);
99
100
        $search      = array('$this->charset', '$this->', '$this,',);
101
        $replacement = array('$dwoo->getCharset()', '$dwoo->', '$dwoo,',);
102
        $content     = str_replace($search, $replacement, $content);
103
104
        $body = 'if (!function_exists(\'' . $funcName . "')) {\nfunction " . $funcName . '(' . $paramstr . ') {' . "\n$init" . Compiler::PHP_CLOSE . $prepend . $content . $append . Compiler::PHP_OPEN . $cleanup . "\n}\n}";
105
        $compiler->addTemplatePlugin($params['name'], $params['*'], $params['uuid'], $body);
106
    }
107
108
    /**
109
     * @param       $name
110
     * @param array $rest
111
     */
112
    public function init($name, array $rest = array())
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $rest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
    }
115
}
116