decodelabs/veneer

Automated static facades

v0.11.6 2024-08-21 15:43 UTC

README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Create automated static frontages for your PHP objects.

Use Veneer to provide easy access to your most commonly used functionality without sacrificing testability.

Get news and updates on the DecodeLabs blog.

Install

composer require decodelabs/veneer

Usage

Say you have a common library class you use regularly:

namespace Some\Random\Library
{
    // This is a library class you use regularly
    class MyThing {
        public function doAThing() {
            echo 'Done!';
        }
    }
}

You can bind a static, automatically generated frontage by:

namespace App\Setup
{
    // This is your environment setup code
    use DecodeLabs\Veneer;
    use Some\Random\Library\MyThing;
    use App\CoolThing;

    Veneer::register(
        MyThing::class, // active object class
        MyThingFrontage::class // frontage class
    );
}

namespace Some\Other\Code
{
    use App\CoolThing;

    // Your general userland code
    CoolThing::doAThing();
}

Plugins

Unfortunately PHP still doesn't have __getStatic() yet so we have to statically declare plugin names at binding time, but they're still useful for creating more expansive interfaces.

Define plugins as properties on your FacadeTarget with a Plugin attribute, include LazyLoad attribute too if it doesn't need to be loaded straight away.

namespace My\Library
{
    use DecodeLabs\Veneer\Plugin;
    use DecodeLabs\Veneer\LazyLoad;

    class MyThing {

        #[Plugin]
        #[LazyLoad]
        public MyPlugin $plugin;
    }


    class MyPlugin
    {
        public function doAThing(): string {
            return 'Hello from plugin1';
        }
    }
}

namespace Some\Other\Code
{
    use My\Library\MyThing;

    MyThing::$plugin->doAThing(); // Hello from plugin1
}

Note, if your target class has a constructor with required parameters, you will need to add decodelabs/slingshot to your project to allow Veneer to instantiate it.

Licensing

Veneer is licensed under the MIT License. See LICENSE for the full license text.