-
-
Save Golpha/1882a12d13c6a4ab3c49 to your computer and use it in GitHub Desktop.
Bootstrap Concept, Database singleton(-alike) Container, Dependency Injection Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Dependencies ( Abhängigkeiten ) laden | |
require __DIR__.'/Database.php'; # database class | |
// bootstrap | |
// Der Bootstrap der Anwendung ist der Teil deiner Anwendung der Objekte erzeugt und konfiguriert | |
// Der Bootstrap wird jedem Script vorgestellt wenn es vom Browser aufgerufen wird ( include ) | |
# Configuration laden | |
$config = require __DIR__.'/Config.php'; | |
# Datebank instanziieren und initialisieren | |
$database = new Database(); | |
$database->connectTo($config['hostname'], $config['username'], $config['password'], $config['database']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
# Hostname des MySQL Servers | |
'hostname' => 'php-de.dev', | |
# Benutzername für die MySQL-Verbindung | |
'username' => 'remote', | |
# Passwort für die MySQL-Verbindung | |
'password' => 'remote', | |
# Name der Datenbank | |
'database' => 'test', | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database { | |
protected $instance; | |
protected $factory; | |
public function __construct(PDO $pdo = null) | |
{ | |
$this->instance = $pdo; | |
} | |
public function get() | |
{ | |
if ( ! $this->instance instanceof PDO ) | |
{ | |
if ( ! $this->factory instanceof Closure ) | |
{ | |
throw new PDOException( | |
'Die Datenbank-Verbindungsdaten müssen hinterlegt werden.' | |
); | |
} | |
$this->instance = call_user_func($this->factory->bindTo($this, get_class($this))); | |
} | |
return $this->instance; | |
} | |
public function query($sql, array $parameters = array()) | |
{ | |
$statement = $this->get()->prepare($sql); | |
$statement->execute($parameters); | |
return $statement; | |
} | |
/** | |
* | |
* Füge hier alle methoden ein die du überschreiben willst, | |
* alternativ kannst du via get() auf die original PDO Instanz zugreifen | |
* | |
*/ | |
public function connectTo($host, $user, $password, $database, $charset = 'utf8') | |
{ | |
if ( $this->instance instanceof PDO ) | |
{ | |
$this->instance = null; | |
} | |
$this->factory = function() use ($host, $user, $password, $database, $charset) | |
{ | |
$dsn = array( | |
'host' => $host, | |
'dbname' => $database, | |
'charset' => $charset, | |
); | |
$dsnString = 'mysql:'.http_build_query($dsn, '', ';'); | |
return new PDO($dsnString, $user, $password); | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Model { | |
protected $database; | |
public function __construct(Database $database) | |
{ | |
$this->database = $database; | |
} | |
public function getTables() | |
{ | |
return iterator_to_array($this->database->query('SHOW TABLES')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Dependencies ( Abhängigkeiten ) laden | |
require __DIR__.'/Bootstrap.php'; # bootstrap | |
require __DIR__.'/Model.php'; # model class | |
$model = new Model($database); | |
foreach ( $model->getTables() as $row ) | |
{ | |
var_dump($row); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment