/** * Class constructor. * * The Database argument MUST be connected before passing it to the * constructor. * * @since 1.0.0 * @access public * * @param Freyja\Database\Database $database */ public function __construct(Database $database) { $this->database = $database; $this->filename = getcwd() . '/db/schema.yml'; if (file_exists($this->filename)) { // Put file content into an array. $schema = Yaml::parse(file_get_contents($this->filename)); // Check if $database exists in the array and put its schema into the // object property. $this->schema = isset($schema[$database->getName()]) ? $schema[$database->getName()] : array(); if (!isset($this->schema['tables'])) { $this->schema['tables'] = array(); } // At this point $schema has this structure: // `array('tables'=>array())` // and the internal array may or may not contain some tables. // $schema is the schema of $database, therefore it must be dumped by Yaml // in this way: `array($database->getName() => $schema)`. } }
/** * Update schema. * * Update object property and yaml configuration file as well. * * @since 1.0.0 * @access private * * @param */ private function updateSchema() { // Retrieve current schema. $schema = array(); if (file_exists($this->filename)) { $schema = Yaml::parse(file_get_contents($this->filename)); } // Replace database schema with the updated one. // Create it if not already there. $schema[$this->database->getName()] = $this->schema; // Rewrite yaml file. $yaml_string = Yaml::dump($schema); if (!file_exists(getcwd() . '/db')) { mkdir(getcwd() . '/db'); } file_put_contents($this->filename, $yaml_string); }