Updated layout file format to XML

Fixed config & install inconstinstencies
Fixed form management updates

Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
Gregory PLANCHAT 2012-01-24 22:27:32 +01:00
commit d40313279a
29 changed files with 692 additions and 918 deletions

View file

@ -40,6 +40,11 @@ class Wootook_Core_Config_Node
return $this;
}
public function getParent()
{
return $this->_parent;
}
public function setConfig($path, $value)
{
$explodedPath = explode('/', $path);
@ -131,6 +136,9 @@ class Wootook_Core_Config_Node
private function _resolveAttributeName($key)
{
if (is_numeric($key)) {
return $key;
}
if (!isset(self::$_attributeNameCache[$key])) {
self::$_attributeNameCache[$key] = str_replace(' ', '-', strtolower(preg_replace('#[A-Z]#', ' \\1', $key)));
}
@ -183,7 +191,11 @@ class Wootook_Core_Config_Node
public function offsetSet($offset, $value)
{
$this->_children[$offset] = $value;
if ($offset === null) {
$this->_children[] = $value;
} else {
$this->_children[$offset] = $value;
}
return $value;
}

View file

@ -76,15 +76,17 @@ class Wootook_Core_Form
public function addElement($name, $type = 'text', Array $validators = array())
{
if ($type instanceof Wootook_Core_Form_ElementAbstract) {
$this->_elements[$name] = $name;
$this->_elements[$name] = $type;
$this->_elements[$name]->setForm($this);
} else {
$element = $this->_elementLoader->load($type);
if ($element === null) {
trigger_error(sprintf('Element %1$s (type: %2$s) could not be created.', $name, $type), E_USER_WARNING);
Wootook_Core_ErrorProfiler::getSingleton()
->addException(new Wootook_Core_Exception_RuntimeException(sprintf('Element %1$s (type: %2$s) could not be created.', $name, $type)));
return $this;
}
$element->setForm($this);
$this->_elements[$name] = $element;
}
$this->_elements[$name]->setName($name);

View file

@ -17,7 +17,7 @@ abstract class Wootook_Core_Form_ElementAbstract
}
$this->setName($name);
$this->setParams($params);
$this->setAllParams($params);
}
abstract public function getType();

View file

@ -3,17 +3,16 @@
class Wootook_Core_Form_Validator_Alnum
extends Wootook_Core_Form_Validator_Regex
{
public function __construct()
{
parent::__construct('#[^[:alnum:]]#');
}
public function validate(Wootook_Core_Form_FieldAbstract $field, $data)
public function validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
if ($this->_validate($field, $data)) {
$this->_getSession($field)
->addError('Field "%s" should only contain alphanumeric characters.', $field->getName());
if ($this->_validate($element, $data)) {
$this->_getSession($element)
->addError('Field "%s" should only contain alphanumeric characters.', $element->getName());
return false;
}

View file

@ -3,17 +3,16 @@
class Wootook_Core_Form_Validator_Alnum
extends Wootook_Core_Form_Validator_Regex
{
public function __construct()
{
parent::__construct('#[^[:alpha:]]#');
}
public function validate(Wootook_Core_Form_FieldAbstract $field, $data)
public function validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
if ($this->_validate($field, $data)) {
$this->_getSession($field)
->addError('Field "%s" should only contain alphabetic characters.', $field->getName());
if ($this->_validate($element, $data)) {
$this->_getSession($element)
->addError('Field "%s" should only contain alphabetic characters.', $element->getName());
return false;
}

View file

@ -8,11 +8,11 @@ class Wootook_Core_Form_Validator_Email
parent::__construct('#^[[:alnum:]\._\-]@[[:alnum:]\._\-]\.[a-z]{2,}$#');
}
public function validate(Wootook_Core_Form_FieldAbstract $field, $data)
public function validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
if (!$this->_validate($field, $data)) {
$this->_getSession($field)
->addError('Field "%s" should contain an email.', $field->getName());
if (!$this->_validate($element, $data)) {
$this->_getSession($element)
->addError('Field "%s" should contain an email.', $element->getName());
return false;
}

View file

@ -3,9 +3,9 @@
class Wootook_Core_Form_Validator_FormKey
extends Wootook_Core_Form_ValidatorAbstract
{
public function validate(Wootook_Core_Form_FieldAbstract $field, $data)
public function validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
$form = $field->getForm();
$form = $element->getForm();
$session = $form->getSession();
if ($session->getFormKey(false) == $data) {

View file

@ -9,11 +9,11 @@ class Wootook_Core_Form_Validator_Hex
parent::__construct('#(?:0x)?[^0-9a-f]#i');
}
public function validate(Wootook_Core_Form_FieldAbstract $field, $data)
public function validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
if ($this->_validate($field, $data)) {
$this->_getSession($field)
->addError('Field "%s" should only contain numeric characters.', $field->getName());
if ($this->_validate($element, $data)) {
$this->_getSession($element)
->addError('Field "%s" should only contain numeric characters.', $element->getName());
return false;
}

View file

@ -8,11 +8,11 @@ class Wootook_Core_Form_Validator_Email
parent::__construct('#^[[:alnum:]\._\-]\.[a-z]{2,}$#');
}
public function validate(Wootook_Core_Form_FieldAbstract $field, $data)
public function validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
if (!$this->_validate($field, $data)) {
$this->_getSession($field)
->addError('Field "%s" should contain a host name.', $field->getName());
if (!$this->_validate($element, $data)) {
$this->_getSession($element)
->addError('Field "%s" should contain a host name.', $element->getName());
return false;
}

View file

@ -9,11 +9,11 @@ class Wootook_Core_Form_Validator_Numeric
parent::__construct('#[^[:digit:]]#');
}
public function validate(Wootook_Core_Form_FieldAbstract $field, $data)
public function validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
if ($this->_validate($field, $data)) {
$this->_getSession($field)
->addError('Field "%s" should only contain numeric characters.', $field->getName());
if ($this->_validate($element, $data)) {
$this->_getSession($element)
->addError('Field "%s" should only contain numeric characters.', $element->getName());
return false;
}

View file

@ -10,18 +10,18 @@ class Wootook_Core_Form_Validator_Regex
$this->_expression = $expression;
}
public function validate(Wootook_Core_Form_FieldAbstract $field, $data)
public function validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
if (!$this->_validate($field, $data)) {
$this->_getSession($field)
->addError('Field "%s" does not match the validation pattern.', $field->getName());
if (!$this->_validate($element, $data)) {
$this->_getSession($element)
->addError('Field "%s" does not match the validation pattern.', $element->getName());
return false;
}
return true;
}
protected function _validate(Wootook_Core_Form_FieldAbstract $field, $data)
protected function _validate(Wootook_Core_Form_ElementAbstract $element, $data)
{
return preg_match($this->_expression, $data);
}

View file

@ -2,14 +2,14 @@
abstract class Wootook_Core_Form_ValidatorAbstract
{
abstract public function validate(Wootook_Core_Form_FieldAbstract $field, $data);
abstract public function validate(Wootook_Core_Form_ElementAbstract $element, $data);
protected function _getSession(Wootook_Core_Form_FieldAbstract $field)
protected function _getSession(Wootook_Core_Form_ElementAbstract $element)
{
$form = $field->getForm();
$form = $element->getForm();
if (!$form instanceof Wootook_Core_Form) {
return null;
}
return $form->getSession();
return $element->getSession();
}
}

View file

@ -70,9 +70,14 @@ class Wootook_Core_Layout
$this->setPackage(Wootook::getWebsiteConfig('package'));
$this->setTheme(Wootook::getGameConfig('theme'));
$parser = new Wootook_Core_Layout_Parser();
foreach ($fileList as $layoutFile) {
try {
$layoutData = include $this->_getLayoutPath($layoutFile);
if (preg_match('#\.php$#', $layoutFile)) {
$layoutData = include $this->_getLayoutPath($layoutFile);
} else {
$layoutData = $parser->parse($this->_getLayoutPath($layoutFile), $this->_getLayoutCachePath($layoutFile), 3600);
}
} catch (Wootook_Core_Exception_LayoutException $e) {
Wootook_Core_ErrorProfiler::getSingleton()->addException($e);
continue;
@ -444,6 +449,22 @@ class Wootook_Core_Layout
return $this->_view->render();
}
protected function _getLayoutCachePath($file)
{
$package = $this->getPackage();
$theme = $this->getTheme();
$pattern = APPLICATION_PATH . "cache/" . __CLASS__ . "--{$this->getDomain()}-%s-%s-{$file}.cache";
if (empty($package)) {
$package = self::DEFAULT_PACKAGE;
}
if (empty($theme)) {
$theme = self::DEFAULT_THEME;
}
return sprintf($pattern, $package, $theme);
}
protected function _getLayoutPath($file)
{
$package = $this->getPackage();

View file

@ -0,0 +1,132 @@
<?php
class Wootook_Core_Layout_Parser
{
/**
*
* Enter description here ...
* @param unknown_type $filename
*/
public function parse($filename, $cachePath = null, $cacheLifetime = 86400)
{
if ($cachePath !== null && Wootook::fileExists($cachePath) && $cacheLifetime > 0 && (time() - filemtime($cachePath) > $cacheLifetime)) {
return include $cachePath;
}
$parsedData = $this->_parse($filename);
if ($cachePath !== null) {
$parsedData->save($cachePath);
}
return $parsedData->toArray();
}
protected function _parse($filename)
{
$layout = simplexml_load_file($filename);
if ($layout->getName() !== 'layout') {
return array();
}
$result = new Wootook_Core_Config_Adapter_Array();
foreach ($layout->handle as $handle) {
$name = (string) $handle['name'];
if (empty($name)) {
continue;
}
$handleNode = new Wootook_Core_Config_Node(array(), $result);
$result->$name = $handleNode;
if (isset($handle->update) && isset($handle->update[0]['name'])) {
$handleNode->update = (string) $handle->update[0]['name'];
}
if (isset($handle->block)) {
$this->_parseBlock($handle->block[0], $handleNode);
}
if (isset($handle->reference)) {
$referenceListNode = new Wootook_Core_Config_Node(array(), $handleNode);
$handleNode->reference = $referenceListNode;
foreach ($handle->reference as $reference) {
$referenceName = (string) $reference['name'];
if (empty($referenceName)) {
continue;
}
$referenceNode = new Wootook_Core_Config_Node(array(), $referenceListNode);
$referenceListNode->$referenceName = $referenceNode;
$this->_parseBlockBody($reference, $referenceNode);
}
}
}
return $result;
}
protected function _parseBlock(SimpleXMLElement $handle, Wootook_Core_Config_Node $blockNode)
{
foreach ($handle->attributes() as $attributeName => $attributeValue) {
$blockNode->$attributeName = (string) $attributeValue;
}
$this->_parseBlockBody($handle, $blockNode);
}
protected function _parseBlockBody(SimpleXMLElement $handle, Wootook_Core_Config_Node $blockNode)
{
if (isset($handle->block)) {
$childrenNode = new Wootook_Core_Config_Node(array(), $blockNode);
$blockNode->children = $childrenNode;
foreach ($handle->block as $block) {
$blockName = (string) $block['name'];
if (empty($blockName)) {
continue;
}
$childNode = new Wootook_Core_Config_Node(array(), $childrenNode);
$childrenNode->$blockName = $childNode;
$this->_parseBlock($block, $childNode);
}
}
if (isset($handle->action)) {
$actionListNode = new Wootook_Core_Config_Node(array(), $blockNode);
$blockNode->actions = $actionListNode;
foreach ($handle->action as $action) {
$methodName = (string) $action['method'];
if (empty($methodName)) {
continue;
}
$actionNode = new Wootook_Core_Config_Node(array(
'method' => $methodName,
'params' => array()
), $actionListNode);
$actionListNode[] = $actionNode;
if (isset($action->params)) {
$paramsNode = $actionNode->params;
foreach ($action->params[0]->param as $param) {
$paramName = (string) $param['name'];
if (empty($paramName)) {
continue;
}
$paramsNode->$paramName = (string) $param;
}
}
}
}
}
}

View file

@ -77,7 +77,7 @@ class Wootook_Database
$dsn = "mysql:dbname={$params->database};host={$params->hostname}";
if (is_numeric($params->port)) {
$dsn .= ";port={$params->database}";
$dsn .= ";port={$params->port}";
}
$event = Wootook::dispatchEvent('database.prepare-options', array(

View file

@ -8,25 +8,4 @@ class Wootook_Empire_Model_Galaxy_Position
$this->_tableName = 'galaxy';
$this->_idFieldNames = array('id_planet');
}
static function initPlanetListerner($eventData)
{
if (!isset($eventData['planet'])) {
return;
}
$planet = $eventData['planet'];
if (!$planet->isPlanet()) {
return;
}
$galaxy = new self();
$galaxy
->setData('galaxy', $planet->getGalaxy())
->setData('system', $planet->getSystem())
->setData('planet', $planet->getPosition())
->setData('id_planet', $planet->getId())
->save()
;
}
}

View file

@ -376,6 +376,17 @@ class Wootook_Empire_Model_User
$planet->save();
if ($planet->isPlanet()) {
$galaxy = new Wootook_Empire_Model_Galaxy_Position();
$galaxy
->setData('galaxy', $planet->getGalaxy())
->setData('system', $planet->getSystem())
->setData('planet', $planet->getPosition())
->setData('id_planet', $planet->getId())
->save()
;
}
Wootook::dispatchEvent('planet.init', array(
'planet' => $planet,
'user' => $this