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

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/behavior/SfPropelBehaviorSymfonyBehaviors.php

https://github.com/rickycheers/SHARCK-ERP
PHP | 342 lines | 216 code | 59 blank | 67 comment | 22 complexity | 42537835b63ba6f2569b5a863683a31e MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0, ISC
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Adds support for symfony's {@link sfMixer} behaviors.
  11. *
  12. * @package sfPropelPlugin
  13. * @subpackage behavior
  14. * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  15. * @version SVN: $Id: SfPropelBehaviorSymfonyBehaviors.php 28958 2010-04-01 13:56:17Z fabien $
  16. */
  17. class SfPropelBehaviorSymfonyBehaviors extends SfPropelBehaviorBase
  18. {
  19. public function preDelete()
  20. {
  21. if ($this->isDisabled())
  22. {
  23. return;
  24. }
  25. return <<<EOF
  26. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}:delete:pre') as \$callable)
  27. {
  28. if (call_user_func(\$callable, \$this, \$con))
  29. {
  30. \$con->commit();
  31. return;
  32. }
  33. }
  34. EOF;
  35. }
  36. public function postDelete()
  37. {
  38. if ($this->isDisabled())
  39. {
  40. return;
  41. }
  42. return <<<EOF
  43. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}:delete:post') as \$callable)
  44. {
  45. call_user_func(\$callable, \$this, \$con);
  46. }
  47. EOF;
  48. }
  49. public function preSave()
  50. {
  51. if ($this->isDisabled())
  52. {
  53. return;
  54. }
  55. return <<<EOF
  56. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}:save:pre') as \$callable)
  57. {
  58. if (is_integer(\$affectedRows = call_user_func(\$callable, \$this, \$con)))
  59. {
  60. \$con->commit();
  61. return \$affectedRows;
  62. }
  63. }
  64. EOF;
  65. }
  66. public function postSave()
  67. {
  68. if ($this->isDisabled())
  69. {
  70. return;
  71. }
  72. return <<<EOF
  73. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}:save:post') as \$callable)
  74. {
  75. call_user_func(\$callable, \$this, \$con, \$affectedRows);
  76. }
  77. EOF;
  78. }
  79. public function objectMethods()
  80. {
  81. if ($this->isDisabled())
  82. {
  83. return;
  84. }
  85. return <<<EOF
  86. /**
  87. * Calls methods defined via {@link sfMixer}.
  88. */
  89. public function __call(\$method, \$arguments)
  90. {
  91. if (!\$callable = sfMixer::getCallable('Base{$this->getTable()->getPhpName()}:'.\$method))
  92. {
  93. throw new sfException(sprintf('Call to undefined method Base{$this->getTable()->getPhpName()}::%s', \$method));
  94. }
  95. array_unshift(\$arguments, \$this);
  96. return call_user_func_array(\$callable, \$arguments);
  97. }
  98. EOF;
  99. }
  100. public function staticMethods()
  101. {
  102. if ($this->isDisabled())
  103. {
  104. return;
  105. }
  106. return <<<EOF
  107. /**
  108. * Returns the name of the hook to call from inside the supplied method.
  109. *
  110. * @param string \$method The calling method
  111. *
  112. * @return string A hook name for {@link sfMixer}
  113. *
  114. * @throws LogicException If the method name is not recognized
  115. */
  116. static private function getMixerPreSelectHook(\$method)
  117. {
  118. if (preg_match('/^do(Select|Count)(Join(All(Except)?)?|Stmt)?/', \$method, \$match))
  119. {
  120. return sprintf('Base{$this->getTable()->getPhpName()}Peer:%s:%1\$s', 'Count' == \$match[1] ? 'doCount' : \$match[0]);
  121. }
  122. throw new LogicException(sprintf('Unrecognized function "%s"', \$method));
  123. }
  124. EOF;
  125. }
  126. public function preSelect()
  127. {
  128. if ($this->isDisabled())
  129. {
  130. return;
  131. }
  132. return <<<EOF
  133. foreach (sfMixer::getCallables(self::getMixerPreSelectHook(__FUNCTION__)) as \$sf_hook)
  134. {
  135. call_user_func(\$sf_hook, 'Base{$this->getTable()->getPhpName()}Peer', \$criteria, \$con);
  136. }
  137. EOF;
  138. }
  139. public function objectFilter(& $script)
  140. {
  141. if ($this->isDisabled())
  142. {
  143. return;
  144. }
  145. if ($this->getTable()->getAttribute('behaviors'))
  146. {
  147. $script .= $this->getBehaviorsInclude();
  148. }
  149. }
  150. public function peerFilter(& $script)
  151. {
  152. if ($this->isDisabled())
  153. {
  154. return;
  155. }
  156. $doInsertPre = <<<EOF
  157. // symfony_behaviors behavior
  158. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}Peer:doInsert:pre') as \$sf_hook)
  159. {
  160. if (false !== \$sf_hook_retval = call_user_func(\$sf_hook, 'Base{$this->getTable()->getPhpName()}Peer', \$values, \$con))
  161. {
  162. return \$sf_hook_retval;
  163. }
  164. }
  165. EOF;
  166. $doUpdatePre = <<<EOF
  167. // symfony_behaviors behavior
  168. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}Peer:doUpdate:pre') as \$sf_hook)
  169. {
  170. if (false !== \$sf_hook_retval = call_user_func(\$sf_hook, 'Base{$this->getTable()->getPhpName()}Peer', \$values, \$con))
  171. {
  172. return \$sf_hook_retval;
  173. }
  174. }
  175. EOF;
  176. // add doInsert and doUpdate hooks
  177. $class = new sfClassManipulator($script);
  178. $class->filterMethod('doInsert', array($this, 'filterDoInsert'));
  179. $class->wrapMethod('doInsert', $doInsertPre);
  180. $class->filterMethod('doUpdate', array($this, 'filterDoUpdate'));
  181. $class->wrapMethod('doUpdate', $doUpdatePre);
  182. $script = $class->getCode();
  183. // add symfony behavior configuration file
  184. if ($this->createBehaviorsFile())
  185. {
  186. $script .= $this->getBehaviorsInclude();
  187. }
  188. }
  189. /**
  190. * Filters the generated doInsert method.
  191. *
  192. * @param string $line
  193. *
  194. * @return string
  195. */
  196. public function filterDoInsert($line)
  197. {
  198. if (false !== strpos($line, 'return'))
  199. {
  200. $doInsertPost = <<<EOF
  201. // symfony_behaviors behavior
  202. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}Peer:doInsert:post') as \$sf_hook)
  203. {
  204. call_user_func(\$sf_hook, 'Base{$this->getTable()->getPhpName()}Peer', \$values, \$con, \$pk);
  205. }
  206. EOF;
  207. $line = $doInsertPost.$line;
  208. }
  209. return $line;
  210. }
  211. /**
  212. * Filters the generated doUpdate method.
  213. *
  214. * @param string $line
  215. *
  216. * @return string
  217. */
  218. public function filterDoUpdate($line)
  219. {
  220. if (false !== strpos($line, 'return'))
  221. {
  222. $replace = str_replace('return', '$ret =', $line);
  223. $doUpdatePost = <<<EOF
  224. // symfony_behaviors behavior
  225. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}Peer:doUpdate:post') as \$sf_hook)
  226. {
  227. call_user_func(\$sf_hook, 'Base{$this->getTable()->getPhpName()}Peer', \$values, \$con, \$ret);
  228. }
  229. return \$ret;
  230. EOF;
  231. $line = $replace.$doUpdatePost;
  232. }
  233. return $line;
  234. }
  235. /**
  236. * Creates the current model's behaviors configuration file.
  237. *
  238. * Any existing behaviors file will be either deleted or overwritten.
  239. *
  240. * @return boolean Returns true if the model has behaviors
  241. */
  242. protected function createBehaviorsFile()
  243. {
  244. $file = $this->getBehaviorsFilePath(true);
  245. if (file_exists($file))
  246. {
  247. unlink($file);
  248. }
  249. if ($behaviors = $this->getTable()->getAttribute('behaviors'))
  250. {
  251. $code = <<<EOF
  252. <?php
  253. sfPropelBehavior::add('{$this->getTable()->getPhpName()}', %s);
  254. EOF;
  255. file_put_contents($file, sprintf($code, var_export(unserialize($behaviors), true)));
  256. return true;
  257. }
  258. }
  259. /**
  260. * Returns PHP code for including the current model's behaviors configuration file.
  261. *
  262. * @return string
  263. */
  264. protected function getBehaviorsInclude()
  265. {
  266. return <<<EOF
  267. // symfony_behaviors behavior
  268. include_once '{$this->getBehaviorsFilePath()}';
  269. EOF;
  270. }
  271. /**
  272. * Returns the path to the current model's behaviors configuration file.
  273. *
  274. * @param boolean $absolute
  275. *
  276. * @return string
  277. */
  278. protected function getBehaviorsFilePath($absolute = false)
  279. {
  280. $base = $absolute ? sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR : '';
  281. return $base.ClassTools::getFilePath($this->getTable()->getPackage().'.om', sprintf('Base%sBehaviors', $this->getTable()->getPhpName()));
  282. }
  283. }