1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Macros.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:Widgets! |
9
|
|
|
* @subpackage Latte |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 10.10.14 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\Widgets\Latte; |
18
|
|
|
|
19
|
|
|
use Nette\Bridges; |
20
|
|
|
use Nette\Utils; |
21
|
|
|
|
22
|
|
|
use Latte; |
23
|
|
|
use Latte\Compiler; |
24
|
|
|
use Latte\MacroNode; |
25
|
|
|
use Latte\PhpWriter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Widgets latte macros definition |
29
|
|
|
* |
30
|
|
|
* @package iPublikuj:Widgets! |
31
|
|
|
* @subpackage Latte |
32
|
|
|
* |
33
|
|
|
* @author Adam Kadlec <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
final class Macros extends Bridges\ApplicationLatte\UIMacros |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @param Compiler $compiler |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public static function install(Compiler $compiler) : void |
43
|
|
|
{ |
44
|
|
|
$me = new static($compiler); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {widget positionName} |
48
|
|
|
*/ |
49
|
|
|
$me->addMacro('widgets', [$me, 'macroWidgets']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param MacroNode $node |
54
|
|
|
* @param PhpWriter $writer |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
* |
58
|
|
|
* @throws Latte\CompileException |
59
|
|
|
*/ |
60
|
|
|
public static function macroWidgets(MacroNode $node, PhpWriter $writer) : string |
61
|
|
|
{ |
62
|
|
|
$words = $node->tokenizer->fetchWords(); |
63
|
|
|
if (!$words) { |
64
|
|
|
throw new Latte\CompileException('Missing widgets position name in {widgets}'); |
65
|
|
|
} |
66
|
|
|
$name = $writer->formatWord($words[0]); |
67
|
|
|
$method = isset($words[1]) ? ucfirst($words[1]) : ''; |
68
|
|
|
$method = Utils\Strings::match($method, '#^\w*\z#') ? "render$method" : "{\"render$method\"}"; |
69
|
|
|
|
70
|
|
|
$tokens = $node->tokenizer; |
71
|
|
|
$pos = $tokens->position; |
72
|
|
|
$param = $writer->formatArray(); |
73
|
|
|
$tokens->position = $pos; |
74
|
|
|
|
75
|
|
|
while ($tokens->nextToken()) { |
76
|
|
|
if ($tokens->isCurrent('=>') && !$tokens->depth) { |
77
|
|
|
$wrap = TRUE; |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (empty($wrap)) { |
83
|
|
|
$param = substr($param, 1, -1); // removes array() or [] |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$prefix = ''; |
87
|
|
|
|
88
|
|
|
if (is_string($name) && strpos($name, '-') === FALSE) { |
89
|
|
|
$prefix = '"widgets-" .'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return "/* line $node->startLine */ " |
93
|
|
|
. ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '') |
94
|
|
|
. '$_tmp = $this->global->uiControl->getComponent('. $prefix . $name . '); ' |
95
|
|
|
. 'if ($_tmp instanceof Nette\Application\UI\IRenderable) $_tmp->redrawControl(NULL, FALSE); ' |
96
|
|
|
. ($node->modifiers === '' |
97
|
|
|
? "\$_tmp->$method($param);" |
98
|
|
|
: $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean());") |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.