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

Debug toolbar