1
|
|
|
<?php |
2
|
|
|
namespace Goetas\Twital\Node; |
3
|
|
|
|
4
|
|
|
use Goetas\Twital\Compiler; |
5
|
|
|
use Goetas\Twital\Exception; |
6
|
|
|
use Goetas\Twital\Helper\DOMHelper; |
7
|
|
|
use Goetas\Twital\Node; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @author Asmir Mustafic <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class EmbedNode implements Node |
15
|
|
|
{ |
16
|
6 |
|
public function visit(\DOMElement $node, Compiler $context) |
17
|
|
|
{ |
18
|
6 |
|
if ($node->hasAttribute("from-exp")) { |
19
|
|
|
$filename = $node->getAttribute("from-exp"); |
20
|
6 |
|
} elseif ($node->hasAttribute("from")) { |
21
|
6 |
|
$filename = '"' . $node->getAttribute("from") . '"'; |
22
|
6 |
|
} else { |
23
|
|
|
throw new Exception("The 'from' or 'from-exp' attribute is required"); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// remove any non-element node |
27
|
6 |
|
foreach (iterator_to_array($node->childNodes) as $child) { |
28
|
6 |
|
if (!($child instanceof \DOMElement)) { |
29
|
6 |
|
$child->parentNode->removeChild($child); |
30
|
6 |
|
} |
31
|
6 |
|
} |
32
|
|
|
|
33
|
6 |
|
$context->compileChilds($node); |
34
|
|
|
|
35
|
6 |
|
$code = "embed {$filename}"; |
36
|
|
|
|
37
|
6 |
|
if ($node->hasAttribute("ignore-missing") && $node->hasAttribute("ignore-missing") !== false) { |
38
|
3 |
|
$code .= " ignore missing"; |
39
|
3 |
|
} |
40
|
6 |
|
if ($node->hasAttribute("with")) { |
41
|
3 |
|
$code .= " with " . $node->getAttribute("with"); |
42
|
3 |
|
} |
43
|
6 |
|
if ($node->hasAttribute("only") && $node->getAttribute("only") !== "false") { |
44
|
3 |
|
$code .= " only"; |
45
|
3 |
|
} |
46
|
|
|
|
47
|
6 |
|
$ext = $context->createControlNode($code); |
48
|
|
|
|
49
|
6 |
|
$set = iterator_to_array($node->childNodes); |
50
|
|
|
|
51
|
6 |
|
$n = $node->ownerDocument->createTextNode("\n"); |
52
|
6 |
|
array_unshift($set, $n); |
53
|
6 |
|
array_unshift($set, $ext); |
54
|
|
|
|
55
|
6 |
|
$set[] = $context->createControlNode("endembed"); |
56
|
|
|
|
57
|
6 |
|
DOMHelper::replaceWithSet($node, $set); |
58
|
6 |
|
} |
59
|
|
|
} |
60
|
|
|
|