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