* class MyPrintAction implements ezcWorkflowServiceObject * { * private $whatToSay; * * public function __construct( $whatToSay ) * { * $this->whatToSay = $whatToSay; * } * * public function execute() * { * print $this->whatToSay; * return true; // we're finished, activate next node * } * } * * $workflow = new ezcWorkflow( 'Test' ); * * $action = new ezcWorkflowNodeAction( array( "class" => "MyPrintAction", * "arguments" => "No. 1 The larch!" ) ); * $action->addOutNode( $workflow->endNode ); * $workflow->startNode->addOutNode( $action ); * * * @package Workflow * @version 1.1rc1 */ class ezcWorkflowNodeAction extends ezcWorkflowNode { /** * Constructs a new action node with the configuration $configuration. * * Configuration format * * * @param mixed $configuration * @throws ezcWorkflowDefinitionStorageException */ public function __construct( $configuration ) { if ( is_string( $configuration ) ) { $configuration = array( 'class' => $configuration ); } if ( !isset( $configuration['arguments'] ) ) { $configuration['arguments'] = array(); } parent::__construct( $configuration ); } /** * Executes this node by creating the service object and calling its execute() method. * * If the service object returns true, the output node will be activated. * If the service node returns false the workflow will be suspended * unless there are other activated nodes. An action node suspended this way * will be executed again the next time the workflow is resumed. * * @param ezcWorkflowExecution $execution * @return boolean true when the node finished execution, * and false otherwise * @ignore */ public function execute( ezcWorkflowExecution $execution ) { $object = $this->createObject(); $finished = $object->execute( $execution ); // Execution of the Service Object has finished. if ( $finished !== false ) { $this->activateNode( $execution, $this->outNodes[0] ); return parent::execute( $execution ); } // Execution of the Service Object has not finished. else { return false; } } /** * Generate node configuration from XML representation. * * @param DOMElement $element */ public static function configurationFromXML( DOMElement $element ) { $configuration = array( 'class' => $element->getAttribute( 'serviceObjectClass' ), 'arguments' => array() ); if ( $element->childNodes->item( 1 ) instanceof DOMElement && $element->childNodes->item( 1 )->tagName == 'arguments' ) { foreach ( $element->childNodes->item( 1 )->childNodes as $argument ) { if ( $argument instanceof DOMElement ) { $configuration['arguments'][] = ezcWorkflowDefinitionStorageXml::xmlToVariable( $argument ); } } } return $configuration; } /** * Generate XML representation of this node's configuration. * * @param DOMElement $element */ public function configurationToXML( DOMElement $element ) { $element->setAttribute( 'serviceObjectClass', $this->configuration['class'] ); if ( !empty( $this->configuration['arguments'] ) ) { $xmlArguments = $element->appendChild( $element->ownerDocument->createElement( 'arguments' ) ); foreach ( $this->configuration['arguments'] as $argument ) { $xmlArguments->appendChild( ezcWorkflowDefinitionStorageXml::variableToXml( $argument, $element->ownerDocument ) ); } } } /** * Returns a textual representation of this node. * * @return string * @ignore */ public function __toString() { try { $object = $this->createObject(); } catch ( ezcBaseAutoloadException $e ) { return 'Class not found.'; } catch ( ezcWorkflowExecutionException $e ) { return $e->getMessage(); } return (string)$object; } /** * Returns the service object as specified by the configuration. * * @return ezcWorkflowServiceObject */ protected function createObject() { if ( !class_exists( $this->configuration['class'] ) ) { throw new ezcWorkflowExecutionException( 'Class not found.' ); } $class = new ReflectionClass( $this->configuration['class'] ); if ( !$class->implementsInterface( 'ezcWorkflowServiceObject' ) ) { throw new ezcWorkflowExecutionException( 'Class does not implement the ezcWorkflowServiceObject interface.' ); } if ( !empty( $this->configuration['arguments'] ) ) { return $class->newInstanceArgs( $this->configuration['arguments'] ); } else { return $class->newInstance(); } } } ?>