simple.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the StateMachine package.
  4. *
  5. * (c) Alexandre Bacco
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require '../vendor/autoload.php';
  11. require 'DomainObject.php';
  12. $config = array(
  13. 'graph' => 'myGraphA', // Name of the current graph - there can be many of them attached to the same object
  14. 'property_path' => 'stateA', // Property path of the object actually holding the state
  15. 'states' => array(
  16. 'checkout',
  17. 'pending',
  18. 'confirmed',
  19. 'cancelled'
  20. ),
  21. 'transitions' => array(
  22. 'create' => array(
  23. 'from' => array('checkout'),
  24. 'to' => 'pending'
  25. ),
  26. 'confirm' => array(
  27. 'from' => array('checkout', 'pending'),
  28. 'to' => 'confirmed'
  29. ),
  30. 'cancel' => array(
  31. 'from' => array('confirmed'),
  32. 'to' => 'cancelled'
  33. )
  34. ),
  35. 'callbacks' => array(
  36. 'guard' => array(
  37. 'guard-cancel' => array(
  38. 'to' => array('cancelled'), // Will be called only for transitions going to this state
  39. 'do' => function() { var_dump('guarding to cancelled state'); return false; }
  40. )
  41. ),
  42. 'before' => array(
  43. 'from-checkout' => array(
  44. 'from' => array('checkout'), // Will be called only for transitions coming from this state
  45. 'do' => function() { var_dump('from checkout transition'); }
  46. )
  47. ),
  48. 'after' => array(
  49. 'on-confirm' => array(
  50. 'on' => array('confirm'), // Will be called only on this transition
  51. 'do' => function() { var_dump('on confirm transition'); }
  52. ),
  53. 'to-cancelled' => array(
  54. 'to' => array('cancelled'), // Will be called only for transitions going to this state
  55. 'do' => function() { var_dump('to cancel transition'); }
  56. ),
  57. 'confirm-date' => array(
  58. 'on' => array('confirm'),
  59. 'do' => array('object', 'setConfirmedNow'), // 'object' will be replaced by the object undergoing the transition
  60. ),
  61. )
  62. )
  63. );
  64. // Our object
  65. $object = new DomainObject;
  66. // State machine is created being given an object and a config
  67. $stateMachine = new \SM\StateMachine\StateMachine($object, $config);
  68. // Current state is checkout
  69. var_dump($stateMachine->getState());
  70. // Return true, we can apply this transition
  71. var_dump($stateMachine->can('create'));
  72. // Return true, this transitions is applied
  73. // In addition, callback 'from-checkout' is called
  74. var_dump($stateMachine->apply('create'));
  75. // Current state is pending
  76. var_dump($stateMachine->getState());
  77. // All possible transitions for pending state are just "confirm"
  78. var_dump($stateMachine->getPossibleTransitions());
  79. // Return false, this transition is not applied
  80. // 2nd argument is soft mode: it returns false instead of throwing an exception
  81. var_dump($stateMachine->apply('cancel', true));
  82. // Current state is still pending
  83. var_dump($stateMachine->getState());
  84. // Return true, this transition is applied
  85. // In addition, callback 'on-confirm' is called
  86. // And callback 'confirm-date' calls the method 'setConfirmedNow' on the object itself
  87. var_dump($stateMachine->apply('confirm'));
  88. // Current state is confirmed
  89. var_dump($stateMachine->getState());
  90. // Returns false, as it is guarded
  91. var_dump($stateMachine->can('cancel'));
  92. // Current state is still confirmed
  93. var_dump($stateMachine->getState());