/** * Resets the processor state. */ public function reset() { if (sizeof(self::$_sections) > 0) { self::$_stack = new SplStack(); self::$_sections = array(); } }
/** * Processes the opt:include node. * @internal * @param Opt_Xml_Node $node The recognized node. */ public function processNode(Opt_Xml_Node $node) { $params = array('file' => array(0 => self::OPTIONAL, self::EXPRESSION, null, 'str'), 'view' => array(0 => self::OPTIONAL, self::EXPRESSION, NULL, 'parse'), 'from' => array(0 => self::OPTIONAL, self::ID, NULL), 'default' => array(0 => self::OPTIONAL, self::EXPRESSION, NULL, 'str'), 'import' => array(0 => self::OPTIONAL, self::BOOL, NULL), 'branch' => array(0 => self::OPTIONAL, self::EXPRESSION, NULL, 'str'), '__UNKNOWN__' => array(0 => self::OPTIONAL, self::EXPRESSION, 'parse')); $vars = $this->_extractAttributes($node, $params); // Conditional attribute control. if (!isset($params['from']) && !isset($params['file']) && !isset($params['view'])) { throw new Opt_IncludeNoAttributes($node->getXmlName()); } // Possible section integration $codeBegin = ''; $codeEnd = ''; $viewExistenceCond = ''; if (isset($params['from'])) { $section = Opt_Instruction_BaseSection::getSection($params['from']); if (is_null($section)) { throw new Opt_SectionNotFound_Exception('opt:include', $params['from']); } $section['format']->assign('item', 'view'); $view = $section['format']->get('section:variable'); $viewExistenceCond = '!' . $view . ' instanceof Opt_View ||'; } if (isset($params['view'])) { $view = $params['view']; $viewExistenceCond = '!' . $view . ' instanceof Opt_View || '; } elseif (isset($params['file'])) { $codeBegin = '$view = new Opt_View(' . $params['file'] . ');'; $view = '$view'; $codeEnd = ' unset($view); '; } // Compile the import if ($params['import'] == 'yes') { if (isset($params['file'])) { $codeBegin .= $view . '->_data = $this->_data; '; } else { $codeBegin .= $view . '->_data = array_merge(' . $view . '->_data, $this->_data); '; } } foreach ($vars as $name => $value) { $codeBegin .= $view . '->' . $name . ' = ' . $value . '; '; } if (isset($params['branch'])) { $codeBegin .= $view . '->setBranch(' . $params['branch'] . '); '; } if (!is_null($params['default'])) { $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, $codeBegin . ' if(' . $viewExistenceCond . '!' . $view . '->_parse($output, false)){ ' . $view . '->_template = ' . $params['default'] . '; ' . $view . '->_parse($output, true); } ' . $codeEnd); } elseif ($node->hasChildren()) { $node->addBefore(Opt_Xml_Buffer::TAG_CONTENT_BEFORE, $codeBegin . ' if(' . $viewExistenceCond . '!' . $view . '->_parse($output, false)){ '); $node->addAfter(Opt_Xml_Buffer::TAG_CONTENT_AFTER, ' } ' . $codeEnd); $this->_process($node); } else { $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, $codeBegin . ' ' . $view . '->_parse($output, $exception); ' . $codeEnd); } }
protected function _build($hookName) { if ($hookName == 'section:init') { $section = $this->_getVar('section'); if (!is_null($section['parent'])) { $parent = Opt_Instruction_BaseSection::getSection($section['parent']); $parent['format']->assign('item', $section['name']); if ($parent['format']->property('section:useReference')) { return '$_sect' . $section['name'] . '_vals = &' . $parent['format']->get('section:variable') . '; '; } return '$_sect' . $section['name'] . '_vals = ' . $parent['format']->get('section:variable') . '; '; } elseif (!is_null($section['datasource'])) { return '$_sect' . $section['name'] . '_vals = ' . $section['datasource'] . '; '; } else { $this->assign('item', $section['name']); return '$_sect' . $section['name'] . '_vals = &' . $this->get('variable:main') . '; '; } } else { return parent::_build($hookName); } return NULL; }
protected function _build($hookName) { if ($hookName == 'section:init') { $section = $this->_getVar('section'); // Choose the data source. if (!is_null($section['parent'])) { $parent = Opt_Instruction_BaseSection::getSection($section['parent']); $parent['format']->assign('item', $section['name']); $ds = $parent['format']->get('section:variable'); } elseif (!is_null($section['datasource'])) { $ds = $section['datasource']; } else { $this->assign('item', $section['name']); $ds = $this->get('variable:main'); } if (!is_object($this->_decorated)) { throw new Opt_FormatNotDecorated_Exception('StaticGenerator'); } $ref = $this->_decorated->property('section:useReference') ? ' & ' : ''; return ' if(' . $ds . ' instanceof Opt_Generator_Interface){ $_sect' . $section['name'] . '_vals = ' . $ds . '->generate(\'' . $section['name'] . '\'); }else{ $_sect' . $section['name'] . '_vals = ' . $ref . $ds . '; } '; } }
protected function _build($hookName) { if ($hookName == 'section:init') { $section = $this->_getVar('section'); // Choose the data source. if (!is_null($section['parent'])) { $parent = Opt_Instruction_BaseSection::getSection($section['parent']); $parent['format']->assign('item', $section['name']); $ds = $parent['format']->get('section:variable'); } elseif (!is_null($section['datasource'])) { $ds = $section['datasource']; } else { $this->assign('item', $section['name']); $ds = $this->get('variable:main'); } // This format must be decorated by the user, because it does not support directly the rest // of "section" namespace. if (!is_object($this->_decorated)) { throw new Opt_FormatNotDecorated_Exception('StaticGenerator'); } return ' $_sect' . $section['name'] . '_vals = array(); if(' . $ds . ' instanceof Opt_Generator_Interface){ $_sect' . $section['name'] . '_vals = ' . $ds . '->generate(\'' . $section['name'] . '\'); } '; } }
/** * Builds a PHP code for the specified hook name. * * @param String $hookName The hook name * @return String The output PHP code */ protected function _build($hookName) { switch ($hookName) { // Initializes the section by obtaining the list of items to display case 'section:init': $section = $this->_getVar('section'); if ($section['parent'] !== null) { $parent = Opt_Instruction_BaseSection::getSection($section['parent']); $parent['format']->assign('item', $section['name']); return '$_sect' . $section['name'] . '_vals = ' . $parent['format']->get('section:variable') . '; '; } $form = Opf_View_Instruction_Form::getProcessedForm(); if ($section['order'] == 'desc') { return '$_sect' . $section['name'] . '_vals = array_reverse($_form->getItems(\'' . $section['name'] . '\')); '; } return '$_sect' . $section['name'] . '_vals = $_form->getItems(\'' . $section['name'] . '\'); '; // The end of the section loop. // The end of the section loop. case 'section:endLoop': return ' } '; // The condition that should test if the section is not empty. // The condition that should test if the section is not empty. case 'section:isNotEmpty': $section = $this->_getVar('section'); return 'is_array($_sect' . $section['name'] . '_vals) && ($_sect' . $section['name'] . '_cnt = sizeof($_sect' . $section['name'] . '_vals)) > 0'; // The code block after the condition // The code block after the condition case 'section:started': return ''; // The code block before the end of the conditional block. // The code block before the end of the conditional block. case 'section:finished': return ''; // The code block after the conditional block // The code block after the conditional block case 'section:done': $section = $this->_getVar('section'); return ''; // The code block before entering the loop. // The code block before entering the loop. case 'section:loopBefore': return ''; // The default loop for the ascending order. // The default loop for the ascending order. case 'section:startAscLoop': $section = $this->_getVar('section'); return 'foreach($_sect' . $section['name'] . '_vals as $_sect' . $section['name'] . '_i => $_sect' . $section['name'] . '_v){ '; // The default loop for the descending order. // The default loop for the descending order. case 'section:startDescLoop': $section = $this->_getVar('section'); return 'foreach($_sect' . $section['name'] . '_vals as $_sect' . $section['name'] . '_i => $_sect' . $section['name'] . '_v){ '; // Retrieving the whole section item. // Retrieving the whole section item. case 'section:item': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_v'; // Retrieving a variable from a section item. // Retrieving a variable from a section item. case 'section:variable': $section = $this->_getVar('section'); if ($this->isDecorating()) { return '$_sect' . $section['name'] . '_v' . $this->_decorated->get('item:item'); } return '$_sect' . $section['name'] . '_v->' . $this->_getVar('item'); // Resetting the section to the first element. // Resetting the section to the first element. case 'section:reset': $section = $this->_getVar('section'); return 'reset($_sect' . $section['name'] . '_vals);'; break; // Moving to the next element. // Moving to the next element. case 'section:next': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_i++;'; // Checking whether the iterator is valid. // Checking whether the iterator is valid. case 'section:valid': $section = $this->_getVar('section'); return 'isset($_sect' . $section['name'] . '_vals[$_sect' . $section['name'] . '_i])'; // Populate the current element // Populate the current element case 'section:populate': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_v = current($_sect' . $section['name'] . '_vals); $_sect' . $section['name'] . '_i = key($_sect' . $section['name'] . '_vals);'; // The code that returns the number of items in the section; // The code that returns the number of items in the section; case 'section:count': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_cnt'; // Section item size. // Section item size. case 'section:size': $section = $this->_getVar('section'); return '($_sect' . $section['name'] . '_v instanceof Countable ? $_sect' . $section['name'] . '_v->count() : -1)'; // Section iterator. // Section iterator. case 'section:iterator': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_i'; // Testing the first element. // Testing the first element. case 'section:isFirst': return '$_sect' . $section['nesting'] . '_i == 0'; // Testing the last element. // Testing the last element. case 'section:isLast': return '$_sect' . $section['nesting'] . '_i == ($_sect' . $section['name'] . '_cnt-1)'; // Testing the extreme element. // Testing the extreme element. case 'section:isExtreme': $section = $this->_getVar('section'); return '(($_sect' . $section['nesting'] . '_i == ($_sect' . $section['name'] . '_cnt-1)) || ($_sect' . $section['nesting'] . '_i == 0))'; case 'item:item': switch ($this->_getVar('item')) { case 'component': case 'widget': return '->getWidget()'; case 'name': return '->getName()'; case 'valid': return '->isValid()'; } return ''; default: return NULL; } }
/** * Build a PHP code for the specified hook name. * * @param String $hookName The hook name * @return String The output PHP code */ protected function _build($hookName) { switch ($hookName) { // Initializes the section by obtaining the list of items to display case 'section:init': $section = $this->_getVar('section'); if (!is_null($section['parent'])) { $parent = Opt_Instruction_BaseSection::getSection($section['parent']); $parent['format']->assign('item', $section['name']); return '$_sect' . $section['name'] . '_vals = ' . $parent['format']->get('section:variable') . '; '; } elseif (!is_null($section['datasource'])) { return '$_sect' . $section['name'] . '_vals = ' . $section['datasource'] . '; '; } else { $this->assign('item', $section['name']); return '$_sect' . $section['name'] . '_vals = ' . $this->get('variable:main') . '; '; } // The end of the section loop. // The end of the section loop. case 'section:endLoop': return ' } '; // The condition that should test if the section is not empty. // The condition that should test if the section is not empty. case 'section:isNotEmpty': $section = $this->_getVar('section'); return 'is_object($_sect' . $section['name'] . '_vals) && ($_sect' . $section['name'] . '_vals instanceof Traversable) && ($_sect' . $section['name'] . '_vals instanceof Countable) && ($_sect' . $section['name'] . '_vals->count() > 0)'; // The code block after the condition // The code block after the condition case 'section:started': // The code block before the end of the conditional block. // The code block before the end of the conditional block. case 'section:finished': // The code block after the conditional block // The code block after the conditional block case 'section:done': // The code block before entering the loop. // The code block before entering the loop. case 'section:loopBefore': $section = $this->_getVar('section'); if ($section['order'] == 'desc') { return ' $tmp = array(); foreach($_sect' . $section['name'] . '_vals as $i => $v){ $tmp[$i] = $v; } $_sect' . $section['name'] . '_vals = &$tmp; '; } return ''; // The default loop for the ascending order. // The default loop for the ascending order. case 'section:startAscLoop': $section = $this->_getVar('section'); return 'foreach($_sect' . $section['name'] . '_vals as $_sect' . $section['name'] . '_i => $_sect' . $section['name'] . '_v){ '; // The default loop for the descending order. // The default loop for the descending order. case 'section:startDescLoop': $section = $this->_getVar('section'); return 'foreach($_sect' . $section['name'] . '_vals as $_sect' . $section['name'] . '_i => $_sect' . $section['name'] . '_v){ '; // Retrieving the whole section item. // Retrieving the whole section item. case 'section:item': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_v'; // Retrieving a variable from a section item. // Retrieving a variable from a section item. case 'section:variable': $section = $this->_getVar('section'); if ($this->isDecorating()) { return '$_sect' . $section['name'] . '_v' . $this->_decorated->get('item:item'); } return '$_sect' . $section['name'] . '_v->' . $this->_getVar('item'); // Resetting the section to the first element. // Resetting the section to the first element. case 'section:reset': $section = $this->_getVar('section'); if ($section['order'] == 'asc') { return '$_sect' . $section['name'] . '_vals->reset();'; } else { return 'end($_sect' . $section['name'] . '_vals); $_sect' . $section['name'] . '_v = current($_sect' . $section['name'] . '_vals); $_sect' . $section['name'] . '_i = key($_sect' . $section['name'] . '_vals); '; } break; // Moving to the next element. // Moving to the next element. case 'section:next': $section = $this->_getVar('section'); if ($section['order'] == 'asc') { return '$_sect' . $section['name'] . '_vals->next();'; } else { return 'prev($_sect' . $section['name'] . '_vals);'; } break; // Checking whether the iterator is valid. // Checking whether the iterator is valid. case 'section:valid': $section = $this->_getVar('section'); return 'isset($_sect' . $section['name'] . '_vals[$_sect' . $section['nesting'] . '_i])'; // Populate the current element // Populate the current element case 'section:populate': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_v = current($_sect' . $section['name'] . '_vals); $_sect' . $section['name'] . '_i = key($_sect' . $section['name'] . '_vals);'; // The code that returns the number of items in the section; // The code that returns the number of items in the section; case 'section:count': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_cnt'; // Section item size. // Section item size. case 'section:size': $section = $this->_getVar('section'); return '($_sect' . $section['name'] . '_v instanceof Countable ? $_sect' . $section['name'] . '_v->count() : -1)'; // Section iterator. // Section iterator. case 'section:iterator': $section = $this->_getVar('section'); return '$_sect' . $section['name'] . '_i'; // Testing the first element. // Testing the first element. case 'section:isFirst': $section = $this->_getVar('section'); if ($section['order'] == 'asc') { return '$_sect' . $section['nesting'] . '_i == 0'; } else { return '$_sect' . $section['nesting'] . '_i == ($_sect' . $section['name'] . '_cnt-1)'; } // Testing the last element. // Testing the last element. case 'section:isLast': $section = $this->_getVar('section'); if ($section['order'] == 'asc') { return '$_sect' . $section['nesting'] . '_i == ($_sect' . $section['name'] . '_cnt-1)'; } else { return '$_sect' . $section['nesting'] . '_i == 0'; } // Testing the extreme element. // Testing the extreme element. case 'section:isExtreme': $section = $this->_getVar('section'); return '(($_sect' . $section['nesting'] . '_i == ($_sect' . $section['name'] . '_cnt-1)) || ($_sect' . $section['nesting'] . '_i == 0))'; // The variable access. // The variable access. case 'variable:main': $this->_applyVars = false; $item = $this->_getVar('item'); return 'variableHandler(\'' . $item . '\')'; case 'variable:assign': $this->_applyVars = false; $item = $this->_getVar('item'); return 'modifyVariable(\'' . $item . '\', ' . $this->_getVar('value') . ')'; case 'item:item': return '->readItem(\'' . $this->_getVar('item') . '\')'; case 'item:assign': return '->saveItem(\'' . $this->_getVar('item') . '\',' . $this->_getVar('value') . ')'; default: return NULL; } }