Addon.php

<?php

namespace Lia;

class Addon {

    /**
     * Fully qualified name for accessing this addon
     */
    public string $fqn;

    /**
     * The short name of this addon
     */
    public string $name;

    /** Package */
    public ?\Lia\Package $package;
    public ?\Lia $lia;

    public function __construct(?\Lia\Package $package=null){
        $this->package = $package;
        $this->lia = $package->lia ?? null;

        if (!isset($this->name)){
            $addon_name = strtolower(get_class($this));
            $pos = strrpos($addon_name, '\\');
            if ($pos!==false)$pos +=1;
            $this->name = substr($addon_name, $pos);
        }
        if (!isset($this->fqn)){
            throw new \Exception("Every addon MUST declare `public string \$fqn = 'fqn'`, where fqn SHOULD be like `vendor:package.addon_name`\n\n");
        }

        if ($package!=null){
            $this->lia->addons[$this->name] = $this;
            $this->lia->fqn_addons[$this->fqn] = $this;
            $package->addons[$this->name] = $this;
        }
    }


    /** 
    * Called when Liaison has been setup for the package, but the Package is not fully ready yet.
    * 
    * For `\Lia\Package` subclasses, call `$package->init_lia()` to invoke every addon's `init_lia($package)` method.
    * For `\Lia\Package\Server` subclasses, `$package->init_lia()` is automatically called at the end of `__construct()` just before `onPackageReady()` will be called.
    *
    * @NOTE Addon::init_lia() is called with paramaters, but not defined with them, because existing addons are already implementing the naked version of this method. This will surely be updated in the future, likely with a different method name.
    * 
    */
    public function init_lia() {}

    /**
     * Called when the package is fully setup, for addons to do any final setup steps.
     *
     * For `\Lia\Package` subclasses, call `$package->ready()` to invoke every addon's `onPackageReady($package)` method.
     * For `\Lia\Package\Server` subclasses, `$package->ready()` is automatically called at the VERY end of `__construct()`, AFTER `init_lia($package)` has been called on every addon.
     *
     * @NOTE Addon::onPackageReady() is called with paramaters, but not defined with them, because existing addons are already implementing the naked version of this method. This will surely be updated in the future, likely with a different method name.
     */
    public function onPackageReady(){}
}