tan5en5/codeigniter-debugbar

使用 PHP Debug Bar 的 CodeIgniter Debug Bar。

安装数: 40,890

依赖关系: 0

建议者: 0

安全性: 0

星标: 57

关注者: 4

分支: 26

类型:codeigniter-third-party

dev-master 2017-01-19 14:01 UTC

This package is not auto-updated.

Last update: 2024-09-19 09:40:50 UTC


README

  • PHP 5.5+(由 CodeIgniter 推荐使用)
  • CodeIgniter 3.1.x

安装

如果应用根目录中没有 composer.json 文件,则创建它。在文件中添加以下文本

{
    "require": {
        "tan5en5/codeigniter-debugbar": "dev-master"
    }
}

启用 Composer(在 application/config/config.php 中定位)

$config['composer_autoload'] = realpath(APPPATH.'../vendor/autoload.php');

启用 Debugbar 包(在 application/config/autoload.php 中定位)

$autoload['packages'] = array(APPPATH.'third_party/codeigniter-debugbar');

如果您想记录消息和异常,还可以加载控制台库

$autoload['libraries'] = array('console');

来使用它。

$this->console->exception(new Exception('test exception'));
$this->console->debug('Debug message');
$this->console->info('Info message');
$this->console->warning('Warning message');
$this->console->error('Error message');

然后,像平常一样启用分析器。

$this->output->enable_profiler(TRUE);

为了完成安装,请添加以下头部标签

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/highlight.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/github.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">

配置

复制位于 application/third_party/codeigniter-debugbar/config/profiler.php 的配置文件到 application/config/profiler.php

要配置分析器,请阅读 CodeIgniter 分析器文档

CodeIgniter Debug Bar 添加了 4 个新部分

  • CodeIgniter 信息:显示关于 CodeIgniter 的信息(版本、环境和区域)。
  • 消息:显示消息(必须加载控制台库)。
  • 异常:显示异常(必须加载控制台库)。
  • 包含的文件:显示包含或所需的文件。

您可以直接在分析器配置文件中配置 PHP Debug Bar,有关更多信息,请阅读 PHP Debug Bar 文档

高级配置

AJAX

默认情况下,ajax 调试数据通过头部发送,但如果您发送大量数据,可能会导致浏览器出现问题。如果您在配置文件中设置 open_handler_url,它将使用存储处理程序和打开处理程序在 AJAX 请求后加载数据。

以下是一个 open_handler_url 设置的示例。

$config['open_handler_url'] = get_instance()->config->site_url('debug/open_handler');

此代码将在 application/controllers/Debug.php 中。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use DebugBar\DebugBar;
use DebugBar\OpenHandler;
use DebugBar\Storage\FileStorage;

class Debug extends CI_Controller 
{
    public function open_handler()
    {
        $this->output->enable_profiler(FALSE);
        $this->config->load('profiler', TRUE);
        $path = $this->config->item('cache_path', 'profiler');
        $cache_path = ($path === '') ? APPPATH.'cache/debugbar/' : $path;
        $debugbar = new DebugBar();
        $debugbar->setStorage(new FileStorage($cache_path));
        $openHandler = new OpenHandler($debugbar);
        $data = $openHandler->handle(NULL, FALSE, FALSE);

        $this->output
            ->set_content_type('application/json')
            ->set_output($data);
    }
}

输出

有两种选项可以用于处理自定义分析器输出。

  • display_assets:是否显示内容资源(默认:TRUE)
  • display_javascript:是否显示内联脚本(默认:TRUE)

如果将 display_assets 设置为 false,您必须手动处理资源输出,为此,您可以使用 CI_Profiler::css_assets()CI_Profiler::js_assets(),它们的行为与 JavascriptRenderer::dumpJsAssets()JavascriptRenderer::dumpJsAssets() 相同,请参阅 PHP Debug Bar 文档

如果将 display_javascript 设置为 false,您必须手动处理内联脚本,为此,您可以使用 CI_Profiler::inline_script()重要:它使用 <script> 标签显示内联脚本!)。

以下是如何使用的示例

<?php

    /**
     * This method handle custom profiler output if profiler is enable, except
     * for json output.
     */
    public function _output($output)
    {
        if (stripos($this->output->get_content_type(), 'json') !== false)
        {
            echo $output;
            return;
        }

        if ($this->output->enable_profiler)
        {
            $this->appendAssets()
                ->appendBody('<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>')
                ->appendBody('<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/highlight.min.js"></script>')
                ->appendHeader('<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/github.min.css">')
                ->appendHeader('<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">')
                ->appendInlineScript(trim(str_replace(['<script type="text/javascript">', '</script>'], ['', ''], CI_Profiler::inline_script())));
        }

        echo $output;
    }

    /**
     * This method will write PhpDebugbar assets files if they don't exist in
     * public directory and add them to the output with custom functions.
     */
    protected function appendAssets()
    {
        $files = ['css_assets' => 'public/css/PhpDebugbar.css', 'js_assets' => 'public/js/PhpDebugbar.js'];

        foreach ($files as $function => $filepath)
        {
            if (!file_exists(FCPATH.$filepath))
            {
                forward_static_call_array(array('CI_Profiler', $function), array(FCPATH.$filepath));
            }
        }

        return $this->appendBody('<script type="text/javascript" src="'.base_url($files['js_assets']).'"></script>')
            ->appendHeader('<link rel="stylesheet" href="'.base_url($files['css_assets']).'">');
    }

重要:处理分析器输出的函数只能在 CodeIgniter 控制器函数 _output() 中使用。

许可证

MIT 许可证 (MIT)

版权所有 (c) 2014-2017 Anthony Tansens

在此条件下,授予任何获得此软件和相关文档副本(“软件”)的人免费使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得软件的人进行上述行为,前提如下

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的还是其他形式的,包括但不限于对适销性、适用于特定目的和非侵权的保证。在任何情况下,作者或版权持有人不对任何索赔、损害或其他责任承担责任,无论这些责任是因合同行为、侵权或其他行为引起的,以及与软件或软件的使用或其他方式相关。