Ad hoc adapter classes 06

I’m collecting the pieces to create a console command to wrap WordPress themes and plugins classes in custom adapter classes with “ease”.

Putting the pieces together

In my previous post I’ve drafted the target workflow I’d like to achieve to generate ad-hoc adapter classes like this

twp adapter:wrap ClassOneUsingAdapter  ClassTwoUsingAdapter ClassThreeUsingAdapter -var=wpf

where the work-in-progress syntax of the command would be

twp adapter:wrap <class or list of classes to wrap> [--var=<variable>] [--autoload=<path to autoload file>] [--out=<path to output file>] [--name=<name of output class>] [--namespace=<namespace of output class>] [--interface=<interfaces of output class>] [--extends=<parent class of output class>] [--wp-config=<path to headless wp-config file>]

All of the above is not a short list of parameters but I will use Symfony Console Component to be able to develop something that’s robust, friendly, interactive and as streamlined as possible.

The process behind the generation

The command will parse the classes looking for calls to the variable storing the adapter class instance and record the called methods; a class like this

class SomeClass {
    protected $wpf;

    public function __construct($wpf = null){
        $this->wpf = $wpf ? $wpf : new Adapter();
    }

    public function public_method(){
        $this->wpf->method_one();
        $this->wpf->method_two();
    }

    protected function protected_method(){
        $this->wpf->method_three();
        $this->wpf->method_four();
    }
}

will generate an array of calls like ['method_one', 'method_two', 'method_three', 'method_four'] when the specified variable is --var=wpf. A WordPress headless session is started, this semi-passive code parsing happens and the adapter class is built using information retrieved via reflection.
While the process seems and is cumbersome the context it’s supposed to happen in, development of a WordPress theme or plugin using TDD techniques, should have all the components in place already.

Next

Parsing the classes, loading an headless WordPress session and generating a custom adapter class are all done and I will be assembling them in a chain to create the command.