/libraries/rokcommon/RokCommon/State/Context.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 172 lines · 99 code · 18 blank · 55 comment · 13 complexity · 52ef7eb2c545334779eaaee617ff409d MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: Context.php 53534 2012-06-06 18:21:34Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. *
  8. * Original Copyright below
  9. */
  10. defined('ROKCOMMON') or die;
  11. /**
  12. *
  13. * The contents of this file are subject to the Mozilla Public
  14. * License Version 1.1 (the "License"); you may not use this file
  15. * except in compliance with the License. You may obtain a copy
  16. * of the License at http://www.mozilla.org/MPL/
  17. *
  18. * Software distributed under the License is distributed on an
  19. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  20. * implied. See the License for the specific language governing
  21. * rights and limitations under the License.
  22. *
  23. * The Original Code is State Machine Compiler (SMC).
  24. *
  25. * The Initial Developer of the Original Code is Charles W. Rapp.
  26. * Portions created by Charles W. Rapp are
  27. * Copyright (C) 2005. Charles W. Rapp.
  28. * All Rights Reserved.
  29. *
  30. * Port (from the Python port) to PHP5 by Toni Arnold
  31. *
  32. * Contributor(s):
  33. *
  34. * See: http://smc.sourceforge.net/
  35. *
  36. */
  37. class RokCommon_State_Context
  38. {
  39. protected $_state;
  40. protected $_previous_state;
  41. protected $_state_stack;
  42. protected $_transition;
  43. protected $_debug_flag;
  44. public function __construct($init_state)
  45. {
  46. $this->_state = $init_state;
  47. $this->_previous_state = NULL;
  48. $this->_state_stack = array();
  49. $this->_transition = NULL;
  50. $this->_debug_flag = FALSE;
  51. // using STDERR works only on cli, but explicitly opening
  52. // stderr writes to /var/log/apache2/error_log
  53. $this->_debug_stream = fopen("php://stderr", "w");
  54. }
  55. // Returns the debug flag's current setting.
  56. public function getDebugFlag()
  57. {
  58. return $this->_debug_flag;
  59. }
  60. // Sets the debug flag.
  61. // A true value means debugging is on and false means off.
  62. public function setDebugFlag($flag)
  63. {
  64. $this->_debug_flag = $flag;
  65. }
  66. // Returns the stream to which debug output is written.
  67. public function getDebugStream()
  68. {
  69. return $this->_debug_stream;
  70. }
  71. // Sets the debug output stream.
  72. public function setDebugStream($stream)
  73. {
  74. $this->_debug_stream = $stream;
  75. }
  76. // Is this state machine already inside a transition?
  77. // True if state is undefined.
  78. public function isInTransition()
  79. {
  80. if ($this->_state == NULL)
  81. return TRUE;
  82. else
  83. return FALSE;
  84. }
  85. // Returns the current transition's name.
  86. // Used only for debugging purposes.
  87. public function getTransition()
  88. {
  89. return $this->_transition;
  90. }
  91. // Clears the current state.
  92. public function clearState()
  93. {
  94. $this->_previous_state = $this->_state;
  95. $this->_state = NULL;
  96. }
  97. // Returns the state which a transition left.
  98. // May be Null
  99. public function getPreviousState()
  100. {
  101. return $this->_previous_state;
  102. }
  103. // Sets the current state to the specified state.
  104. public function setState($state)
  105. {
  106. if (!is_subclass_of($state,'RokCommon_State'))
  107. throw new Exception('$state should be of class State');
  108. $this->_state = $state;
  109. if ($this->_debug_flag)
  110. fwrite($this->_debug_stream, "NEW STATE : {$this->_state->getName()}\n");
  111. }
  112. // Returns True if the state stack is empty and False otherwise.
  113. public function isStateStackEmpty()
  114. {
  115. return count($this->_state_stack) == 0;
  116. }
  117. // Returns the state stack's depth.
  118. public function getStateStackDepth()
  119. {
  120. return count($this->_state_stack);
  121. }
  122. // Push the current state on top of the state stack
  123. // and make the specified state the current state.
  124. public function pushState($state)
  125. {
  126. if (!is_subclass_of($state,'RokCommon_State'))
  127. throw new Exception('$state should be of class State');
  128. if ($this->_state != NULL)
  129. array_push($this->_state_stack, $this->_state);
  130. $this->_state = $state;
  131. if ($this->_debug_flag)
  132. fwrite($this->_debug_stream, "PUSH TO STATE: {$this->_state->getName()}\n");
  133. }
  134. // Make the state on top of the state stack the current state.
  135. public function popState()
  136. {
  137. if (count($this->_state_stack) == 0)
  138. {
  139. if ($this->_debug_flag)
  140. fwrite($this->_debug_stream, "POPPING ON EMPTY STATE STACK.\n");
  141. throw new Exception('empty state stack');
  142. } else
  143. {
  144. $this->_state = array_pop($this->_state_stack);
  145. if ($this->_debug_flag)
  146. fwrite($this->_debug_stream, "POP TO STATE : {$this->_state->getName()}\n");
  147. }
  148. }
  149. // Remove all states from the state stack.
  150. public function emptyStateStack()
  151. {
  152. $this->_state_stack = array();
  153. }
  154. }