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-19 |
14
|
|
|
* @link http://dwoo.org/ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Dwoo\Plugins\Blocks; |
18
|
|
|
|
19
|
|
|
use Dwoo\Compiler; |
20
|
|
|
use Dwoo\IElseable; |
21
|
|
|
use Dwoo\ICompilable\Block as ICompilableBlock; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Acts as a php elseif block, allowing you to add one more condition |
25
|
|
|
* if the previous one(s) didn't match. See the {if} plugin for syntax details. |
26
|
|
|
* This software is provided 'as-is', without any express or implied warranty. |
27
|
|
|
* In no event will the authors be held liable for any damages arising from the use of this software. |
28
|
|
|
*/ |
29
|
|
|
class PluginElseif extends PluginIf implements ICompilableBlock, IElseable |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param array $rest |
33
|
|
|
*/ |
34
|
|
|
public function init(array $rest) |
35
|
|
|
{ |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Compiler $compiler |
40
|
|
|
* @param array $params |
41
|
|
|
* @param string $prepend |
42
|
|
|
* @param string $append |
43
|
|
|
* @param string $type |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type) |
48
|
|
|
{ |
49
|
|
|
$preContent = ''; |
50
|
|
|
while (true) { |
51
|
|
|
$preContent .= $compiler->removeTopBlock(); |
52
|
|
|
$block = &$compiler->getCurrentBlock(); |
53
|
|
|
$interfaces = class_implements($block['class']); |
54
|
|
|
if (in_array('Dwoo\IElseable', $interfaces) !== false) { |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$params['initialized'] = true; |
60
|
|
|
$compiler->injectBlock($type, $params); |
61
|
|
|
|
62
|
|
|
return $preContent; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Compiler $compiler |
67
|
|
|
* @param array $params |
68
|
|
|
* @param string $prepend |
69
|
|
|
* @param string $append |
70
|
|
|
* @param string $content |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) |
75
|
|
|
{ |
76
|
|
|
if (!isset($params['initialized'])) { |
77
|
|
|
return ''; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$tokens = $compiler->getParamTokens($params); |
81
|
|
|
$params = $compiler->getCompiledParams($params); |
82
|
|
|
|
83
|
|
|
$pre = Compiler::PHP_OPEN . 'elseif (' . implode(' ', self::replaceKeywords($params['*'], $tokens['*'], $compiler)) . ") {\n" . Compiler::PHP_CLOSE; |
84
|
|
|
$post = Compiler::PHP_OPEN . "\n}" . Compiler::PHP_CLOSE; |
85
|
|
|
|
86
|
|
|
if (isset($params['hasElse'])) { |
87
|
|
|
$post .= $params['hasElse']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$block = &$compiler->getCurrentBlock(); |
91
|
|
|
$block['params']['hasElse'] = $pre . $content . $post; |
92
|
|
|
|
93
|
|
|
return ''; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|