Updated front controller
Fixed DML queries errors (partly implemented) Fixed minor bugs Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
parent
3dde300c80
commit
70145967d0
14 changed files with 136 additions and 70 deletions
|
|
@ -384,7 +384,7 @@ class Wootook
|
|||
return $config;
|
||||
}
|
||||
|
||||
$select = $adapter->select('core_config');
|
||||
$select = $adapter->select($adapter->getTable('core_config'));
|
||||
|
||||
switch ($type) {
|
||||
case 'website':
|
||||
|
|
@ -653,6 +653,12 @@ class Wootook
|
|||
return self::$_globalConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @static
|
||||
* @param null $path
|
||||
* @param null $gameKey
|
||||
* @return Wootook_Core_Config_Node
|
||||
*/
|
||||
public static function getWebsiteConfig($path = null, $websiteKey = null)
|
||||
{
|
||||
if (self::$_config === null) {
|
||||
|
|
@ -675,6 +681,12 @@ class Wootook
|
|||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @static
|
||||
* @param null $path
|
||||
* @param null $gameKey
|
||||
* @return Wootook_Core_Config_Node
|
||||
*/
|
||||
public static function getGameConfig($path = null, $gameKey = null)
|
||||
{
|
||||
if (self::$_config === null) {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,10 @@ class Wootook_Core_Database_Adapter_Pdo_Mysql
|
|||
$dsn .= ";port={$config->database}";
|
||||
}
|
||||
|
||||
if (!isset($options[Wootook_Core_Database_ConnectionManager::ATTR_ERRMODE])) {
|
||||
$options[Wootook_Core_Database_ConnectionManager::ATTR_ERRMODE] = Wootook_Core_Database_ConnectionManager::ERRMODE_EXCEPTION;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->_handler = new PDO($dsn, $config->username, $config->password, $options);
|
||||
} catch (PDOException $e) {
|
||||
|
|
|
|||
|
|
@ -119,6 +119,10 @@ class Wootook_Core_Database_ConnectionManager
|
|||
self::ATTR_ERRMODE => self::ERRMODE_EXCEPTION
|
||||
);
|
||||
|
||||
/**
|
||||
* @param $connectionName
|
||||
* @return Wootook_Core_Database_Adapter_Adapter
|
||||
*/
|
||||
public function getConnection($connectionName)
|
||||
{
|
||||
if (empty($connectionName) || $connectionName === null) {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ abstract class Wootook_Core_Database_Sql_DmlFilterableQuery
|
|||
public function where($condition, $value = null)
|
||||
{
|
||||
if ($condition instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) {
|
||||
$this->_placeholders[] = $condition;
|
||||
$this->_parts[self::WHERE][] = $condition;
|
||||
} else if (is_string($condition)) {
|
||||
if ($value === null) {
|
||||
|
|
|
|||
|
|
@ -61,10 +61,11 @@ abstract class Wootook_Core_Database_Sql_DmlQuery
|
|||
|
||||
public function beforePrepare(Wootook_Core_Database_Statement_Statement $statement)
|
||||
{
|
||||
/*
|
||||
foreach ($this->_placeholders as $placeholder) {
|
||||
$placeholder->beforePrepare($statement);
|
||||
}
|
||||
|
||||
*/
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,16 +31,22 @@ class Wootook_Core_Database_Sql_Insert
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function set($column, $value)
|
||||
public function set($column, $value = null)
|
||||
{
|
||||
if ($value instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) {
|
||||
$this->_placeholders[] = $column;
|
||||
if (!is_array($column)) {
|
||||
$column = array($column => $value);
|
||||
}
|
||||
|
||||
$this->_parts[self::COLUMNS][] = array(
|
||||
foreach ($column as $field => $value) {
|
||||
if ($field instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) {
|
||||
$this->_placeholders[] = $field;
|
||||
}
|
||||
|
||||
$this->_parts[self::SET][] = array(
|
||||
'value' => $value,
|
||||
'field' => $column
|
||||
'field' => $field
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,17 @@ class Wootook_Core_Database_Sql_Placeholder_Param
|
|||
{
|
||||
parent::beforeExcute($statement);
|
||||
|
||||
$statement->bindValue($this->_paramName, $this->_value);
|
||||
if (is_numeric($this->_value)) {
|
||||
$type = Wootook_Core_Database_ConnectionManager::PARAM_INT;
|
||||
} else if (is_bool($this->_value)) {
|
||||
$type = Wootook_Core_Database_ConnectionManager::PARAM_BOOL;
|
||||
} else if (is_string($this->_value)) {
|
||||
$type = Wootook_Core_Database_ConnectionManager::PARAM_STR;
|
||||
} else {
|
||||
$type = null;
|
||||
}
|
||||
|
||||
$statement->bindValue($this->_paramName, $this->_value, $type);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,8 +34,8 @@ class Wootook_Core_Database_Sql_Select
|
|||
self::JOIN => array(),
|
||||
self::WHERE => array(),
|
||||
self::UNION => array(),
|
||||
self::LIMIT => array(),
|
||||
self::OFFSET => array(),
|
||||
self::LIMIT => null,
|
||||
self::OFFSET => null,
|
||||
self::GROUP => array(),
|
||||
self::HAVING => array(),
|
||||
self::ORDER => array(),
|
||||
|
|
|
|||
|
|
@ -21,6 +21,9 @@ class Wootook_Core_Database_Sql_Update
|
|||
$this->_parts = array(
|
||||
self::INTO => array(),
|
||||
self::SET => array(),
|
||||
self::WHERE => array(),
|
||||
self::LIMIT => null,
|
||||
self::OFFSET => null,
|
||||
);
|
||||
} else if (isset($this->_parts[$part])) {
|
||||
$this->_parts[$part] = array();
|
||||
|
|
@ -29,16 +32,22 @@ class Wootook_Core_Database_Sql_Update
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function set($column, $value)
|
||||
public function set($column, $value = null)
|
||||
{
|
||||
if ($value instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) {
|
||||
$this->_placeholders[] = $column;
|
||||
if (!is_array($column)) {
|
||||
$column = array($column => $value);
|
||||
}
|
||||
|
||||
$this->_parts[self::COLUMNS][] = array(
|
||||
foreach ($column as $field => $value) {
|
||||
if ($field instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) {
|
||||
$this->_placeholders[] = $field;
|
||||
}
|
||||
|
||||
$this->_parts[self::SET][] = array(
|
||||
'value' => $value,
|
||||
'field' => $column
|
||||
'field' => $field
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -65,15 +74,12 @@ class Wootook_Core_Database_Sql_Update
|
|||
}
|
||||
|
||||
switch ($part) {
|
||||
case self::COLUMNS:
|
||||
case self::SET:
|
||||
return $this->renderSet();
|
||||
break;
|
||||
case self::INTO:
|
||||
return $this->renderInto();
|
||||
break;
|
||||
case self::SELECT:
|
||||
return $this->renderSelect();
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
@ -81,17 +87,20 @@ class Wootook_Core_Database_Sql_Update
|
|||
|
||||
public function renderSet()
|
||||
{
|
||||
$values = array();
|
||||
$fields = array();
|
||||
foreach ($this->_parts[self::SET] as $field) {
|
||||
if ($field['value'] instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) {
|
||||
$fields[] = "{$this->_connection->quoteIdentifier($field['field'])}={$field['value']->toString()}";
|
||||
$values[] = $field['value']->toString();
|
||||
$fields[] = $this->_connection->quoteIdentifier($field['field']);
|
||||
} else {
|
||||
$fields[] = "{$this->_connection->quoteIdentifier($field['field'])}={$this->_connection->quote($field['value'])}";
|
||||
$values[] = $this->_connection->quote($field['value']);
|
||||
$fields[] = $this->_connection->quoteIdentifier($field['field']);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($fields)) {
|
||||
return "\nSET " . implode(", ", $fields);
|
||||
return ' (' . implode(', ', $fields). ")\nVALUES (" . implode(", ", $values) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -103,21 +112,16 @@ class Wootook_Core_Database_Sql_Update
|
|||
$output = "{$this->_connection->quoteIdentifier($this->_parts[self::INTO]['table'])}";
|
||||
}
|
||||
|
||||
return "INSERT INTO " . $output;
|
||||
return "UPDATE " . $output;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
if (empty($this->_parts[self::SELECT])) {
|
||||
return implode('', array(
|
||||
$this->renderInto(),
|
||||
$this->renderColumns(),
|
||||
));
|
||||
} else {
|
||||
return implode('', array(
|
||||
$this->renderInto(),
|
||||
$this->renderSelect(),
|
||||
$this->renderSet(),
|
||||
$this->renderWhere(),
|
||||
$this->renderLimit()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,26 +22,6 @@ class Wootook_Core_Mvc_Controller_Front
|
|||
),
|
||||
self::ROUTE_DEFAULT => array(
|
||||
'modules' => array(
|
||||
'core' => array(
|
||||
'class' => 'Wootook_Core_Controller_',
|
||||
'path' => 'Wootook/Core/Controller'
|
||||
),
|
||||
'admin' => array(
|
||||
'class' => 'Wootook_Admin_Controller_',
|
||||
'path' => 'Wootook/Admin/Controller'
|
||||
),
|
||||
'player' => array(
|
||||
'class' => 'Wootook_Player_Controller_',
|
||||
'path' => 'Wootook/Player/Controller'
|
||||
),
|
||||
'empire' => array(
|
||||
'class' => 'Wootook_Empire_Controller_',
|
||||
'path' => 'Wootook/Empire/Controller'
|
||||
),
|
||||
'legacies-empire' => array(
|
||||
'class' => 'Legacies_Empire_Controller_',
|
||||
'path' => 'Legacies/Empire/Controller'
|
||||
)
|
||||
),
|
||||
'defaults' => array(
|
||||
'module' => 'core',
|
||||
|
|
@ -156,12 +136,21 @@ class Wootook_Core_Mvc_Controller_Front
|
|||
while ($loop++ < 100) {
|
||||
$moduleKey = $this->_request->getModuleName();
|
||||
if (empty($moduleKey)) {
|
||||
$this->_forward('index', 'index', 'core');
|
||||
$this->_forward(
|
||||
$this->_routes[$route]['defaults']['action'],
|
||||
$this->_routes[$route]['defaults']['controller'],
|
||||
$this->_routes[$route]['defaults']['module']
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($this->_routes[$route]['modules'][$moduleKey])) {
|
||||
$this->_forward('no-route', 'error', 'core');
|
||||
$this->_forward(
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['action'],
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['controller'],
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['module']
|
||||
);
|
||||
$route = self::ROUTE_ERROR;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -178,7 +167,12 @@ class Wootook_Core_Mvc_Controller_Front
|
|||
}
|
||||
|
||||
if (!class_exists($controllerClass, false)) {
|
||||
$this->_forward('no-route', 'error', 'core');
|
||||
$this->_forward(
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['action'],
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['controller'],
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['module']
|
||||
);
|
||||
$route = self::ROUTE_ERROR;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -189,7 +183,12 @@ class Wootook_Core_Mvc_Controller_Front
|
|||
|
||||
$actionMethod = $this->_getActionMethod($actionKey);
|
||||
if (!method_exists($controllerClass, $actionMethod)) {
|
||||
$this->_forward('no-route', 'error', 'core');
|
||||
$this->_forward(
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['action'],
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['controller'],
|
||||
$this->_routes[self::ROUTE_ERROR]['defaults']['module']
|
||||
);
|
||||
$route = self::ROUTE_ERROR;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -223,4 +222,25 @@ class Wootook_Core_Mvc_Controller_Front
|
|||
{
|
||||
$this->_response->render();
|
||||
}
|
||||
|
||||
public function addModule($frontName, $namespace, $path)
|
||||
{
|
||||
$this->_routes[self::ROUTE_DEFAULT]['modules'][$frontName] = array(
|
||||
'class' => $namespace,
|
||||
'path' => $path
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDefaults($module, $controller, $action)
|
||||
{
|
||||
$this->_routes[self::ROUTE_DEFAULT]['defaults'] = array(
|
||||
'module' => $module,
|
||||
'controller' => $controller,
|
||||
'action' => $action
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ class Wootook_Core_Mvc_Controller_Request_Http
|
|||
{
|
||||
parent::__construct($options);
|
||||
|
||||
$this->_baseUrl = Wootook::getBaseUrl();
|
||||
$this->_baseUrl = Wootook::getBaseUrl('link');
|
||||
$baseUri = substr($this->_baseUrl, strpos($this->_baseUrl, '/', 8)); // Get the path from the DocumentRoot
|
||||
|
||||
$params = '';
|
||||
|
|
|
|||
|
|
@ -79,14 +79,14 @@ abstract class Wootook_Core_Mvc_Model_Entity
|
|||
if ($this->getId() !== null) {
|
||||
$update = $adapter->update()
|
||||
->into($adapter->getTable($this->getTableName()))
|
||||
->where("{$adapter->quoteIdentifier($this->getIdFieldName())}=:id");
|
||||
->where(new Wootook_Core_Database_Sql_Placeholder_Expression("{$adapter->quoteIdentifier($this->getIdFieldName())}=:id", array('id' => $this->getId())));
|
||||
|
||||
foreach ($this->getDataMapper()->encode($this, $this->getChangedDatas()) as $field => $value) {
|
||||
$update->set($field, new Wootook_Core_Database_Sql_Placeholder_Param($field, $value));
|
||||
}
|
||||
try {
|
||||
$statement = $update->prepare();
|
||||
$statement->execute(array('id' => $this->getId()));
|
||||
$statement->execute();
|
||||
} catch (Wootook_Core_Exception_Database_AdapterError $e) {
|
||||
throw new Wootook_Core_Exception_DataAccessException('Could not save data: ' . $e->getMessage(), null, $e);
|
||||
} catch (Wootook_Core_Exception_Database_StatementError $e) {
|
||||
|
|
@ -97,13 +97,13 @@ abstract class Wootook_Core_Mvc_Model_Entity
|
|||
->into($adapter->getTable($this->getTableName()));
|
||||
|
||||
foreach ($this->getDataMapper()->encode($this, $this->getAllDatas()) as $field => $value) {
|
||||
$insert->set($field, new Wootook_Core_Database_Sql_Placeholder_Param($field, $value));
|
||||
$insert->set($field, new Wootook_Core_Database_Sql_Placeholder_Param($field, $value, $type));
|
||||
}
|
||||
try {
|
||||
$statement = $insert->prepare();
|
||||
$statement->execute();
|
||||
|
||||
$id = $adapter->lastInsertId($table);
|
||||
$id = $adapter->lastInsertId($adapter->getTable($this->getTableName()));
|
||||
$this->setId($id);
|
||||
} catch (Wootook_Core_Exception_Database_AdapterError $e) {
|
||||
throw new Wootook_Core_Exception_DataAccessException('Could not save data: ' . $e->getMessage(), null, $e);
|
||||
|
|
|
|||
|
|
@ -34,5 +34,9 @@ require_once dirname(__FILE__) .'/application/bootstrap.php';
|
|||
$frontController = new Wootook_Core_Mvc_Controller_Front(Wootook::getRequest(), Wootook::getResponse());
|
||||
|
||||
$frontController
|
||||
->addModule('core', 'Wootook_Core_Controller_', 'Wootook/Core/Controller')
|
||||
->addModule('player', 'Wootook_Player_Controller_', 'Wootook/Player/Controller')
|
||||
->addModule('empire', 'Wootook_Empire_Controller_', 'Wootook/Empire/Controller')
|
||||
->addModule('legacies-empire', 'Legacies_Empire_Controller_', 'Legacies/Empire/Controller')
|
||||
->dispatch()
|
||||
->send();
|
||||
|
|
|
|||
Loading…
Reference in a new issue