Skip to content

Commit

Permalink
Make event dispatcher in View and WebView optional (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Mar 15, 2024
1 parent 6ab2c25 commit 57372ba
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
`withFallbackExtension()` and `getFallbackExtensions()` (@rustamwin)
- Bug #232: Fix render templates that contain dots in their name (@rustamwin)
- New #242: Add `View::getLocale()` and `WebView::getLocale()` methods (@Tigrov)
- Enh #250: Make event dispatcher in `View` and `WebView` optional (@vjik)

## 8.0.0 February 16, 2023

Expand Down
8 changes: 4 additions & 4 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ final class View implements ViewInterface

/**
* @param string $basePath The full path to the base directory of views.
* @param EventDispatcherInterface $eventDispatcher The event dispatcher instance.
* @param EventDispatcherInterface|null $eventDispatcher The event dispatcher instance.
*/
public function __construct(string $basePath, EventDispatcherInterface $eventDispatcher)
public function __construct(string $basePath, ?EventDispatcherInterface $eventDispatcher = null)
{
$this->basePath = $basePath;
$this->state = new ViewState();
Expand Down Expand Up @@ -64,15 +64,15 @@ public function beginPage(): void
{
ob_start();
ob_implicit_flush(false);

Check warning on line 66 in src/View.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ public function beginPage() : void { ob_start(); - ob_implicit_flush(false); + ob_implicit_flush(true); $this->eventDispatcher?->dispatch(new PageBegin($this)); } /**

Check warning on line 66 in src/View.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ public function beginPage() : void { ob_start(); - ob_implicit_flush(false); + $this->eventDispatcher?->dispatch(new PageBegin($this)); } /**
$this->eventDispatcher->dispatch(new PageBegin($this));
$this->eventDispatcher?->dispatch(new PageBegin($this));
}

/**
* Marks the ending of a view.
*/
public function endPage(): void
{
$this->eventDispatcher->dispatch(new PageEnd($this));
$this->eventDispatcher?->dispatch(new PageEnd($this));

ob_end_flush();
}
Expand Down
10 changes: 9 additions & 1 deletion src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
trait ViewTrait
{
private EventDispatcherInterface $eventDispatcher;
private ?EventDispatcherInterface $eventDispatcher;

private string $basePath;
private ?ViewContextInterface $context = null;
Expand Down Expand Up @@ -586,6 +586,10 @@ abstract protected function createAfterRenderEvent(
*/
private function beforeRender(string $viewFile, array $parameters): bool
{
if ($this->eventDispatcher === null) {
return true;
}

$event = $this->createBeforeRenderEvent($viewFile, $parameters);
$event = $this->eventDispatcher->dispatch($event);
/** @var StoppableEventInterface $event */
Expand All @@ -607,6 +611,10 @@ private function beforeRender(string $viewFile, array $parameters): bool
*/
private function afterRender(string $viewFile, array $parameters, string $result): string
{
if ($this->eventDispatcher === null) {
return $result;
}

$event = $this->createAfterRenderEvent($viewFile, $parameters, $result);

/** @var AfterRenderEventInterface $event */
Expand Down
14 changes: 7 additions & 7 deletions src/WebView.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ final class WebView implements ViewInterface

/**
* @param string $basePath The full path to the base directory of views.
* @param EventDispatcherInterface $eventDispatcher The event dispatcher instance.
* @param EventDispatcherInterface|null $eventDispatcher The event dispatcher instance.
*/
public function __construct(string $basePath, EventDispatcherInterface $eventDispatcher)
public function __construct(string $basePath, ?EventDispatcherInterface $eventDispatcher = null)
{
$this->basePath = $basePath;
$this->state = new WebViewState();
Expand All @@ -117,7 +117,7 @@ public function withClearedState(): static
public function head(): void
{
echo sprintf(self::PLACEHOLDER_HEAD, $this->getPlaceholderSignature());
$this->eventDispatcher->dispatch(new Head($this));
$this->eventDispatcher?->dispatch(new Head($this));
}

/**
Expand All @@ -126,15 +126,15 @@ public function head(): void
public function beginBody(): void
{
echo sprintf(self::PLACEHOLDER_BODY_BEGIN, $this->getPlaceholderSignature());
$this->eventDispatcher->dispatch(new BodyBegin($this));
$this->eventDispatcher?->dispatch(new BodyBegin($this));
}

/**
* Marks the ending of an HTML body section.
*/
public function endBody(): void
{
$this->eventDispatcher->dispatch(new BodyEnd($this));
$this->eventDispatcher?->dispatch(new BodyEnd($this));
echo sprintf(self::PLACEHOLDER_BODY_END, $this->getPlaceholderSignature());
}

Expand All @@ -145,7 +145,7 @@ public function beginPage(): void
{
ob_start();
ob_implicit_flush(false);
$this->eventDispatcher->dispatch(new PageBegin($this));
$this->eventDispatcher?->dispatch(new PageBegin($this));
}

/**
Expand All @@ -157,7 +157,7 @@ public function beginPage(): void
*/
public function endPage(bool $ajaxMode = false): void
{
$this->eventDispatcher->dispatch(new PageEnd($this));
$this->eventDispatcher?->dispatch(new PageEnd($this));

$content = ob_get_clean();

Expand Down
5 changes: 2 additions & 3 deletions tests/TestSupport/TestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Psr\EventDispatcher\EventDispatcherInterface;
use Yiisoft\Files\FileHelper;
use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher;
use Yiisoft\View\View;
use Yiisoft\View\WebView;

Expand All @@ -24,15 +23,15 @@ public static function createView(?EventDispatcherInterface $eventDispatcher = n
{
return new View(
dirname(__DIR__) . '/public/view',
$eventDispatcher ?? new SimpleEventDispatcher(),
$eventDispatcher,
);
}

public static function createWebView(?EventDispatcherInterface $eventDispatcher = null): WebView
{
return new WebView(
dirname(__DIR__) . '/public/view',
$eventDispatcher ?? new SimpleEventDispatcher(),
$eventDispatcher,
);
}
}

0 comments on commit 57372ba

Please sign in to comment.