Development

/branches/1.2/lib/routing/sfObjectRouteCollection.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/routing/sfObjectRouteCollection.class.php

Revision 24591, 8.7 kB (checked in by FabianLange, 5 years ago)

[1.2, 1.3, 1.4] added requirements to DELETE action of sfObjectRouteCollection.class.php (fixes #7634)

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