PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/Opl/Opt/Instruction/Include.php

https://bitbucket.org/kandsten/hitta.sverok.se
PHP | 109 lines | 84 code | 10 blank | 15 comment | 11 complexity | 335ea2e38e1f886b923a23ad998f6acb MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. /*
  3. * OPEN POWER LIBS <http://www.invenzzia.org>
  4. *
  5. * This file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE. It is also available through
  7. * WWW at this URL: <http://www.invenzzia.org/license/new-bsd>
  8. *
  9. * Copyright (c) Invenzzia Group <http://www.invenzzia.org>
  10. * and other contributors. See website for details.
  11. *
  12. * $Id: Include.php 231 2009-09-19 07:13:14Z zyxist $
  13. */
  14. class Opt_Instruction_Include extends Opt_Compiler_Processor
  15. {
  16. protected $_name = 'include';
  17. public function configure()
  18. {
  19. $this->_addInstructions(array('opt:include'));
  20. } // end configure();
  21. public function processNode(Opt_Xml_Node $node)
  22. {
  23. $params = array(
  24. 'file' => array(0 => self::OPTIONAL, self::STRING, null),
  25. 'view' => array(0 => self::OPTIONAL, self::EXPRESSION, NULL),
  26. 'from' => array(0 => self::OPTIONAL, self::ID, NULL),
  27. 'default' => array(0 => self::OPTIONAL, self::STRING, NULL),
  28. 'import' => array(0 => self::OPTIONAL, self::BOOL, NULL),
  29. 'branch' => array(0 => self::OPTIONAL, self::STRING, NULL),
  30. '__UNKNOWN__' => array(0 => self::OPTIONAL, self::EXPRESSION)
  31. );
  32. $vars = $this->_extractAttributes($node, $params);
  33. // Conditional attribute control.
  34. if(!isset($params['from']) && !isset($params['file']) && !isset($params['view']))
  35. {
  36. throw new Opt_IncludeNoAttributes($node->getXmlName());
  37. }
  38. // Possible section integration
  39. $codeBegin = '';
  40. $codeEnd = '';
  41. $viewExistenceCond = '';
  42. if(isset($params['from']))
  43. {
  44. $section = Opt_Instruction_BaseSection::getSection($params['from']);
  45. if(is_null($section))
  46. {
  47. throw new Opt_SectionNotFound_Exception('opt:include', $params['from']);
  48. }
  49. $section['format']->assign('item', 'view');
  50. $view = $section['format']->get('section:variable');
  51. $viewExistenceCond = '!'.$view.' instanceof Opt_View ||';
  52. }
  53. if(isset($params['view']))
  54. {
  55. $view = $params['view'];
  56. $viewExistenceCond = '!'.$view.' instanceof Opt_View || ';
  57. }
  58. elseif(isset($params['file']))
  59. {
  60. $codeBegin = '$view = new Opt_View('.$params['file'].');';
  61. $view = '$view';
  62. $codeEnd = ' unset($view); ';
  63. }
  64. // Compile the import
  65. if($params['import'] == 'yes')
  66. {
  67. if(isset($params['file']))
  68. {
  69. $codeBegin .= $view.'->_data = $this->_data; ';
  70. }
  71. else
  72. {
  73. $codeBegin .= $view.'->_data = array_merge('.$view.'->_data, $this->_data); ';
  74. }
  75. }
  76. foreach($vars as $name => $value)
  77. {
  78. $codeBegin .= $view.'->'.$name.' = '.$value.'; ';
  79. }
  80. if(isset($params['branch']))
  81. {
  82. $codeBegin .= $view.'->setBranch('.$params['branch'].'); ';
  83. }
  84. if(!is_null($params['default']))
  85. {
  86. $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, $codeBegin.' if('.$viewExistenceCond.'!'.$view.'->_parse($output, false)){ '.$view.'->_template = '.$params['default'].'; '.$view.'->_parse($output, true); } '.$codeEnd);
  87. }
  88. elseif($node->hasChildren())
  89. {
  90. $node->addBefore(Opt_Xml_Buffer::TAG_CONTENT_BEFORE, $codeBegin.' if('.$viewExistenceCond.'!'.$view.'->_parse($output, false)){ ');
  91. $node->addAfter(Opt_Xml_Buffer::TAG_CONTENT_AFTER, ' } '.$codeEnd);
  92. $this->_process($node);
  93. }
  94. else
  95. {
  96. $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, $codeBegin.' '.$view.'->_parse($output, $exception); '.$codeEnd);
  97. }
  98. } // end processNode();
  99. } // end Opt_Instruction_Include;