system/Debug/Toolbar.php
public function prepare()
	{
		if (CI_DEBUG && ! is_cli())
		{
			global $app;

			$request  = Services::request();
			$response = Services::response();

			$toolbar = Services::toolbar(config(Toolbar::class));
			$stats   = $app->getPerformanceStats();
			$data    = $toolbar->run(
				$stats['startTime'],
				$stats['totalTime'],
				$request,
				$response
			);

			helper('filesystem');

			// Updated to time() so we can get history
			$time = time();

			if (! is_dir(WRITEPATH . 'debugbar'))
			{
				mkdir(WRITEPATH . 'debugbar', 0777);
			}

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

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

			// Non-HTML formats should not include the debugbar
			// then we send headers saying where to find the debug data
			// for this response
			if ($request->isAJAX() || strpos($format, 'html') === false)
			{
				$response->setHeader('Debugbar-Time', $time)
						 ->setHeader('Debugbar-Link', site_url("?debugbar_time={$time}"))
						 ->getBody();

				return;
			}

			$script = PHP_EOL
				. '<script type="text/javascript" {csp-script-nonce} id="debugbar_loader" '
				. 'data-time="' . $time . '" '
				. 'src="' . site_url() . '?debugbar"></script>'
				. '<script type="text/javascript" {csp-script-nonce} id="debugbar_dynamic_script"></script>'
				. '<style type="text/css" {csp-style-nonce} id="debugbar_dynamic_style"></style>'
				. PHP_EOL;

			if (strpos($response->getBody(), '</body>') !== false)
			{
				$response->setBody(
					str_replace('</body>', $script . '</body>', $response->getBody())
				);

				return;
			}

			$response->appendBody($script);
		}
	}
public function prepare()
	{
		if (CI_DEBUG && ! is_cli())
		{
			global $app;

			$request  = Services::request();
			$response = Services::response();

			$toolbar = Services::toolbar(config(Toolbar::class));
			$stats   = $app->getPerformanceStats();
			$data    = $toolbar->run(
				$stats['startTime'],
				$stats['totalTime'],
				$request,
				$response
			);

			helper('filesystem');

			// Updated to time() so we can get history
			$time = time();

			if (! is_dir(WRITEPATH . 'debugbar'))
			{
				mkdir(WRITEPATH . 'debugbar', 0777);
			}

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

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

			// Non-HTML formats should not include the debugbar
			// then we send headers saying where to find the debug data
			// for this response
			if ($request->isAJAX() || strpos($format, 'html') === false)
			{
				$response->setHeader('Debugbar-Time', $time)
						 ->setHeader('Debugbar-Link', site_url("?debugbar_time={$time}"))
						 ->getBody();

				return;
			}

			$script = PHP_EOL
				. '<script type="text/javascript" {csp-script-nonce} id="debugbar_loader" '
				. 'data-time="' . $time . '" '
				. 'src="' . site_url() . '?debugbar"></script>'
				. '<script type="text/javascript" {csp-script-nonce} id="debugbar_dynamic_script"></script>'
				. '<style type="text/css" {csp-style-nonce} id="debugbar_dynamic_style"></style>'
				. PHP_EOL;

			if (strpos($response->getBody(), '</body>') !== false)
			{
				$response->setBody(
					str_replace('</body>', $script . '</body>', $response->getBody())
				);

				return;
			}

			$response->appendBody($script);
		}
	}
--------------------------------------------------------------------