PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/nooku/libraries/koowa/controller/abstract.php

https://github.com/bhar1red/anahita
PHP | 323 lines | 221 code | 25 blank | 77 comment | 15 complexity | 2c7474a996e752f68844d0dc1911b815 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: abstract.php 4628 2012-05-06 19:56:43Z johanjanssens $
  4. * @package Koowa_Controller
  5. * @copyright Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  6. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  7. * @link http://www.nooku.org
  8. */
  9. /**
  10. * Abstract Controller Class
  11. *
  12. * Note: Concrete controllers must have a singular name
  13. *
  14. * @author Johan Janssens <johan@nooku.org>
  15. * @package Koowa_Controller
  16. * @uses KMixinClass
  17. * @uses KCommandChain
  18. */
  19. abstract class KControllerAbstract extends KObject
  20. {
  21. /**
  22. * Array of class methods to call for a given action.
  23. *
  24. * @var array
  25. */
  26. protected $_action_map = array();
  27. /**
  28. * The class actions
  29. *
  30. * @var array
  31. */
  32. protected $_actions = array();
  33. /**
  34. * Has the controller been dispatched
  35. *
  36. * @var boolean
  37. */
  38. protected $_dispatched;
  39. /**
  40. * The request information
  41. *
  42. * @var array
  43. */
  44. protected $_request = null;
  45. /**
  46. * Constructor.
  47. *
  48. * @param object An optional KConfig object with configuration options.
  49. */
  50. public function __construct( KConfig $config = null)
  51. {
  52. //If no config is passed create it
  53. if(!isset($config)) $config = new KConfig();
  54. parent::__construct($config);
  55. //Set the dispatched state
  56. $this->_dispatched = $config->dispatched;
  57. //Set the mixer in the config
  58. $config->mixer = $this;
  59. // Mixin the command interface
  60. $this->mixin(new KMixinCommand($config));
  61. // Mixin the behavior interface
  62. $this->mixin(new KMixinBehavior($config));
  63. //Set the request
  64. $this->setRequest((array) KConfig::unbox($config->request));
  65. }
  66. /**
  67. * Initializes the default configuration for the object
  68. *
  69. * Called from {@link __construct()} as a first step of object instantiation.
  70. *
  71. * @param object An optional KConfig object with configuration options.
  72. * @return void
  73. */
  74. protected function _initialize(KConfig $config)
  75. {
  76. $config->append(array(
  77. 'command_chain' => $this->getService('koowa:command.chain'),
  78. 'dispatch_events' => true,
  79. 'event_dispatcher' => $this->getService('koowa:event.dispatcher'),
  80. 'enable_callbacks' => true,
  81. 'dispatched' => false,
  82. 'request' => null,
  83. 'behaviors' => array(),
  84. ));
  85. parent::_initialize($config);
  86. }
  87. /**
  88. * Has the controller been dispatched
  89. *
  90. * @return boolean Returns true if the controller has been dispatched
  91. */
  92. public function isDispatched()
  93. {
  94. return $this->_dispatched;
  95. }
  96. /**
  97. * Execute an action by triggering a method in the derived class.
  98. *
  99. * @param string The action to execute
  100. * @param object A command context object
  101. * @return mixed|false The value returned by the called method, false in error case.
  102. * @throws KControllerException
  103. */
  104. public function execute($action, KCommandContext $context)
  105. {
  106. $action = strtolower($action);
  107. //Update the context
  108. $context->action = $action;
  109. $context->caller = $this;
  110. //Find the mapped action
  111. if (isset( $this->_action_map[$action] )) {
  112. $command = $this->_action_map[$action];
  113. } else {
  114. $command = $action;
  115. }
  116. //Execute the action
  117. if($this->getCommandChain()->run('before.'.$command, $context) !== false)
  118. {
  119. $method = '_action'.ucfirst($command);
  120. if(!method_exists($this, $method))
  121. {
  122. if(isset($this->_mixed_methods[$command])) {
  123. $context->result = $this->_mixed_methods[$command]->execute('action.'.$command, $context);
  124. } else {
  125. throw new KControllerException("Can't execute '$command', method: '$method' does not exist");
  126. }
  127. }
  128. else $context->result = $this->$method($context);
  129. $this->getCommandChain()->run('after.'.$command, $context);
  130. }
  131. return $context->result;
  132. }
  133. /**
  134. * Mixin an object
  135. *
  136. * @param object An object that implements KMinxInterface
  137. * @return KObject
  138. */
  139. public function mixin(KMixinInterface $object)
  140. {
  141. if($object instanceof KControllerBehaviorAbstract)
  142. {
  143. foreach($object->getMethods() as $method)
  144. {
  145. if(substr($method, 0, 7) == '_action') {
  146. $this->_actions[] = strtolower(substr($method, 7));
  147. }
  148. }
  149. $this->_actions = array_unique(array_merge($this->_actions, array_keys($this->_action_map)));
  150. }
  151. return parent::mixin($object);
  152. }
  153. /**
  154. * Gets the available actions in the controller.
  155. *
  156. * @return array Array[i] of action names.
  157. */
  158. public function getActions()
  159. {
  160. if(!$this->_actions)
  161. {
  162. $this->_actions = array();
  163. foreach($this->getMethods() as $method)
  164. {
  165. if(substr($method, 0, 7) == '_action') {
  166. $this->_actions[] = strtolower(substr($method, 7));
  167. }
  168. }
  169. $this->_actions = array_unique(array_merge($this->_actions, array_keys($this->_action_map)));
  170. }
  171. return $this->_actions;
  172. }
  173. /**
  174. * Get the request information
  175. *
  176. * @return KConfig A KConfig object with request information
  177. */
  178. public function getRequest()
  179. {
  180. return $this->_request;
  181. }
  182. /**
  183. * Set the request information
  184. *
  185. * @param array An associative array of request information
  186. * @return KControllerBread
  187. */
  188. public function setRequest(array $request)
  189. {
  190. $this->_request = new KConfig();
  191. foreach($request as $key => $value) {
  192. $this->$key = $value;
  193. }
  194. return $this;
  195. }
  196. /**
  197. * Register (map) an action to a method in the class.
  198. *
  199. * @param string The action.
  200. * @param string The name of the method in the derived class to perform
  201. * for this action.
  202. * @return KControllerAbstract
  203. */
  204. public function registerActionAlias( $alias, $action )
  205. {
  206. $alias = strtolower( $alias );
  207. if ( !in_array($alias, $this->getActions()) ) {
  208. $this->_action_map[$alias] = $action;
  209. }
  210. //Force reload of the actions
  211. $this->_actions = array_unique(array_merge($this->_actions, array_keys($this->_action_map)));
  212. return $this;
  213. }
  214. /**
  215. * Set a request properties
  216. *
  217. * @param string The property name.
  218. * @param mixed The property value.
  219. */
  220. public function __set($property, $value)
  221. {
  222. $this->_request->$property = $value;
  223. }
  224. /**
  225. * Get a request property
  226. *
  227. * @param string The property name.
  228. * @return string The property value.
  229. */
  230. public function __get($property)
  231. {
  232. $result = null;
  233. if(isset($this->_request->$property)) {
  234. $result = $this->_request->$property;
  235. }
  236. return $result;
  237. }
  238. /**
  239. * Execute a controller action by it's name.
  240. *
  241. * Function is also capable of checking is a behavior has been mixed succesfully
  242. * using is[Behavior] function. If the behavior exists the function will return
  243. * TRUE, otherwise FALSE.
  244. *
  245. * @param string Method name
  246. * @param array Array containing all the arguments for the original call
  247. * @see execute()
  248. */
  249. public function __call($method, $args)
  250. {
  251. //Handle action alias method
  252. if(in_array($method, $this->getActions()))
  253. {
  254. //Get the data
  255. $data = !empty($args) ? $args[0] : array();
  256. //Create a context object
  257. if(!($data instanceof KCommandContext))
  258. {
  259. $context = $this->getCommandContext();
  260. $context->data = $data;
  261. $context->result = false;
  262. }
  263. else $context = $data;
  264. //Execute the action
  265. return $this->execute($method, $context);
  266. }
  267. //Check if a behavior is mixed
  268. $parts = KInflector::explode($method);
  269. if($parts[0] == 'is' && isset($parts[1]))
  270. {
  271. if(!isset($this->_mixed_methods[$method])) {
  272. return false;
  273. }
  274. return true;
  275. }
  276. return parent::__call($method, $args);
  277. }
  278. }