PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/plugins/DbFinderPlugin/lib/routing/DbFinderRoute.php

http://opac-sbweb.googlecode.com/
PHP | 240 lines | 187 code | 25 blank | 28 comment | 20 complexity | ce8cc7157b18789291c7a48d9ca727b9 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the DbFinder package.
  4. * (c) Francois Zaninotto <francois.zaninotto@symfony-project.com>
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * DbFinderRoute is a parent class for routes bound to a Model class via DbFinder.
  12. *
  13. * @package DbFinder
  14. * @author Francois Zaninotto <francois.zaninotto@symfony-project.com>
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. abstract class DbFinderRoute extends sfRequestRoute
  18. {
  19. protected
  20. $qs = null,
  21. $finders = array();
  22. /**
  23. * Constructor.
  24. *
  25. * @param string $pattern The pattern to match
  26. * @param array $defaults An array of default parameter values
  27. * @param array $requirements An array of requirements for parameters (regexes)
  28. * @param array $options An array of options
  29. *
  30. * @see sfObjectRoute
  31. */
  32. public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array())
  33. {
  34. $this->pattern = trim($pattern);
  35. $this->defaults = $defaults;
  36. $this->requirements = $requirements;
  37. $this->options = $options;
  38. }
  39. public function getModelOptions($key)
  40. {
  41. if (!array_key_exists($key, $this->options))
  42. {
  43. throw new InvalidArgumentException(sprintf('This route has no %s model defined in its options', $key));
  44. }
  45. return $this->options[$key];
  46. }
  47. public function setModelOptions($key, $options)
  48. {
  49. $this->options[$key] = array_merge($options, $this->options[$key]);
  50. return $this->options[$key];
  51. }
  52. public function setFinder($key, sfModelFinder $finder)
  53. {
  54. if (!$this->isBound())
  55. {
  56. throw new LogicException('The route is not bound.');
  57. }
  58. $this->finders[$key] = $finder;
  59. }
  60. public function getFinder($key)
  61. {
  62. if (!array_key_exists($key, $this->finders))
  63. {
  64. $options = $this->getModelOptions($key);
  65. $finder = DbFinder::from($options['model']);
  66. if (isset($options['finder_methods']))
  67. {
  68. foreach ($options['finder_methods'] as $finder_methods)
  69. {
  70. $finder->$finder_methods();
  71. }
  72. }
  73. $this->finders[$key] = $finder;
  74. }
  75. return $this->finders[$key];
  76. }
  77. public function applyConditions($key)
  78. {
  79. $finder = $this->getFinder($key);
  80. $options = $this->getModelOptions($key);
  81. // Exceptions need to be caught and ignored, so the route must mimick DbFinder::filter() instead of actually using it
  82. foreach ($this->getRealVariables($key) as $variable)
  83. {
  84. $camlVariable = sfInflector::camelize($variable);
  85. $customMethod = 'filterBy' . $camlVariable;
  86. if(method_exists($finder, $customMethod))
  87. {
  88. $finder->$customMethod($this->parameters[$variable]);
  89. }
  90. else
  91. {
  92. try
  93. {
  94. $finder->filterBy($camlVariable, $this->parameters[$variable]);
  95. }
  96. catch (Exception $e)
  97. {
  98. // don't add condition if the variable cannot be mapped to a column
  99. }
  100. }
  101. }
  102. return $this;
  103. }
  104. public function applyFilters($key)
  105. {
  106. $finder = $this->getFinder($key);
  107. $options = $this->getModelOptions($key);
  108. if(array_key_exists('filter_param', $options) &&
  109. array_key_exists($options['filter_param'], $this->getQueryString()))
  110. {
  111. $allowedFilters = array_key_exists('allowed_filters', $options) ? $options['allowed_filters'] : null;
  112. $finder->filter($this->getQueryString($options['filter_param']), true, $allowedFilters);
  113. }
  114. return $this;
  115. }
  116. public function applyOrder($key)
  117. {
  118. $finder = $this->getFinder($key);
  119. $options = $this->getModelOptions($key);
  120. if(array_key_exists('order_param', $options) &&
  121. array_key_exists($options['order_param'], $this->getQueryString()) &&
  122. array_key_exists('key', $this->getQueryString($options['order_param'])))
  123. {
  124. $order = $this->getQueryString($options['order_param']);
  125. if(array_key_exists('order_keys', $options) &&
  126. !in_array($order['key'], $options['order_keys']))
  127. {
  128. continue;
  129. }
  130. $orderColumn = sfInflector::camelize($order['key']);
  131. $orderDirection = isset($order['direction']) ? $order['direction'] : null;
  132. }
  133. elseif(array_key_exists('default_order', $options))
  134. {
  135. $order = $options['default_order'];
  136. $orderColumn = is_array($order) ? sfInflector::camelize($order[0]) : sfInflector::camelize($order);
  137. $orderDirection = (is_array($order) && isset($order[1])) ? $order[1] : null;
  138. }
  139. if(isset($orderColumn))
  140. {
  141. if(method_exists($finder, 'orderBy' . $orderColumn))
  142. {
  143. call_user_func(array($finder, 'orderBy' . $orderColumn), $orderDirection);
  144. }
  145. else
  146. {
  147. $finder->orderBy($orderColumn, $orderDirection);
  148. }
  149. }
  150. return $this;
  151. }
  152. public function getBoundFinder($key)
  153. {
  154. return $this->
  155. applyConditions($key)->
  156. applyFilters($key)->
  157. applyOrder($key)->
  158. getFinder($key);
  159. }
  160. // Why doesn't sfRoute have info on the query string ?
  161. protected function getQueryString($key = null)
  162. {
  163. if ($this->qs == null)
  164. {
  165. $uri = explode('?', $this->context['request_uri']);
  166. if (isset($uri[1]))
  167. {
  168. parse_str($uri[1], $this->qs);
  169. }
  170. else
  171. {
  172. $this->qs = array();
  173. }
  174. }
  175. if (!is_null($key))
  176. {
  177. return $this->qs[$key];
  178. }
  179. else
  180. {
  181. return $this->qs;
  182. }
  183. }
  184. protected function filterParameters($parameters)
  185. {
  186. if (!is_array($parameters))
  187. {
  188. return $parameters;
  189. }
  190. $params = array();
  191. foreach (array_keys($this->variables) as $variable)
  192. {
  193. $params[$variable] = $parameters[$variable];
  194. }
  195. return $params;
  196. }
  197. protected function getRealVariables($key)
  198. {
  199. $options = $this->getModelOptions($key);
  200. if (array_key_exists('filter_variables', $options))
  201. {
  202. return $options['filter_variables'];
  203. }
  204. $variables = array();
  205. foreach (array_keys($this->variables) as $variable)
  206. {
  207. if (0 === strpos($variable, 'sf_') || in_array($variable, array('module', 'action')))
  208. {
  209. continue;
  210. }
  211. $variables[] = $variable;
  212. }
  213. return $variables;
  214. }
  215. }