* @link https://TheTempusProject.com/libraries/Bedrock * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Bedrock\Classes; use TheTempusProject\Canary\Bin\Canary as Debug; use TheTempusProject\Bedrock\Classes\Config; use TheTempusProject\Bedrock\Classes\Database; class Model { public static $db; public $configName; public $configMatrix = []; public $resourceMatrix = []; public $modelVersion; public $enabled = false; /** * The model constructor. */ public function __construct() { Debug::debug( 'Model Constructed: ' . get_class( $this ) ); self::$db = Database::getInstance(); $this->load(); } /** * This method will remove all the installed model components. * * @return bool - If the uninstall was completed without error */ public function uninstall() { $this->uninstallResources(); $this->uninstallConfigs(); return true; } /** * Install resources needed for the model. * * @return {bool} */ public function installResources() { // should have some sort of DELTA functionality and safeguards $ids = []; if ( empty($this->resourceMatrix) ) { return true; } foreach ( $this->resourceMatrix as $entry ) { foreach ( $entry as $key => $value ) { if ( '{time}' == $value ) { $entry[$key] = time(); } } self::$db->insert( $this->tableName, $entry ); $id = self::$db->lastId(); if ( $id ) { $ids[] = $id; } } return $ids; } public function uninstallResources() { // this one needs some work // should probably save the created resource ID's or something and remove them. // presumably this isn't a big issue because i would imagine the table is removed // there may be future instances where you can create resources for other tables return true; } /** * Install configs needed for the model. * * @return {bool} */ public function installConfigs() { $config = new Config( CONFIG_JSON ); // should have some sort of DELTA functionality and safeguards $config->addCategory( $this->configName ); foreach ( $this->configMatrix as $name => $details ) { $config->add( $this->configName, $name, $details ); } return $config->save(); } public function uninstallConfigs() { if ( empty( $this->configName ) ) { return true; } $config = new Config( CONFIG_JSON ); return $config->removeCategory( $this->configName, true, true ); } /** * Tells the installer which types of integrations your model needs to install. * * @return bool - if the model was loaded without error */ public function load() { return true; } public function getModelVersion() { return $this->modelVersion; } /** * Checks if the model is enabled. * * @return bool - if the model is enabled or not */ public function enabled() { return $this->enabled; } }