Skip to content

Commit 57372ba

Browse files
authored
Make event dispatcher in View and WebView optional (#250)
1 parent 6ab2c25 commit 57372ba

5 files changed

Lines changed: 23 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
`withFallbackExtension()` and `getFallbackExtensions()` (@rustamwin)
1212
- Bug #232: Fix render templates that contain dots in their name (@rustamwin)
1313
- New #242: Add `View::getLocale()` and `WebView::getLocale()` methods (@Tigrov)
14+
- Enh #250: Make event dispatcher in `View` and `WebView` optional (@vjik)
1415

1516
## 8.0.0 February 16, 2023
1617

src/View.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ final class View implements ViewInterface
3434

3535
/**
3636
* @param string $basePath The full path to the base directory of views.
37-
* @param EventDispatcherInterface $eventDispatcher The event dispatcher instance.
37+
* @param EventDispatcherInterface|null $eventDispatcher The event dispatcher instance.
3838
*/
39-
public function __construct(string $basePath, EventDispatcherInterface $eventDispatcher)
39+
public function __construct(string $basePath, ?EventDispatcherInterface $eventDispatcher = null)
4040
{
4141
$this->basePath = $basePath;
4242
$this->state = new ViewState();
@@ -64,15 +64,15 @@ public function beginPage(): void
6464
{
6565
ob_start();
6666
ob_implicit_flush(false);
67-
$this->eventDispatcher->dispatch(new PageBegin($this));
67+
$this->eventDispatcher?->dispatch(new PageBegin($this));
6868
}
6969

7070
/**
7171
* Marks the ending of a view.
7272
*/
7373
public function endPage(): void
7474
{
75-
$this->eventDispatcher->dispatch(new PageEnd($this));
75+
$this->eventDispatcher?->dispatch(new PageEnd($this));
7676

7777
ob_end_flush();
7878
}

src/ViewTrait.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*/
3535
trait ViewTrait
3636
{
37-
private EventDispatcherInterface $eventDispatcher;
37+
private ?EventDispatcherInterface $eventDispatcher;
3838

3939
private string $basePath;
4040
private ?ViewContextInterface $context = null;
@@ -586,6 +586,10 @@ abstract protected function createAfterRenderEvent(
586586
*/
587587
private function beforeRender(string $viewFile, array $parameters): bool
588588
{
589+
if ($this->eventDispatcher === null) {
590+
return true;
591+
}
592+
589593
$event = $this->createBeforeRenderEvent($viewFile, $parameters);
590594
$event = $this->eventDispatcher->dispatch($event);
591595
/** @var StoppableEventInterface $event */
@@ -607,6 +611,10 @@ private function beforeRender(string $viewFile, array $parameters): bool
607611
*/
608612
private function afterRender(string $viewFile, array $parameters, string $result): string
609613
{
614+
if ($this->eventDispatcher === null) {
615+
return $result;
616+
}
617+
610618
$event = $this->createAfterRenderEvent($viewFile, $parameters, $result);
611619

612620
/** @var AfterRenderEventInterface $event */

src/WebView.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ final class WebView implements ViewInterface
8888

8989
/**
9090
* @param string $basePath The full path to the base directory of views.
91-
* @param EventDispatcherInterface $eventDispatcher The event dispatcher instance.
91+
* @param EventDispatcherInterface|null $eventDispatcher The event dispatcher instance.
9292
*/
93-
public function __construct(string $basePath, EventDispatcherInterface $eventDispatcher)
93+
public function __construct(string $basePath, ?EventDispatcherInterface $eventDispatcher = null)
9494
{
9595
$this->basePath = $basePath;
9696
$this->state = new WebViewState();
@@ -117,7 +117,7 @@ public function withClearedState(): static
117117
public function head(): void
118118
{
119119
echo sprintf(self::PLACEHOLDER_HEAD, $this->getPlaceholderSignature());
120-
$this->eventDispatcher->dispatch(new Head($this));
120+
$this->eventDispatcher?->dispatch(new Head($this));
121121
}
122122

123123
/**
@@ -126,15 +126,15 @@ public function head(): void
126126
public function beginBody(): void
127127
{
128128
echo sprintf(self::PLACEHOLDER_BODY_BEGIN, $this->getPlaceholderSignature());
129-
$this->eventDispatcher->dispatch(new BodyBegin($this));
129+
$this->eventDispatcher?->dispatch(new BodyBegin($this));
130130
}
131131

132132
/**
133133
* Marks the ending of an HTML body section.
134134
*/
135135
public function endBody(): void
136136
{
137-
$this->eventDispatcher->dispatch(new BodyEnd($this));
137+
$this->eventDispatcher?->dispatch(new BodyEnd($this));
138138
echo sprintf(self::PLACEHOLDER_BODY_END, $this->getPlaceholderSignature());
139139
}
140140

@@ -145,7 +145,7 @@ public function beginPage(): void
145145
{
146146
ob_start();
147147
ob_implicit_flush(false);
148-
$this->eventDispatcher->dispatch(new PageBegin($this));
148+
$this->eventDispatcher?->dispatch(new PageBegin($this));
149149
}
150150

151151
/**
@@ -157,7 +157,7 @@ public function beginPage(): void
157157
*/
158158
public function endPage(bool $ajaxMode = false): void
159159
{
160-
$this->eventDispatcher->dispatch(new PageEnd($this));
160+
$this->eventDispatcher?->dispatch(new PageEnd($this));
161161

162162
$content = ob_get_clean();
163163

tests/TestSupport/TestHelper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Psr\EventDispatcher\EventDispatcherInterface;
88
use Yiisoft\Files\FileHelper;
9-
use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher;
109
use Yiisoft\View\View;
1110
use Yiisoft\View\WebView;
1211

@@ -24,15 +23,15 @@ public static function createView(?EventDispatcherInterface $eventDispatcher = n
2423
{
2524
return new View(
2625
dirname(__DIR__) . '/public/view',
27-
$eventDispatcher ?? new SimpleEventDispatcher(),
26+
$eventDispatcher,
2827
);
2928
}
3029

3130
public static function createWebView(?EventDispatcherInterface $eventDispatcher = null): WebView
3231
{
3332
return new WebView(
3433
dirname(__DIR__) . '/public/view',
35-
$eventDispatcher ?? new SimpleEventDispatcher(),
34+
$eventDispatcher,
3635
);
3736
}
3837
}

0 commit comments

Comments
 (0)