Completed
Push — master ( a400a4...020752 )
by David
07:41 queued 04:43
created

Dwoo/Plugins/Processors/PluginSmartyCompatible.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Copyright (c) 2013-2016
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Processors
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-18
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Processors;
18
19
use Dwoo\Processor;
20
21
/**
22
 * Performs some template conversions to allow smarty templates to be used by
23
 * the Dwoo compiler.
24
 * This software is provided 'as-is', without any express or implied warranty.
25
 * In no event will the authors be held liable for any damages arising from the use of this software.
26
 */
27
class PluginSmartyCompatible extends Processor
28
{
29
    /**
30
     * @param string $input
31
     *
32
     * @return mixed
33
     */
34
    public function process($input)
35
    {
36
        list($l, $r) = $this->compiler->getDelimiters();
0 ignored issues
show
Documentation Bug introduced by
The method getDelimiters does not exist on object<Dwoo\Core>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37
38
        $rl           = preg_quote($l, '/');
39
        $rr           = preg_quote($r, '/');
40
        $sectionParam = '(?:(name|loop|start|step|max|show)\s*=\s*(\S+))?\s*';
41
        $input        = preg_replace_callback('/' . $rl . '\s*section ' . str_repeat($sectionParam, 6) . '\s*' . $rr . '(.+?)(?:' . $rl . '\s*sectionelse\s*' . $rr . '(.+?))?' . $rl . '\s*\/section\s*' . $rr . '/is', array(
42
            $this,
43
            'convertSection'
44
        ), $input);
45
        $input        = str_replace('$smarty.section.', '$smarty.for.', $input);
46
47
        $smarty = array(
48
            '/' . $rl . '\s*ldelim\s*' . $rr . '/',
49
            '/' . $rl . '\s*rdelim\s*' . $rr . '/',
50
            '/' . $rl . '\s*\$smarty\.ldelim\s*' . $rr . '/',
51
            '/' . $rl . '\s*\$smarty\.rdelim\s*' . $rr . '/',
52
            '/\$smarty\./',
53
            '/' . $rl . '\s*php\s*' . $rr . '/',
54
            '/' . $rl . '\s*\/php\s*' . $rr . '/',
55
            '/\|(@?)strip(\||' . $rr . ')/',
56
            '/' . $rl . '\s*sectionelse\s*' . $rr . '/',
57
        );
58
59
        $dwoo = array(
60
            '\\' . $l,
61
            $r,
62
            '\\' . $l,
63
            $r,
64
            '$dwoo.',
65
            '<?php ',
66
            ' ?>',
67
            '|$1whitespace$2',
68
            $l . 'else' . $r,
69
        );
70
71
        if (preg_match('{\|@([a-z][a-z0-9_]*)}i', $input, $matches)) {
72
            trigger_error('The Smarty Compatibility Module has detected that you use |@' . $matches[1] . ' in your template, this might lead to problems as Dwoo interprets the @ operator differently than Smarty, see http://wiki.dwoo.org/index.php/Syntax#The_.40_Operator', E_USER_NOTICE);
73
        }
74
75
        return preg_replace($smarty, $dwoo, $input);
76
    }
77
78
    /**
79
     * @param array $matches
80
     *
81
     * @return mixed
82
     */
83
    protected function convertSection(array $matches)
84
    {
85
        $params = array();
86
        $index  = 1;
87
        while (!empty($matches[$index]) && $index < 13) {
88
            $params[$matches[$index]] = $matches[$index + 1];
89
            $index += 2;
90
        }
91
92
        return str_replace('[' . trim($params['name'], '"\'') . ']', '[$' . trim($params['name'], '"\'') . ']', $matches[0]);
93
    }
94
}
95