1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
This project is Licenced under The MIT License (MIT) |
4
|
|
|
|
5
|
|
|
Copyright (c) 2014 Christopher Seufert |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
in the Software without restriction, including without limitation the rights |
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in |
15
|
|
|
all copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
THE SOFTWARE. |
24
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
namespace Seufert\Hamle\Tag; |
27
|
|
|
use Seufert\Hamle as H; |
28
|
|
|
use Seufert\Hamle\Exception\ParseError; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* HAMLE Control Tag |
32
|
|
|
* Used for tags starting with the pipe (|) symbol |
33
|
|
|
*/ |
34
|
|
|
class Control extends H\Tag { |
35
|
|
|
/** |
36
|
|
|
* @var string Variable passed to Control Tag |
37
|
|
|
*/ |
38
|
|
|
protected $var; |
39
|
|
|
public $o, $else = false; |
40
|
|
|
static $instCount = 1; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Crate new Control Tag |
44
|
|
|
* @param string $tag Type of Control Tag |
45
|
|
|
* @param \Seufert\Hamle\Tag $parentTag |
46
|
|
|
* @throws ParseError |
47
|
|
|
*/ |
48
|
|
|
function __construct($tag, $parentTag = null) { |
49
|
|
|
parent::__construct(); |
50
|
|
|
$this->o = "\$o" . self::$instCount++; |
51
|
|
|
$this->type = strtolower($tag); |
52
|
|
|
$this->var = ""; |
53
|
|
|
if ($parentTag && $this->type == "else") { |
54
|
|
|
if($parentTag instanceof H\Tag) { |
55
|
|
|
$elseTag = $parentTag->tags[count($parentTag->tags) - 1]; |
56
|
|
|
if($elseTag instanceof H\Tag\Control && |
57
|
|
|
in_array($elseTag->type, array('with', 'if')) |
58
|
|
|
) { |
59
|
|
|
$elseTag->else = true; |
60
|
|
|
} else { |
61
|
|
|
throw new ParseError("You can only use else with |with and |if, you tried |{$parentTag->type}"); |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
throw new ParseError("Unable to use else here"); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
function renderStTag() { |
70
|
|
|
$out = "<" . "?php "; |
71
|
|
|
$scopeName = ""; |
72
|
|
|
if (preg_match('/ as ([a-zA-Z]+)$/', $this->var, $m)) { |
73
|
|
|
$scopeName = $m[1]; |
74
|
|
|
$lookup = substr($this->var, 0, strlen($this->var) - strlen($m[0])); |
75
|
|
|
$hsv = new H\Text(trim($lookup), H\Text::TOKEN_CONTROL); |
76
|
|
|
} else |
77
|
|
|
$hsv = new H\Text($this->var, H\Text::TOKEN_CONTROL); |
78
|
|
|
switch ($this->type) { |
79
|
|
|
case "each": |
80
|
|
|
if ($this->var) |
81
|
|
|
$out .= "foreach(" . $hsv->toPHP() . " as {$this->o}) { \n"; |
82
|
|
|
else |
83
|
|
|
$out .= "foreach(Hamle\\Scope::get() as {$this->o}) { \n"; |
84
|
|
|
$out .= "Hamle\\Scope::add({$this->o}); "; |
85
|
|
|
break; |
86
|
|
|
case "if": |
87
|
|
|
$hsvcomp = new H\Text\Comparison($this->var); |
88
|
|
|
$out .= "if(" . $hsvcomp->toPHP() . ") {"; |
89
|
|
|
break; |
90
|
|
|
case "with": |
91
|
|
|
if ($scopeName) |
92
|
|
|
$out .= "Hamle\\Scope::add(" . $hsv->toPHP() . ", \"$scopeName\");\n;"; |
93
|
|
|
else { |
94
|
|
|
$out .= "if(({$this->o} = " . $hsv->toPHP() . ") && " . |
95
|
|
|
"{$this->o}->valid()) {\n"; |
96
|
|
|
$out .= "Hamle\\Scope::add({$this->o});\n;"; |
97
|
|
|
} |
98
|
|
|
break; |
99
|
|
|
case "else": |
100
|
|
|
$out .= "/* else */"; |
101
|
|
|
break; |
102
|
|
|
case "include": |
103
|
|
|
$file = $hsv->toHTML(); |
104
|
|
|
if($file[0] == "#") |
105
|
|
|
$out .= "echo Hamle\\Run::includeFragment(".$hsv->toPHP().");"; |
106
|
|
|
else |
107
|
|
|
$out .= "echo Hamle\\Run::includeFile(" . $hsv->toPHP() . ");"; |
108
|
|
|
break; |
109
|
|
|
} |
110
|
|
|
return $out . ' ?>'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $s Variable String for control tag |
115
|
|
|
*/ |
116
|
|
|
function setVar($s) { |
117
|
|
|
$this->var = trim($s); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
function renderEnTag() { |
121
|
|
|
$out = '<' . '?php '; |
122
|
|
|
switch ($this->type) { |
123
|
|
|
case "each"; |
124
|
|
|
$out .= 'Hamle\\Scope::done(); '; |
125
|
|
|
$out .= '}'; |
126
|
|
|
if (!$this->var) |
127
|
|
|
$out .= "Hamle\\Scope::get()->rewind();\n"; |
128
|
|
|
break; |
129
|
|
|
case "if": |
130
|
|
|
case "else": |
131
|
|
|
$out .= "}"; |
132
|
|
|
break; |
133
|
|
|
case "with"; |
134
|
|
|
if (!preg_match('/ as ([a-zA-Z]+)$/', $this->var, $m)) { |
135
|
|
|
$out .= 'Hamle\\Scope::done(); '; |
136
|
|
|
$out .= '}'; |
137
|
|
|
} |
138
|
|
|
break; |
139
|
|
|
case "include": |
140
|
|
|
return ""; |
141
|
|
|
break; |
142
|
|
|
} |
143
|
|
|
if ($this->else) $out .= "else{"; |
144
|
|
|
return $out . ' ?>'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
function render($indent = 0, $doIndent = true) { |
148
|
|
|
return parent::render($indent - self::INDENT_SIZE, false); |
149
|
|
|
} |
150
|
|
|
} |