Pink-Crab / Perique-Framework

The Perqiue Plugin Framework for WordPress
5 stars 0 forks source link

Set default view path as the location APP_Factory was called from #56

Closed gin0115 closed 2 years ago

gin0115 commented 2 years ago

Proposed change to the contructor.


/**
 * The base path of the app.
 *
 * @var string
 */
protected $base_path;

public function __construct(?string $base_path = null) {
    $this->app = new App();

    if(null === $base_path){
        $trace = debug_backtrace();
        $this->base_path = isset($trace[0]['file']) ? dirname( $trace[0]['file'] ) : __DIR__;
    } else {
        $this->base_path = dirname( $base_path );
    }
}

This would allow the passing of a path to be passed to the constructor, but if not passed would get the path where the class was created from.

Then when we create the default version PHP engine for Renderable, we can set a better base path.

/**
 * Returns the basic DI rules which are used to set.
 * WPDB
 * Renderable with PHP_Engine implementation
 *
 * @return array<mixed>
 */
protected function default_di_rules(): array {
    return array(
        '*' => array(
            'substitutions' => array(
                Renderable::class => new PHP_Engine( $this->base_path ),
            ),
        ),
    );
}