/protected/components/ezcomponents/Workflow/src/visitors/node_collector.php

https://github.com/kamarulismail/kamarul-playground · PHP · 142 lines · 61 code · 16 blank · 65 comment · 8 complexity · 5e4ca1fc2b14906edd5d3e0bc585f365 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcWorkflowVisitorNodeCollector class.
  4. *
  5. * @package Workflow
  6. * @version 1.4.1
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * Collects all the nodes in a workflow in an array.
  12. *
  13. * @package Workflow
  14. * @version 1.4.1
  15. * @ignore
  16. */
  17. class ezcWorkflowVisitorNodeCollector extends ezcWorkflowVisitor
  18. {
  19. /**
  20. * Holds the start node object.
  21. *
  22. * @var ezcWorkflowNodeStart
  23. */
  24. protected $startNode;
  25. /**
  26. * Holds the default end node object.
  27. *
  28. * @var ezcWorkflowNodeEnd
  29. */
  30. protected $endNode;
  31. /**
  32. * Holds the finally node object.
  33. *
  34. * @var ezcWorkflowNodeFinally
  35. */
  36. protected $finallyNode;
  37. /**
  38. * Flag that indicates whether the finally node has been visited.
  39. *
  40. * @var boolean
  41. */
  42. protected $finallyNodeVisited = false;
  43. /**
  44. * Holds the visited nodes.
  45. *
  46. * @var array(integer=>ezcWorkflowNode)
  47. */
  48. protected $nodes = array();
  49. /**
  50. * Holds the sequence of node ids.
  51. *
  52. * @var integer
  53. */
  54. protected $nextId = 0;
  55. /**
  56. * Flag that indicates whether the node list has been sorted.
  57. *
  58. * @var boolean
  59. */
  60. protected $sorted = false;
  61. /**
  62. * Constructor.
  63. *
  64. * @param ezcWorkflow $workflow
  65. */
  66. public function __construct( ezcWorkflow $workflow )
  67. {
  68. parent::__construct();
  69. $workflow->accept( $this );
  70. }
  71. /**
  72. * Perform the visit.
  73. *
  74. * @param ezcWorkflowVisitable $visitable
  75. */
  76. protected function doVisit( ezcWorkflowVisitable $visitable )
  77. {
  78. if ( $visitable instanceof ezcWorkflow )
  79. {
  80. $visitable->startNode->setId( ++$this->nextId );
  81. $this->startNode = $visitable->startNode;
  82. $visitable->endNode->setId( ++$this->nextId );
  83. $this->endNode = $visitable->endNode;
  84. if ( count( $visitable->finallyNode->getOutNodes() ) > 0 )
  85. {
  86. $this->finallyNode = $visitable->finallyNode;
  87. $visitable->finallyNode->setId( ++$this->nextId );
  88. }
  89. }
  90. else if ( $visitable instanceof ezcWorkflowNode )
  91. {
  92. if ( $visitable !== $this->startNode &&
  93. $visitable !== $this->endNode &&
  94. $visitable !== $this->finallyNode )
  95. {
  96. $id = ++$this->nextId;
  97. $visitable->setId( $id );
  98. }
  99. else
  100. {
  101. $id = $visitable->getId();
  102. }
  103. $this->nodes[$id] = $visitable;
  104. }
  105. }
  106. /**
  107. * Returns the collected nodes.
  108. *
  109. * @return array
  110. */
  111. public function getNodes()
  112. {
  113. if ( $this->finallyNode !== null && !$this->finallyNodeVisited )
  114. {
  115. $this->finallyNode->accept( $this );
  116. $this->finallyNode = true;
  117. }
  118. if ( !$this->sorted )
  119. {
  120. ksort( $this->nodes );
  121. $this->sorted = true;
  122. }
  123. return $this->nodes;
  124. }
  125. }
  126. ?>