Compo.php

<?php

namespace Lia;

/**
 * Base class for components. Components don't have to, but generally should extend from this class.
 *
 * @featured
 */
class Compo implements \LiaisonInterface\PackageLifeCycle {

    use \Lia\CompoTrait\Scanner;

    /**
     * The class name (no namespace)
     * The name is used by package to retrieve components
     * @featured
     */
    public $name;
    /**
     * The Package dir
     * @featured
     */
    protected $dir;
    /**
     * I think I used to put configs on components? But they're all on package now, I think
     * @deprecated
     */
    protected $configs = [];
    /**
     * Liaison instance
     * @featured
     */
    public \Liaison $lia;

    public function __construct($package){
        $this->onCreate();
        $this->package = $package;
        $this->dir = $package->dir;
        $this->lia = $package->lia;
        if (!is_object($package))throw new \Exception("package is not an object...");
        $class = get_class($this);
        $this->name = array_slice(explode('\\',$class),-1)[0];

        $package->addComponent($this->name, $this);
        $this->onReady();

        $this->lia->arset($package->name().':compo',strtolower($this->name()), $this);
    }

    public function name(){
        return $this->name;
    }

    /**
     * Do initialization before ANYTHING else is done.
     * @tag lifecycle, featured
     */
    public function onCreate(){}
    /**
     * Called at end of __construct() for the majority of setup that does NOT depend on other components.
     * @tag lifecycle, featured
     */
    public function onReady(){}
    /**
     * After all components are loaded into the package, but BEFORE everything else is finished (views, routes, and other items may not be available, but apis will be)
     * @tag lifecycle, featured
     * @todo this is also documented on the interface. I think I should remove the doc from here (but that would mess up my '@featured' thing, unless Code Scrawl got REALLY smart)
     */
    public function onComponentsLoaded(){}
    /**
     * After the package is completely ready. All routes, views, componenets, configs, etc have been loaded.
     * @tag lifecycle, featured
     */
    public function onPackageReady(){}

    /**
     * Schedule prefix-scanning to happen during PreAllPackagesReady event
     * This generally should NOT be overridden by your component.
     *
     * I'm doing it this way so components won't have to call parent::onTheEvent
     */
    public function onPrePackageReady(){
        if ($this->lia->hasApi('lia:event.schedule')){
            $this->lia->api('lia:event.schedule','PreAllPackagesReady', [$this, 'autoHandlePrefixedMethods']);
        }
    }
    
    /**
     * Get the path to the configured directory inside this component's package.
     *
     * @param  string $dirName configured directory key/name.
     * @return string directory path
     * @featured
     */
    public function dir(string $dirName): string{
        return $this->package->dir($dirName);
    }

}