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:
Gregory PLANCHAT 2012-03-21 01:41:56 +01:00
commit 70145967d0
14 changed files with 136 additions and 70 deletions

View file

@ -384,7 +384,7 @@ class Wootook
return $config; return $config;
} }
$select = $adapter->select('core_config'); $select = $adapter->select($adapter->getTable('core_config'));
switch ($type) { switch ($type) {
case 'website': case 'website':
@ -653,6 +653,12 @@ class Wootook
return self::$_globalConfig; return self::$_globalConfig;
} }
/**
* @static
* @param null $path
* @param null $gameKey
* @return Wootook_Core_Config_Node
*/
public static function getWebsiteConfig($path = null, $websiteKey = null) public static function getWebsiteConfig($path = null, $websiteKey = null)
{ {
if (self::$_config === null) { if (self::$_config === null) {
@ -675,6 +681,12 @@ class Wootook
return $config; return $config;
} }
/**
* @static
* @param null $path
* @param null $gameKey
* @return Wootook_Core_Config_Node
*/
public static function getGameConfig($path = null, $gameKey = null) public static function getGameConfig($path = null, $gameKey = null)
{ {
if (self::$_config === null) { if (self::$_config === null) {

View file

@ -16,6 +16,10 @@ class Wootook_Core_Database_Adapter_Pdo_Mysql
$dsn .= ";port={$config->database}"; $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 { try {
$this->_handler = new PDO($dsn, $config->username, $config->password, $options); $this->_handler = new PDO($dsn, $config->username, $config->password, $options);
} catch (PDOException $e) { } catch (PDOException $e) {

View file

@ -119,6 +119,10 @@ class Wootook_Core_Database_ConnectionManager
self::ATTR_ERRMODE => self::ERRMODE_EXCEPTION self::ATTR_ERRMODE => self::ERRMODE_EXCEPTION
); );
/**
* @param $connectionName
* @return Wootook_Core_Database_Adapter_Adapter
*/
public function getConnection($connectionName) public function getConnection($connectionName)
{ {
if (empty($connectionName) || $connectionName === null) { if (empty($connectionName) || $connectionName === null) {

View file

@ -26,6 +26,7 @@ abstract class Wootook_Core_Database_Sql_DmlFilterableQuery
public function where($condition, $value = null) public function where($condition, $value = null)
{ {
if ($condition instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) { if ($condition instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) {
$this->_placeholders[] = $condition;
$this->_parts[self::WHERE][] = $condition; $this->_parts[self::WHERE][] = $condition;
} else if (is_string($condition)) { } else if (is_string($condition)) {
if ($value === null) { if ($value === null) {

View file

@ -61,10 +61,11 @@ abstract class Wootook_Core_Database_Sql_DmlQuery
public function beforePrepare(Wootook_Core_Database_Statement_Statement $statement) public function beforePrepare(Wootook_Core_Database_Statement_Statement $statement)
{ {
/*
foreach ($this->_placeholders as $placeholder) { foreach ($this->_placeholders as $placeholder) {
$placeholder->beforePrepare($statement); $placeholder->beforePrepare($statement);
} }
*/
return $this; return $this;
} }

View file

@ -31,16 +31,22 @@ class Wootook_Core_Database_Sql_Insert
return $this; return $this;
} }
public function set($column, $value) public function set($column, $value = null)
{ {
if ($value instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) { if (!is_array($column)) {
$this->_placeholders[] = $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, 'value' => $value,
'field' => $column 'field' => $field
); );
}
return $this; return $this;
} }

View file

@ -21,7 +21,17 @@ class Wootook_Core_Database_Sql_Placeholder_Param
{ {
parent::beforeExcute($statement); 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; return $this;
} }

View file

@ -34,8 +34,8 @@ class Wootook_Core_Database_Sql_Select
self::JOIN => array(), self::JOIN => array(),
self::WHERE => array(), self::WHERE => array(),
self::UNION => array(), self::UNION => array(),
self::LIMIT => array(), self::LIMIT => null,
self::OFFSET => array(), self::OFFSET => null,
self::GROUP => array(), self::GROUP => array(),
self::HAVING => array(), self::HAVING => array(),
self::ORDER => array(), self::ORDER => array(),

View file

@ -21,6 +21,9 @@ class Wootook_Core_Database_Sql_Update
$this->_parts = array( $this->_parts = array(
self::INTO => array(), self::INTO => array(),
self::SET => array(), self::SET => array(),
self::WHERE => array(),
self::LIMIT => null,
self::OFFSET => null,
); );
} else if (isset($this->_parts[$part])) { } else if (isset($this->_parts[$part])) {
$this->_parts[$part] = array(); $this->_parts[$part] = array();
@ -29,16 +32,22 @@ class Wootook_Core_Database_Sql_Update
return $this; return $this;
} }
public function set($column, $value) public function set($column, $value = null)
{ {
if ($value instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) { if (!is_array($column)) {
$this->_placeholders[] = $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, 'value' => $value,
'field' => $column 'field' => $field
); );
}
return $this; return $this;
} }
@ -65,15 +74,12 @@ class Wootook_Core_Database_Sql_Update
} }
switch ($part) { switch ($part) {
case self::COLUMNS: case self::SET:
return $this->renderSet(); return $this->renderSet();
break; break;
case self::INTO: case self::INTO:
return $this->renderInto(); return $this->renderInto();
break; break;
case self::SELECT:
return $this->renderSelect();
break;
} }
return null; return null;
@ -81,17 +87,20 @@ class Wootook_Core_Database_Sql_Update
public function renderSet() public function renderSet()
{ {
$values = array();
$fields = array(); $fields = array();
foreach ($this->_parts[self::SET] as $field) { foreach ($this->_parts[self::SET] as $field) {
if ($field['value'] instanceof Wootook_Core_Database_Sql_Placeholder_Placeholder) { 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 { } 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)) { 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'])}"; $output = "{$this->_connection->quoteIdentifier($this->_parts[self::INTO]['table'])}";
} }
return "INSERT INTO " . $output; return "UPDATE " . $output;
} }
public function render() public function render()
{ {
if (empty($this->_parts[self::SELECT])) {
return implode('', array( return implode('', array(
$this->renderInto(), $this->renderInto(),
$this->renderColumns(), $this->renderSet(),
$this->renderWhere(),
$this->renderLimit()
)); ));
} else {
return implode('', array(
$this->renderInto(),
$this->renderSelect(),
));
}
} }
} }

View file

@ -22,26 +22,6 @@ class Wootook_Core_Mvc_Controller_Front
), ),
self::ROUTE_DEFAULT => array( self::ROUTE_DEFAULT => array(
'modules' => 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( 'defaults' => array(
'module' => 'core', 'module' => 'core',
@ -156,12 +136,21 @@ class Wootook_Core_Mvc_Controller_Front
while ($loop++ < 100) { while ($loop++ < 100) {
$moduleKey = $this->_request->getModuleName(); $moduleKey = $this->_request->getModuleName();
if (empty($moduleKey)) { 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; continue;
} }
if (!isset($this->_routes[$route]['modules'][$moduleKey])) { 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; continue;
} }
@ -178,7 +167,12 @@ class Wootook_Core_Mvc_Controller_Front
} }
if (!class_exists($controllerClass, false)) { 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; continue;
} }
@ -189,7 +183,12 @@ class Wootook_Core_Mvc_Controller_Front
$actionMethod = $this->_getActionMethod($actionKey); $actionMethod = $this->_getActionMethod($actionKey);
if (!method_exists($controllerClass, $actionMethod)) { 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; continue;
} }
@ -223,4 +222,25 @@ class Wootook_Core_Mvc_Controller_Front
{ {
$this->_response->render(); $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;
}
} }

View file

@ -19,7 +19,7 @@ class Wootook_Core_Mvc_Controller_Request_Http
{ {
parent::__construct($options); 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 $baseUri = substr($this->_baseUrl, strpos($this->_baseUrl, '/', 8)); // Get the path from the DocumentRoot
$params = ''; $params = '';

View file

@ -79,14 +79,14 @@ abstract class Wootook_Core_Mvc_Model_Entity
if ($this->getId() !== null) { if ($this->getId() !== null) {
$update = $adapter->update() $update = $adapter->update()
->into($adapter->getTable($this->getTableName())) ->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) { foreach ($this->getDataMapper()->encode($this, $this->getChangedDatas()) as $field => $value) {
$update->set($field, new Wootook_Core_Database_Sql_Placeholder_Param($field, $value)); $update->set($field, new Wootook_Core_Database_Sql_Placeholder_Param($field, $value));
} }
try { try {
$statement = $update->prepare(); $statement = $update->prepare();
$statement->execute(array('id' => $this->getId())); $statement->execute();
} catch (Wootook_Core_Exception_Database_AdapterError $e) { } catch (Wootook_Core_Exception_Database_AdapterError $e) {
throw new Wootook_Core_Exception_DataAccessException('Could not save data: ' . $e->getMessage(), null, $e); throw new Wootook_Core_Exception_DataAccessException('Could not save data: ' . $e->getMessage(), null, $e);
} catch (Wootook_Core_Exception_Database_StatementError $e) { } catch (Wootook_Core_Exception_Database_StatementError $e) {
@ -97,13 +97,13 @@ abstract class Wootook_Core_Mvc_Model_Entity
->into($adapter->getTable($this->getTableName())); ->into($adapter->getTable($this->getTableName()));
foreach ($this->getDataMapper()->encode($this, $this->getAllDatas()) as $field => $value) { 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 { try {
$statement = $insert->prepare(); $statement = $insert->prepare();
$statement->execute(); $statement->execute();
$id = $adapter->lastInsertId($table); $id = $adapter->lastInsertId($adapter->getTable($this->getTableName()));
$this->setId($id); $this->setId($id);
} catch (Wootook_Core_Exception_Database_AdapterError $e) { } catch (Wootook_Core_Exception_Database_AdapterError $e) {
throw new Wootook_Core_Exception_DataAccessException('Could not save data: ' . $e->getMessage(), null, $e); throw new Wootook_Core_Exception_DataAccessException('Could not save data: ' . $e->getMessage(), null, $e);

View file

@ -34,5 +34,9 @@ require_once dirname(__FILE__) .'/application/bootstrap.php';
$frontController = new Wootook_Core_Mvc_Controller_Front(Wootook::getRequest(), Wootook::getResponse()); $frontController = new Wootook_Core_Mvc_Controller_Front(Wootook::getRequest(), Wootook::getResponse());
$frontController $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() ->dispatch()
->send(); ->send();