You are here

class HamlNode in Sassy 7

HamlNode class. Base class for all Haml nodes. @package PHamlP @subpackage Haml.tree

Hierarchy

Expanded class hierarchy of HamlNode

File

phamlp/haml/tree/HamlNode.php, line 27

View source
class HamlNode {

  /**
   * @var HamlNode root node of this node
   */
  protected $root;

  /**
   * @var HamlNode parent of this node
   */
  protected $parent;

  /**
   * @var array children of this node
   */
  protected $children = array();

  /**
   * @var array source line token
   */
  public $token;

  /**
   * @var boolean whether to show the output in the browser for debug
   */
  public $showOutput;

  /**
   * @var boolean whether to show the source in the browser for debug
   */
  public $showSource;

  /**
   * @var string content to render
   */
  public $content;

  /**
   * @var string output buffer
   */
  protected $output;

  /**
   * @var HamlRenderer Renderer object
   */
  private $_r;

  /**
   * @var array Options
   */
  private $_o;
  public function __construct($content, $parent) {
    $this->content = $content;
    if (!is_null($parent)) {

      // $parent === null for "else" code blocks
      $this->parent = $parent;
      $this->root = $parent->root;
      $parent->children[] = $this;
    }
  }

  /**
   * Getter.
   * @param string name of property to get
   * @return mixed return value of getter function
   */
  public function __get($name) {
    $getter = 'get' . ucfirst($name);
    if (method_exists($this, $getter)) {
      return $this
        ->{$getter}();
    }
    throw new HamlNodeException('No getter function for {what}', array(
      '{what}' => $name,
    ));
  }

  /**
   * Setter.
   * @param string name of property to set
   * @return mixed value of property
   * @return HamlNode this node
   */
  public function __set($name, $value) {
    $setter = 'set' . ucfirst($name);
    if (method_exists($this, $setter)) {
      $this
        ->{$setter}($value);
      return $this;
    }
    throw new HamlNodeException('No setter function for {what}', array(
      '{what}' => $name,
    ));
  }

  /**
   * Return a value indicating if this node has a parent
   * @return array the node's parent
   */
  public function hasParent() {
    return !empty($this->parent);
  }

  /**
   * Returns the node's content and that of its child nodes
   * @param integer the indent level. This is to allow properly indented output
   * that filters (e.g. Sass) may need.
   * @return string the node's content and that of its child nodes
   */
  public function getContent($indentLevel = 0) {
    $output = str_repeat(' ', 2 * $indentLevel++) . $this->content . "\n";
    foreach ($this->children as $child) {
      $output .= $child
        ->getContent($indentLevel);
    }

    // foreach
    return $output;
  }

  /**
   * Returns the node's parent
   * @return array the node's parent
   */
  public function getParent() {
    return $this->parent;
  }

  /**
   * Returns a value indicating if this node has children
   * @return boolean true if the node has children, false if not
   */
  public function hasChildren() {
    return !empty($this->children);
  }

  /**
   * Returns the node's children
   * @return array the node's children
   */
  public function getChildren() {
    return $this->children;
  }

  /**
   * Returns the last child node of this node.
   * @return HamlNode the last child node of this node
   */
  public function getLastChild() {
    return $this->children[count($this->children) - 1];
  }

  /**
   * Returns the indent level of this node.
   * @return integer the indent level of this node
   */
  private function getLevel() {
    return $this->token['level'];
  }

  /**
   * Sets the indent level of this node.
   * Used during rendering to give correct indentation.
   * @param integer the indent level of this node
   * @return HamlNode this node
   */
  private function setLevel($level) {
    $this->token['level'] = $level;
    return $this;
  }

  /**
   * Returns the source for this node
   * @return string the source for this node
   */
  private function getSource() {
    return $this->token[HamlParser::HAML_SOURCE];
  }

  /**
   * Returns the source for this node
   * @return string the source for this node
   */
  private function getLine() {
    return $this->token['line'];
  }

  /**
   * Returns the filename for this node
   * @return string the filename for this node
   */
  private function getFilename() {
    return $this->token['filename'];
  }

  /**
   * Returns the options.
   * @return array the options
   */
  public function getOptions() {
    if (empty($this->_o)) {
      $this->_r = $this->root->options;
    }
    return $this->_o;
  }

  /**
   * Returns the renderer.
   * @return HamlRenderer the rendered
   */
  public function getRenderer() {
    if (empty($this->_r)) {
      $this->_r = $this->root->renderer;
    }
    return $this->_r;
  }
  public function render() {
    $output = $this->renderer
      ->renderContent($this);
    foreach ($this->children as $child) {
      $output .= $child
        ->render();
    }

    // foreach
    return $this
      ->debug($output);
  }
  protected function debug($output) {
    $output = $this->showSource ? $this
      ->showSource($output) : $output;
    return $this->showOutput && $this->line['indentLevel'] == 0 ? nl2br(str_replace(' ', ' ', htmlspecialchars($output))) : $output;
  }

  /**
   * Adds a comment with source debug information for the current line to the output.
   * The debug information is:
   * + source file (relative to the application path)
   * + line number
   * + indent level
   * + source code
   * @param array source line(s) that generated the ouput
   */
  protected function showSource($output) {
    return "<!--\n  ({$this->line['file']} {$this->line['number']}:{$this->line['indentLevel']})\n  {$this->line[HamlParser::HAML_SOURCE]}\n-->\n{$output}";
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HamlNode::$children protected property *
HamlNode::$content public property *
HamlNode::$output protected property *
HamlNode::$parent protected property *
HamlNode::$root protected property *
HamlNode::$showOutput public property *
HamlNode::$showSource public property *
HamlNode::$token public property *
HamlNode::$_o private property *
HamlNode::$_r private property *
HamlNode::debug protected function
HamlNode::getChildren public function * Returns the node's children *
HamlNode::getContent public function * Returns the node's content and that of its child nodes *
HamlNode::getFilename private function * Returns the filename for this node *
HamlNode::getLastChild public function * Returns the last child node of this node. *
HamlNode::getLevel private function * Returns the indent level of this node. *
HamlNode::getLine private function * Returns the source for this node *
HamlNode::getOptions public function * Returns the options. *
HamlNode::getParent public function * Returns the node's parent *
HamlNode::getRenderer public function * Returns the renderer. *
HamlNode::getSource private function * Returns the source for this node *
HamlNode::hasChildren public function * Returns a value indicating if this node has children *
HamlNode::hasParent public function * Return a value indicating if this node has a parent *
HamlNode::render public function 7
HamlNode::setLevel private function * Sets the indent level of this node. * Used during rendering to give correct indentation. *
HamlNode::showSource protected function * Adds a comment with source debug information for the current line to the output. * The debug information is: * + source file (relative to the application path) * + line number * + indent level * + source code *
HamlNode::__construct public function 4
HamlNode::__get public function * Getter. *
HamlNode::__set public function * Setter. *