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

michalsn / codeigniter-htmx / 13707691824

06 Mar 2025 08:36PM UTC coverage: 74.783%. First build
13707691824

Pull #83

github

web-flow
Merge 7f79fbae7 into 2ec7f6426
Pull Request #83: chore: update workflow to support PHP 8.4

17 of 22 new or added lines in 6 files covered. (77.27%)

258 of 345 relevant lines covered (74.78%)

7.91 hits per line

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

2.04
/src/Debug/Toolbar.php
1
<?php
2

3
namespace Michalsn\CodeIgniterHtmx\Debug;
4

5
use CodeIgniter\Debug\Toolbar as BaseToolbar;
6
use CodeIgniter\HTTP\DownloadResponse;
7
use CodeIgniter\HTTP\RequestInterface;
8
use CodeIgniter\HTTP\ResponseInterface;
9
use CodeIgniter\I18n\Time;
10
use Config\Services;
11
use Config\Toolbar as ToolbarConfig;
12
use Kint\Kint;
13
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest;
14
use Michalsn\CodeIgniterHtmx\HTTP\Response;
15

16
class Toolbar extends BaseToolbar
17
{
18
    /**
19
     * Prepare for debugging.
20
     *
21
     * @psalm-suppress UndefinedClass
22
     */
23
    public function prepare(?RequestInterface $request = null, ?ResponseInterface $response = null)
24
    {
25
        /**
26
         * @var IncomingRequest|null $request
27
         * @var Response|null        $response
28
         */
29
        if (CI_DEBUG && ! is_cli()) {
2✔
30
            $app = Services::codeigniter();
×
31

32
            $request ??= Services::request();
×
33
            $response ??= Services::response();
×
34

35
            // Disable the toolbar for downloads
36
            if ($response instanceof DownloadResponse) {
×
37
                return;
×
38
            }
39

40
            $toolbar = Services::toolbar(config(ToolbarConfig::class));
×
41
            $stats   = $app->getPerformanceStats();
×
42
            $data    = $toolbar->run(
×
43
                $stats['startTime'],
×
44
                $stats['totalTime'],
×
45
                $request,
×
NEW
46
                $response,
×
47
            );
×
48

49
            helper('filesystem');
×
50

51
            // Updated to microtime() so we can get history
52
            $time = sprintf('%.6f', Time::now()->format('U.u'));
×
53

54
            if (! is_dir(WRITEPATH . 'debugbar')) {
×
55
                mkdir(WRITEPATH . 'debugbar', 0777);
×
56
            }
57

58
            write_file(WRITEPATH . 'debugbar/debugbar_' . $time . '.json', $data, 'w+');
×
59

60
            $format = $response->getHeaderLine('content-type');
×
61

62
            // Non-HTML formats should not include the debugbar
63
            // then we send headers saying where to find the debug data
64
            // for this response
65
            if ($request->isAJAX() || $request->isHtmx() || ! str_contains($format, 'html')) {
×
66
                $response->setHeader('Debugbar-Time', "{$time}")
×
67
                    ->setHeader('Debugbar-Link', site_url("?debugbar_time={$time}"));
×
68

69
                return;
×
70
            }
71

72
            $oldKintMode        = Kint::$mode_default;
×
73
            Kint::$mode_default = Kint::MODE_RICH;
×
74
            $kintScript         = @Kint::dump('');
×
75
            Kint::$mode_default = $oldKintMode;
×
76
            $kintScript         = substr($kintScript, 0, strpos($kintScript, '</style>') + 8);
×
77
            $kintScript         = ($kintScript === '0') ? '' : $kintScript;
×
78

79
            $script = PHP_EOL
×
80
                . '<script ' . csp_script_nonce() . ' id="debugbar_loader" '
×
81
                . 'data-time="' . $time . '" '
×
82
                . 'src="' . site_url() . '?debugbar"></script>'
×
83
                . '<script ' . csp_script_nonce() . ' id="debugbar_dynamic_script"></script>'
×
84
                . '<style ' . csp_style_nonce() . ' id="debugbar_dynamic_style"></style>'
×
85
                . $kintScript
×
86
                . PHP_EOL;
×
87

88
            if (str_contains($response->getBody(), '<head>')) {
×
89
                $response->setBody(
×
90
                    preg_replace(
×
91
                        '/<head>/',
×
92
                        '<head>' . $script,
×
93
                        $response->getBody(),
×
NEW
94
                        1,
×
NEW
95
                    ),
×
96
                );
×
97

98
                return;
×
99
            }
100

101
            $response->appendBody($script);
×
102
        }
103
    }
104
}
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

© 2025 Coveralls, Inc