Skip to content

Commit c6fd362

Browse files
authored
Make beforeRender() and afterRender() methods protected (#171)
1 parent f554806 commit c6fd362

1 file changed

Lines changed: 46 additions & 27 deletions

File tree

src/BaseView.php

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public function render(string $view, array $parameters = []): string
369369
/**
370370
* Renders a view file.
371371
*
372-
* If the theme was not set {@see withTheme()}, it will try to render the themed version of the view file
372+
* If the theme was set {@see withTheme()}, it will try to render the themed version of the view file
373373
* as long as it is available.
374374
*
375375
* If the renderer was set {@see withRenderers()}, the method will use it to render the view file. Otherwise,
@@ -463,63 +463,82 @@ public function localize(string $file, ?string $language = null, ?string $source
463463
return is_file($desiredFile) ? $desiredFile : $file;
464464
}
465465

466+
/**
467+
* Clears the data for working with the event loop.
468+
*/
469+
public function clear(): void
470+
{
471+
$this->viewFiles = [];
472+
}
473+
474+
/**
475+
* Creates an event that occurs before rendering.
476+
*
477+
* @param string $viewFile The view file to be rendered.
478+
* @param array $parameters The parameter array passed to the {@see renderFile()} method.
479+
*
480+
* @return StoppableEventInterface The stoppable event instance.
481+
*/
482+
abstract protected function createBeforeRenderEvent(string $viewFile, array $parameters): StoppableEventInterface;
483+
484+
/**
485+
* Creates an event that occurs after rendering.
486+
*
487+
* @param string $viewFile The view file being rendered.
488+
* @param array $parameters The parameter array passed to the {@see renderFile()} method.
489+
* @param string $result The rendering result of the view file.
490+
*
491+
* @return AfterRenderEventInterface The event instance.
492+
*/
493+
abstract protected function createAfterRenderEvent(
494+
string $viewFile,
495+
array $parameters,
496+
string $result
497+
): AfterRenderEventInterface;
498+
466499
/**
467500
* This method is invoked right before {@see renderFile()} renders a view file.
468501
*
469-
* The default implementation will trigger the {@see BeforeRender()} event. If you override this method, make sure
470-
* you call the parent implementation first.
502+
* The default implementations will trigger the {@see \Yiisoft\View\Event\View\BeforeRender}
503+
* or {@see \Yiisoft\View\Event\WebView\BeforeRender} event. If you override this method,
504+
* make sure you call the parent implementation first.
471505
*
472506
* @param string $viewFile The view file to be rendered.
473-
* @param array $parameters The parameter array passed to the {@see render()} method.
507+
* @param array $parameters The parameter array passed to the {@see renderFile()} method.
474508
*
475509
* @return bool Whether to continue rendering the view file.
476510
*/
477-
public function beforeRender(string $viewFile, array $parameters): bool
511+
protected function beforeRender(string $viewFile, array $parameters): bool
478512
{
479513
$event = $this->createBeforeRenderEvent($viewFile, $parameters);
480514
$event = $this->eventDispatcher->dispatch($event);
481515
/** @var StoppableEventInterface $event */
482516
return !$event->isPropagationStopped();
483517
}
484518

485-
abstract protected function createBeforeRenderEvent(string $viewFile, array $parameters): StoppableEventInterface;
486-
487519
/**
488520
* This method is invoked right after {@see renderFile()} renders a view file.
489521
*
490-
* The default implementation will trigger the {@see AfterRender} event. If you override this method, make sure you
491-
* call the parent implementation first.
522+
* The default implementations will trigger the {@see \Yiisoft\View\Event\View\AfterRender}
523+
* or {@see \Yiisoft\View\Event\WebView\AfterRender} event. If you override this method,
524+
* make sure you call the parent implementation first.
492525
*
493526
* @param string $viewFile The view file being rendered.
494-
* @param array $parameters The parameter array passed to the {@see render()} method.
495-
* @param string $output The rendering result of the view file.
527+
* @param array $parameters The parameter array passed to the {@see renderFile()} method.
528+
* @param string $result The rendering result of the view file.
496529
*
497530
* @return string Updated output. It will be passed to {@see renderFile()} and returned.
498531
*/
499-
public function afterRender(string $viewFile, array $parameters, string $output): string
532+
protected function afterRender(string $viewFile, array $parameters, string $result): string
500533
{
501-
$event = $this->createAfterRenderEvent($viewFile, $parameters, $output);
534+
$event = $this->createAfterRenderEvent($viewFile, $parameters, $result);
502535

503536
/** @var AfterRenderEventInterface $event */
504537
$event = $this->eventDispatcher->dispatch($event);
505538

506539
return $event->getResult();
507540
}
508541

509-
abstract protected function createAfterRenderEvent(
510-
string $viewFile,
511-
array $parameters,
512-
string $result
513-
): AfterRenderEventInterface;
514-
515-
/**
516-
* Clears the data for working with the event loop.
517-
*/
518-
public function clear(): void
519-
{
520-
$this->viewFiles = [];
521-
}
522-
523542
/**
524543
* Finds the view file based on the given view name.
525544
*

0 commit comments

Comments
 (0)