Passed
Pull Request — master (#195)
by Sergei
08:08 queued 05:57
created

View::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Psr\EventDispatcher\StoppableEventInterface;
9
use Yiisoft\View\Event\AfterRenderEventInterface;
10
use Yiisoft\View\Event\View\AfterRender;
11
use Yiisoft\View\Event\View\BeforeRender;
12
use Yiisoft\View\Event\View\PageBegin;
13
use Yiisoft\View\Event\View\PageEnd;
14
15
use Yiisoft\View\State\ViewState;
16
17
use function ob_end_flush;
18
use function ob_implicit_flush;
19
use function ob_start;
20
21
/**
22
 * View represents an instance of a view for use in an any environment.
23
 *
24
 * View provides a set of methods (e.g. {@see View::render()}) for rendering purpose.
25
 *
26
 * @property ViewState $state
27
 */
28
final class View implements ViewInterface
29
{
30
    use ViewTrait;
31
32
    private ViewState $state;
33
34
    /**
35
     * @param string $basePath The full path to the base directory of views.
36
     * @param ViewState $state
37
     * @param EventDispatcherInterface $eventDispatcher The event dispatcher instance.
38
     */
39 31
    public function __construct(string $basePath, ViewState $state, EventDispatcherInterface $eventDispatcher)
40
    {
41 31
        $this->basePath = $basePath;
42 31
        $this->state = $state;
43 31
        $this->eventDispatcher = $eventDispatcher;
44 31
        $this->setPlaceholderSalt(__DIR__);
45 31
    }
46
47
    /**
48
     * Marks the beginning of a view.
49
     */
50 1
    public function beginPage(): void
51
    {
52 1
        ob_start();
53
        /** @psalm-suppress InvalidArgument */
54 1
        PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0);
55
56 1
        $this->eventDispatcher->dispatch(new PageBegin($this));
57 1
    }
58
59
    /**
60
     * Marks the ending of a view.
61
     */
62 1
    public function endPage(): void
63
    {
64 1
        $this->eventDispatcher->dispatch(new PageEnd($this));
65
66 1
        ob_end_flush();
67 1
    }
68
69 9
    protected function createBeforeRenderEvent(string $viewFile, array $parameters): StoppableEventInterface
70
    {
71 9
        return new BeforeRender($this, $viewFile, $parameters);
72
    }
73
74 8
    protected function createAfterRenderEvent(
75
        string $viewFile,
76
        array $parameters,
77
        string $result
78
    ): AfterRenderEventInterface {
79 8
        return new AfterRender($this, $viewFile, $parameters, $result);
80
    }
81
}
82