Commit a686d78e authored by tino.goratsch@hotmail.com's avatar [email protected]

- added a very simple wrapper class around the Doctrine DBAL

- BC-Break: switched ACP3 config from config.php to config.yml
- added an extra entry point if you want to run the ACP3 in debug mode
parent c63ff656
......@@ -50,11 +50,9 @@ class Application
date_default_timezone_set('UTC');
// DB-Config des ACP3 laden
$path = ACP3_DIR . 'config/config.php';
$path = ACP3_DIR . 'config/config.yml';
if (is_file($path) === false || filesize($path) === 0) {
exit('The ACP3 is not correctly installed. Please navigate to the <a href="' . ROOT_DIR . 'installation/">installation wizard</a> and follow its instructions.');
} else {
require_once $path;
}
}
......@@ -141,34 +139,16 @@ class Application
{
$file = UPLOADS_DIR . 'cache/sql/container.php';
$config = new DBAL\Configuration();
$connectionParams = array(
'dbname' => CONFIG_DB_NAME,
'user' => CONFIG_DB_USER,
'password' => CONFIG_DB_PASSWORD,
'host' => CONFIG_DB_HOST,
'driver' => 'pdo_mysql',
'charset' => 'utf8'
);
$db = DBAL\DriverManager::getConnection($connectionParams, $config);
define('DB_PRE', CONFIG_DB_PRE);
if (file_exists($file) && (!defined('DEBUG') || DEBUG === false)) {
if (is_file($file) && (!defined('DEBUG') || DEBUG === false)) {
require_once $file;
$this->container = new \ACP3ServiceContainer();
$this->container->set('core.db', $db);
// Systemeinstellungen laden
$this->container
->get('core.config.system')
->getSettingsAsConstants();
// Pfade zum Theme setzen
define('DESIGN_PATH', ROOT_DIR . 'designs/' . CONFIG_DESIGN . '/');
define('DESIGN_PATH_INTERNAL', ACP3_ROOT_DIR . 'designs/' . CONFIG_DESIGN . '/');
define('DESIGN_PATH_ABSOLUTE', HOST_NAME . DESIGN_PATH);
$this->_setThemeConstants();
$this->container->get('core.view')->setRenderer('smarty');
} else {
......@@ -177,17 +157,12 @@ class Application
$loader->load(ACP3_DIR . 'config/services.yml');
$loader->load(CLASSES_DIR . 'View/Renderer/Smarty/services.yml');
$this->container->set('core.db', $db);
// Systemeinstellungen laden
$this->container
->get('core.config.system')
->getSettingsAsConstants();
// Pfade zum Theme setzen
define('DESIGN_PATH', ROOT_DIR . 'designs/' . CONFIG_DESIGN . '/');
define('DESIGN_PATH_INTERNAL', ACP3_ROOT_DIR . 'designs/' . CONFIG_DESIGN . '/');
define('DESIGN_PATH_ABSOLUTE', HOST_NAME . DESIGN_PATH);
$this->_setThemeConstants();
// Try to get all available services
/** @var Modules $modules */
......@@ -209,6 +184,16 @@ class Application
}
}
/**
* Pfade zum Theme setzen
*/
private function _setThemeConstants()
{
define('DESIGN_PATH', ROOT_DIR . 'designs/' . CONFIG_DESIGN . '/');
define('DESIGN_PATH_INTERNAL', ACP3_ROOT_DIR . 'designs/' . CONFIG_DESIGN . '/');
define('DESIGN_PATH_ABSOLUTE', HOST_NAME . DESIGN_PATH);
}
/**
* Gibt die Seite aus
*/
......@@ -233,7 +218,7 @@ class Application
} catch (Core\Exceptions\UnauthorizedAccess $e) {
$redirectUri = base64_encode($request->originalQuery);
$redirect->temporary('users/index/login/redirect_' . $redirectUri);
} catch(Core\Exceptions\AccessForbidden $e) {
} catch (Core\Exceptions\AccessForbidden $e) {
$redirect->temporary('errors/index/403');
} catch (Core\Exceptions\ControllerActionNotFound $e) {
Core\Logger::error('404', 'Request: ' . $request->query);
......
......@@ -46,17 +46,16 @@ class ACL
protected $resources = [];
/**
* Konstruktor - erzeugt die ACL für den jeweiligen User
*
* @param Auth $auth
* @param \Doctrine\DBAL\Connection $db
* @param Auth $auth
* @param Modules $modules
* @param Permissions\Model $permissionsModel
* @param Permissions\Cache $permissionsCache
*/
public function __construct(
Auth $auth,
Modules $modules,
Permissions\Model $permissionsModel,
Permissions\Cache $permissionsCache,
\Doctrine\DBAL\Connection $db
Permissions\Cache $permissionsCache
)
{
$this->auth = $auth;
......
......@@ -49,7 +49,7 @@ class Auth
*/
protected $userInfo = [];
/**
* @var \Doctrine\DBAL\Connection
* @var \ACP3\Core\DB
*/
protected $db;
/**
......@@ -69,12 +69,12 @@ class Auth
* Findet heraus, falls der ACP3_AUTH Cookie gesetzt ist, ob der
* Seitenbesucher auch wirklich ein registrierter Benutzer des ACP3 ist
*
* @param \Doctrine\DBAL\Connection $db
* @param \ACP3\Core\DB $db
* @param \ACP3\Core\Session $session
* @param \ACP3\Core\Helpers\Secure $secureHelper
*/
function __construct(
\Doctrine\DBAL\Connection $db,
DB $db,
Session $session,
Secure $secureHelper)
{
......
......@@ -56,7 +56,7 @@ class Breadcrumb
protected $router;
public function __construct(
\Doctrine\DBAL\Connection $db,
DB $db,
Lang $lang,
Request $request,
Router $router
......@@ -75,7 +75,7 @@ class Breadcrumb
$request->mod . '/' . $request->controller . '/',
$request->mod
);
$items = $db->executeQuery('SELECT p.title, p.uri, p.left_id, p.right_id FROM ' . DB_PRE . 'menu_items AS c, ' . DB_PRE . 'menu_items AS p WHERE c.left_id BETWEEN p.left_id AND p.right_id AND c.uri IN(?) GROUP BY p.uri ORDER BY p.left_id ASC', array($in), array(\Doctrine\DBAL\Connection::PARAM_STR_ARRAY))->fetchAll();
$items = $db->getConnection()->executeQuery('SELECT p.title, p.uri, p.left_id, p.right_id FROM ' . $db->getPrefix() . 'menu_items AS c, ' . $db->getPrefix() . 'menu_items AS p WHERE c.left_id BETWEEN p.left_id AND p.right_id AND c.uri IN(?) GROUP BY p.uri ORDER BY p.left_id ASC', array($in), array(\Doctrine\DBAL\Connection::PARAM_STR_ARRAY))->fetchAll();
$c_items = count($items);
// Dynamische Seite (ACP3 intern)
......
......@@ -2,19 +2,13 @@
namespace ACP3\Core;
use ACP3\Modules\System;
use Doctrine\DBAL\Connection;
/**
* Manages the various module settings
*
* @author Tino Goratsch
* @package ACP3\Core
*/
class Config
{
/**
* @var \Doctrine\DBAL\Connection
*/
protected $db;
/**
* @var Model
*/
......@@ -28,10 +22,9 @@ class Config
*/
protected $module = '';
public function __construct(Connection $db, $module)
public function __construct(DB $db, $module)
{
$this->cache = new Cache($module);
$this->db = $db;
$this->module = strtolower($module);
$this->systemModel = new System\Model($db);
}
......
......@@ -32,7 +32,8 @@ class Admin extends Frontend
Core\Context\Frontend $frontendContext,
Core\Validator\Rules\Misc $validate,
Core\Session $session,
Core\Router\Aliases $aliases)
Core\Router\Aliases $aliases
)
{
parent::__construct(
$frontendContext,
......
<?php
namespace ACP3\Core;
use \Doctrine\DBAL;
/**
* Class DB
* @package ACP3\Core
*/
class DB
{
/**
* @var DBAL\Connection
*/
protected $connection;
/**
* @var string
*/
protected $name = '';
/**
* @var string
*/
protected $prefix = '';
/**
* @param $dbHost
* @param $dbName
* @param $dbUser
* @param $dbPassword
* @param string $dbTablePrefix
* @param string $dbDriver
* @param string $dbCharset
* @throws DBAL\DBALException
*/
public function __construct($dbHost, $dbName, $dbUser, $dbPassword, $dbTablePrefix = '', $dbDriver = 'pdo_mysql', $dbCharset = 'utf8')
{
$config = new DBAL\Configuration();
$connectionParams = array(
'dbname' => $dbName,
'user' => $dbUser,
'password' => $dbPassword,
'host' => $dbHost,
'driver' => $dbDriver,
'charset' => $dbCharset
);
$this->connection = DBAL\DriverManager::getConnection($connectionParams, $config);
$this->prefix = $dbTablePrefix;
}
/**
* @return DBAL\Connection
*/
public function getConnection()
{
return $this->connection;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
}
\ No newline at end of file
......@@ -22,6 +22,11 @@ class Alerts
*/
protected $request;
/**
* @param Core\Request $request
* @param Core\View $view
* @param Output $outputHelper
*/
public function __construct(
Core\Request $request,
Core\View $view,
......
......@@ -24,6 +24,11 @@ class RewriteInternalUri
*/
protected $aliasesValidator;
/**
* @param Core\Modules $modules
* @param Core\Router $router
* @param Core\Validator\Rules\Router\Aliases $aliasValidator
*/
public function __construct(
Core\Modules $modules,
Core\Router $router,
......
......@@ -22,6 +22,11 @@ class RedirectMessages
*/
private $view;
/**
* @param Core\Redirect $redirect
* @param Core\Request $request
* @param Core\View $view
*/
public function __construct(
Core\Redirect $redirect,
Core\Request $request,
......
......@@ -14,6 +14,9 @@ class Secure
*/
protected $view;
/**
* @param Core\View $view
*/
public function __construct(Core\View $view)
{
$this->view = $view;
......@@ -114,5 +117,4 @@ class Secure
}
}
}
}
\ No newline at end of file
}
\ No newline at end of file
<?php
namespace ACP3\Core\Helpers;
use ACP3\Core\DB;
/**
* Class Sort
* @package ACP3\Core\Helpers
......@@ -8,14 +10,14 @@ namespace ACP3\Core\Helpers;
class Sort
{
/**
* @var \Doctrine\DBAL\Connection
* @var DB
*/
protected $db;
/**
* @param \Doctrine\DBAL\Connection $db
* @param DB $db
*/
public function __construct(\Doctrine\DBAL\Connection $db)
public function __construct(DB $db)
{
$this->db = $db;
}
......@@ -63,10 +65,10 @@ class Sort
*/
private function _moveOneStep($action, $table, $idField, $sortField, $id, $where = '')
{
$this->db->beginTransaction();
$this->db->getConnection()->beginTransaction();
try {
$id = (int)$id;
$table = DB_PRE . $table;
$table = $this->db->getPrefix() . $table;
// Zusätzliche WHERE-Bedingung
$where = !empty($where) ? 'a.' . $where . ' = b.' . $where . ' AND ' : '';
......@@ -75,24 +77,24 @@ class Sort
$queryString = 'SELECT a.%2$s AS other_id, a.%3$s AS other_sort, b.%3$s AS elem_sort FROM %1$s AS a, %1$s AS b WHERE %5$sb.%2$s = %4$s AND a.%3$s %6$s b.%3$s ORDER BY a.%3$s %7$s LIMIT 1';
if ($action === 'up') {
$query = $this->db->fetchAssoc(sprintf($queryString, $table, $idField, $sortField, $id, $where, '<', 'DESC'));
$query = $this->db->getConnection()->fetchAssoc(sprintf($queryString, $table, $idField, $sortField, $id, $where, '<', 'DESC'));
} else {
$query = $this->db->fetchAssoc(sprintf($queryString, $table, $idField, $sortField, $id, $where, '>', 'ASC'));
$query = $this->db->getConnection()->fetchAssoc(sprintf($queryString, $table, $idField, $sortField, $id, $where, '>', 'ASC'));
}
if (!empty($query)) {
// Sortierreihenfolge des aktuellen Elementes zunächst auf 0 setzen
// um Probleme mit möglichen Duplicate-Keys zu umgehen
$this->db->update($table, array($sortField => 0), array($idField => $id));
$this->db->update($table, array($sortField => $query['elem_sort']), array($idField => $query['other_id']));
$this->db->getConnection()->update($table, array($sortField => 0), array($idField => $id));
$this->db->getConnection()->update($table, array($sortField => $query['elem_sort']), array($idField => $query['other_id']));
// Element nun den richtigen Wert zuweisen
$this->db->update($table, array($sortField => $query['other_sort']), array($idField => $id));
$this->db->getConnection()->update($table, array($sortField => $query['other_sort']), array($idField => $id));
$this->db->commit();
$this->db->getConnection()->commit();
return true;
}
} catch (\Exception $e) {
$this->db->rollback();
$this->db->getConnection()->rollback();
}
return false;
......
......@@ -13,8 +13,7 @@ class StringFormatter
/**
* Macht einen String URL sicher
*
* @param string $var
* Die unzuwandelnde Variable
* @param string $var Die unzuwandelnde Variable
*
* @return string
*/
......
......@@ -64,7 +64,7 @@ class Mailer
* Initializes PHPMailer and sets the basic configuration parameters
* @param View $view
*/
public function __construct(View $view = null)
public function __construct(View $view)
{
$this->view = $view;
$this->phpMailer = new \PHPMailer(true);
......
......@@ -3,9 +3,8 @@
namespace ACP3\Core;
/**
* Description of Model
*
* @author Tino Goratsch
* Class Model
* @package ACP3\Core
*/
class Model
{
......@@ -15,22 +14,14 @@ class Model
*/
const TABLE_NAME = '';
/**
* @var \Doctrine\DBAL\Connection
* @var
*/
protected $db;
/**
* The table prefix
*
* @var string
*/
protected $prefix = DB_PRE;
/**
* Injects the dependencies
*
* @param \Doctrine\DBAL\Connection $db
* @param DB $db
*/
public function __construct(\Doctrine\DBAL\Connection $db)
public function __construct(DB $db)
{
$this->db = $db;
}
......@@ -45,15 +36,15 @@ class Model
*/
public function insert(array $params, $tableName = '')
{
$this->db->beginTransaction();
$this->db->getConnection()->beginTransaction();
try {
$tableName = !empty($tableName) ? $tableName : static::TABLE_NAME;
$this->db->insert($this->prefix . $tableName, $params);
$lastId = (int)$this->db->lastInsertId();
$this->db->commit();
$this->db->getConnection()->insert($this->db->getPrefix() . $tableName, $params);
$lastId = (int)$this->db->getConnection()->lastInsertId();
$this->db->getConnection()->commit();
return $lastId;
} catch (\Exception $e) {
$this->db->rollback();
$this->db->getConnection()->rollback();
Logger::error('database', $e->getMessage());
return false;
}
......@@ -70,15 +61,15 @@ class Model
*/
public function delete($id, $field = '', $tableName = '')
{
$this->db->beginTransaction();
$this->db->getConnection()->beginTransaction();
try {
$tableName = !empty($tableName) ? $tableName : static::TABLE_NAME;
$field = empty($field) ? 'id' : $field;
$bool = $this->db->delete($this->prefix . $tableName, is_array($id) ? $id : array($field => (int)$id));
$this->db->commit();
$bool = $this->db->getConnection()->delete($this->db->getPrefix() . $tableName, is_array($id) ? $id : array($field => (int)$id));
$this->db->getConnection()->commit();
return $bool;
} catch (\Exception $e) {
$this->db->rollback();
$this->db->getConnection()->rollback();
Logger::error('database', $e->getMessage());
return false;
}
......@@ -95,15 +86,15 @@ class Model
*/
public function update(array $params, $id, $tableName = '')
{
$this->db->beginTransaction();
$this->db->getConnection()->beginTransaction();
try {
$tableName = !empty($tableName) ? $tableName : static::TABLE_NAME;
$where = is_array($id) === true ? $id : array('id' => $id);
$bool = $this->db->update($this->prefix . $tableName, $params, $where);
$this->db->commit();
$bool = $this->db->getConnection()->update($this->db->getPrefix() . $tableName, $params, $where);
$this->db->getConnection()->commit();
return $bool;
} catch (\Exception $e) {
$this->db->rollback();
$this->db->getConnection()->rollback();
Logger::error('database', $e->getMessage());
return false;
}
......@@ -128,4 +119,4 @@ class Model
return '';
}
}
}
\ No newline at end of file
......@@ -9,10 +9,6 @@ use ACP3\Modules\System;
*/
class Modules
{
/**
* @var \Doctrine\DBAL\Connection
*/
protected $db;
/**
* @var Lang
*/
......@@ -35,11 +31,10 @@ class Modules
private $allModules = [];
public function __construct(
\Doctrine\DBAL\Connection $db,
DB $db,
Lang $lang
)
{
$this->db = $db;
$this->lang = $lang;
$this->cache = new Cache('modules');
$this->systemModel = new System\Model($db);
......
......@@ -26,7 +26,7 @@ abstract class AbstractInstaller implements InstallerInterface
const SCHEMA_VERSION = 0;
/**
* @var \Doctrine\DBAL\Connection
* @var Core\DB
*/
protected $db;
/**
......@@ -53,12 +53,12 @@ abstract class AbstractInstaller implements InstallerInterface
protected $specialResources = [];
/**
* @param \Doctrine\DBAL\Connection $db
* @param Core\DB $db
* @param System\Model $systemModel
* @param Permissions\Model $permissionsModel
*/
public function __construct(
\Doctrine\DBAL\Connection $db,
Core\DB $db,
System\Model $systemModel,
Permissions\Model $permissionsModel
)
......@@ -124,18 +124,18 @@ abstract class AbstractInstaller implements InstallerInterface
{
if (count($queries) > 0) {
$search = array('{pre}', '{engine}', '{charset}');
$replace = array(DB_PRE, 'ENGINE=MyISAM', 'CHARACTER SET `utf8` COLLATE `utf8_general_ci`');
$replace = array($this->db->getPrefix(), 'ENGINE=MyISAM', 'CHARACTER SET `utf8` COLLATE `utf8_general_ci`');
$this->db->beginTransaction();
$this->db->getConnection()->beginTransaction();
try {
foreach ($queries as $query) {
if (!empty($query)) {
$this->db->query(str_replace($search, $replace, $query));
$this->db->getConnection()->query(str_replace($search, $replace, $query));
}
}
$this->db->commit();
$this->db->getConnection()->commit();
} catch (\Exception $e) {
$this->db->rollBack();
$this->db->getConnection()->rollBack();
Core\Logger::warning('installer', $e);
return false;
......@@ -174,7 +174,7 @@ abstract class AbstractInstaller implements InstallerInterface
protected function installSettings(array $settings)
{
if (count($settings) > 0) {
$this->db->beginTransaction();
$this->db->getConnection()->beginTransaction();
try {
foreach ($settings as $key => $value) {
$insertValues = array(
......@@ -185,9 +185,9 @@ abstract class AbstractInstaller implements InstallerInterface
);
$this->systemModel->insert($insertValues, System\Model::TABLE_NAME_SETTINGS);
}
$this->db->commit();
$this->db->getConnection()->commit();
} catch (\Exception $e) {
$this->db->rollback();
$this->db->getConnection()->rollback();
Core\Logger::warning('installer', $e);
return false;
......@@ -499,6 +499,6 @@ abstract class AbstractInstaller implements InstallerInterface
*/
public function moduleIsInstalled($moduleName)
{
return $this->db->fetchColumn('SELECT COUNT(*) FROM ' . DB_PRE . 'modules WHERE name = ?', array($moduleName)) == 1;
return $this->db->getConnection()->fetchColumn('SELECT COUNT(*) FROM ' . $this->db->getPrefix() . 'modules WHERE name = ?', array($moduleName)) == 1;
}
}
\ No newline at end of file
<?php
namespace ACP3\Core\Modules;
/**
* Interface InstallerInterface
* @package ACP3\Core\Modules
*/
interface InstallerInterface
{
/**
......
......@@ -23,20 +23,23 @@ class NestedSet
protected $enableBlocks;
/**
* @var \Doctrine\DBAL\Connection
* @var DB
*/
protected $db;
/**
*
* @param \Doctrine\DBAL\Connection $db
* @param string $tableName
* @param bool $enableBlocks
* @param DB $db
* @param $tableName
* @param bool $enableBlocks
*/
public function __construct(\Doctrine\DBAL\Connection $db, $tableName, $enableBlocks = false)
public function __construct(
DB $db,
$tableName,
$enableBlocks = false
)
{
$this->db = $db;
$this->tableName = DB_PRE . $tableName;
$this->db = $db->getConnection();
$this->tableName = $db->getPrefix() . $tableName;
$this->enableBlocks = $enableBlocks;
}
......@@ -90,7 +93,7 @@ class NestedSet
*
* @param integer $parentId
* ID der übergeordneten Seite
* @param array $insertValues
* @param array $insertValues
*
* @return boolean
*/
......@@ -161,7 +164,7 @@ class NestedSet
* ID des neuen Elternelements
* @param integer $blockId
* ID des neuen Blocks
* @param array $updateValues
* @param array $updateValues
*
* @return boolean
*/
......@@ -273,7 +276,7 @@ class NestedSet
* Methode zum Umsortieren von Knoten
*
* @param integer $id
* @param string $mode
* @param string $mode
*
* @return boolean
*/
......
......@@ -70,6 +70,15 @@ class Pagination
*/
private $pagination = [];
/**
* @param Auth $auth
* @param Lang $lang
* @param SEO $seo
* @param Request $request
* @param Router $router
* @param View $view
* @param Misc $miscValidator
*/
function __construct(
Auth $auth,
Lang $lang,