PackageList.php

<?php

namespace Lia\Compo;

class PackageList extends \Lia\Compo {


    /**
     * The alternate I'm suggesting is:
     *
     * Each package when constructed would do:
     * $lia->arset('lia:package', 'package_name', $this)
     *
     * And each component when constucted would do:
     * $lia->set('package_name:compo.compo_name', $this);
     *
     * Then compo('compo_name') returns $lia->get('compo_name'). Namespace optional
     *
     */


    protected $packages = [];

    public function onReady(){
        $lia = $this->lia;

        $lia->default('lia:compo', []);

        $lia->addApi('lia:package.getCompo', [$this,'compo']);
        $lia->addApiMethod('lia:package.getCompo', 'compo');

        $lia->addApi('lia:package.add',[$this,'addPackage']);
        $lia->addApiMethod('lia:package.add', 'addPackage');

        $lia->addApi('lia:package.get',[$this,'getPackage']);
        $lia->addApiMethod('lia:package.get', 'getPackage');
    }
    public function addPackage($package, $name){
        $this->lia->arset('lia:package', $name, $package);
    }

    public function getPackage($name){
        return $this->lia->arget('package', $name);
    }

    /** 
     * Get a compo(nent) from available packages
     *
     * @param $name `namespace:ComponentClassName` <- `namespace:` is optional. `ComponentClassName` does NOT include the class's namespace
     */
    public function compo($name){
        $name = strtolower($name);
        if (($pos = strpos($name, ':'))!==false){
            $key = substr($name, 0,$pos).':compo';
            $name = substr($name,$pos+1);
        } else $key = 'compo';

        return $this->lia->arget($key, $name);


        var_dump("bad");
        exit;
        return $this->lia->

        return;
        $fqn = $name;
        $parts=explode(':',$name);
        $namespace=null;
        $compoName=null;
        if (count($parts)==2){
            $namespace = $parts[0];
            $compoName = $parts[1];
        } else if (count($parts)==1){
            $compoName = $parts[0];
        }

        $list = [];

        $packageCount = 0;
        foreach ($this->packages as $name=>$package){
            if (($namespace==null||strtolower($name)==strtolower($namespace))
            ){
                $packageCount++;
                $compo = $package->compo($compoName);
                if ($compo!==null)$list[] = $compo;
            }
        }
        if ($packageCount==0&&$namespace!=null){
            throw new \Lia\Exception\Base("There is no package for namespace '${namespace}', so compo for '${fqn}' can not be loaded.");
        } else if (count($this->packages)==0){
            throw new \Lia\Exception\Base("There are no packages from which to laod components.");
        } else if (count($list)>1){
            throw new \Lia\Exception\Base("Multiple compos were returned for '${fqn}'. Try providing a namespace like \$lia->compo('namespace:componame')");
        } else if (count($list)==0){
            throw new \Lia\Exception\Base("No compos were found for '${fqn}', but the package for '${namespace}:' does exist.");
        }

        return $list[0];
    }

}