Server.php

<?php

namespace Lia\Package;

/**
 *
 * @test loading of config.json
 */
#[\AllowDynamicProperties]
class Server extends \Lia\Package {

    public string $fqn = 'lia:server';

    public string $name = 'server';

    /** key=>value params to automatically `extract()` into public files */
    public array $public_file_params = [];

    /**
     * Setting this directly does not work, bc routes are setup during the constructor.
     *
     * Instead do `$lia->set('package_name.base_url', '/base_url/');`
     */
    public $base_url = '';

    /**
     * @param $path the url path within the package
     * @return url with the base_url prepended
     */
    public function url(string $path){
        return str_replace(
            '//', '/',
            $this->base_url.$path
        );
    }

    public function __construct($lia, $name='lia:server', $dir=null, $base_url=null){
        parent::__construct($lia, $name, $dir);

        $config = $dir.'/config.json';
        if (is_file($config)){
            $props = json_decode(file_get_contents($config), true);
            foreach ($props as $p=>$value){
                if (property_exists($this, $p))$this->$p = $value;
                unset($props[$p]);
            }
        }
        if (!is_null($base_url)){
            $this->base_url = $base_url;
        }

        $this->dir = $dir ?? dirname(__DIR__);
        $this->load_addons('addon');

        if (!isset($lia->cache->dir))$lia->cache->dir = $this->dir('cache');

        // var_dump($this->dir);
        // exit;
        $public_dir = $this->dir.'/public/';
        $patterns = $lia->fqn_addons['lia:server.router']
                         ->dir_to_patterns($public_dir);

        $results = $lia->fqn_addons['lia:server.hook']->call(\Lia\Hooks::PACKAGE_ROUTE_PATTERNS_LOADED, $this, $patterns);
        $num_results = count($results);
        if ($num_results == 1){
            $patterns = $results[0];
        } else if ($num_results > 1){
            throw new \Exception("Only one hook may be registered to '".\Lia\Hooks::PACKAGE_ROUTE_PATTERNS_LOADED."'. ".count($results)." were registered.");
        } // else zero, do nothing

        // var_dump($patterns);

        // var_dump($dir);
        // print_r($patterns);
        foreach ($patterns as $file=>$pattern){
            $full_pattern = $this->base_url.$pattern;

            // echo "\nPattern: $full_pattern";
            // echo "\n   File: $public_dir/$file";
            $lia->addRoute($full_pattern,$public_dir.'/'.$file, $this);
        }
        // echo "\n\n\n-----------\n\n";
        // echo "\n\n\n-----------\n\n";

        $view_dir = $this->dir.'/view/';
        if (is_dir($view_dir)){
            $lia->fqn_addons['lia:server.view']->addDir($view_dir, $this);
        }


        // @NOTE I'm adding dir `/theme/` as a view dir in Server package setup, and I don't know why. I should remove this probably.
        $theme_dir = $this->dir.'/theme/';
        if (is_dir($theme_dir)){
            $lia->fqn_addons['lia:server.view']->addDir($theme_dir, $this);
        }

        // echo 'pre init';
        $this->init_lia();
        // echo 'READYhere';
        // exit;
        $this->ready();
    }

    // @NOTE old 'setup_public_routes()' method has been disabled. It had an error & doesn't appear to be used
    //public function setup_public_routes($dir, $prefix){
        //$patterns = $this->lia->fqn_addons['lia:server.router']
                         //->dir_to_patterns($dir);

        //foreach ($patterns as $file=>$pattern){
            //$lia->addRoute($prefix.$pattern,$public_dir.'/'.$file, $this);
        //}
    //}

    static public function main($lia){
        return new static($lia, 'server', dirname(__DIR__));
    }
    static public function main_dir(){
        return dirname(__DIR__);
    }

    public function goto($url){
        $url = $this->base_url.'/'.$url;
        $url = str_replace(['///','//'], '/', $url);
        $this->lia->goto($url);
        // $this->lia->dump();
    }

}