composer 自动加载,以及laravel dd函数的构建

创建composer文件

$ composer init

命令流程

$ composer init

Welcome to the Composer config generator

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [ailuoy/design]:
Description []: Design patterns
Author [ailuoy <3211028@qq.com>, n to skip]:
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []: MIT

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

{
    "name": "ailuoy/design",
    "description": "Design patterns",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "ailuoy",
            "email": "3211028@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Do you confirm generation [yes]? yes

参照laravel框架创建以下目录

# 应用目录
$ mkdir app
# 存放各种函数,之后的dd函数,会放在这里
$ mkdir app\Handles
# 入口文件
$ mkdir public
$ touch public/index.php

增加composer.json自动加载

"autoload":{
        "psr-4": {
            "App\\": "app/"
        }
    }
$ composer update

增加laravel dd方法

引入symfony/var-dumper

$ composer require symfony/var-dumper

增加类文件

我是放在了app\Handles\Dumper.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);
    }
}

修改composer.json

"autoload":{
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/Handles/Dumper.php"
        ]
    }
$ composer dump-autoload

入口文件

require __DIR__.'/../vendor/autoload.php';
// 我这里创建了一个单列
use App\Singleton\Singleton;

$singleton_one  = Singleton::getInstance();
$singleton_two  = Singleton::getInstance();

$singleton_one->name = 1;
$singleton_two->name = 2;

//这里使用了laravel的 dd函数
dd($singleton_one,$singleton_two);

效果

Singleton {#4 ▼
  +name: 2
}
Singleton {#4 ▼
  +name: 2
}