public function forDNDeployment(\DNDeployment $obj)
 {
     $loader = new Finite\Loader\ArrayLoader(['class' => 'DNDeployment', 'states' => [DNDeployment::STATE_NEW => ['type' => StateInterface::TYPE_INITIAL], DNDeployment::STATE_SUBMITTED => ['type' => StateInterface::TYPE_NORMAL], DNDeployment::STATE_INVALID => ['type' => StateInterface::TYPE_NORMAL], DNDeployment::STATE_APPROVED => ['type' => StateInterface::TYPE_NORMAL], DNDeployment::STATE_REJECTED => ['type' => StateInterface::TYPE_NORMAL], DNDeployment::STATE_QUEUED => ['type' => StateInterface::TYPE_NORMAL], DNDeployment::STATE_DEPLOYING => ['type' => StateInterface::TYPE_NORMAL], DNDeployment::STATE_ABORTING => ['type' => StateInterface::TYPE_NORMAL], DNDeployment::STATE_COMPLETED => ['type' => StateInterface::TYPE_FINAL], DNDeployment::STATE_FAILED => ['type' => StateInterface::TYPE_FINAL], DNDeployment::STATE_DELETED => ['type' => StateInterface::TYPE_FINAL]], 'transitions' => [DNDeployment::TR_NEW => ['from' => [DNDeployment::STATE_SUBMITTED], 'to' => DNDeployment::STATE_NEW], DNDeployment::TR_SUBMIT => ['from' => [DNDeployment::STATE_NEW], 'to' => DNDeployment::STATE_SUBMITTED], DNDeployment::TR_QUEUE => ['from' => [DNDeployment::STATE_NEW, DNDeployment::STATE_APPROVED], 'to' => DNDeployment::STATE_QUEUED], DNDeployment::TR_INVALIDATE => ['from' => [DNDeployment::STATE_NEW, DNDeployment::STATE_SUBMITTED, DNDeployment::STATE_APPROVED, DNDeployment::STATE_REJECTED], 'to' => DNDeployment::STATE_INVALID], DNDeployment::TR_APPROVE => ['from' => [DNDeployment::STATE_NEW, DNDeployment::STATE_SUBMITTED], 'to' => DNDeployment::STATE_APPROVED], DNDeployment::TR_REJECT => ['from' => [DNDeployment::STATE_SUBMITTED], 'to' => DNDeployment::STATE_REJECTED], DNDeployment::TR_DEPLOY => ['from' => [DNDeployment::STATE_QUEUED], 'to' => DNDeployment::STATE_DEPLOYING], DNDeployment::TR_ABORT => ['from' => [DNDeployment::STATE_QUEUED, DNDeployment::STATE_DEPLOYING, DNDeployment::STATE_ABORTING], 'to' => DNDeployment::STATE_ABORTING], DNDeployment::TR_COMPLETE => ['from' => [DNDeployment::STATE_DEPLOYING], 'to' => DNDeployment::STATE_COMPLETED], DNDeployment::TR_FAIL => ['from' => [DNDeployment::STATE_NEW, DNDeployment::STATE_SUBMITTED, DNDeployment::STATE_INVALID, DNDeployment::STATE_REJECTED, DNDeployment::STATE_APPROVED, DNDeployment::STATE_QUEUED, DNDeployment::STATE_DEPLOYING, DNDeployment::STATE_ABORTING], 'to' => DNDeployment::STATE_FAILED], DNDeployment::TR_DELETE => ['from' => [DNDeployment::STATE_NEW, DNDeployment::STATE_SUBMITTED, DNDeployment::STATE_INVALID, DNDeployment::STATE_APPROVED, DNDeployment::STATE_REJECTED], 'to' => DNDeployment::STATE_DELETED]]]);
     $stateMachine = Injector::inst()->get('Finite\\StateMachine\\StateMachine', true, [$obj]);
     // if we have already initialised a state machine for the given \DNDeployment
     // then don't re-initialise it again, as this causes bugs with transition events
     // being called multiple times.
     if (empty(self::$initialised[$obj->ID])) {
         $loader->load($stateMachine);
         $stateMachine->initialize();
         $this->addHandlers($stateMachine);
         self::$initialised[$obj->ID] = true;
     }
     return $stateMachine;
 }
Exemple #2
0
    }
    public function setFiniteState($state)
    {
        $this->state = $state;
    }
    public function display()
    {
        echo 'Hello, I\'m a document and I\'m currently at the ', $this->state, ' state.', "\n";
    }
}
// Configure your graph
$document = new Document();
$stateMachine = new Finite\StateMachine\StateMachine($document);
$loader = new Finite\Loader\ArrayLoader(array('class' => 'Document', 'states' => array('draft' => array('type' => Finite\State\StateInterface::TYPE_INITIAL, 'properties' => array('deletable' => true, 'editable' => true)), 'proposed' => array('type' => Finite\State\StateInterface::TYPE_NORMAL, 'properties' => array()), 'accepted' => array('type' => Finite\State\StateInterface::TYPE_FINAL, 'properties' => array('printable' => true))), 'transitions' => array('propose' => array('from' => array('draft'), 'to' => 'proposed'), 'accept' => array('from' => array('proposed'), 'to' => 'accepted'), 'reject' => array('from' => array('proposed'), 'to' => 'draft')), 'callbacks' => array('before' => array(array('from' => '-proposed', 'do' => function (Finite\StatefulInterface $document, \Finite\Event\TransitionEvent $e) {
    echo 'Applying transition ' . $e->getTransition()->getName(), "\n";
}), array('from' => 'proposed', 'do' => function () {
    echo 'Applying transition from proposed state', "\n";
})), 'after' => array(array('to' => array('accepted'), 'do' => array($document, 'display'))))));
$loader->load($stateMachine);
$stateMachine->initialize();
$stateMachine->getDispatcher()->addListener(\Finite\Event\FiniteEvents::PRE_TRANSITION, function (\Finite\Event\TransitionEvent $e) {
    echo 'This is a pre transition', "\n";
});
$foobar = 42;
$stateMachine->getDispatcher()->addListener(\Finite\Event\FiniteEvents::POST_TRANSITION, \Finite\Event\Callback\CallbackBuilder::create($stateMachine)->setCallable(function () use($foobar) {
    echo "\$foobar is {$foobar} and this is a post transition\n";
})->getCallback());
$stateMachine->apply('propose');
$stateMachine->apply('reject');
$stateMachine->apply('propose');
$stateMachine->apply('accept');
Exemple #3
0
        return $this->paymentStatus;
    }
    public function setShippingStatus($shippingStatus)
    {
        $this->shippingStatus = $shippingStatus;
    }
    public function getShippingStatus()
    {
        return $this->shippingStatus;
    }
}
$order = new Order();
// Configure the payment graph
$paymentStateMachine = new Finite\StateMachine\StateMachine($order);
$paymentLoader = new Finite\Loader\ArrayLoader(['class' => 'Order', 'property_path' => 'paymentStatus', 'states' => [Order::PAYMENT_PENDING => ['type' => Finite\State\StateInterface::TYPE_INITIAL], Order::PAYMENT_ACCEPTED => ['type' => Finite\State\StateInterface::TYPE_FINAL], Order::PAYMENT_REFUSED => ['type' => Finite\State\StateInterface::TYPE_FINAL]], 'transitions' => ['accept' => ['from' => [Order::PAYMENT_PENDING], 'to' => Order::PAYMENT_ACCEPTED], 'refuse' => ['from' => [Order::PAYMENT_PENDING], 'to' => Order::PAYMENT_REFUSED]]]);
$paymentLoader->load($paymentStateMachine);
$paymentStateMachine->initialize();
// Configure the shipping graph
$shippingStateMachine = new Finite\StateMachine\StateMachine($order);
$shippingLoader = new Finite\Loader\ArrayLoader(['class' => 'Order', 'property_path' => 'shippingStatus', 'states' => [Order::SHIPPING_PENDING => ['type' => Finite\State\StateInterface::TYPE_INITIAL], Order::SHIPPING_PARTIAL => ['type' => Finite\State\StateInterface::TYPE_NORMAL], Order::SHIPPING_SHIPPED => ['type' => Finite\State\StateInterface::TYPE_FINAL]], 'transitions' => ['ship_partially' => ['from' => [Order::SHIPPING_PENDING], 'to' => Order::SHIPPING_PARTIAL], 'ship' => ['from' => [Order::SHIPPING_PENDING, Order::SHIPPING_PARTIAL], 'to' => Order::SHIPPING_SHIPPED]]]);
$shippingLoader->load($shippingStateMachine);
$shippingStateMachine->initialize();
// Working with workflows
// Current state
var_dump($paymentStateMachine->getCurrentState()->getName());
var_dump($paymentStateMachine->getCurrentState()->getProperties());
// Available transitions
var_dump($paymentStateMachine->getCurrentState()->getTransitions());
var_dump($paymentStateMachine->can('accept'));
$paymentStateMachine->apply('accept');
var_dump($paymentStateMachine->getCurrentState()->getName());
 public function __construct(\Akzo\Scheme $scheme)
 {
     $this->_scheme = $scheme;
     parent::__construct($this->_scheme);
     $stateMachineDescription = array('class' => '\\Akzo\\Scheme', 'states' => array(\Akzo\Scheme\State::CREATED => array('type' => \Finite\State\StateInterface::TYPE_INITIAL, 'properties' => array('deletable' => true, 'editable' => true)), \Akzo\Scheme\State::STAGED => array('type' => \Finite\State\StateInterface::TYPE_NORMAL, 'properties' => array('deletable' => true, 'editable' => true)), \Akzo\Scheme\State::TO_BE_REVIEWED => array('type' => \Finite\State\StateInterface::TYPE_NORMAL, 'properties' => array('deletable' => false, 'editable' => false)), \Akzo\Scheme\State::TO_BE_APPROVED => array('type' => \Finite\State\StateInterface::TYPE_NORMAL, 'properties' => array('deletable' => false, 'editable' => false)), \Akzo\Scheme\State::APPROVED => array('type' => \Finite\State\StateInterface::TYPE_FINAL, 'properties' => array('deletable' => false, 'editable' => false)), \Akzo\Scheme\State::UPDATE_REQUESTED => array('type' => \Finite\State\StateInterface::TYPE_NORMAL, 'properties' => array('deletable' => true, 'editable' => true))), 'transitions' => array(\Akzo\Scheme\StateTransition::STAGE_SCHEME => array('from' => array(\Akzo\Scheme\State::CREATED), 'to' => \Akzo\Scheme\State::STAGED), \Akzo\Scheme\StateTransition::UPDATE_STAGED_SCHEME => array('from' => array(\Akzo\Scheme\State::STAGED), 'to' => \Akzo\Scheme\State::STAGED), \Akzo\Scheme\StateTransition::INITIATE_CREATED_SCHEME => array('from' => array(\Akzo\Scheme\State::CREATED), 'to' => \Akzo\Scheme\State::TO_BE_REVIEWED), \Akzo\Scheme\StateTransition::INITIATE_STAGED_SCHEME => array('from' => array(\Akzo\Scheme\State::STAGED), 'to' => \Akzo\Scheme\State::TO_BE_REVIEWED), \Akzo\Scheme\StateTransition::REVIEW_SCHEME => array('from' => array(\Akzo\Scheme\State::TO_BE_REVIEWED), 'to' => \Akzo\Scheme\State::TO_BE_APPROVED), \Akzo\Scheme\StateTransition::APPROVE_SCHEME => array('from' => array(\Akzo\Scheme\State::TO_BE_APPROVED), 'to' => \Akzo\Scheme\State::APPROVED), \Akzo\Scheme\StateTransition::REQUEST_SCHEME_UPDATE => array('from' => array(\Akzo\Scheme\State::TO_BE_REVIEWED, \Akzo\Scheme\State::TO_BE_APPROVED), 'to' => \Akzo\Scheme\State::UPDATE_REQUESTED), \Akzo\Scheme\StateTransition::REQUEST_SCHEME_REVIEW => array('from' => array(\Akzo\Scheme\State::TO_BE_APPROVED), 'to' => \Akzo\Scheme\State::TO_BE_REVIEWED), \Akzo\Scheme\StateTransition::UPDATE_SCHEME => array('from' => array(\Akzo\Scheme\State::UPDATE_REQUESTED), 'to' => \Akzo\Scheme\State::UPDATE_REQUESTED), \Akzo\Scheme\StateTransition::INITIATE_UPDATED_SCHEME => array('from' => array(\Akzo\Scheme\State::UPDATE_REQUESTED), 'to' => \Akzo\Scheme\State::TO_BE_REVIEWED), \Akzo\Scheme\StateTransition::EDIT_APPROVED_SCHEME => array('from' => array(\Akzo\Scheme\State::APPROVED), 'to' => \Akzo\Scheme\State::UPDATE_REQUESTED)), 'callbacks' => array('after' => array(array('from' => \Akzo\Scheme\State::CREATED, 'to' => \Akzo\Scheme\State::STAGED, 'do' => function ($scheme, $event) {
         \Akzo\Scheme\Service::getInstance()->createSaveScheme($scheme);
     }), array('from' => \Akzo\Scheme\State::STAGED, 'to' => \Akzo\Scheme\State::STAGED, 'do' => function ($scheme, $event) {
         \Akzo\Scheme\Service::getInstance()->updateSaveScheme($scheme);
     }), array('from' => \Akzo\Scheme\State::UPDATE_REQUESTED, 'to' => \Akzo\Scheme\State::UPDATE_REQUESTED, 'do' => function ($scheme, $event) {
         \Akzo\Scheme\Service::getInstance()->updateSaveScheme($scheme);
     }), array('from' => \Akzo\Scheme\State::CREATED, 'to' => \Akzo\Scheme\State::TO_BE_REVIEWED, 'do' => function ($scheme, $event) {
         \Akzo\Scheme\Service::getInstance()->createSaveScheme($scheme);
         // TODO: Merge this will the below savePublicTransition function
         // Send notification to initiator, reviewer, approver for sending request for re-review of scheme
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the above sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }), array('from' => \Akzo\Scheme\State::STAGED, 'to' => \Akzo\Scheme\State::TO_BE_REVIEWED, 'do' => function ($scheme, $event) {
         \Akzo\Scheme\Service::getInstance()->updateSaveScheme($scheme);
         // TODO: Merge this will the below savePublicTransition function
         // Send notification to initiator, reviewer, approver for sending request for re-review of scheme
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the above sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }), array('from' => \Akzo\Scheme\State::UPDATE_REQUESTED, 'to' => \Akzo\Scheme\State::TO_BE_REVIEWED, 'do' => function ($scheme, $event) {
         \Akzo\Scheme\Service::getInstance()->updateSaveScheme($scheme);
         // TODO: Merge this will the below savePublicTransition function
         // Send notification to initiator, reviewer, approver for sending request for re-review of scheme
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the above sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }), array('from' => \Akzo\Scheme\State::TO_BE_REVIEWED, 'to' => \Akzo\Scheme\State::UPDATE_REQUESTED, 'do' => function ($scheme, $event) {
         // Just update the state of scheme in the database
         $scheme->save();
         // TODO: Merge this will the below savePublicTransition function
         // Send notification to initiator, reviewer, approver for sending request for re-review of scheme
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the above sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }), array('from' => \Akzo\Scheme\State::TO_BE_REVIEWED, 'to' => \Akzo\Scheme\State::TO_BE_APPROVED, 'do' => function ($scheme, $event) {
         // Just update the state of scheme in the database
         $scheme->save();
         // TODO: Merge this will the below savePublicTransition function
         // Send notification to initiator, reviewer, approver for sending request for re-review of scheme
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the above sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }), array('from' => \Akzo\Scheme\State::TO_BE_APPROVED, 'to' => \Akzo\Scheme\State::UPDATE_REQUESTED, 'do' => function ($scheme, $event) {
         // Just update the state of scheme in the database
         $scheme->save();
         // TODO: Merge this will the below savePublicTransition function
         // Send notification to initiator, reviewer, approver for sending request for re-review of scheme
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the above sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }), array('from' => \Akzo\Scheme\State::TO_BE_APPROVED, 'to' => \Akzo\Scheme\State::TO_BE_REVIEWED, 'do' => function ($scheme, $event) {
         // Just update the state of scheme in the database
         $scheme->save();
         // TODO: Merge this will the below savePublicTransition function
         // Send notification to initiator, reviewer, approver for sending request for re-review of scheme
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the above sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }), array('from' => \Akzo\Scheme\State::TO_BE_APPROVED, 'to' => \Akzo\Scheme\State::APPROVED, 'do' => function ($scheme, $event) {
         // Just update the state of scheme in the database
         $scheme->save();
         $schemeID = $scheme->code;
         $schemeService = \Akzo\Scheme\Service::getInstance();
         $schemeService->initiatePDFCreation($schemeID);
         // Send notification to initiator, reviewer, approver for intimating that scheme has been approved
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }), array('from' => \Akzo\Scheme\State::APPROVED, 'to' => \Akzo\Scheme\State::UPDATE_REQUESTED, 'do' => function ($scheme, $event) {
         // Just update the state of scheme in the database
         $scheme->save();
         // TODO: Merge this will the below savePublicTransition function
         // Send notification to initiator, reviewer, approver for sending request for re-review of scheme
         \Akzo\Scheme\Notifier::sendNotification($scheme, $event->getTransition());
         // TODO: Merge this will the above sendNotification function
         // Save the state transition
         \Akzo\Scheme\Service::getInstance()->savePublicTransition($scheme, $event->getTransition());
     }))));
     $loader = new \Finite\Loader\ArrayLoader($stateMachineDescription);
     $loader->load($this);
     $this->initialize();
 }