Erster Stand des mvc-Frameworks
This commit is contained in:
127
rendering/Helper/Database.php
Normal file
127
rendering/Helper/Database.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Singleton-Klasse zur Herstellung der Datenbank-Verbindung
|
||||
*
|
||||
* @author Christian Steinle
|
||||
* @date 02.08.2016
|
||||
*
|
||||
* @copyright CS medien- & kommunikationssysteme (http://www.steinle-computer.de)
|
||||
*/
|
||||
|
||||
namespace Helper;
|
||||
|
||||
use mysqli;
|
||||
|
||||
|
||||
class Database
|
||||
{
|
||||
/**
|
||||
* Name der Datenbank-Tabelle für das Entsprechende Modell
|
||||
*/
|
||||
const TBL_NAME = '';
|
||||
|
||||
/**
|
||||
* Name des Datenbank-Feldes mit dem PrimaryKey
|
||||
*/
|
||||
const PRIMARY_KEY = '';
|
||||
|
||||
/**
|
||||
* Sortierung der Elemente für die Methode getIndex
|
||||
*/
|
||||
const ORDER_BY = '';
|
||||
|
||||
/**
|
||||
* Der Filter für den Aufruf von getIndex()
|
||||
* @var string
|
||||
*/
|
||||
static $filter = '1=1';
|
||||
|
||||
/**
|
||||
* Hier werden die Daten von getIndex() gespeichert
|
||||
* @var array
|
||||
*/
|
||||
static $data = array();
|
||||
|
||||
/**
|
||||
* @var \mysqli|null
|
||||
*/
|
||||
protected static $db = null;
|
||||
|
||||
|
||||
protected function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
protected function __clone()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stellt die Datenbank-Verbindung her
|
||||
*
|
||||
* return void
|
||||
*/
|
||||
final protected static function getInstance()
|
||||
{
|
||||
if (null === self::$db)
|
||||
{
|
||||
self::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
|
||||
self::$db->set_charset('utf8');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Liefert ein assoziatives Array des Datensatzes mit der übergebenen ID
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
final public static function getItem($id)
|
||||
{
|
||||
$sql = 'SELECT * FROM ' . static::TBL_NAME . ' WHERE ' . static::PRIMARY_KEY . ' = ' . $id . ';';
|
||||
$data = self::query($sql);
|
||||
return (!is_array($data) || empty($data)) ? array() : current($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Liefert ein mehrdimensionales, assoziatives Array mit allen Datensätzen, die zum Filter passen
|
||||
* als Schlüssel der ersten Dimension dient der Wert des Primär-Schlüssels der Datenbank-Tabelle
|
||||
* Legt alle Daten in self::$data ab
|
||||
*
|
||||
* @see self::query()
|
||||
* @see self::$data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
final public static function getIndex()
|
||||
{
|
||||
$sql = 'SELECT * FROM ' . static::TBL_NAME . ' WHERE ' . static::$filter . ' ' . static::ORDER_BY . ';';
|
||||
self::$data = self::query($sql);
|
||||
return self::$data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Liefert ein mehrdimensionales, assoziatives Array mit allen Datensätzen, die zum SQL-Query passen
|
||||
* als Schlüssel der ersten Dimension dient der Wert des Primär-Schlüssels der Datenbank-Tabelle
|
||||
*
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
final protected static function query($sql)
|
||||
{
|
||||
self::getInstance();
|
||||
$result = self::$db->query($sql);
|
||||
$data = array();
|
||||
while ($tmpData = $result->fetch_assoc())
|
||||
{
|
||||
$data[$tmpData[static::PRIMARY_KEY]] = $tmpData;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
77
rendering/Helper/Environment.php
Normal file
77
rendering/Helper/Environment.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* Hilfsklasse zum Auslesen der Umgebungsvariablen, die in einer .htaccess gesetzt sind
|
||||
*
|
||||
* @author Christian Steinle
|
||||
* @date 02.08.2016
|
||||
*
|
||||
* @copyright CS medien- & kommunikationssysteme (http://www.steinle-computer.de)
|
||||
*/
|
||||
|
||||
namespace Helper;
|
||||
use Singleton\SingletonAbstract;
|
||||
|
||||
|
||||
class Environment extends SingletonAbstract
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected static $_environment = '';
|
||||
|
||||
|
||||
/**
|
||||
* @return Environment|null
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (null === self::$_instance)
|
||||
{
|
||||
self::$_instance = new self;
|
||||
self::setEnvironment();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Falls ein Skript via Apache gestartet wird, zieht getenv.
|
||||
* Andernfalls werden die Informationen aus einer .htaccess im FileSystem ausgelesen.
|
||||
*/
|
||||
protected static function setEnvironment()
|
||||
{
|
||||
$environment = getenv('ENVIRONMENT');
|
||||
if ($environment === false)
|
||||
{
|
||||
$tmpPath = __DIR__;
|
||||
while ($tmpPath != '/')
|
||||
{
|
||||
$htFile = $tmpPath . '/.htaccess';
|
||||
if (is_file($htFile) && is_readable($htFile))
|
||||
{
|
||||
$htContent = file($htFile);
|
||||
foreach ($htContent as $line => $text)
|
||||
{
|
||||
if (strpos($text, 'SetEnv') !== false && strpos($text, 'ENVIRONMENT') !== false)
|
||||
{
|
||||
self::$_environment = trim(str_replace(array('SetEnv', 'ENVIRONMENT'), '', $text));
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
$tmpPath = dirname($tmpPath);
|
||||
}
|
||||
$environment = '';
|
||||
}
|
||||
self::$_environment = $environment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getEnvironment()
|
||||
{
|
||||
return self::$_environment;
|
||||
}
|
||||
}
|
||||
46
rendering/Helper/Registry.php
Normal file
46
rendering/Helper/Registry.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Hilfsklasse zur Registrierung von Variablen
|
||||
*
|
||||
* @author Christian Steinle
|
||||
* @date 02.08.2016
|
||||
*
|
||||
* @copyright CS medien- & kommunikationssysteme (http://www.steinle-computer.de)
|
||||
*/
|
||||
|
||||
namespace Helper;
|
||||
use Singleton\SingletonAbstract;
|
||||
|
||||
|
||||
class Registry extends SingletonAbstract
|
||||
{
|
||||
/**
|
||||
* Hält alle Registrierungsdaten fürs System
|
||||
* @var array
|
||||
*/
|
||||
private $data = array();
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
if (array_key_exists($name, $this->data))
|
||||
{
|
||||
return $this->data[$name];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user