ajthinking/anything

Create code by imagining public apis

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

This package is auto-updated.

Last update: 2024-12-11 08:12:28 UTC


README

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

Imagine public API:s and make it so - a TDD and sketch toy for Laravel projects 🤩

github_readme_gif

Getting started

Install and enable like so:

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

Then, flesh out your idea and execute it in a console context. Lets say we want to build a git client:

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

The last method call ->build() will create this class for you along with all the method stubs:

<?php

namespace App\Support;

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

	// ...
}

When referenced statically like above the first call will typically spawn a static method. However, if the class name contains Facades\ lets say App\Facades\Zonda we will instead make it an instance method and create a facade next to it:

<?php

namespace App\Support\Facades;

use Illuminate\Support\Facades\Facade;

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

Finally, make sure to clean up by discarding the change in bootstrap/app.php with git, or by running php artisan anything:off

Gotchas

This experiment comes with some limitations.

  • anything:on/off commands it makes a little intrusion in your bootstrap/app.php to temporary swap out the console kernel. It assumes you have not made any major modifications to this file.
  • it will only work for classes in the App namespace
  • method arguments are currently ignored
  • assumes everything returns $this
  • when using in tests, make sure your test case uses CreatesApplication. This might not always be the case for unit test setups

License

The MIT License (MIT). Please see License File for more information.