BigDb.php

<?php

namespace Tlf;

/**
 *
 */
class BigDb {

    /** for convenience sql commands like select, insert, update, delete */
    use BigDb\SqlVerbs;
    /** To easily load sql from .sql files */
    use BigDb\SqlFiles;
    /** Integrate with LilDb's LilMigrations class, to handle database versioning */
    use BigDb\Migrations;
    /** For loading of ORMs */
    use BigDb\GetOrms;

    protected \PDO $pdo;
    public \Tlf\LilDb $ldb;
    /** Sql queries, typically from .sql files on disk */
    protected array $sql;
    /** The namespace from which Orm classes should be loaded */
    protected string $orm_namespace;
    /** The name used by BigDbServer to identify this BigDb instance. Does NOT correspond to a mysql database. Also, {}@see get_db_name()} */
    protected string $db_name;


    public function __construct(\PDO $pdo){
        $this->pdo = $pdo;
        $this->ldb = new \Tlf\LilDb($pdo);

        $this->init();
    }

    /**
     * Initialize BigDb by loading queries, setting up Orm loader, and such, using a defined directory structure. 
     * Does nothing on `Tlf\BigDb`, but subclasse init from the directory they are defined in.
     *
     * @override if you're using a non-standard directory structure. If only your root dir is nonstandard, override {@see get_root_dir()}
     */
    public function init(){
        if (!is_subclass_of($this, self::class)) {
            return;
        }

        $root_dir = $this->get_root_dir();
        $this->init_from_dir($root_dir);
    }

    /**
     * Get path to the root of a BigDb library. 
     *
     * @return directory name where your BigDb subclass is defined, or null if not a subclass
     * @override if your BigDb subclass is not in the root directory of your bigdb library. Override {@see init()} or {@see init_from_dir()} if you have more extensive changes.
     */
    public function get_root_dir(): ?string {
        if (!is_subclass_of($this, self::class)){
            return null;
        }
        $refClass = new \ReflectionClass($this);
        $file = $refClass->getFileName();
        $dir = dirname($file);
        return $dir;
    }

    /**
     * Initialize a BigDb library from a directory using the strictly defined directory structure.
     *
     * @param $dir the root dir of your bigdb library
     */
    public function init_from_dir(string $dir){
        
    }

    /**
     * Get a name for the BigDbServer to reference.
     * @return string a string name, typically snake_case. Default implementation returns `$this->db_name` if set, or a snake_case version of the class's basename if `protected $db_name` is not set.
     *
     * @override if setting `$this->db_name` will not work for you AND you don't want a snake_case version of the class's basename.
     */
    public function get_db_name(): string {
        if (isset($this->db_name))return $this->db_name;
        $class_name = get_class($this);
        $parts = explode('\\', $class_name);
        $base = array_pop($parts);
        $snake = preg_replace('/([a-z])([A-Z])([a-z])/', '$1_$2$3', $base);
        $snake = strtolower($snake);
        return $snake;
    }


}