Browse Source

Fix #18858: Reduce memory usage in `yii\base\View::afterRender` method

tags/2.0.44
Leo 3 years ago committed by GitHub
parent
commit
9ed87a0ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 4
      framework/base/View.php
  3. 18
      tests/framework/base/ViewTest.php

1
framework/CHANGELOG.md

@ -15,6 +15,7 @@ Yii Framework 2 Change Log
- Bug #18845: Fix duplicating `id` in `MigrateController::addDefaultPrimaryKey()` (WinterSilence, samdark) - Bug #18845: Fix duplicating `id` in `MigrateController::addDefaultPrimaryKey()` (WinterSilence, samdark)
- Bug #17119: Fix `yii\caching\Cache::multiSet()` to use `yii\caching\Cache::$defaultDuration` when no duration is passed (OscarBarrett) - Bug #17119: Fix `yii\caching\Cache::multiSet()` to use `yii\caching\Cache::$defaultDuration` when no duration is passed (OscarBarrett)
- Bug #18842: Fix `yii\base\Controller::bindInjectedParams()` to not throw error when argument of `ReflectionUnionType` type is passed (bizley) - Bug #18842: Fix `yii\base\Controller::bindInjectedParams()` to not throw error when argument of `ReflectionUnionType` type is passed (bizley)
- Enh #18858: Reduce memory usage in `yii\base\View::afterRender` method (LeoOnTheEarth)
- Bug #18880: Fix `yii\helpers\ArrayHelper::toArray()` for `DateTime` objects in PHP >= 7.4 (rhertogh) - Bug #18880: Fix `yii\helpers\ArrayHelper::toArray()` for `DateTime` objects in PHP >= 7.4 (rhertogh)

4
framework/base/View.php

@ -316,10 +316,10 @@ class View extends Component implements DynamicContentAwareInterface
$event = new ViewEvent([ $event = new ViewEvent([
'viewFile' => $viewFile, 'viewFile' => $viewFile,
'params' => $params, 'params' => $params,
'output' => $output,
]); ]);
$event->output =& $output;
$this->trigger(self::EVENT_AFTER_RENDER, $event); $this->trigger(self::EVENT_AFTER_RENDER, $event);
$output = $event->output;
} }
} }

18
tests/framework/base/ViewTest.php

@ -97,4 +97,22 @@ PHP
$this->assertSame($subViewContent, $view->render('@testviews/base')); $this->assertSame($subViewContent, $view->render('@testviews/base'));
} }
public function testAfterRender()
{
$view = new View();
$filename = 'path/to/file';
$params = ['search' => 'simple', 'replace' => 'new'];
$output = 'This is a simple rendered output. (filename)';
$expectedOutput = 'This is a new rendered output. (path/to/file)';
$view->on(View::EVENT_AFTER_RENDER, function (ViewEvent $event) {
$event->output = str_replace($event->params['search'], $event->params['replace'], $event->output);
$event->output = str_replace('filename', $event->viewFile, $event->output);
});
$view->afterRender($filename, $params, $output);
$this->assertSame($expectedOutput, $output);
}
} }

Loading…
Cancel
Save