-
-
Notifications
You must be signed in to change notification settings - Fork 46
Improvements rendering PHP files #4

Description
@SamMousa commented on Jul 9, 2018, 10:05 AM UTC:
Currently rendering PHP files is an exception with respect to other files in the View
class:
if (isset($this->renderers[$ext])) {
if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
}
/* @var $renderer ViewRenderer */
$renderer = $this->renderers[$ext];
$output = $renderer->render($this, $viewFile, $params);
} else {
$output = $this->renderPhpFile($viewFile, $params);
}
- I propose removing the exception and implementing a
PhpRenderer
and having a default renderer in theView
class.
Currently rendering of PHP files happens inside the object context, this means that the View
object itself is available via $this
. This makes several tools unhappy, since you're calling $this
outside of an objects' context, as far as they can tell.
Since you need to use something like assertions or PHPDoc to type hint the parameters anyway, I feel there could be better ways to make the View
object available, for example by naming it $view
instead of $this
.
- Implement PHP file rendering by including the file inside a closure that has a static scope:
$renderer = function() use ($file, $params) {
extract($params, EXTR_OVERWRITE);
require $file;
};
$_obInitialLevel_ = ob_get_level();
ob_start();
ob_implicit_flush(false);
try {
$renderer->bindTo(null)()
return ob_get_clean();
} catch (\Throwable $e) {
while (ob_get_level() > $_obInitialLevel_) {
if (!@ob_end_clean()) {
ob_clean();
}
}
throw $e;
}
Output buffering is used by the framework to render views widgets among others. Currently PHP file rendering properly checks for output buffer states in error handling, but not in happy flows.
- When rendering a PHP file, the rendering could (and should?) always check that the number of nested buffers is the same before and after rendering. If it differs we should either:
- Throw an error (Clean up your buffers when you're done, Yii is not your mom!)
- Flush the nested buffers until the nesting level returns to the expected value.
This issue was moved by samdark from yiisoft/yii2#16500.