Example #1
0
 public function addNode(RootNode $node = null)
 {
     if ($node === null) {
         return false;
     }
     $node->setParent($this);
     $this->_children[] = $node;
     ++$this->_childrenCount;
     return true;
 }
    public function testNSHelpers()
    {
        $doc = RootNode::create('Pharborist');
        $this->assertTrue($doc->hasNamespace('Pharborist'));
        $this->assertFalse($doc->hasNamespace('\\Drupal'));
        $this->assertContains('Pharborist', $doc->getNamespaceNames());
        $this->assertContains('\\Pharborist', $doc->getNamespaceNames(TRUE));
        $namespaces = $doc->getNamespaces();
        $this->assertInstanceOf('\\Pharborist\\NodeCollection', $namespaces);
        $this->assertCount(1, $namespaces);
        $this->assertNull($doc->getNamespace('Drupal'));
        $ns = $doc->getNamespace('Pharborist');
        $this->assertInstanceOf('\\Pharborist\\Namespaces\\NamespaceNode', $ns);
        $this->assertSame($ns, $namespaces[0]);
        $code = <<<'END'
<?php
namespace RoundTable\
  Knights\
  MontyPython;

class Foo {}
END;
        $doc = Parser::parseSource($code);
        $this->assertTrue($doc->hasNamespace('RoundTable\\Knights\\MontyPython'));
        $this->assertContains('\\RoundTable\\Knights\\MontyPython', $doc->getNamespaceNames(TRUE));
    }
Example #3
0
 public static function getInstance()
 {
     if (isset($_SESSION['instance']) && null !== $_SESSION['instance']) {
         self::$_instance = unserialize($_SESSION['instance']);
     } else {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Example #4
0
 /**
  * Compiles haml from an array of lines.
  *
  * @param array $rawLines        	
  */
 public function parseLines(array $rawLines = array())
 {
     $this->_currLine = 0;
     $this->_lines = $rawLines;
     // $this->removeEmptyLines($rawLines);
     $rootNode = new RootNode();
     $rootNode->setCompiler($this);
     $rootNode->setLineNumber(0);
     $nodeFactory = $this->_hamlphp->getNodeFactory();
     $filterContext = false;
     $filterIndentLevel = 0;
     $filterNode = null;
     for ($len = count($this->_lines); $this->_currLine < $len; ++$this->_currLine) {
         $currLine = $this->_lines[$this->_currLine];
         try {
             if ($this->getIndentLevel($currLine) <= $filterIndentLevel) {
                 $filterContext = false;
             }
             if ($filterContext) {
                 $nd = new HamlNode($currLine);
                 $filterNode->addNode($nd);
             } else {
                 if (trim($currLine) == '') {
                     continue;
                 }
                 $nd = $nodeFactory->createNode($currLine, $this->_currLine, $this);
                 if ($nd instanceof FilterNode) {
                     if ($filterContext) {
                         throw new SyntaxErrorException('You cannot nest filters.');
                     }
                     $filterContext = true;
                     $filterNode = $nd;
                     $filterIndentLevel = $this->getIndentLevel($currLine);
                 }
                 $rootNode->addNode($nd);
             }
         } catch (Exception $e) {
             throw new SyntaxErrorException("Error parsing line:\n{$currLine}\n" . $e->getMessage(), $e->getCode());
         }
     }
     return $rootNode->render();
 }
Example #5
0
 public function main()
 {
     $root = RootNode::getInstance();
     if (!empty($_REQUEST)) {
         if (isset($_REQUEST['command']) && $_REQUEST['command'] == 'add') {
             $root->addNode();
         } elseif (isset($_REQUEST['command']) && $_REQUEST['command'] == 'delete' && isset($_REQUEST['node']) && null !== $_REQUEST['node']) {
             $root->deleteNode((int) $_REQUEST['node']);
         }
         $root = RootNode::getInstance();
     }
     $root::output($root);
 }
Example #6
0
 /**
  * Do final cleanup before returning the parsed text. This includes running through the
  * visitors, checking for errors, and examining unclosed tags.
  *
  * @return string
  */
 private function cleanup()
 {
     // Go through the visitors
     foreach ($this->visitors as $visitor) {
         $this->root->accept($visitor);
     }
     // Add unclosed tag error
     if (count($this->nameStack) > 1) {
         $this->addError('Unclosed Tags', array_slice($this->nameStack, 1));
     }
     // Return the HTML
     return $this->root->getHTML();
 }
Example #7
0
 public function setIndentationLevel($level)
 {
     $oldLevel = $this->getIndentationLevel();
     parent::setIndentationLevel($level);
     $this->_spaces = $this->createSpaces();
     if ($this->hasChildren()) {
         $children = $this->getChildren();
         for ($i = 0, $len = count($children); $i < $len; $i++) {
             $childNode = $children[$i];
             $currentLevel = $this->getIndentationLevel();
             $childLevel = $childNode->getIndentationLevel();
             $oldDiff = $childLevel - $oldLevel;
             $newLevel = $currentLevel + $oldDiff;
             $childNode->setIndentationLevel($newLevel);
         }
     }
 }
Example #8
0
 public function deleteNode($i)
 {
     $root = RootNode::getInstance();
     $root->child = null;
     $_SESSION['instance'] = serialize($this->rebuild($i, $root));
 }