sfObjectRouteCollection.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * sfObjectRouteCollection represents a collection of routes bound to objects.
  11. *
  12. * @package symfony
  13. * @subpackage routing
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfObjectRouteCollection.class.php 13325 2008-11-25 08:18:09Z FabianLange $
  16. */
  17. class sfObjectRouteCollection extends sfRouteCollection
  18. {
  19. protected
  20. $routeClass = 'sfObjectRoute';
  21. /**
  22. * Constructor.
  23. *
  24. * @param array $options An array of options
  25. */
  26. public function __construct(array $options)
  27. {
  28. parent::__construct($options);
  29. if (!isset($this->options['model']))
  30. {
  31. throw new InvalidArgumentException(sprintf('You must pass a "model" option to %s ("%s" route)', get_class($this), $this->options['name']));
  32. }
  33. $this->options = array_merge(array(
  34. 'actions' => false,
  35. 'module' => $this->options['name'],
  36. 'prefix_path' => '/'.$this->options['name'],
  37. 'column' => isset($this->options['column']) ? $this->options['column'] : 'id',
  38. 'with_show' => true,
  39. 'segment_names' => array('edit' => 'edit', 'new' => 'new'),
  40. 'model_methods' => array(),
  41. 'with_wildcard_routes' => false,
  42. ), $this->options);
  43. $this->options['requirements'] = array_merge(array($this->options['column'] => '\d+'), $this->options['requirements']);
  44. $this->options['model_methods'] = array_merge(array('list' => null, 'object' => null), $this->options['model_methods']);
  45. if (isset($this->options['route_class']))
  46. {
  47. $this->routeClass = $this->options['route_class'];
  48. }
  49. $this->generateRoutes();
  50. }
  51. protected function generateRoutes()
  52. {
  53. // collection actions
  54. if (isset($this->options['collection_actions']))
  55. {
  56. foreach ($this->options['collection_actions'] as $action => $methods)
  57. {
  58. $this->routes[$this->getRoute($action)] = $this->getRouteForCollection($action, $methods);
  59. }
  60. }
  61. // "standard" actions
  62. $actions = false === $this->options['actions'] ? $this->getDefaultActions() : $this->options['actions'];
  63. foreach ($actions as $action)
  64. {
  65. $method = 'getRouteFor'.ucfirst($action);
  66. if (!method_exists($this, $method))
  67. {
  68. throw new InvalidArgumentException(sprintf('Unable to generate a route for the "%s" action.', $action));
  69. }
  70. $this->routes[$this->getRoute($action)] = $this->$method();
  71. }
  72. // object actions
  73. if (isset($this->options['object_actions']))
  74. {
  75. foreach ($this->options['object_actions'] as $action => $methods)
  76. {
  77. $this->routes[$this->getRoute($action)] = $this->getRouteForObject($action, $methods);
  78. }
  79. }
  80. if ($this->options['with_wildcard_routes'])
  81. {
  82. // wildcard object actions
  83. $this->routes[$this->getRoute('object')] = new $this->routeClass(
  84. sprintf('%s/:%s/:action.:sf_format', $this->options['prefix_path'], $this->options['column']),
  85. array('module' => $this->options['module'], 'sf_format' => 'html'),
  86. array_merge($this->options['requirements'], array('sf_method' => 'get')),
  87. array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object'])
  88. );
  89. // wildcard collection actions
  90. $this->routes[$this->getRoute('collection')] = new $this->routeClass(
  91. sprintf('%s/:action/action.:sf_format', $this->options['prefix_path']),
  92. array('module' => $this->options['module'], 'sf_format' => 'html'),
  93. array_merge($this->options['requirements'], array('sf_method' => 'post')),
  94. array('model' => $this->options['model'], 'type' => 'list', 'method' => $this->options['model_methods']['list'])
  95. );
  96. }
  97. }
  98. protected function getRouteForCollection($action, $methods)
  99. {
  100. return new $this->routeClass(
  101. sprintf('%s/%s.:sf_format', $this->options['prefix_path'], $action),
  102. array('module' => $this->options['module'], 'action' => $action, 'sf_format' => 'html'),
  103. array_merge($this->options['requirements'], array('sf_method' => $methods)),
  104. array('model' => $this->options['model'], 'type' => 'list', 'method' => $this->options['model_methods']['list'])
  105. );
  106. }
  107. protected function getRouteForObject($action, $methods)
  108. {
  109. return new $this->routeClass(
  110. sprintf('%s/:%s/%s.:sf_format', $this->options['prefix_path'], $this->options['column'], $action),
  111. array('module' => $this->options['module'], 'action' => $action, 'sf_format' => 'html'),
  112. array_merge($this->options['requirements'], array('sf_method' => $methods)),
  113. array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object'])
  114. );
  115. }
  116. protected function getRouteForList()
  117. {
  118. return new $this->routeClass(
  119. sprintf('%s.:sf_format', $this->options['prefix_path']),
  120. array('module' => $this->options['module'], 'action' => $this->getActionMethod('list'), 'sf_format' => 'html'),
  121. array_merge($this->options['requirements'], array('sf_method' => 'get')),
  122. array('model' => $this->options['model'], 'type' => 'list', 'method' => $this->options['model_methods']['list'])
  123. );
  124. }
  125. protected function getRouteForNew()
  126. {
  127. return new $this->routeClass(
  128. sprintf('%s/%s.:sf_format', $this->options['prefix_path'], $this->options['segment_names']['new']),
  129. array('module' => $this->options['module'], 'action' => $this->getActionMethod('new'), 'sf_format' => 'html'),
  130. array_merge($this->options['requirements'], array('sf_method' => 'get')),
  131. array('model' => $this->options['model'], 'type' => 'object')
  132. );
  133. }
  134. protected function getRouteForCreate()
  135. {
  136. return new $this->routeClass(
  137. sprintf('%s.:sf_format', $this->options['prefix_path']),
  138. array('module' => $this->options['module'], 'action' => $this->getActionMethod('create'), 'sf_format' => 'html'),
  139. array_merge($this->options['requirements'], array('sf_method' => 'post')),
  140. array('model' => $this->options['model'], 'type' => 'object')
  141. );
  142. }
  143. protected function getRouteForShow()
  144. {
  145. return new $this->routeClass(
  146. sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], $this->options['column']),
  147. array('module' => $this->options['module'], 'action' => $this->getActionMethod('show'), 'sf_format' => 'html'),
  148. array_merge($this->options['requirements'], array('sf_method' => 'get')),
  149. array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object'])
  150. );
  151. }
  152. protected function getRouteForEdit()
  153. {
  154. return new $this->routeClass(
  155. sprintf('%s/:%s/%s.:sf_format', $this->options['prefix_path'], $this->options['column'], $this->options['segment_names']['edit']),
  156. array('module' => $this->options['module'], 'action' => $this->getActionMethod('edit'), 'sf_format' => 'html'),
  157. array_merge($this->options['requirements'], array('sf_method' => 'get')),
  158. array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object'])
  159. );
  160. }
  161. protected function getRouteForUpdate()
  162. {
  163. return new $this->routeClass(
  164. sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], $this->options['column']),
  165. array('module' => $this->options['module'], 'action' => $this->getActionMethod('update'), 'sf_format' => 'html'),
  166. array_merge($this->options['requirements'], array('sf_method' => 'put')),
  167. array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object'])
  168. );
  169. }
  170. protected function getRouteForDelete()
  171. {
  172. return new $this->routeClass(
  173. sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], $this->options['column']),
  174. array('module' => $this->options['module'], 'action' => $this->getActionMethod('delete'), 'sf_format' => 'html'),
  175. array('sf_method' => 'delete'),
  176. array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object'])
  177. );
  178. }
  179. protected function getDefaultActions()
  180. {
  181. $actions = array('list', 'new', 'create', 'edit', 'update', 'delete');
  182. if ($this->options['with_show'])
  183. {
  184. $actions[] = 'show';
  185. }
  186. return $actions;
  187. }
  188. protected function getRoute($action)
  189. {
  190. return 'list' == $action ? $this->options['name'] : $this->options['name'].'_'.$action;
  191. }
  192. protected function getActionMethod($action)
  193. {
  194. return 'list' == $action ? 'index' : $action;
  195. }
  196. }