Skip to content

Commit

Permalink
Updated front controller
Browse files Browse the repository at this point in the history
Fixed DML queries errors (partly implemented)
Fixed minor bugs

Signed-off-by: Gregory PLANCHAT <[email protected]>
  • Loading branch information
gplanchat committed Mar 21, 2012
1 parent 3dde300 commit 7014596
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 70 deletions.
14 changes: 13 additions & 1 deletion src/application/code/core/Wootook.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private static function _appendDatabaseConfig(Wootook_Core_Config_Node $config,
return $config;
}

$select = $adapter->select('core_config');
$select = $adapter->select($adapter->getTable('core_config'));

switch ($type) {
case 'website':
Expand Down Expand Up @@ -653,6 +653,12 @@ public static function getConfig($path = null)
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) {
Expand All @@ -675,6 +681,12 @@ public static function getWebsiteConfig($path = null, $websiteKey = null)
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public function __construct(Wootook_Core_Config_Node $config, Array $options = a
$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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public static function getSingleton()
self::ATTR_ERRMODE => self::ERRMODE_EXCEPTION
);

/**
* @param $connectionName
* @return Wootook_Core_Database_Adapter_Adapter
*/
public function getConnection($connectionName)
{
if (empty($connectionName) || $connectionName === null) {
Expand Down Expand Up @@ -204,4 +208,4 @@ protected function _load($className, $useSingleton, Array $constructorParams = a
$reflection = new ReflectionClass($className);
return $reflection->newInstanceArgs($constructorParams);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ public function quoteIdentifier($identifier)

public function beforePrepare(Wootook_Core_Database_Statement_Statement $statement)
{
/*
foreach ($this->_placeholders as $placeholder) {
$placeholder->beforePrepare($statement);
}

*/
return $this;
}

Expand Down
20 changes: 13 additions & 7 deletions src/application/code/core/Wootook/Core/Database/Sql/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,22 @@ public function reset($part = null)
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ public function beforeExcute(Wootook_Core_Database_Statement_Statement $statemen
{
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function reset($part = null)
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(),
Expand Down
56 changes: 30 additions & 26 deletions src/application/code/core/Wootook/Core/Database/Sql/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function reset($part = null)
$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();
Expand All @@ -29,16 +32,22 @@ public function reset($part = null)
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;
}
Expand All @@ -65,33 +74,33 @@ public function toString($part = null)
}

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;
}

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) . ')';
}
}

Expand All @@ -103,21 +112,16 @@ public function renderInto()
$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()
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,4 @@ public function errorState()

return $info[0];
}
}
}
68 changes: 44 additions & 24 deletions src/application/code/core/Wootook/Core/Mvc/Controller/Front.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -156,12 +136,21 @@ public function dispatch($route = null)
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;
}

Expand All @@ -178,7 +167,12 @@ public function dispatch($route = null)
}

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;
}

Expand All @@ -189,7 +183,12 @@ public function dispatch($route = null)

$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;
}

Expand Down Expand Up @@ -223,4 +222,25 @@ public function send()
{
$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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(Array $options = array())
{
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 = '';
Expand Down Expand Up @@ -358,4 +358,4 @@ public function setActionName($name)
return $this->setParam($this->getActionKey(), $name);
}
}
}
}
Loading

0 comments on commit 7014596

Please sign in to comment.