From 70145967d0bdbf046f394f02f9ed55850dadbd45 Mon Sep 17 00:00:00 2001 From: Gregory PLANCHAT Date: Wed, 21 Mar 2012 01:41:56 +0100 Subject: [PATCH] Updated front controller Fixed DML queries errors (partly implemented) Fixed minor bugs Signed-off-by: Gregory PLANCHAT --- src/application/code/core/Wootook.php | 14 +++- .../Core/Database/Adapter/Pdo/Mysql.php | 4 ++ .../Core/Database/ConnectionManager.php | 6 +- .../Core/Database/Sql/DmlFilterableQuery.php | 1 + .../Wootook/Core/Database/Sql/DmlQuery.php | 3 +- .../core/Wootook/Core/Database/Sql/Insert.php | 20 ++++-- .../Core/Database/Sql/Placeholder/Param.php | 12 +++- .../core/Wootook/Core/Database/Sql/Select.php | 4 +- .../core/Wootook/Core/Database/Sql/Update.php | 56 ++++++++------- .../Core/Database/Statement/Pdo/Mysql.php | 2 +- .../Wootook/Core/Mvc/Controller/Front.php | 68 ++++++++++++------- .../Core/Mvc/Controller/Request/Http.php | 4 +- .../core/Wootook/Core/Mvc/Model/Entity.php | 8 +-- src/index.php | 4 ++ 14 files changed, 136 insertions(+), 70 deletions(-) diff --git a/src/application/code/core/Wootook.php b/src/application/code/core/Wootook.php index 8a69efc..c11a616 100644 --- a/src/application/code/core/Wootook.php +++ b/src/application/code/core/Wootook.php @@ -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) { diff --git a/src/application/code/core/Wootook/Core/Database/Adapter/Pdo/Mysql.php b/src/application/code/core/Wootook/Core/Database/Adapter/Pdo/Mysql.php index b45bf6a..65c0c31 100644 --- a/src/application/code/core/Wootook/Core/Database/Adapter/Pdo/Mysql.php +++ b/src/application/code/core/Wootook/Core/Database/Adapter/Pdo/Mysql.php @@ -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) { diff --git a/src/application/code/core/Wootook/Core/Database/ConnectionManager.php b/src/application/code/core/Wootook/Core/Database/ConnectionManager.php index cbbad44..b1a098f 100644 --- a/src/application/code/core/Wootook/Core/Database/ConnectionManager.php +++ b/src/application/code/core/Wootook/Core/Database/ConnectionManager.php @@ -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) { @@ -204,4 +208,4 @@ class Wootook_Core_Database_ConnectionManager $reflection = new ReflectionClass($className); return $reflection->newInstanceArgs($constructorParams); } -} \ No newline at end of file +} diff --git a/src/application/code/core/Wootook/Core/Database/Sql/DmlFilterableQuery.php b/src/application/code/core/Wootook/Core/Database/Sql/DmlFilterableQuery.php index c2e3efc..3913fea 100644 --- a/src/application/code/core/Wootook/Core/Database/Sql/DmlFilterableQuery.php +++ b/src/application/code/core/Wootook/Core/Database/Sql/DmlFilterableQuery.php @@ -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) { diff --git a/src/application/code/core/Wootook/Core/Database/Sql/DmlQuery.php b/src/application/code/core/Wootook/Core/Database/Sql/DmlQuery.php index 19b38f7..9f68fd4 100644 --- a/src/application/code/core/Wootook/Core/Database/Sql/DmlQuery.php +++ b/src/application/code/core/Wootook/Core/Database/Sql/DmlQuery.php @@ -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; } diff --git a/src/application/code/core/Wootook/Core/Database/Sql/Insert.php b/src/application/code/core/Wootook/Core/Database/Sql/Insert.php index 8f6dc51..278c830 100644 --- a/src/application/code/core/Wootook/Core/Database/Sql/Insert.php +++ b/src/application/code/core/Wootook/Core/Database/Sql/Insert.php @@ -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( - 'value' => $value, - 'field' => $column - ); + 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' => $field + ); + } return $this; } diff --git a/src/application/code/core/Wootook/Core/Database/Sql/Placeholder/Param.php b/src/application/code/core/Wootook/Core/Database/Sql/Placeholder/Param.php index a6a9ec2..beb16ca 100644 --- a/src/application/code/core/Wootook/Core/Database/Sql/Placeholder/Param.php +++ b/src/application/code/core/Wootook/Core/Database/Sql/Placeholder/Param.php @@ -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; } diff --git a/src/application/code/core/Wootook/Core/Database/Sql/Select.php b/src/application/code/core/Wootook/Core/Database/Sql/Select.php index f3ae5a6..5662416 100644 --- a/src/application/code/core/Wootook/Core/Database/Sql/Select.php +++ b/src/application/code/core/Wootook/Core/Database/Sql/Select.php @@ -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(), diff --git a/src/application/code/core/Wootook/Core/Database/Sql/Update.php b/src/application/code/core/Wootook/Core/Database/Sql/Update.php index fd229ae..6aa2fad 100644 --- a/src/application/code/core/Wootook/Core/Database/Sql/Update.php +++ b/src/application/code/core/Wootook/Core/Database/Sql/Update.php @@ -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( - 'value' => $value, - 'field' => $column - ); + 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' => $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(), - )); - } + return implode('', array( + $this->renderInto(), + $this->renderSet(), + $this->renderWhere(), + $this->renderLimit() + )); } } diff --git a/src/application/code/core/Wootook/Core/Database/Statement/Pdo/Mysql.php b/src/application/code/core/Wootook/Core/Database/Statement/Pdo/Mysql.php index 388bbc3..2ce2e0d 100644 --- a/src/application/code/core/Wootook/Core/Database/Statement/Pdo/Mysql.php +++ b/src/application/code/core/Wootook/Core/Database/Statement/Pdo/Mysql.php @@ -303,4 +303,4 @@ class Wootook_Core_Database_Statement_Pdo_Mysql return $info[0]; } -} \ No newline at end of file +} diff --git a/src/application/code/core/Wootook/Core/Mvc/Controller/Front.php b/src/application/code/core/Wootook/Core/Mvc/Controller/Front.php index 79ebb93..28c3e28 100644 --- a/src/application/code/core/Wootook/Core/Mvc/Controller/Front.php +++ b/src/application/code/core/Wootook/Core/Mvc/Controller/Front.php @@ -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; + } } diff --git a/src/application/code/core/Wootook/Core/Mvc/Controller/Request/Http.php b/src/application/code/core/Wootook/Core/Mvc/Controller/Request/Http.php index 35a5fbb..459c5b9 100644 --- a/src/application/code/core/Wootook/Core/Mvc/Controller/Request/Http.php +++ b/src/application/code/core/Wootook/Core/Mvc/Controller/Request/Http.php @@ -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 = ''; @@ -358,4 +358,4 @@ class Wootook_Core_Mvc_Controller_Request_Http return $this->setParam($this->getActionKey(), $name); } } -} \ No newline at end of file +} diff --git a/src/application/code/core/Wootook/Core/Mvc/Model/Entity.php b/src/application/code/core/Wootook/Core/Mvc/Model/Entity.php index b32bb3c..5c0fb0f 100644 --- a/src/application/code/core/Wootook/Core/Mvc/Model/Entity.php +++ b/src/application/code/core/Wootook/Core/Mvc/Model/Entity.php @@ -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); diff --git a/src/index.php b/src/index.php index dc8654a..7454dc0 100644 --- a/src/index.php +++ b/src/index.php @@ -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();