LiaBC.php

<?php

/**
 * A very hacky backward-compatibility class for Liaison
 */
class LiaBC extends Lia {

    protected $bc_api_map = [];
    protected $bc_api_mediators = [];
    protected $bc_prefix_callables = [];

    public function __construct($options=[]){
        parent::__construct();


        $this->addApi('lia:config.get', [$this, 'get']);
        $this->addApi('lia:config.default', [$this, 'default']);
        $this->addApi('lia:config.set', [$this, 'set']);
        $this->addApi('lia:config.append', [$this, 'append']);


        if (($options['bare']??false))return;
        new \Lia\Package($this, dirname(__DIR__,2),['name'=>'Liaison', 'namespace'=>'lia']);
    }

    public function get($key, $orValue=null){
        // $value = parent::get(str_replace(':','.',$key));
        $value = parent::get($key);
        // var_dump($orValue);
        if ($value===null)return $orValue;
        return $value;
    }

    public function addPrefix($prefix, $callable){
        $this->bc_prefix_callables[$prefix] = $callable;
        $this->addApiPrefix($callable, $prefix);
    }

    public function addApiPrefix($api, $prefix){
        parent::addPrefix($prefix, $api);
    }
    public function getApiPrefixes(){
        return $this->prefixes;
    }

    public function api($name, ...$args){
        // return parent::api($name, ...$args);
        $name = str_replace(':', '.', $name);
        // var_dump($name);
        // var_dump(array_keys($this->methods['lia']['config']));
        return parent::api($name,...$args);
    }

    public function hasApi($name){
        $methods = &$this->methods;
        $name = str_replace(':', '.', $name);
        foreach (explode('.', $name) as $key){
            if (!isset($methods[$key]))return false;
            $methods = &$methods[$key];
        }
        return true;
    }

    public function addApi($name, $callable){
        
        $name = str_replace(':','.', $name);
        $this->addMethod($name, $callable);

        if (isset($this->bc_api_map[$name])){
            if (isset($this->bc_api_mediators[$name])){
                $mediator = $this->bc_api_mediators[$name];
                $callable = $mediator($name, $this->bc_api_map[$name], $callable);
            }
            $this->addMethod($this->bc_api_map[$name], $callable);
        } else if (isset($this->bc_api_map_callables[$name])
            && !isset($this->bc_api_mediators[$name])){
                throw new \Lia\Exception("No mediator found to handle 'api:{$name}', but a duplicate was added.");
        }

        $this->bc_api_map_callables[$name] = $callable;
    }

    public function addApiMethod($api, $method_name){


        $methods = &$this->methods;

        $api = str_replace(':','.', $api);
        foreach (explode('.', $api) as $key){
            $methods = &$methods[$key];
        }

        // echo "\n\n---- addApimethod ---\n\n";
    

        /** if $api was recently mapped to a method, then remap that method to the new callable */
        if (isset($this->bc_api_map[$api])){
            // var_dump('api: '. $api);
            // var_dump('method: '. $method_name);
            // var_dump($this->bc_api_map);
            $this->addMethod($this->bc_api_map[$api], $methods);
            // exit;
        }

        $this->addMethod($method_name, $methods);


        $this->bc_api_map[$api] = $method_name;
    }

    public function removeApi($api_name){

        $api = str_replace(':','.', $api_name);
        foreach (explode('.', $api) as $key){
            $methods = &$methods[$key];
        }

        unset($this->bc_api_map_callables[$api_name]);
        // unset($this->bc_api_map[$api_name]);
    }


    public function addMediator($api, $callable){
        $this->bc_api_mediators[$api] = $callable;
    }
}