ajthinking/anything

通过想象公共API来创建代码

v0.1.0 2022-06-01 08:50 UTC

This package is auto-updated.

Last update: 2024-09-11 07:43:31 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

想象公共API并实现它 - 一个用于Laravel项目的TDD和草图玩具 🤩

github_readme_gif

入门

按照如下方式安装并启用

composer require ajthinking/anything --dev
php artisan anything:on

然后,完善你的想法,并在控制台环境中执行它。比如我们想要构建一个git客户端

// This class does not exist, yet
App\Support\Git::client()
  ->pull()
  ->add('*')
  ->nah()
  ->wip()
  ->unwip()
  ->commit('message')
  ->push()
  ->build() // <-- creates the class!

最后一个方法调用 ->build() 将为你创建这个类以及所有方法占位符

<?php

namespace App\Support;

class Git
{
    public static function client()
    {
        return new static();
    }
    
    public function pull()
    {
        return $this;
    }
    
    public function add()
    {
        return $this;
    }

	// ...
}

当像上面那样静态引用时,第一次调用通常会产生一个静态方法。然而,如果类名包含 Facades\,例如 App\Facades\Zonda,我们将将其作为一个实例方法,并在旁边创建一个外观

<?php

namespace App\Support\Facades;

use Illuminate\Support\Facades\Facade;

class Zonda extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'App\\Zonda';
    }
}

最后,确保通过git丢弃对 bootstrap/app.php 的更改,或者通过运行 php artisan anything:off 来清理

注意事项

这个实验有一些限制。

  • anything:on/off 命令会对你的 bootstrap/app.php 进行一点干扰,以临时交换控制台内核。它假设你未对文件进行任何重大修改。
  • 它只适用于 App 命名空间中的类
  • 当前忽略方法参数
  • 假设所有操作都返回 $this
  • 在使用测试时,确保你的测试用例使用 CreatesApplication。这可能并不总是适用于单元测试设置

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。