Advertisement
Guest User

action API planing

a guest
Nov 20th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.59 KB | None | 0 0
  1. <?php
  2. // plugins: PluginInspectionInterface
  3.  
  4. interface ExecutableInterface extends PluginInspectionInterface {
  5.  
  6.   // Defined by PluginInspectionInterface
  7.   // Possibility to alter parameter definition upon config. -> getDefinition()
  8.   public function getDefinition();
  9.  
  10.   /**
  11.    * @param array $arguments
  12.    *   The arguments using typed data wrappers.
  13.    * @param $state
  14.    *
  15.    * @return array
  16.    *   An array of values for the provided variables.
  17.    */
  18.   public function executeByArguments($arguments, $state);
  19. }
  20.  
  21. /**
  22.  * May return variables, save parameters, ..
  23.  */
  24. interface ActionInterface extends ExecutableInterface {
  25.  
  26. }
  27.  
  28. // Restriction to return booleans. Fixed definition to provide a boolean.
  29. // Further restriction: May not change arguments.
  30. interface ConditionInterface extends ActionInterface {
  31.  
  32. }
  33.  
  34.  
  35. /**
  36.  * Handle saving here???
  37.  */
  38. class ActionExecutionManager {
  39.  
  40.   public function setExecutionState(ExecutionState $state) {
  41.     $this->state = $state;
  42.   }
  43.  
  44.   public function execute(ActionInterface $action) {
  45.     // Extract arguments by applying action config.
  46.     $arguments = array('arg1' => $value);
  47.  
  48.     // Execute and then care about saving.
  49.     $action->executeByArguments($arguments, $this->state);
  50.  
  51.     $this->state->runPostExecutionTasks();
  52.   }
  53. }
  54.  
  55. // Inject execution manager upon creation.
  56. // Inject configuration form manager upon creation.
  57. abstract class ActionBase {
  58.  
  59.   public function execute() {
  60.     return $this->executionManager->execute($this, func_get_args());
  61.   }
  62.  
  63.   public function executeByArguments($arguments, ExecutionState $state) {
  64.     // unwrap arguments.
  65.     $this->execute($arg1, $args2, $state);
  66.   }
  67.  
  68.   public function evaluate($node, $title) {
  69.     $node->title = $title;
  70.   }
  71. }
  72.  
  73. // Move to annotations.
  74. function hook_action_info() {
  75.   $defintion = array(
  76.     'label' => t('Update node title'),
  77.     'parameter' => array(
  78.       'node' => array(
  79.         'type' => 'node',
  80.         'label' => t('Content'),
  81.         'save' => TRUE,
  82.       ),
  83.       'text' => array(
  84.         'type' => 'string',
  85.         'label' => t('Text'),
  86.       ),
  87.     ),
  88.     'provides' => array(
  89.       'new var' => array(
  90.         'text' => array(
  91.           'type' => 'string',
  92.           'label' => 'fooo',
  93.         )
  94.       )
  95.     ),
  96.   );
  97. }
  98.  
  99. function example_usage() {
  100.   $action_manager = new ActionManager();
  101.   $condition_manager = new ConditionManager();
  102.  
  103.   $action_manager->createInstance('name')
  104.     ->setArgument('node', $node)
  105.     ->setArgument('text', 'new title')
  106.     ->execute();
  107.  
  108.   $boolean = $condition_manager->createInstance('condition_name')
  109.     ->setArgument('text', 'new title')
  110.     ->execute();
  111.  
  112.   $action = $action_manager->createInstance('name')
  113.     ->setArgumentBySelector('node', 'user2')
  114.     ->setArgument('text','new title')
  115.     ->execute();
  116.  
  117.   // Store action configuration.
  118.   $config = $action->getConfiguration();
  119.   // Read/write config to CMI.
  120.   $action_manager->createInstance('name')
  121.     ->applyConfiguration($config)
  122.     ->execute();
  123.  
  124.   $form = $action_manager->form($action);
  125.   $action_manager->formValidate($action, $form, $form_state);
  126.   $action_manager->formSubmit($action, $form, $form_State);
  127.  
  128.   list($variable1, $variable2) = $action_manager->createInstance('name')
  129.     ->setArgumentBySelector('node', 'node.referenced_node.entity')
  130.     ->setArgument('text','new title')
  131.     ->addProcessor('text', 'token') // done by the form code?
  132.     ->addProcessor('date', 'offset', '+1 day') // done by the form code?
  133.     ->execute();
  134.  
  135. }
  136.  
  137. // Rules: RuleElementPlugin -> "condition", "action", "loop", "or", ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement