/code/libraries/koowa/database/behavior/abstract.php

https://github.com/ercanozkaya/nooku-server-base · PHP · 201 lines · 77 code · 23 blank · 101 comment · 7 complexity · 1b067d3a3c6565f3f85371c742282226 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: abstract.php 3690 2011-07-13 00:25:08Z johanjanssens $
  4. * @category Koowa
  5. * @package Koowa_Database
  6. * @subpackage Behavior
  7. * @copyright Copyright (C) 2007 - 2010 Johan Janssens. All rights reserved.
  8. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  9. */
  10. /**
  11. * Abstract Database Behavior
  12. *
  13. * @author Johan Janssens <johan@nooku.org>
  14. * @category Koowa
  15. * @package Koowa_Database
  16. * @subpackage Behavior
  17. */
  18. abstract class KDatabaseBehaviorAbstract extends KMixinAbstract implements KDatabaseBehaviorInterface
  19. {
  20. /**
  21. * The behavior identifier
  22. *
  23. * @var KIdentifierInterface
  24. */
  25. protected $_identifier;
  26. /**
  27. * The behavior priority
  28. *
  29. * @var KIdentifierInterface
  30. */
  31. protected $_priority;
  32. /**
  33. * Constructor.
  34. *
  35. * @param object An optional KConfig object with configuration options
  36. */
  37. public function __construct( KConfig $config = null)
  38. {
  39. $this->_identifier = $config->identifier;
  40. parent::__construct($config);
  41. $this->_priority = $config->priority;
  42. //Automatically mixin the behavior with the mixer (table object)
  43. if($config->auto_mixin) {
  44. $this->mixin($this);
  45. }
  46. }
  47. /**
  48. * Initializes the options for the object
  49. *
  50. * Called from {@link __construct()} as a first step of object instantiation.
  51. *
  52. * @param object An optional KConfig object with configuration options
  53. * @return void
  54. */
  55. protected function _initialize(KConfig $config)
  56. {
  57. $config->append(array(
  58. 'priority' => KCommand::PRIORITY_NORMAL,
  59. 'auto_mixin' => false
  60. ));
  61. parent::_initialize($config);
  62. }
  63. /**
  64. * Get the object identifier
  65. *
  66. * @return KIdentifier
  67. * @see KObjectIdentifiable
  68. */
  69. public function getIdentifier()
  70. {
  71. return $this->_identifier;
  72. }
  73. /**
  74. * Get the priority of a behavior
  75. *
  76. * @return integer The command priority
  77. */
  78. public function getPriority()
  79. {
  80. return $this->_priority;
  81. }
  82. /**
  83. * Command handler
  84. *
  85. * This function transmlated the command name to a command handler function of
  86. * the format '_beforeX[Command]' or '_afterX[Command]. Command handler
  87. * functions should be declared protected.
  88. *
  89. * @param string The command name
  90. * @param object The command context
  91. * @return boolean Can return both true or false.
  92. */
  93. final public function execute( $name, KCommandContext $context)
  94. {
  95. $identifier = clone $context->caller->getIdentifier();
  96. $type = array_pop($identifier->path);
  97. $parts = explode('.', $name);
  98. $method = '_'.$parts[0].ucfirst($type).ucfirst($parts[1]);
  99. if(method_exists($this, $method))
  100. {
  101. if($context->data instanceof KDatabaseRowInterface) {
  102. $this->setMixer($context->data);
  103. }
  104. return $this->$method($context);
  105. }
  106. return true;
  107. }
  108. /**
  109. * Saves the row or rowset in the database.
  110. *
  111. * This function specialises the KDatabaseRow or KDatabaseRowset save
  112. * function and auto-disables the tables command chain to prevent recursive
  113. * looping.
  114. *
  115. * @return KDatabaseRowAbstract or KDatabaseRowsetAbstract
  116. * @see KDatabaseRow::save or KDatabaseRowset::save
  117. */
  118. public function save()
  119. {
  120. $this->getTable()->getCommandChain()->disable();
  121. $this->_mixer->save();
  122. $this->getTable()->getCommandChain()->enable();
  123. return $this->_mixer;
  124. }
  125. /**
  126. * Deletes the row form the database.
  127. *
  128. * This function specialises the KDatabaseRow or KDatabaseRowset delete
  129. * function and auto-disables the tables command chain to prevent recursive
  130. * looping.
  131. *
  132. * @return KDatabaseRowAbstract
  133. */
  134. public function delete()
  135. {
  136. $this->getTable()->getCommandChain()->disable();
  137. $this->_mixer->delete();
  138. $this->getTable()->getCommandChain()->enable();
  139. return $this->_mixer;
  140. }
  141. /**
  142. * Get an object handle
  143. *
  144. * This function only returns a valid handle if one or more command handler
  145. * functions are defined. A commend handler function needs to follow the
  146. * following format : '_afterX[Event]' or '_beforeX[Event]' to be
  147. * recognised.
  148. *
  149. * @return string A string that is unique, or NULL
  150. * @see execute()
  151. */
  152. public function getHandle()
  153. {
  154. $methods = $this->getMethods();
  155. foreach($methods as $method)
  156. {
  157. if(substr($method, 0, 7) == '_before' || substr($method, 0, 6) == '_after') {
  158. return parent::getHandle();
  159. }
  160. }
  161. return null;
  162. }
  163. /**
  164. * Get the methods that are available for mixin based
  165. *
  166. * This function also dynamically adds a function of format is[Behavior]
  167. * to allow client code to check if the behavior is callable.
  168. *
  169. * @param object The mixer requesting the mixable methods.
  170. * @return array An array of methods
  171. */
  172. public function getMixableMethods(KObject $mixer = null)
  173. {
  174. $methods = parent::getMixableMethods($mixer);
  175. $methods[] = 'is'.ucfirst($this->_identifier->name);
  176. return array_diff($methods, array('execute', 'save', 'delete'));
  177. }
  178. }