• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / mvc / 16927505441

22 Jul 2025 07:08PM UTC coverage: 100.0%. Remained the same
16927505441

push

github

natanfelles
Add info about order in which commands are added to the console

1836 of 1836 relevant lines covered (100.0%)

10.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Debug/AppCollector.php
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
 */
10
namespace Framework\MVC\Debug;
11

12
use Framework\Debug\Collector;
13
use Framework\Debug\Debugger;
14
use Framework\MVC\App;
15
use ReflectionClass;
16
use ReflectionMethod;
17

18
/**
19
 * Class AppCollector.
20
 *
21
 * @package mvc
22
 */
23
class AppCollector extends Collector
24
{
25
    protected App $app;
26
    protected float $startTime;
27
    protected float $endTime;
28
    protected int $startMemory;
29
    protected int $endMemory;
30

31
    public function setApp(App $app) : static
32
    {
33
        $this->app = $app;
11✔
34
        if (!isset($this->startTime)) {
11✔
35
            $this->setStartTime();
2✔
36
        }
37
        if (!isset($this->startMemory)) {
11✔
38
            $this->setStartMemory();
2✔
39
        }
40
        return $this;
11✔
41
    }
42

43
    public function setStartTime(?float $microtime = null) : static
44
    {
45
        $this->startTime = $microtime ?? \microtime(true);
11✔
46
        return $this;
11✔
47
    }
48

49
    public function setEndTime(?float $microtime = null) : static
50
    {
51
        $this->endTime = $microtime ?? \microtime(true);
10✔
52
        return $this;
10✔
53
    }
54

55
    public function setStartMemory(?int $memoryUsage = null) : static
56
    {
57
        $this->startMemory = $memoryUsage ?? \memory_get_usage();
11✔
58
        return $this;
11✔
59
    }
60

61
    public function setEndMemory(?int $memoryUsage = null) : static
62
    {
63
        $this->endMemory = $memoryUsage ?? \memory_get_usage();
10✔
64
        return $this;
10✔
65
    }
66

67
    public function getActivities() : array
68
    {
69
        $activities = [];
4✔
70
        $activities[] = [
4✔
71
            'collection' => 'App',
4✔
72
            'collector' => $this->getName(),
4✔
73
            'class' => static::class,
4✔
74
            'description' => 'Initialization',
4✔
75
            'start' => $_SERVER['REQUEST_TIME_FLOAT'],
4✔
76
            'end' => $this->startTime,
4✔
77
        ];
4✔
78
        $activities[] = [
4✔
79
            'collector' => $this->getName(),
4✔
80
            'class' => static::class,
4✔
81
            'description' => 'Runtime',
4✔
82
            'start' => $this->startTime,
4✔
83
            'end' => $this->endTime,
4✔
84
        ];
4✔
85
        foreach ($this->getServices() as $service => $data) {
4✔
86
            foreach ($data as $item) {
4✔
87
                $activities[] = [
4✔
88
                    'collector' => $this->getName(),
4✔
89
                    'class' => static::class,
4✔
90
                    'description' => 'Load service ' . $service . ':' . $item['name'],
4✔
91
                    'start' => $item['start'],
4✔
92
                    'end' => $item['end'],
4✔
93
                ];
4✔
94
            }
95
        }
96
        return $activities;
4✔
97
    }
98

99
    public function getContents() : string
100
    {
101
        if (!isset($this->endTime)) {
10✔
102
            $this->setEndTime(\microtime(true));
7✔
103
        }
104
        if (!isset($this->endMemory)) {
10✔
105
            $this->setEndMemory(\memory_get_usage());
7✔
106
        }
107
        \ob_start(); ?>
10✔
108
        <p><strong>Started at:</strong> <?= \date('r', (int) $this->startTime) ?></p>
10✔
109
        <p><strong>Runtime:</strong> <?= Debugger::roundSecondsToMilliseconds($this->endTime - $this->startTime) ?> ms
10✔
110
        </p>
111
        <p>
112
            <strong>Memory:</strong> <?=
10✔
113
            Debugger::convertSize($this->endMemory - $this->startMemory) ?>
10✔
114
        </p>
10✔
115
        <h1>Services</h1>
116
        <h2>Loaded Service Instances</h2>
117
        <?= $this->renderLoadedServices() ?>
10✔
118
        <h2>Available Services</h2>
10✔
119
        <?php
120
        echo $this->renderAvailableServices();
10✔
121
        return \ob_get_clean(); // @phpstan-ignore-line
10✔
122
    }
123

124
    protected function renderLoadedServices() : string
125
    {
126
        $services = $this->getServices();
10✔
127
        $total = 0;
10✔
128
        foreach ($services as $data) {
10✔
129
            $total += \count($data);
9✔
130
        }
131
        if ($total === 0) {
10✔
132
            return '<p>No service instance has been loaded.</p>';
1✔
133
        }
134
        \ob_start(); ?>
9✔
135
        <p>Total of <?= $total ?> service instance<?= $total !== 1 ? 's' : '' ?> loaded.</p>
9✔
136
        <table>
137
            <thead>
138
            <tr>
139
                <th>Service</th>
140
                <th>Instances</th>
141
                <th title="Milliseconds">Time to Load</th>
142
            </tr>
143
            </thead>
144
            <tbody>
145
            <?php foreach ($services as $service => $data): ?>
9✔
146
                <?php $count = \count($data) ?>
9✔
147
                <tr>
9✔
148
                    <td rowspan="<?= $count ?>"><?= $service ?></td>
9✔
149
                    <td><?= $data[0]['name'] ?></td>
9✔
150
                    <td><?= Debugger::roundSecondsToMilliseconds($data[0]['end'] - $data[0]['start']) ?></td>
9✔
151
                </tr>
152
                <?php for ($i = 1; $i < $count; $i++): ?>
9✔
153
                    <tr>
2✔
154
                        <td><?= $data[$i]['name'] ?></td>
2✔
155
                        <td><?= Debugger::roundSecondsToMilliseconds($data[$i]['end'] - $data[$i]['start']) ?></td>
2✔
156
                    </tr>
157
                <?php endfor ?>
158
            <?php endforeach ?>
9✔
159
            </tbody>
9✔
160
        </table>
9✔
161
        <?php
9✔
162
        return \ob_get_clean(); // @phpstan-ignore-line
9✔
163
    }
164

165
    /**
166
     * @return array<string,mixed>
167
     */
168
    protected function getServices() : array
169
    {
170
        $result = [];
10✔
171
        foreach ($this->getData() as $data) {
10✔
172
            if (!isset($result[$data['service']])) {
9✔
173
                $result[$data['service']] = [];
9✔
174
            }
175
            $result[$data['service']][] = [
9✔
176
                'name' => $data['instance'],
9✔
177
                'start' => $data['start'],
9✔
178
                'end' => $data['end'],
9✔
179
            ];
9✔
180
        }
181
        return $result;
10✔
182
    }
183

184
    protected function renderAvailableServices() : string
185
    {
186
        \ob_start();
10✔
187
        $services = [];
10✔
188
        $class = new ReflectionClass($this->app);
10✔
189
        $methods = $class->getMethods(ReflectionMethod::IS_STATIC);
10✔
190
        foreach ($methods as $method) {
10✔
191
            if (!$method->isPublic()) {
10✔
192
                continue;
10✔
193
            }
194
            $name = $method->getName();
10✔
195
            if (\in_array($name, [
10✔
196
                'config',
10✔
197
                'getService',
10✔
198
                'setService',
10✔
199
                'removeService',
10✔
200
                'isCli',
10✔
201
                'setIsCli',
10✔
202
                'isDebugging',
10✔
203
            ], true)) {
10✔
204
                continue;
10✔
205
            }
206
            $param = $method->getParameters()[0] ?? null;
10✔
207
            if (!$param || $param->getName() !== 'instance') {
10✔
208
                continue;
10✔
209
            }
210
            if ($param->getType()?->getName() !== 'string') { // @phpstan-ignore-line
10✔
211
                continue;
10✔
212
            }
213
            $instances = [];
10✔
214
            if ($param->isDefaultValueAvailable()) {
10✔
215
                $instances[] = $param->getDefaultValue();
10✔
216
            }
217
            foreach ((array) $this->app::config()->getInstances($name) as $inst => $s) {
10✔
218
                $instances[] = $inst;
8✔
219
            }
220
            $instances = \array_unique($instances);
10✔
221
            \sort($instances);
10✔
222
            $services[$name] = [
10✔
223
                'returnType' => $method->getReturnType()?->getName(), // @phpstan-ignore-line
10✔
224
                'instances' => $instances,
10✔
225
            ];
10✔
226
        }
227
        \ksort($services);
10✔
228
        $countServices = \count($services);
10✔
229
        $s = 0; ?>
10✔
230
        <p>There are <?= $countServices ?> services available.</p>
10✔
231
        <table>
232
            <thead>
233
            <tr>
234
                <th>#</th>
235
                <th>Service</th>
236
                <th>Config Instances</th>
237
                <th>Return Type</th>
238
            </tr>
239
            </thead>
240
            <tbody>
241
            <?php foreach ($services as $name => $data): ?>
10✔
242
                <?php $count = \count($data['instances']) ?>
10✔
243
                <tr>
10✔
244
                    <td rowspan="<?= $count ?>"><?= ++$s ?></td>
10✔
245
                    <td rowspan="<?= $count ?>"><?= $name ?></td>
10✔
246
                    <td><?= $data['instances'][0] ?></td>
10✔
247
                    <td rowspan="<?= $count ?>"><?= $data['returnType'] ?></td>
10✔
248
                </tr>
249
                <?php for ($i = 1; $i < $count; $i++): ?>
10✔
250
                    <tr>
7✔
251
                        <td><?= $data['instances'][$i] ?></td>
7✔
252
                    </tr>
253
                <?php endfor ?>
254
            <?php endforeach ?>
10✔
255
            </tbody>
10✔
256
        </table>
10✔
257
        <?php
10✔
258
        return \ob_get_clean(); // @phpstan-ignore-line
10✔
259
    }
260
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc