Php.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\TemplateEngine;
  7. use Magento\Framework\View\Element\BlockInterface;
  8. use Magento\Framework\View\TemplateEngineInterface;
  9. /**
  10. * Template engine that enables PHP templates to be used for rendering
  11. */
  12. class Php implements TemplateEngineInterface
  13. {
  14. /**
  15. * Current block
  16. *
  17. * @var BlockInterface
  18. */
  19. protected $_currentBlock;
  20. /**
  21. * Helper factory
  22. *
  23. * @var \Magento\Framework\ObjectManagerInterface
  24. */
  25. protected $_helperFactory;
  26. /**
  27. * Constructor
  28. *
  29. * @param \Magento\Framework\ObjectManagerInterface $helperFactory
  30. */
  31. public function __construct(\Magento\Framework\ObjectManagerInterface $helperFactory)
  32. {
  33. $this->_helperFactory = $helperFactory;
  34. }
  35. /**
  36. * Render output
  37. *
  38. * Include the named PHTML template using the given block as the $this
  39. * reference, though only public methods will be accessible.
  40. *
  41. * @param BlockInterface $block
  42. * @param string $fileName
  43. * @param array $dictionary
  44. * @return string
  45. * @throws \Exception
  46. */
  47. public function render(BlockInterface $block, $fileName, array $dictionary = [])
  48. {
  49. ob_start();
  50. try {
  51. $tmpBlock = $this->_currentBlock;
  52. $this->_currentBlock = $block;
  53. extract($dictionary, EXTR_SKIP);
  54. include $fileName;
  55. $this->_currentBlock = $tmpBlock;
  56. } catch (\Exception $exception) {
  57. ob_end_clean();
  58. throw $exception;
  59. }
  60. /** Get output buffer. */
  61. $output = ob_get_clean();
  62. return $output;
  63. }
  64. /**
  65. * Redirects methods calls to the current block
  66. *
  67. * This is needed because the templates are included in the context of this engine
  68. * rather than in the context of the block.
  69. *
  70. * @param string $method
  71. * @param array $args
  72. * @return mixed
  73. */
  74. public function __call($method, $args)
  75. {
  76. return call_user_func_array([$this->_currentBlock, $method], $args);
  77. }
  78. /**
  79. * Redirects isset calls to the current block
  80. *
  81. * This is needed because the templates are included in the context of this engine rather than
  82. * in the context of the block.
  83. *
  84. * @param string $name
  85. * @return bool
  86. */
  87. public function __isset($name)
  88. {
  89. return isset($this->_currentBlock->{$name});
  90. }
  91. /**
  92. * Allows read access to properties of the current block
  93. *
  94. * This is needed because the templates are included in the context of this engine rather
  95. * than in the context of the block.
  96. *
  97. * @param string $name
  98. * @return mixed
  99. */
  100. public function __get($name)
  101. {
  102. return $this->_currentBlock->{$name};
  103. }
  104. /**
  105. * Get helper singleton
  106. *
  107. * @param string $className
  108. * @return \Magento\Framework\App\Helper\AbstractHelper
  109. * @throws \LogicException
  110. */
  111. public function helper($className)
  112. {
  113. $helper = $this->_helperFactory->get($className);
  114. if (false === $helper instanceof \Magento\Framework\App\Helper\AbstractHelper) {
  115. throw new \LogicException($className . ' doesn\'t extends Magento\Framework\App\Helper\AbstractHelper');
  116. }
  117. return $helper;
  118. }
  119. }