7.7. Opt_Compiler_Processor class
7.7.12. processNode()
7.7.11. processAttribute()
« Previous
7.7.13. processSystemVar()
Next »

7.7.12. processNode()

ConstructOptional method for implementation by the programmer
Visibilitypublic
Referencevoid processNode(Opt_Xml_Node $node)
Argument list
$node - Opt_Xml_Node
The node to process.

This method is used to implement, how to process the XML tags registered by the instruction processor. It is called automatically by the compiler every time it founds any matching tag and is executed before the tag children. As an argument, it takes the node to be processed.

OPT does not send the instruction node children to be processed by the compiler. The processor has to do it on its own with Opt_Compiler_Processor::_process().

In order to illustrate, how to implement the content of this method, we show one of the real implementations from OPT source code: the opt:for instruction:

public function processNode(Opt_Xml_Node $node)
{
    // Step 1
    $params = array(
        'begin' => array(0 => self::REQUIRED, self::ASSIGN_EXPR),
        'while' => array(0 => self::REQUIRED, self::ASSIGN_EXPR),
        'iterate' => array(0 => self::REQUIRED, self::ASSIGN_EXPR),
        'separator' => $this->getSeparatorConfig()
    );
    $this->_extractAttributes($node, $params);
 
    // Step 2
    $this->_nesting++;
 
    // Step 3
    $node->addBefore(Opt_Xml_Buffer::TAG_BEFORE, ' for('.$params['begin'].'; '.$params['while'].'; '.$params['iterate'].'){ ');
    $node->addAfter(Opt_Xml_Buffer::TAG_AFTER, ' } ');
 
    $this->processSeparator('$__for'.$this->_nesting, $params['separator'], $node);
 
    // Step 4
    $node->set('postprocess', true);
    $this->_process($node);
} // end processNode();

The description of the steps:

  1. As this processor registers only one tag, we are sure that $node always points to the opt:for. We begin the compilation with parsing the instruction attributes with Opt_Compiler_Processor::_extractAttributes().

  2. We modify some of the processor object fields. In this case, $this->_nesting represents the current opt:for nesting level. We need this value to create unique variable names for the very loop.

  3. With Opt_Xml_Buffer::addBefore() and Opt_Xml_Buffer::addAfter() we bind some PHP code that will replace the instruction tag in the output file to the node buffers. Note that we fill the parts of the for declaration with the attribute values that have already been parsed as OPT expressions.

  4. As the node content is not processed by default, we have to call Opt_Compiler_Processor::_process() on the node. This method registers its children in the processing queue. Moreover, once the children are processed, we have to decrement the nesting counter. We can do it by setting postprocess to true on the $node. It forces the compiler to send our opt:for again to the processor after it finishes with the children. However, the next processing will be performed by Opt_Compiler_Processor::postprocessNode().

Some instructions may require much more sophisticated implementations.

See also:

7.7.12. processNode()
7.7. Opt_Compiler_Processor class
« Previous
7.7.11. processAttribute()
Next »
7.7.13. processSystemVar()