PHP 全局使用 Laravel 辅助函数 dd

  • 全局安装
composer global require symfony/var-dumper
  • 配置 php.ini
    php.ini 中找到 auto_prepend_file,然后写上你相对应的路径,比如我的路径:
/Users/mymac/.composer/vendor/autoload.php
  • 更新 composer
composer global update
##这样一来就可以使用 dump() 了。不过还不够,我们还要把 dd() 的代码放进来。
  • 准备文件
    找个地方复制以下代码,我直接命名成 laravel_helper_dd.php
<?php

# install symfony/var-dump to your project
# composer require symfony/var-dumper

// use namespace
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper as SymfonyHtmlDumper;

/**
 * Class HtmlDumper
 */
class HtmlDumper extends SymfonyHtmlDumper
{
    /**
     * Colour definitions for output.
     *
     * @var array
     */
    protected $styles = [
        'default' => 'background-color:#fff; color:#222; line-height:1.2em; font-weight:normal; font:12px Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:100000',
        'num' => 'color:#a71d5d',
        'const' => 'color:#795da3',
        'str' => 'color:#df5000',
        'cchr' => 'color:#222',
        'note' => 'color:#a71d5d',
        'ref' => 'color:#a0a0a0',
        'public' => 'color:#795da3',
        'protected' => 'color:#795da3',
        'private' => 'color:#795da3',
        'meta' => 'color:#b729d9',
        'key' => 'color:#df5000',
        'index' => 'color:#a71d5d',
    ];
}

/**
 * Class Dumper
 */
class Dumper
{
    /**
     * Dump a value with elegance.
     *
     * @param  mixed  $value
     * @return void
     */
    public function dump($value)
    {
        if (class_exists(CliDumper::class)) {
            $dumper = 'cli' === PHP_SAPI ? new CliDumper : new HtmlDumper;
            $dumper->dump((new VarCloner)->cloneVar($value));
        } else {
            var_dump($value);
        }
    }
}

if (! function_exists('dd')) {
    /**
     * Dump the passed variables and end the script.
     *
     * @param  mixed
     * @return void
     */
    function dd(...$args)
    {
        foreach ($args as $x) {
            (new Dumper)->dump($x);
        }
        die(1);
    }
}

if (! function_exists('dda')) {
    /**
     * Dump the passed array variables and end the script.
     *
     * @param  mixed
     * @return void
     */
    function dda(...$args)
    {
        foreach ($args as $x) {
            (new Dumper)->dump($x->toArray());
        }
        die(1);
    }
}

记下这个文件的路径,比如我的是:/Users/mymac/.composer/laravel_helper_dd.php

  • 更改全局 composer.json
{
  ....
    "require": {
        .....
        "symfony/var-dumper": "^3.3"
    },
    "autoload": {
        "files": [
                ##加上刚刚文件的目录
                "/Users/mymac/.composer/laravel_helper_dd.php"
        ]
    }
}
  • 更新composer
composer global dump-autoload

ishow.bind-time.com Copyright © 2020 小肥青王二狗

  Previous post laravel 数据库迁移
Next post   liunx上重启php和nginx

添加新评论

无论有多困难,都坚强地抬头挺胸,告诉所有人,你并非他们想象的那样不堪一击。

每个人心里都有一段伤痕,时间才是最好的疗剂。

人总是珍惜未得到的,而遗忘了所拥有的。

退一步,并不象征我认输;放手,并不表示我放弃;微笑,并不意味我快乐!

人海中再回首,朋友真诚依旧,生命里重逢,心境平和温柔,往事如风,岁月如歌,漫漫人生路,苍桑几许,幸福几何!