Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
53 / 53
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ViewCollector
100.00% covered (success)
100.00%
53 / 53
100.00% covered (success)
100.00%
5 / 5
12
100.00% covered (success)
100.00%
1 / 1
 setView
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getActivities
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getContents
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
 renderRenderedViews
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 getSortedData
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework MVC Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\MVC\Debug;
11
12use Framework\Debug\Collector;
13use Framework\MVC\View;
14
15/**
16 * Class ViewCollector.
17 *
18 * @package mvc
19 */
20class ViewCollector extends Collector
21{
22    protected View $view;
23
24    public function setView(View $view) : static
25    {
26        $this->view = $view;
27        return $this;
28    }
29
30    public function getActivities() : array
31    {
32        $activities = [];
33        foreach ($this->getSortedData() as $index => $data) {
34            $activities[] = [
35                'collector' => $this->getName(),
36                'class' => static::class,
37                'description' => 'Render view ' . ($index + 1),
38                'start' => $data['start'],
39                'end' => $data['end'],
40            ];
41        }
42        return $activities;
43    }
44
45    public function getContents() : string
46    {
47        $baseDir = $this->view->getBaseDir();
48        $extension = $this->view->getExtension();
49        $layoutPrefix = $this->view->getLayoutPrefix();
50        $includePrefix = $this->view->getIncludePrefix();
51        \ob_start();
52        if (isset($baseDir)): ?>
53            <p><strong>Base Directory:</strong> <?= \htmlentities($baseDir) ?></p>
54        <?php
55        endif; ?>
56        <p><strong>Extension:</strong> <?= \htmlentities($extension) ?></p>
57        <?php
58        if ($layoutPrefix !== ''): ?>
59            <p><strong>Layout Prefix:</strong> <?= \htmlentities($layoutPrefix) ?>
60            </p>
61        <?php
62        endif;
63        if ($includePrefix !== ''): ?>
64            <p><strong>Include Prefix:</strong> <?= \htmlentities($includePrefix) ?>
65            </p>
66        <?php
67        endif ?>
68        <h1>Rendered Views</h1>
69        <?php
70        echo $this->renderRenderedViews();
71        return \ob_get_clean(); // @phpstan-ignore-line
72    }
73
74    protected function renderRenderedViews() : string
75    {
76        if ( ! $this->hasData()) {
77            return '<p>No view has been rendered.</p>';
78        }
79        $data = $this->getSortedData();
80        \ob_start();
81        $count = \count($data); ?>
82        <p>Total of <?= $count ?> rendered view file<?= $count > 1 ? 's' : '' ?>.</p>
83        <table>
84            <thead>
85            <tr>
86                <th>#</th>
87                <th>File</th>
88                <th>Type</th>
89                <th>Time to Render</th>
90            </tr>
91            </thead>
92            <tbody>
93            <?php foreach ($data as $index => $item): ?>
94                <tr title="<?= \htmlentities($item['filepath']) ?>">
95                    <td><?= $index + 1 ?></td>
96                    <td><?= \htmlentities($item['file']) ?></td>
97                    <td><?= \htmlentities($item['type']) ?></td>
98                    <td><?= \round($item['end'] - $item['start'], 6) ?></td>
99                </tr>
100            <?php endforeach ?>
101            </tbody>
102        </table>
103        <?php
104        return \ob_get_clean(); // @phpstan-ignore-line
105    }
106
107    /**
108     * @return array<mixed>
109     */
110    protected function getSortedData() : array
111    {
112        $data = $this->getData();
113        \usort($data, static function ($d1, $d2) {
114            return $d1['start'] <=> $d2['start'];
115        });
116        return $data;
117    }
118}