diff --git a/src/Goetas/Twital/Attribute.php b/src/Goetas/Twital/Attribute.php index e576e84..0e1db6c 100644 --- a/src/Goetas/Twital/Attribute.php +++ b/src/Goetas/Twital/Attribute.php @@ -1,8 +1,6 @@ ownerElement; diff --git a/src/Goetas/Twital/Attribute/AttrAttribute.php b/src/Goetas/Twital/Attribute/AttrAttribute.php index 0a72e18..2da0410 100644 --- a/src/Goetas/Twital/Attribute/AttrAttribute.php +++ b/src/Goetas/Twital/Attribute/AttrAttribute.php @@ -3,9 +3,8 @@ use Goetas\Twital\Attribute; use Goetas\Twital\Compiler; -use DOMAttr; use Goetas\Twital\Helper\ParserHelper; -use Exception; +use Goetas\Twital\Exception; /** * @@ -14,13 +13,12 @@ */ class AttrAttribute implements Attribute { - public static function getVarname(\DOMNode $node) { return "__a9" . strtr(spl_object_hash($node), "-","_"); } - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; $expressions = ParserHelper::staticSplitExpression($att->value, ","); diff --git a/src/Goetas/Twital/Attribute/BaseAttribute.php b/src/Goetas/Twital/Attribute/BaseAttribute.php index 900dacd..28e8e6e 100644 --- a/src/Goetas/Twital/Attribute/BaseAttribute.php +++ b/src/Goetas/Twital/Attribute/BaseAttribute.php @@ -3,7 +3,6 @@ use Goetas\Twital\Attribute as AttributeBase; use Goetas\Twital\Compiler; -use DOMAttr; /** * @@ -12,7 +11,7 @@ */ class BaseAttribute implements AttributeBase { - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; diff --git a/src/Goetas/Twital/Attribute/BlockInnerAttribute.php b/src/Goetas/Twital/Attribute/BlockInnerAttribute.php index ed4c343..223eb81 100644 --- a/src/Goetas/Twital/Attribute/BlockInnerAttribute.php +++ b/src/Goetas/Twital/Attribute/BlockInnerAttribute.php @@ -3,7 +3,6 @@ use Goetas\Twital\Attribute as AttributeBase; use Goetas\Twital\Compiler; -use DOMAttr; /** * This will translate '
foo
' into '
{% block name%}foo{% endblock %}
' @@ -12,7 +11,7 @@ */ class BlockInnerAttribute implements AttributeBase { - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; @@ -20,7 +19,7 @@ public function visit(DOMAttr $att, Compiler $context) if ($node->firstChild) { $node->insertBefore($pi, $node->firstChild); - }else{ + } else { $node->appendChild($pi); } diff --git a/src/Goetas/Twital/Attribute/BlockOuterAttribute.php b/src/Goetas/Twital/Attribute/BlockOuterAttribute.php index 6031f9b..5df521c 100644 --- a/src/Goetas/Twital/Attribute/BlockOuterAttribute.php +++ b/src/Goetas/Twital/Attribute/BlockOuterAttribute.php @@ -3,7 +3,6 @@ use Goetas\Twital\Attribute as AttributeBase; use Goetas\Twital\Compiler; -use DOMAttr; /** * This will translate '
foo
' into '{% block name%}
foo
{% endblock %}' @@ -13,7 +12,7 @@ */ class BlockOuterAttribute implements AttributeBase { - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; diff --git a/src/Goetas/Twital/Attribute/CaptureAttribute.php b/src/Goetas/Twital/Attribute/CaptureAttribute.php index 29a7e40..f9aaa8d 100644 --- a/src/Goetas/Twital/Attribute/CaptureAttribute.php +++ b/src/Goetas/Twital/Attribute/CaptureAttribute.php @@ -3,7 +3,6 @@ use Goetas\Twital\Attribute; use Goetas\Twital\Compiler; -use DOMAttr; /** * @@ -12,8 +11,7 @@ */ class CaptureAttribute implements Attribute { - - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; diff --git a/src/Goetas/Twital/Attribute/ContentAttribute.php b/src/Goetas/Twital/Attribute/ContentAttribute.php index 9917295..183e352 100644 --- a/src/Goetas/Twital/Attribute/ContentAttribute.php +++ b/src/Goetas/Twital/Attribute/ContentAttribute.php @@ -3,7 +3,6 @@ use Goetas\Twital\Attribute; use Goetas\Twital\Compiler; -use DOMAttr; use Goetas\Twital\Helper\DOMHelper; /** @@ -13,8 +12,7 @@ */ class ContentAttribute implements Attribute { - - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; DOMHelper::removeChilds($node); diff --git a/src/Goetas/Twital/Attribute/ElseAttribute.php b/src/Goetas/Twital/Attribute/ElseAttribute.php index 3459ccd..6f47600 100644 --- a/src/Goetas/Twital/Attribute/ElseAttribute.php +++ b/src/Goetas/Twital/Attribute/ElseAttribute.php @@ -3,8 +3,6 @@ use Goetas\Twital\Attribute as AttributeBase; use Goetas\Twital\Compiler; -use DOMAttr; -use Goetas\Twital\Twital; use Goetas\Twital\Exception; /** @@ -14,11 +12,11 @@ */ class ElseAttribute implements AttributeBase { - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; - if(!$prev = IfAttribute::findPrevElement($node)){ + if (!$prev = IfAttribute::findPrevElement($node)) { throw new Exception("The attribute 'elseif' must be the very next sibling of an 'if' of 'elseif' attribute"); } diff --git a/src/Goetas/Twital/Attribute/ElseIfAttribute.php b/src/Goetas/Twital/Attribute/ElseIfAttribute.php index 088f9aa..e040f6b 100644 --- a/src/Goetas/Twital/Attribute/ElseIfAttribute.php +++ b/src/Goetas/Twital/Attribute/ElseIfAttribute.php @@ -3,16 +3,21 @@ use Goetas\Twital\Attribute as AttributeBase; use Goetas\Twital\Compiler; -use DOMAttr; use Goetas\Twital\Twital; use Goetas\Twital\Exception; + +/** + * + * @author Asmir Mustafic + * + */ class ElseIfAttribute implements AttributeBase { - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; - if(!$prev = IfAttribute::findPrevElement($node)){ + if (!$prev = IfAttribute::findPrevElement($node)) { throw new Exception("The attribute 'elseif' must be the very next sibling of an 'if' of 'elseif' attribute"); } @@ -22,7 +27,7 @@ public function visit(DOMAttr $att, Compiler $context) if (!($nextElement = IfAttribute::findNextElement($node)) || (!$nextElement->hasAttributeNS(Twital::NS, 'elseif') && !$nextElement->hasAttributeNS(Twital::NS, 'else'))) { $pi = $context->createControlNode("endif"); $node->parentNode->insertBefore($pi, $node->nextSibling); // insert after - }else{ + } else { IfAttribute::removeWhitespace($node); } diff --git a/src/Goetas/Twital/Attribute/IfAttribute.php b/src/Goetas/Twital/Attribute/IfAttribute.php index 45cf3eb..9bc17d2 100644 --- a/src/Goetas/Twital/Attribute/IfAttribute.php +++ b/src/Goetas/Twital/Attribute/IfAttribute.php @@ -3,7 +3,6 @@ use Goetas\Twital\Attribute as AttributeBase; use Goetas\Twital\Compiler; -use DOMAttr; use Goetas\Twital\Twital; /** @@ -13,7 +12,7 @@ */ class IfAttribute implements AttributeBase { - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; $pi = $context->createControlNode("if " . html_entity_decode($att->value)); @@ -22,14 +21,14 @@ public function visit(DOMAttr $att, Compiler $context) if (!($nextElement = self::findNextElement($node)) || (!$nextElement->hasAttributeNS(Twital::NS, 'elseif') && !$nextElement->hasAttributeNS(Twital::NS, 'else'))) { $pi = $context->createControlNode("endif"); $node->parentNode->insertBefore($pi, $node->nextSibling); // insert after - }else{ + } else { self::removeWhitespace($node); } $node->removeAttributeNode($att); } public static function removeWhitespace(\DOMElement $element) { - while ($el = $element->nextSibling){ + while ($el = $element->nextSibling) { if ($el instanceof \DOMText) { $element->parentNode->removeChild($el); } else { @@ -40,27 +39,29 @@ public static function removeWhitespace(\DOMElement $element) public static function findNextElement(\DOMElement $element) { $next = $element; - while ($next = $next->nextSibling){ - if($next instanceof \DOMText && trim($next->textContent)){ - return null; - } - if ($next instanceof \DOMElement){ - return $next; - } - } - return null; + while ($next = $next->nextSibling) { + if ($next instanceof \DOMText && trim($next->textContent)) { + return null; + } + if ($next instanceof \DOMElement) { + return $next; + } + } + + return null; } public static function findPrevElement(\DOMElement $element) { $prev = $element; - while ($prev = $prev->previousSibling){ - if($prev instanceof \DOMText && trim($prev->textContent)){ + while ($prev = $prev->previousSibling) { + if ($prev instanceof \DOMText && trim($prev->textContent)) { return null; } - if ($prev instanceof \DOMElement){ + if ($prev instanceof \DOMElement) { return $prev; } } + return null; } } diff --git a/src/Goetas/Twital/Attribute/OmitAttribute.php b/src/Goetas/Twital/Attribute/OmitAttribute.php index 172e84c..d2f9f36 100644 --- a/src/Goetas/Twital/Attribute/OmitAttribute.php +++ b/src/Goetas/Twital/Attribute/OmitAttribute.php @@ -3,8 +3,6 @@ use Goetas\Twital\Attribute; use Goetas\Twital\Compiler; -use DOMAttr; -use Goetas\Twital\Helper\DOMHelper; /** * @@ -13,8 +11,7 @@ */ class OmitAttribute implements Attribute { - - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; diff --git a/src/Goetas/Twital/Attribute/SetAttribute.php b/src/Goetas/Twital/Attribute/SetAttribute.php index 9d18b48..f9f8fd9 100644 --- a/src/Goetas/Twital/Attribute/SetAttribute.php +++ b/src/Goetas/Twital/Attribute/SetAttribute.php @@ -3,7 +3,6 @@ use Goetas\Twital\Attribute; use Goetas\Twital\Compiler; -use DOMAttr; use Goetas\Twital\Helper\ParserHelper; /** @@ -13,8 +12,7 @@ */ class SetAttribute implements Attribute { - - public function visit(DOMAttr $att, Compiler $context) + public function visit(\DOMAttr $att, Compiler $context) { $node = $att->ownerElement; diff --git a/src/Goetas/Twital/Compiler.php b/src/Goetas/Twital/Compiler.php index 9f23291..404d60b 100644 --- a/src/Goetas/Twital/Compiler.php +++ b/src/Goetas/Twital/Compiler.php @@ -8,7 +8,6 @@ */ class Compiler { - /** * * @var array @@ -39,17 +38,13 @@ public function __construct(Twital $twital, array $lexerOptions = array()) 'tag_variable' => array( '{{', '}}' - ), - 'interpolation' => array( - '#{', - '}' ) ), $lexerOptions); } /** * - * @return DOMDocument + * @return \DOMDocument */ public function getDocument() { @@ -59,22 +54,24 @@ public function getDocument() /** * * @param string $content - * @return DOMCDATASection + * @return \DOMCDATASection */ public function createPrintNode($content) { $printPart = $this->getLexerOption('tag_variable'); + return $this->document->createCDATASection("__[__{$printPart[0]} {$content} {$printPart[1]}__]__"); } /** * * @param string $content - * @return DOMCDATASection + * @return \DOMCDATASection */ public function createControlNode($content) { $printPart = $this->getLexerOption('tag_block'); + return $this->document->createCDATASection("__[__{$printPart[0]} " . $content . " {$printPart[1]}__]__"); } @@ -82,6 +79,7 @@ private function getLexerOption($name) { return $this->lexerOptions[$name]; } + /** * @param \DOMDocument $doc * @return void @@ -146,4 +144,4 @@ public function compileChilds(\DOMNode $node) } } } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/EventDispatcher/SourceEvent.php b/src/Goetas/Twital/EventDispatcher/SourceEvent.php index 08d432a..fd4d5b5 100644 --- a/src/Goetas/Twital/EventDispatcher/SourceEvent.php +++ b/src/Goetas/Twital/EventDispatcher/SourceEvent.php @@ -23,28 +23,31 @@ class SourceEvent extends Event protected $template; public function __construct(Twital $twital, $template) { - $this->twital = $twital; - $this->template = $template; + $this->twital = $twital; + $this->template = $template; } /** * @return \Goetas\Twital\Twital */ - public function getTwital() { - return $this->twital; - } - /** - * @return string - */ - public function getTemplate() { - return $this->template; - } + public function getTwital() + { + return $this->twital; + } + /** + * @return string + */ + public function getTemplate() + { + return $this->template; + } - /** - * @param string $template - * @return void - */ - public function setTemplate($template) { - $this->template = $template; - } + /** + * @param string $template + * @return void + */ + public function setTemplate($template) + { + $this->template = $template; + } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/EventDispatcher/TemplateEvent.php b/src/Goetas/Twital/EventDispatcher/TemplateEvent.php index a8bcb46..8786890 100644 --- a/src/Goetas/Twital/EventDispatcher/TemplateEvent.php +++ b/src/Goetas/Twital/EventDispatcher/TemplateEvent.php @@ -24,28 +24,31 @@ class TemplateEvent extends Event protected $template; public function __construct(Twital $twital, Template $template) { - $this->twital = $twital; - $this->template = $template; + $this->twital = $twital; + $this->template = $template; } /** * @return \Goetas\Twital\Twital */ - public function getTwital() { - return $this->twital; - } - /** - * @return \Goetas\Twital\Template - */ - public function getTemplate() { - return $this->template; - } + public function getTwital() + { + return $this->twital; + } + /** + * @return \Goetas\Twital\Template + */ + public function getTemplate() + { + return $this->template; + } - /** - * @param \Goetas\Twital\Template $template - * @return void - */ - public function setTemplate(Template $template) { - $this->template = $template; - } + /** + * @param \Goetas\Twital\Template $template + * @return void + */ + public function setTemplate(Template $template) + { + $this->template = $template; + } } diff --git a/src/Goetas/Twital/EventSubscriber/CustomNamespaceRawSubscriber.php b/src/Goetas/Twital/EventSubscriber/CustomNamespaceRawSubscriber.php index f4ec3f0..5d913b1 100644 --- a/src/Goetas/Twital/EventSubscriber/CustomNamespaceRawSubscriber.php +++ b/src/Goetas/Twital/EventSubscriber/CustomNamespaceRawSubscriber.php @@ -11,7 +11,6 @@ */ class CustomNamespaceRawSubscriber implements EventSubscriberInterface { - public static function getSubscribedEvents() { return array( @@ -45,4 +44,4 @@ public function addCustomNamespace(SourceEvent $event) $event->setTemplate($xml); } } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/EventSubscriber/CustomNamespaceSubscriber.php b/src/Goetas/Twital/EventSubscriber/CustomNamespaceSubscriber.php index abc10ec..f936a2d 100644 --- a/src/Goetas/Twital/EventSubscriber/CustomNamespaceSubscriber.php +++ b/src/Goetas/Twital/EventSubscriber/CustomNamespaceSubscriber.php @@ -12,10 +12,11 @@ */ class CustomNamespaceSubscriber implements EventSubscriberInterface { - public static function getSubscribedEvents(){ - return array( - 'compiler.post_load'=>'addCustomNamespace' - ); + public static function getSubscribedEvents() + { + return array( + 'compiler.post_load'=>'addCustomNamespace' + ); } /** * @@ -36,4 +37,4 @@ public function addCustomNamespace(TemplateEvent $event) } } } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/EventSubscriber/DOMMessSubscriber.php b/src/Goetas/Twital/EventSubscriber/DOMMessSubscriber.php index f6ea24e..f3b106a 100644 --- a/src/Goetas/Twital/EventSubscriber/DOMMessSubscriber.php +++ b/src/Goetas/Twital/EventSubscriber/DOMMessSubscriber.php @@ -12,7 +12,6 @@ */ class DOMMessSubscriber implements EventSubscriberInterface { - public static function getSubscribedEvents() { return array( @@ -45,9 +44,8 @@ public function removeCdata(SourceEvent $event) public function fixAttributes(SourceEvent $event) { - $event->setTemplate(preg_replace_callback('/ __attr__="(__a[0-9a-f]+)"/', function ($mch) - { + $event->setTemplate(preg_replace_callback('/ __attr__="(__a[0-9a-f]+)"/', function ($mch) { return '{% for ____ak,____av in ' . $mch[1] . ' if ____av|length>0 %} {{____ak | raw}}="{{ ____av|join(\'\') }}"{% endfor %}'; }, $event->getTemplate())); } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/EventSubscriber/FixHtmlEntitiesInExpressionSubscriber.php b/src/Goetas/Twital/EventSubscriber/FixHtmlEntitiesInExpressionSubscriber.php index 07c1d6e..c503937 100644 --- a/src/Goetas/Twital/EventSubscriber/FixHtmlEntitiesInExpressionSubscriber.php +++ b/src/Goetas/Twital/EventSubscriber/FixHtmlEntitiesInExpressionSubscriber.php @@ -13,12 +13,10 @@ */ class FixHtmlEntitiesInExpressionSubscriber implements EventSubscriberInterface { - public static function getSubscribedEvents() { return array( 'compiler.pre_load' => 'addPlaceholderOnLoad', - //'compiler.pre_dump' => 'addPlaceholder', 'compiler.post_dump' => 'removePlaceholder' ); } @@ -48,25 +46,25 @@ public function addPlaceholderOnLoad(SourceEvent $event) '{#' => '#}' ); $offset = 0; - while(preg_match ("/".implode("|", array_map('preg_quote', array_keys($exprs)))."/" , $source , $matches , PREG_OFFSET_CAPTURE, $offset)){ + while (preg_match("/" . implode("|", array_map('preg_quote', array_keys($exprs))) . "/", $source, $matches, PREG_OFFSET_CAPTURE, $offset)) { - $source = substr($source, 0, $matches[0][1]).$this->placeholder[0].substr($source, $matches[0][1]); + $source = substr($source, 0, $matches[0][1]) . $this->placeholder[0] . substr($source, $matches[0][1]); - $startoffset = $offset = $matches[0][1]+strlen($matches[0][0])+strlen($this->placeholder[0]); + $startoffset = $offset = $matches[0][1] + strlen($matches[0][0]) + strlen($this->placeholder[0]); - do{ + do { $matches2 = array(); - if(preg_match ("/".preg_quote($exprs[$matches[0][0]])."/" , $source , $matches2 , PREG_OFFSET_CAPTURE, $offset)){ + if (preg_match("/" . preg_quote($exprs[$matches[0][0]]) . "/", $source, $matches2, PREG_OFFSET_CAPTURE, $offset)) { - $offset = $matches2[0][1]+strlen($matches2[0][0]); + $offset = $matches2[0][1] + strlen($matches2[0][0]); $inApex = false; - for ($i = $startoffset; $i <$offset; $i ++) { + for ($i = $startoffset; $i < $offset; $i ++) { $chr = $source[$i]; if ($chr == "'" || $chr == '"') { $j = 1; - while ($i>=$j && $source[$i - $j] === '\\') { + while ($i >= $j && $source[$i - $j] === '\\') { $j ++; } @@ -79,23 +77,22 @@ public function addPlaceholderOnLoad(SourceEvent $event) } } } - if(!$inApex){ - $original = $offset-$startoffset; - $encoded = htmlspecialchars(substr($source, $startoffset, $offset-$startoffset), ENT_COMPAT, 'UTF-8'); - - $source = substr($source, 0, $startoffset). $encoded .$this->placeholder[1].substr($source, $offset); + if (! $inApex) { + $original = $offset - $startoffset; + $encoded = htmlspecialchars(substr($source, $startoffset, $offset - $startoffset), ENT_COMPAT, 'UTF-8'); - $offset +=strlen($this->placeholder[1])+(strlen($encoded)-$original); + $source = substr($source, 0, $startoffset) . $encoded . $this->placeholder[1] . substr($source, $offset); + $offset += strlen($this->placeholder[1]) + (strlen($encoded) - $original); } - }else{ + } else { break; } - } while($inApex && $offsetsetTemplate($source); } + /** * * @param SourceEvent $event diff --git a/src/Goetas/Twital/Extension.php b/src/Goetas/Twital/Extension.php index 6e9e3aa..5373c4a 100644 --- a/src/Goetas/Twital/Extension.php +++ b/src/Goetas/Twital/Extension.php @@ -14,11 +14,11 @@ interface Extension * first level as namespace and second level as attribute name. * Example: * - * array( - * 'http://www.w3.org/1998/Math/MathML' => array( - * 'math'=> new MathML\MathAttribute() - * ) - * ) + * array( + * 'http://www.w3.org/1998/Math/MathML' => array( + * 'math'=> new MathML\MathAttribute() + * ) + * ) * * * @return array @@ -31,11 +31,11 @@ public function getAttributes(); * first level as namespace and second level as attribute name. * Example: * - * array( - * 'http://www.w3.org/1998/Math/MathML' => array( - * 'math'=> new MathML\MathNode() - * ) - * ) + * array( + * 'http://www.w3.org/1998/Math/MathML' => array( + * 'math'=> new MathML\MathNode() + * ) + * ) * * * @return array @@ -48,4 +48,4 @@ public function getNodes(); */ public function getSubscribers(); -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/Extension/AbstractExtension.php b/src/Goetas/Twital/Extension/AbstractExtension.php index adcb385..e1a35f6 100644 --- a/src/Goetas/Twital/Extension/AbstractExtension.php +++ b/src/Goetas/Twital/Extension/AbstractExtension.php @@ -10,7 +10,6 @@ */ abstract class AbstractExtension implements Extension { - public function getAttributes() { return array(); diff --git a/src/Goetas/Twital/Extension/CoreExtension.php b/src/Goetas/Twital/Extension/CoreExtension.php index 14b43d5..190bf64 100644 --- a/src/Goetas/Twital/Extension/CoreExtension.php +++ b/src/Goetas/Twital/Extension/CoreExtension.php @@ -67,7 +67,7 @@ public function getNodes() $nodes[Twital::NS]['omit'] = new Node\OmitNode(); $nodes[Twital::NS]['embed'] = new Node\EmbedNode(); $nodes[Twital::NS]['use'] = new Node\UseNode(); + return $nodes; } } - diff --git a/src/Goetas/Twital/Helper/DOMHelper.php b/src/Goetas/Twital/Helper/DOMHelper.php index b37bb48..39cd8ed 100644 --- a/src/Goetas/Twital/Helper/DOMHelper.php +++ b/src/Goetas/Twital/Helper/DOMHelper.php @@ -1,6 +1,11 @@ + * + */ class DOMHelper { public static function removeChilds(\DOMNode $ref) @@ -9,6 +14,7 @@ public static function removeChilds(\DOMNode $ref) $ref->removeChild($ref->firstChild); } } + public static function insertAfterSet(\DOMNode $node, array $newNodes) { $ref = $node; @@ -26,10 +32,12 @@ public static function replaceWithSet(\DOMNode $node, array $newNodes) self::insertAfterSet($node, $newNodes); $node->parentNode->removeChild($node); } + public static function remove(\DOMNode $ref) { return $ref->parentNode->removeChild($ref); } + public static function checkNamespaces(\DOMElement $element, array $namespaces = array()) { if ($element->namespaceURI === null && preg_match('/^([a-z0-9\-]+):(.+)$/i', $element->nodeName, $mch) && isset($namespaces[$mch[1]])) { @@ -71,6 +79,7 @@ public static function copyElementInNs($oldElement, $newNamespace) $element->appendChild($child); } $oldElement->parentNode->replaceChild($element, $oldElement); + return $element; } } diff --git a/src/Goetas/Twital/Helper/ParserHelper.php b/src/Goetas/Twital/Helper/ParserHelper.php index 5b834f7..a3884bb 100644 --- a/src/Goetas/Twital/Helper/ParserHelper.php +++ b/src/Goetas/Twital/Helper/ParserHelper.php @@ -1,11 +1,15 @@ + * + */ class ParserHelper { - private static $closing = array( '}' => '{', ')' => '(', @@ -49,8 +53,8 @@ public static function staticSplitExpression($str, $splitter, $limit = 0) if (! count($in) && $chr === $splitter) { $parts[] = substr($str, $prev, $i - $prev); $prev = $i + 1; - if($limit>1 && count($parts)==($limit-1)){ - break; + if ($limit>1 && count($parts)==($limit-1)) { + break; } } } diff --git a/src/Goetas/Twital/Node/BlockNode.php b/src/Goetas/Twital/Node/BlockNode.php index 91c5d95..b009e94 100644 --- a/src/Goetas/Twital/Node/BlockNode.php +++ b/src/Goetas/Twital/Node/BlockNode.php @@ -4,7 +4,7 @@ use Goetas\Twital\Node; use Goetas\Twital\Compiler; use Goetas\Twital\Helper\DOMHelper; -use Exception; +use Goetas\Twital\Exception; use Goetas\Twital\Twital; /** @@ -19,9 +19,6 @@ public function visit(\DOMElement $node, Compiler $context) if (! $node->hasAttribute("name")) { throw new Exception("Name attribute is required"); } - $xp = new \DOMXPath($node->ownerDocument); - - $currPrima = $node->previousSibling; $sandbox = $node->ownerDocument->createElementNS(Twital::NS, "sandbox"); $node->parentNode->insertBefore($sandbox,$node); @@ -40,4 +37,4 @@ public function visit(\DOMElement $node, Compiler $context) DOMHelper::replaceWithSet($sandbox, iterator_to_array($sandbox->childNodes)); DOMHelper::replaceWithSet($node, iterator_to_array($node->childNodes)); } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/Node/EmbedNode.php b/src/Goetas/Twital/Node/EmbedNode.php index c388937..22b00e8 100644 --- a/src/Goetas/Twital/Node/EmbedNode.php +++ b/src/Goetas/Twital/Node/EmbedNode.php @@ -2,10 +2,9 @@ namespace Goetas\Twital\Node; use Goetas\Twital\Node; -use Exception; +use Goetas\Twital\Exception; use Goetas\Twital\Compiler; use Goetas\Twital\Helper\DOMHelper; -use Goetas\Twital\Twital; /** * @@ -14,7 +13,6 @@ */ class EmbedNode implements Node { - public function visit(\DOMElement $node, Compiler $context) { if ($node->hasAttribute("from-exp")) { @@ -58,4 +56,4 @@ public function visit(\DOMElement $node, Compiler $context) DOMHelper::replaceWithSet($node, $set); } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/Node/ExtendsNode.php b/src/Goetas/Twital/Node/ExtendsNode.php index a57df70..6603aed 100644 --- a/src/Goetas/Twital/Node/ExtendsNode.php +++ b/src/Goetas/Twital/Node/ExtendsNode.php @@ -2,10 +2,9 @@ namespace Goetas\Twital\Node; use Goetas\Twital\Node; -use Exception; +use Goetas\Twital\Exception; use Goetas\Twital\Compiler; use Goetas\Twital\Helper\DOMHelper; -use Goetas\Twital\Twital; /** * @@ -14,7 +13,6 @@ */ class ExtendsNode implements Node { - public function visit(\DOMElement $node, Compiler $context) { if ($node->hasAttribute("from-exp")) { @@ -26,8 +24,8 @@ public function visit(\DOMElement $node, Compiler $context) } // remove any non-element node - foreach (iterator_to_array($node->childNodes) as $child){ - if(!($child instanceof \DOMElement)){ + foreach (iterator_to_array($node->childNodes) as $child) { + if (!($child instanceof \DOMElement)) { $child->parentNode->removeChild($child); } } @@ -37,7 +35,7 @@ public function visit(\DOMElement $node, Compiler $context) $ext = $context->createControlNode("extends {$filename}"); $set = iterator_to_array($node->childNodes); - if(count($set)){ + if (count($set)) { $n = $node->ownerDocument->createTextNode("\n"); array_unshift($set, $n); } @@ -46,4 +44,4 @@ public function visit(\DOMElement $node, Compiler $context) DOMHelper::replaceWithSet($node, $set); } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/Node/ImportNode.php b/src/Goetas/Twital/Node/ImportNode.php index 49f712e..4678875 100644 --- a/src/Goetas/Twital/Node/ImportNode.php +++ b/src/Goetas/Twital/Node/ImportNode.php @@ -12,7 +12,6 @@ */ class ImportNode implements Node { - public function visit(\DOMElement $node, Compiler $context) { if ($node->hasAttribute("from-exp")) { @@ -36,4 +35,4 @@ public function visit(\DOMElement $node, Compiler $context) $node->parentNode->replaceChild($pi, $node); } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/Node/IncludeNode.php b/src/Goetas/Twital/Node/IncludeNode.php index 84d45e0..7c4386a 100644 --- a/src/Goetas/Twital/Node/IncludeNode.php +++ b/src/Goetas/Twital/Node/IncludeNode.php @@ -12,7 +12,6 @@ */ class IncludeNode implements Node { - public function visit(\DOMElement $node, Compiler $context) { $code = "include "; @@ -25,7 +24,6 @@ public function visit(\DOMElement $node, Compiler $context) throw new Exception("The 'from' or 'from-exp' attribute is required"); } - if ($node->hasAttribute("ignore-missing") && $node->getAttribute("ignore-missing") !== "false") { $code .= " ignore missing"; } @@ -42,4 +40,4 @@ public function visit(\DOMElement $node, Compiler $context) $pi = $context->createControlNode($code); $node->parentNode->replaceChild($pi, $node); } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/Node/MacroNode.php b/src/Goetas/Twital/Node/MacroNode.php index 9f967e3..096ef2a 100644 --- a/src/Goetas/Twital/Node/MacroNode.php +++ b/src/Goetas/Twital/Node/MacroNode.php @@ -4,7 +4,7 @@ use Goetas\Twital\Node; use Goetas\Twital\Compiler; use Goetas\Twital\Helper\DOMHelper; -use Exception; +use Goetas\Twital\Exception; /** * @@ -13,7 +13,6 @@ */ class MacroNode implements Node { - public function visit(\DOMElement $node, Compiler $context) { if (! $node->hasAttribute("name")) { @@ -27,7 +26,6 @@ public function visit(\DOMElement $node, Compiler $context) $start = $context->createControlNode("macro " . $node->getAttribute("name") . "(" . $node->getAttribute("args") . ")"); array_unshift($set, $start); - $set[] = $context->createControlNode("endmacro"); DOMHelper::replaceWithSet($node, $set); diff --git a/src/Goetas/Twital/Node/OmitNode.php b/src/Goetas/Twital/Node/OmitNode.php index ca6e057..0227bb3 100644 --- a/src/Goetas/Twital/Node/OmitNode.php +++ b/src/Goetas/Twital/Node/OmitNode.php @@ -18,4 +18,4 @@ public function visit(\DOMElement $node, Compiler $context) $context->compileChilds($node); DOMHelper::replaceWithSet($node, iterator_to_array($node->childNodes)); } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/Node/UseNode.php b/src/Goetas/Twital/Node/UseNode.php index d93e8cd..539efb6 100644 --- a/src/Goetas/Twital/Node/UseNode.php +++ b/src/Goetas/Twital/Node/UseNode.php @@ -3,7 +3,7 @@ use Goetas\Twital\Node; use Goetas\Twital\Compiler; -use Exception; +use Goetas\Twital\Exception; /** * @@ -14,7 +14,6 @@ class UseNode implements Node { public function visit(\DOMElement $node, Compiler $context) { - $code = "use "; if ($node->hasAttribute("from")) { @@ -30,4 +29,4 @@ public function visit(\DOMElement $node, Compiler $context) $pi = $context->createControlNode($code); $node->parentNode->replaceChild($pi, $node); } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/SourceAdapter.php b/src/Goetas/Twital/SourceAdapter.php index 51f8ef2..9c2764b 100644 --- a/src/Goetas/Twital/SourceAdapter.php +++ b/src/Goetas/Twital/SourceAdapter.php @@ -10,12 +10,15 @@ interface SourceAdapter { /** * Gets the raw template source code and return a {Goetas\Twital\Template} instance. + * * @param string $string * @return \Goetas\Twital\Template */ public function load($string); + /** * Gets a {Goetas\Twital\Template} instance and return the raw template source code. + * * @param \Goetas\Twital\Template $dom * @return string */ diff --git a/src/Goetas/Twital/SourceAdapter/HTML5Adapter.php b/src/Goetas/Twital/SourceAdapter/HTML5Adapter.php index 7b1de93..271a97b 100644 --- a/src/Goetas/Twital/SourceAdapter/HTML5Adapter.php +++ b/src/Goetas/Twital/SourceAdapter/HTML5Adapter.php @@ -65,10 +65,10 @@ public function dump(Template $template) { $metadata = $template->getMetadata(); - if (! $metadata['doctype']) { return HTML5::saveHTML($template->getDocument()->childNodes); } + return HTML5::saveHTML($template->getDocument()->childNodes); } @@ -87,4 +87,4 @@ protected function collectMetadata(\DOMDocument $dom, $source) return $metadata; } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/SourceAdapter/XHTMLAdapter.php b/src/Goetas/Twital/SourceAdapter/XHTMLAdapter.php index 7d3bab8..b48b4de 100644 --- a/src/Goetas/Twital/SourceAdapter/XHTMLAdapter.php +++ b/src/Goetas/Twital/SourceAdapter/XHTMLAdapter.php @@ -26,6 +26,7 @@ public function dump(Template $template) $source .= $dom->saveXML($node); } } + return $this->replaceShortTags($source); } diff --git a/src/Goetas/Twital/SourceAdapter/XMLAdapter.php b/src/Goetas/Twital/SourceAdapter/XMLAdapter.php index ffc594a..d3ff5cf 100644 --- a/src/Goetas/Twital/SourceAdapter/XMLAdapter.php +++ b/src/Goetas/Twital/SourceAdapter/XMLAdapter.php @@ -52,6 +52,7 @@ public function dump(Template $template) foreach ($dom->childNodes as $node) { $source .= $dom->saveXML($node); } + return $source; } } diff --git a/src/Goetas/Twital/Template.php b/src/Goetas/Twital/Template.php index 2f39611..d2bf2b3 100644 --- a/src/Goetas/Twital/Template.php +++ b/src/Goetas/Twital/Template.php @@ -10,7 +10,6 @@ */ class Template { - /** * The template {DOMDocument} * @@ -54,4 +53,4 @@ public function getMetadata() { return $this->metadata; } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/Twital.php b/src/Goetas/Twital/Twital.php index 0faf80e..1c0b7fa 100644 --- a/src/Goetas/Twital/Twital.php +++ b/src/Goetas/Twital/Twital.php @@ -13,7 +13,6 @@ */ class Twital { - const NS = 'urn:goetas:twital'; protected $extensionsInitialized = false; @@ -62,12 +61,14 @@ public function getEventDispatcher() public function getNodes() { $this->initExtensions(); + return $this->nodes; } public function getAttributes() { $this->initExtensions(); + return $this->attributes; } @@ -78,7 +79,7 @@ protected function initExtensions() $this->attributes = array_merge_recursive($this->attributes, $extension->getAttributes()); $this->nodes = array_merge_recursive($this->nodes, $extension->getNodes()); - foreach ($extension->getSubscribers() as $subscriber){ + foreach ($extension->getSubscribers() as $subscriber) { $this->dispatcher->addSubscriber($subscriber); } } @@ -111,6 +112,7 @@ public function compile(SourceAdapter $adapter, $source) $sourceEvent = new SourceEvent($this, $source); $this->dispatcher->dispatch('compiler.post_dump', $sourceEvent); + return $sourceEvent->getTemplate(); } @@ -128,4 +130,4 @@ public function getExtensions() { return $this->extensions; } -} \ No newline at end of file +} diff --git a/src/Goetas/Twital/TwitalLoader.php b/src/Goetas/Twital/TwitalLoader.php index ef22587..020c052 100644 --- a/src/Goetas/Twital/TwitalLoader.php +++ b/src/Goetas/Twital/TwitalLoader.php @@ -13,7 +13,6 @@ */ class TwitalLoader implements \Twig_LoaderInterface { - /** * Array of patterns used to decide if a template is twital-compilable or not. * Items are strings or callbacks @@ -35,10 +34,11 @@ class TwitalLoader implements \Twig_LoaderInterface * @var Twital */ protected $twital; + /** * Creates a new Twital loader. * @param \Twig_LoaderInterface $loader - * @param Compiler $twital + * @param Twital $twital * @param bool $addDefaults If NULL, some standard rules will be used (`*.twital.*` and `*.twital`). */ public function __construct(\Twig_LoaderInterface $loader = null, Twital $twital = null, $addDefaults = true) @@ -57,18 +57,19 @@ public function __construct(\Twig_LoaderInterface $loader = null, Twital $twital } } - /** * Add a new pattern that can decide if a template is twital-compilable or not. * If $pattern is a string, then must be a valid regex that matches the template filename. * If $pattern is a callback, then must return true if the template is compilable, false otherwise. * * @param string|callback $pattern + * @param SourceAdapter $adapter * @return \Goetas\Twital\TwitalLoader */ public function addSourceAdapter($pattern, SourceAdapter $adapter) { $this->sourceAdapters[$pattern] = $adapter; + return $this; } @@ -85,6 +86,7 @@ public function getSourceAdapters() /** * Decide if a template is twital-compilable or not. * + * @param string $name * @return SourceAdapter */ public function getSourceAdapter($name) @@ -94,6 +96,7 @@ public function getSourceAdapter($name) return $adapter; } } + return null; } @@ -152,17 +155,20 @@ public function getLoader() public function setLoader(\Twig_LoaderInterface $loader) { $this->loader = $loader; + return $this; } + /** * @return \Goetas\Twital\Twital */ - public function getTwital() { + public function getTwital() + { + if ($this->twital===null) { + $this->twital = new Twital(); + } - if ($this->twital===null) { - $this->twital = new Twital(); - } - return $this->twital; - } + return $this->twital; + } -} \ No newline at end of file +} diff --git a/tests/Goetas/Twital/Tests/CoreAttributeTest.php b/tests/Goetas/Twital/Tests/CoreAttributeTest.php index ce45c4a..4d78ca6 100644 --- a/tests/Goetas/Twital/Tests/CoreAttributeTest.php +++ b/tests/Goetas/Twital/Tests/CoreAttributeTest.php @@ -4,10 +4,8 @@ use Goetas\Twital\Twital; use Goetas\Twital\SourceAdapter\XMLAdapter; - class CoreAttributeTest extends \PHPUnit_Framework_TestCase { - /** * Prepares the environment before running a test. */ @@ -56,5 +54,3 @@ public function getData() ); } } - - diff --git a/tests/Goetas/Twital/Tests/CoreNodesTest.php b/tests/Goetas/Twital/Tests/CoreNodesTest.php index ea27d14..c963df7 100644 --- a/tests/Goetas/Twital/Tests/CoreNodesTest.php +++ b/tests/Goetas/Twital/Tests/CoreNodesTest.php @@ -2,12 +2,9 @@ namespace Goetas\Twital\Tests; use Goetas\Twital\Twital; -use Goetas\Twital\SourceAdapter\XMLAdapter; - abstract class CoreNodesTest extends \PHPUnit_Framework_TestCase { - protected $twital; protected $sourceAdapter; /** @@ -20,7 +17,7 @@ protected function setUp() $this->sourceAdapter = $this->getSourceAdapter(); } - protected abstract function getSourceAdapter(); + abstract protected function getSourceAdapter(); /** * @dataProvider getData @@ -79,6 +76,7 @@ public function getDataFormTemplates() $expected, ); } + return $data; } @@ -92,5 +90,3 @@ public function testVisitNodeTemplates($source, $expected) } } - - diff --git a/tests/Goetas/Twital/Tests/DynamicAttrAttributeTest.php b/tests/Goetas/Twital/Tests/DynamicAttrAttributeTest.php index ba8d24e..c3097c9 100644 --- a/tests/Goetas/Twital/Tests/DynamicAttrAttributeTest.php +++ b/tests/Goetas/Twital/Tests/DynamicAttrAttributeTest.php @@ -2,10 +2,8 @@ namespace Goetas\Twital\Tests; use Goetas\Twital\TwitalLoader; -use Goetas\Twital\Twital; use Goetas\Twital\SourceAdapter\XMLAdapter; - class DynamicAttrAttributeTest extends \PHPUnit_Framework_TestCase { /** @@ -48,8 +46,8 @@ public function getData() array('a', 'a'), array('a', 'a'), array('a', 'a'), - - array('
content
', '
content
', array('condition'=>0)), + + array('
content
', '
content
', array('condition'=>0)), array('
content
', '
content
', array('condition'=>1)), array('
content
', '
content
', array('condition'=>1)), @@ -78,5 +76,3 @@ public function testInvalidVisitAttribute($source) $this->twig->render($source); } } - - diff --git a/tests/Goetas/Twital/Tests/Event/SourceEventTest.php b/tests/Goetas/Twital/Tests/Event/SourceEventTest.php index cb0a558..4e617d9 100644 --- a/tests/Goetas/Twital/Tests/Event/SourceEventTest.php +++ b/tests/Goetas/Twital/Tests/Event/SourceEventTest.php @@ -6,6 +6,8 @@ class SourceEventTest extends \PHPUnit_Framework_TestCase { + private $twital; + /** * Prepares the environment before running a test. */ @@ -38,5 +40,3 @@ public function testBaseSetter() $this->assertSame($templateNew, $ist->getTemplate()); } } - - diff --git a/tests/Goetas/Twital/Tests/Event/TemplateEventTest.php b/tests/Goetas/Twital/Tests/Event/TemplateEventTest.php index cd163d0..28b6f5e 100644 --- a/tests/Goetas/Twital/Tests/Event/TemplateEventTest.php +++ b/tests/Goetas/Twital/Tests/Event/TemplateEventTest.php @@ -6,6 +6,9 @@ class TemplateEventTest extends \PHPUnit_Framework_TestCase { + private $twital; + private $template; + /** * Prepares the environment before running a test. */ @@ -39,5 +42,3 @@ public function testBaseSetter() $this->assertSame($template, $ist->getTemplate()); } } - - diff --git a/tests/Goetas/Twital/Tests/ExpressionParserTest.php b/tests/Goetas/Twital/Tests/ExpressionParserTest.php index 0b85b08..9231ed7 100644 --- a/tests/Goetas/Twital/Tests/ExpressionParserTest.php +++ b/tests/Goetas/Twital/Tests/ExpressionParserTest.php @@ -1,14 +1,10 @@ assertEquals($expected, $splitted); } public function getWrongData() @@ -91,5 +86,3 @@ public function getData() ); } } - - diff --git a/tests/Goetas/Twital/Tests/Html5CoreNodesTest.php b/tests/Goetas/Twital/Tests/Html5CoreNodesTest.php index 437fdf6..2b3f933 100644 --- a/tests/Goetas/Twital/Tests/Html5CoreNodesTest.php +++ b/tests/Goetas/Twital/Tests/Html5CoreNodesTest.php @@ -1,17 +1,15 @@ assertContainsOnlyInstancesOf('Goetas\Twital\SourceAdapter', $adapters); foreach ($this->getRequiredAdapters() as $class) { - $filteredAdapters = array_filter($adapters, function ($adapter) use($class) - { + $filteredAdapters = array_filter($adapters, function ($adapter) use ($class) { return is_a($adapter, $class); }); $this->assertGreaterThanOrEqual(1, count($filteredAdapters), "Cant find any $class adapter"); @@ -110,5 +107,3 @@ public function testNonTwitalFile() $twitalLoader->getSource('aaa.txt'); } } - - diff --git a/tests/Goetas/Twital/Tests/XhtmlCoreNodesTest.php b/tests/Goetas/Twital/Tests/XhtmlCoreNodesTest.php index d541792..daac4b6 100644 --- a/tests/Goetas/Twital/Tests/XhtmlCoreNodesTest.php +++ b/tests/Goetas/Twital/Tests/XhtmlCoreNodesTest.php @@ -1,14 +1,12 @@