Permalink
Browse files

Redesigned Operation API.

  • Loading branch information...
olvlvl committed Mar 20, 2011
1 parent d4069e7 commit 98b6964ac8b9be0152653ac9f49ca5903be59ee2
Showing with 1,039 additions and 957 deletions.
  1. +8 −1 CHANGELOG
  2. +8 −1 config/core.php
  3. +2 −2 config/routes.php
  4. +54 −0 operations/core__aloha.php
  5. +36 −0 operations/core__ping.php
  6. +64 −0 operations/delete.php
  7. +156 −0 operations/save.php
  8. +10 −76 wdcore.php
  9. +30 −712 wdmodule.php
  10. +671 −165 wdoperation.php
View
@@ -1,4 +1,11 @@
-2011-03-12 # 0.9.0-dev
+2011-03-14 # 0.10.0-dev
+
+[NEW] Redesigned Operation API.
+
+
+
+
+2011-03-10 # 0.9.0
* Modules accessor:
View
@@ -1,5 +1,7 @@
<?php
+$operations_path = $path . 'operations' . DIRECTORY_SEPARATOR;
+
return array
(
'autoload' => array
@@ -25,7 +27,12 @@
'WdRoute' => $path . 'wdroute.php',
'WdSession' => $path . 'wdsession.php',
'WdTranslator' => $path . 'wdi18n.php',
- 'WdUploaded' => $path . 'wduploaded.php'
+ 'WdUploaded' => $path . 'wduploaded.php',
+
+ 'delete_WdOperation' => $operations_path . 'delete.php',
+ 'save_WdOperation' => $operations_path . 'save.php',
+ 'core__aloha_WdOperation' => $operations_path . 'core__aloha.php',
+ 'core__ping_WdOperation' => $operations_path . 'core__ping.php'
),
'cache configs' => false,
View
@@ -4,11 +4,11 @@
(
'/api/core/aloha' => array
(
- 'callback' => array('WdCore', 'operation_aloha')
+ 'class' => 'core__aloha_WdOperation'
),
'/api/core/ping' => array
(
- 'callback' => array('WdCore', 'operation_ping')
+ 'class' => 'core__ping_WdOperation'
)
);
View
@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * This file is part of the WdCore package.
+ *
+ * (c) Olivier Laviale <olivier.laviale@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Displays information about the core and its modules.
+ */
+class core__aloha_WdOperation extends WdOperation
+{
+ protected function validate()
+ {
+ return true;
+ }
+
+ protected function process()
+ {
+ global $core;
+
+ $enabled = array();
+ $disabled = array();
+
+ foreach ($core->modules->descriptors as $module_id => $descriptor)
+ {
+ if (!empty($descriptor[WdModule::T_DISABLED]))
+ {
+ $disabled[] = $module_id;
+
+ continue;
+ }
+
+ $enabled[] = $module_id;
+ }
+
+ sort($enabled);
+ sort($disabled);
+
+ header('Content-Type: text/plain; charset=utf-8');
+
+ $rc = 'WdCore version ' . WdCore::VERSION . ' is running here with:';
+ $rc .= PHP_EOL . PHP_EOL . implode(PHP_EOL, $enabled);
+ $rc .= PHP_EOL . PHP_EOL . 'Disabled modules:';
+ $rc .= PHP_EOL . PHP_EOL . implode(PHP_EOL, $disabled);
+ $rc .= PHP_EOL . PHP_EOL . strip_tags(implode(PHP_EOL, WdDebug::fetchMessages('debug')));
+
+ return $rc;
+ }
+}
View
@@ -0,0 +1,36 @@
+<?php
+
+/*
+ * This file is part of the WdCore package.
+ *
+ * (c) Olivier Laviale <olivier.laviale@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Keeps the user's session alive. Only already created sessions are kept alive, new sessions
+ * are *not* created.
+ */
+class core__ping_WdOperation extends WdOperation
+{
+ protected function validate()
+ {
+ return true;
+ }
+
+ protected function process()
+ {
+ global $core;
+
+ header('Content-Type: text/plain; charset=utf-8');
+
+ if (WdSession::exists())
+ {
+ $core->session;
+ }
+
+ return 'pong';
+ }
+}
View
@@ -0,0 +1,64 @@
+<?php
+
+/*
+ * This file is part of the WdCore package.
+ *
+ * (c) Olivier Laviale <olivier.laviale@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Deletes a record.
+ */
+class delete_WdOperation extends WdOperation
+{
+ /**
+ * Controls for the operation: permission(manage), record and ownership.
+ *
+ * @see WdOperation::__get_controls()
+ */
+ protected function __get_controls()
+ {
+ return array
+ (
+ self::CONTROL_PERMISSION => WdModule::PERMISSION_MANAGE,
+ self::CONTROL_RECORD => true,
+ self::CONTROL_OWNERSHIP => true
+ )
+
+ + parent::__get_controls();
+ }
+
+ protected function validate()
+ {
+ return true;
+ }
+
+ /**
+ * Delete the target record.
+ *
+ * @see WdOperation::process()
+ */
+ protected function process()
+ {
+ $key = $this->key;
+
+ if (!$this->module->model->delete($key))
+ {
+ wd_log_error('Unable to delete the record %key from %module.', array('%key' => $key, '%module' => (string) $this->module));
+
+ return;
+ }
+
+ if (isset($this->params['#location']))
+ {
+ $this->location = $this->params['#location'];
+ }
+
+ wd_log_done('The record %key has been delete from %module.', array('%key' => $key, '%module' => $this->module->title), 'delete');
+
+ return $key;
+ }
+}
View
@@ -0,0 +1,156 @@
+<?php
+
+/*
+ * This file is part of the WdCore package.
+ *
+ * (c) Olivier Laviale <olivier.laviale@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * The "save" operation is used to create or update a record.
+ */
+class save_WdOperation extends WdOperation
+{
+ /**
+ * Change controls:
+ *
+ * CONTROL_PERMISSION => WdModule::PERMISSION_CREATE
+ * CONTROL_OWNERSHIP => true
+ * CONTROL_FORM => true
+ *
+ * @param WdOperation $operation
+ * @return array The controls of the operation.
+ */
+ protected function __get_controls()
+ {
+ return array
+ (
+ self::CONTROL_PERMISSION => WdModule::PERMISSION_CREATE,
+ self::CONTROL_RECORD => true,
+ self::CONTROL_OWNERSHIP => true,
+ self::CONTROL_FORM => true
+ )
+
+ + parent::__get_controls();
+ }
+
+ /**
+ * Overrides the getter to prevent exceptions when the operation key is empty.
+ *
+ * @see WdOperation::__get_record()
+ */
+ protected function __get_record()
+ {
+ return $this->key ? parent::__get_record() : null;
+ }
+
+ /**
+ * Overrides the method in order for the control to pass if the operation key is empty, which
+ * is the case when creating a new record.
+ *
+ * @see WdOperation::control_record()
+ */
+ protected function control_record()
+ {
+ return $this->key ? parent::control_record() : true;
+ }
+
+ /**
+ * Filters out the operation's parameters, which are not defined as fields by the
+ * primary model of the module, and take care of filtering or resolving properties values.
+ *
+ * Fields defined as 'boolean'
+ * ---------------------------
+ *
+ * The value of the property is filtered using the filter_var() function and the
+ * FILTER_VALIDATE_BOOLEAN filter. If the property in the operation params is empty, the
+ * property value is set the `false`.
+ *
+ * Fields defined as 'varchar'
+ * ---------------------------
+ *
+ * If the property is not empty in the operation params, the property value is trimed using the
+ * trim() function, ensuring that there is no leading or trailing white spaces.
+ *
+ * @see WdOperation::__get_properties()
+ * @return array The properties of the operation.
+ */
+ protected function __get_properties()
+ {
+ $schema = $this->module->model->get_extended_schema();
+ $fields = $schema['fields'];
+ $properties = array_intersect_key($this->params, $fields);
+
+ foreach ($fields as $identifier => $definition)
+ {
+ $type = $definition['type'];
+
+ if ($type == 'boolean')
+ {
+ if (empty($properties[$identifier]))
+ {
+ $properties[$identifier] = false;
+
+ continue;
+ }
+
+ $properties[$identifier] = filter_var($properties[$identifier], FILTER_VALIDATE_BOOLEAN);
+ }
+ else if ($type == 'varchar')
+ {
+ if (empty($properties[$identifier]) || !is_string($properties[$identifier]))
+ {
+ continue;
+ }
+
+ $properties[$identifier] = trim($properties[$identifier]);
+ }
+ }
+
+ return $properties;
+ }
+
+ /**
+ * The method simply returns true.
+ *
+ * @see WdOperation::validate()
+ */
+ protected function validate()
+ {
+ return true;
+ }
+
+ /**
+ * Creates or updates a record in the module's primary model.
+ *
+ * A record is created if the operation's key is empty, otherwise an existing record is
+ * updated.
+ *
+ * The method uses the `properties` property to get the properties used to create or update
+ * the record.
+ *
+ * @return array An array composed of the save mode ('update' or 'create') and the record's
+ * key.
+ * @throws WdException when saving the record fails.
+ */
+ protected function process()
+ {
+ $key = $this->key;
+ $record_key = $this->module->model->save($this->properties, $key);
+ $log_params = array('%key' => $key, '%module' => $this->module->title);
+
+ if (!$record_key)
+ {
+ throw new WdException($key ? 'Unable to update record %key in %module.' : 'Unable to create record in %module.', $log_params);
+ }
+
+ $this->location = $_SERVER['REQUEST_URI'];
+
+ wd_log_done($key ? 'The record %key in %module has been saved.' : 'A new record has been saved in %module.', $log_params, 'save');
+
+ return array('mode' => $key ? 'update' : 'create', 'key' => $record_key);
+ }
+}
Oops, something went wrong.

0 comments on commit 98b6964

Please sign in to comment.