Skip to content

Commit

Permalink
Merge branch 'hason-clean'
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed May 22, 2014
2 parents 0d2eca1 + ba1563e commit dd4f9cd
Show file tree
Hide file tree
Showing 50 changed files with 210 additions and 241 deletions.
3 changes: 0 additions & 3 deletions src/Goetas/Twital/Attribute.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace Goetas\Twital;

use DOMAttr;

/**
* Represents the handler for custom attributes.
*
Expand All @@ -11,7 +9,6 @@
*/
interface Attribute
{

/**
* Stop processing current node children.
*
Expand Down
1 change: 0 additions & 1 deletion src/Goetas/Twital/Attribute/AttrAppendAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class AttrAppendAttribute extends AttrAttribute
{

public function visit(\DOMAttr $att, Compiler $context)
{
$node = $att->ownerElement;
Expand Down
6 changes: 2 additions & 4 deletions src/Goetas/Twital/Attribute/AttrAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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, ",");
Expand Down
3 changes: 1 addition & 2 deletions src/Goetas/Twital/Attribute/BaseAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Goetas\Twital\Attribute as AttributeBase;
use Goetas\Twital\Compiler;
use DOMAttr;

/**
*
Expand All @@ -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;

Expand Down
5 changes: 2 additions & 3 deletions src/Goetas/Twital/Attribute/BlockInnerAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Goetas\Twital\Attribute as AttributeBase;
use Goetas\Twital\Compiler;
use DOMAttr;

/**
* This will translate '<div t:block-inner="name">foo</div>' into '<div>{% block name%}foo{% endblock %}</div>'
Expand All @@ -12,15 +11,15 @@
*/
class BlockInnerAttribute implements AttributeBase
{
public function visit(DOMAttr $att, Compiler $context)
public function visit(\DOMAttr $att, Compiler $context)
{
$node = $att->ownerElement;

$pi = $context->createControlNode("block " . $att->value);

if ($node->firstChild) {
$node->insertBefore($pi, $node->firstChild);
}else{
} else {
$node->appendChild($pi);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Goetas/Twital/Attribute/BlockOuterAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Goetas\Twital\Attribute as AttributeBase;
use Goetas\Twital\Compiler;
use DOMAttr;

/**
* This will translate '<div t:block-outer="name">foo</div>' into '{% block name%}<div>foo</div>{% endblock %}'
Expand All @@ -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;

Expand Down
4 changes: 1 addition & 3 deletions src/Goetas/Twital/Attribute/CaptureAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Goetas\Twital\Attribute;
use Goetas\Twital\Compiler;
use DOMAttr;

/**
*
Expand All @@ -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;

Expand Down
4 changes: 1 addition & 3 deletions src/Goetas/Twital/Attribute/ContentAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Goetas\Twital\Attribute;
use Goetas\Twital\Compiler;
use DOMAttr;
use Goetas\Twital\Helper\DOMHelper;

/**
Expand All @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions src/Goetas/Twital/Attribute/ElseAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

use Goetas\Twital\Attribute as AttributeBase;
use Goetas\Twital\Compiler;
use DOMAttr;
use Goetas\Twital\Twital;
use Goetas\Twital\Exception;

/**
Expand All @@ -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");
}

Expand Down
13 changes: 9 additions & 4 deletions src/Goetas/Twital/Attribute/ElseIfAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*
*/
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");
}

Expand All @@ -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);
}

Expand Down
33 changes: 17 additions & 16 deletions src/Goetas/Twital/Attribute/IfAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Goetas\Twital\Attribute as AttributeBase;
use Goetas\Twital\Compiler;
use DOMAttr;
use Goetas\Twital\Twital;

/**
Expand All @@ -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));
Expand All @@ -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 {
Expand All @@ -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;
}
}
5 changes: 1 addition & 4 deletions src/Goetas/Twital/Attribute/OmitAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

use Goetas\Twital\Attribute;
use Goetas\Twital\Compiler;
use DOMAttr;
use Goetas\Twital\Helper\DOMHelper;

/**
*
Expand All @@ -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;

Expand Down
4 changes: 1 addition & 3 deletions src/Goetas/Twital/Attribute/SetAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Goetas\Twital\Attribute;
use Goetas\Twital\Compiler;
use DOMAttr;
use Goetas\Twital\Helper\ParserHelper;

/**
Expand All @@ -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;

Expand Down
16 changes: 7 additions & 9 deletions src/Goetas/Twital/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
class Compiler
{

/**
*
* @var array
Expand Down Expand Up @@ -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()
{
Expand All @@ -59,29 +54,32 @@ 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]}__]__");
}

private function getLexerOption($name)
{
return $this->lexerOptions[$name];
}

/**
* @param \DOMDocument $doc
* @return void
Expand Down Expand Up @@ -146,4 +144,4 @@ public function compileChilds(\DOMNode $node)
}
}
}
}
}
Loading

0 comments on commit dd4f9cd

Please sign in to comment.