facut pana la video 7

This commit is contained in:
Cerbu Andrei Mihnea
2023-05-10 00:47:48 +03:00
parent 1abdc313f4
commit 90f12ab916
40 changed files with 1644 additions and 41 deletions
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace app\core;
class Database{
public \PDO $pdo;
public function __construct(array $config){
$dsn = $config['dsn'] ?? '';
$user = $config['user'] ?? '';
$password = $config['password'] ?? '';
$this->pdo = new \PDO($dsn, $user, $password);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
public function applyMigrations(){
$this->createMigrationsTable();
$appliedMigrations = $this->getAppliedMigrations();
$newMigrations = [];
$files = scandir(Application::$ROOT_DIR.'/migrations');
$toApplyMigrations = array_diff($files, $appliedMigrations);
foreach($toApplyMigrations as $migration){
if($migration === '.' || $migration === '..'){
continue;
}
require_once Application::$ROOT_DIR.'/migrations/'.$migration;
$className = pathinfo($migration, PATHINFO_FILENAME);
$instance = new $className();
$this->log("Applying migration $migration" . PHP_EOL);
$instance->up();
$this->log("Applied migration $migration" . PHP_EOL);
$newMigrations[] = $migration;
}
if(!empty($newMigrations)){
$this->saveMigrations($newMigrations);
}else{
$this->log('All migrations are applied');
}
}
public function createMigrationsTable(){
$this->pdo->exec("
CREATE TABLE IF NOT EXISTS migrations (
id INT AUTO_INCREMENT,
migration varchar(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)
ENGINE=INNODB;");
}
public function getAppliedMigrations(){
$statement = $this->pdo->prepare("SELECT migration FROM migrations");
$statement->execute();
return $statement->fetchAll(\PDO::FETCH_COLUMN);
}
public function saveMigrations(array $migrations){
$str = implode(",", array_map(fn($m) => "('$m')", $migrations));
$statement = $this->pdo->prepare("INSERT INTO migrations (migration) VALUES $str");
$statement->execute();
}
public function prepare($sql){
return $this->pdo->prepare($sql);
}
protected function log($message){
echo '['.date('Y-m-d H:i:s').'] - '.$message.PHP_EOL;
}
}