| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
'default_params' => array(), |
|---|
| 49 |
), $this->options); |
|---|
| 50 |
|
|---|
| 51 |
$this->options['requirements'] = array_merge(array($this->options['column'] => 'id' == $this->options['column'] ? '\d+' : null), $this->options['requirements']); |
|---|
| 52 |
$this->options['model_methods'] = array_merge(array('list' => null, 'object' => null), $this->options['model_methods']); |
|---|
| 53 |
|
|---|
| 54 |
if (isset($this->options['route_class'])) |
|---|
| 55 |
{ |
|---|
| 56 |
$this->routeClass = $this->options['route_class']; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
$this->generateRoutes(); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
protected function generateRoutes() |
|---|
| 63 |
{ |
|---|
| 64 |
|
|---|
| 65 |
if (isset($this->options['collection_actions'])) |
|---|
| 66 |
{ |
|---|
| 67 |
foreach ($this->options['collection_actions'] as $action => $methods) |
|---|
| 68 |
{ |
|---|
| 69 |
$this->routes[$this->getRoute($action)] = $this->getRouteForCollection($action, $methods); |
|---|
| 70 |
} |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
$actions = false === $this->options['actions'] ? $this->getDefaultActions() : $this->options['actions']; |
|---|
| 75 |
foreach ($actions as $action) |
|---|
| 76 |
{ |
|---|
| 77 |
$method = 'getRouteFor'.ucfirst($action); |
|---|
| 78 |
if (!method_exists($this, $method)) |
|---|
| 79 |
{ |
|---|
| 80 |
throw new InvalidArgumentException(sprintf('Unable to generate a route for the "%s" action.', $action)); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
$this->routes[$this->getRoute($action)] = $this->$method(); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
if (isset($this->options['object_actions'])) |
|---|
| 88 |
{ |
|---|
| 89 |
foreach ($this->options['object_actions'] as $action => $methods) |
|---|
| 90 |
{ |
|---|
| 91 |
$this->routes[$this->getRoute($action)] = $this->getRouteForObject($action, $methods); |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
if ($this->options['with_wildcard_routes']) |
|---|
| 96 |
{ |
|---|
| 97 |
|
|---|
| 98 |
$this->routes[$this->getRoute('object')] = new $this->routeClass( |
|---|
| 99 |
sprintf('%s/:%s/:action.:sf_format', $this->options['prefix_path'], $this->options['column']), |
|---|
| 100 |
array_merge(array('module' => $this->options['module'], 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 101 |
array_merge($this->options['requirements'], array('sf_method' => array('get', 'head'))), |
|---|
| 102 |
array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object']) |
|---|
| 103 |
); |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
$this->routes[$this->getRoute('collection')] = new $this->routeClass( |
|---|
| 107 |
sprintf('%s/:action/action.:sf_format', $this->options['prefix_path']), |
|---|
| 108 |
array_merge(array('module' => $this->options['module'], 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 109 |
array_merge($this->options['requirements'], array('sf_method' => 'post')), |
|---|
| 110 |
array('model' => $this->options['model'], 'type' => 'list', 'method' => $this->options['model_methods']['list']) |
|---|
| 111 |
); |
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
protected function getRouteForCollection($action, $methods) |
|---|
| 116 |
{ |
|---|
| 117 |
return new $this->routeClass( |
|---|
| 118 |
sprintf('%s/%s.:sf_format', $this->options['prefix_path'], $action), |
|---|
| 119 |
array_merge(array('module' => $this->options['module'], 'action' => $action, 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 120 |
array_merge($this->options['requirements'], array('sf_method' => $methods)), |
|---|
| 121 |
array('model' => $this->options['model'], 'type' => 'list', 'method' => $this->options['model_methods']['list']) |
|---|
| 122 |
); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
protected function getRouteForObject($action, $methods) |
|---|
| 126 |
{ |
|---|
| 127 |
return new $this->routeClass( |
|---|
| 128 |
sprintf('%s/:%s/%s.:sf_format', $this->options['prefix_path'], $this->options['column'], $action), |
|---|
| 129 |
array_merge(array('module' => $this->options['module'], 'action' => $action, 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 130 |
array_merge($this->options['requirements'], array('sf_method' => $methods)), |
|---|
| 131 |
array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object']) |
|---|
| 132 |
); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
protected function getRouteForList() |
|---|
| 136 |
{ |
|---|
| 137 |
return new $this->routeClass( |
|---|
| 138 |
sprintf('%s.:sf_format', $this->options['prefix_path']), |
|---|
| 139 |
array_merge(array('module' => $this->options['module'], 'action' => $this->getActionMethod('list'), 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 140 |
array_merge($this->options['requirements'], array('sf_method' => array('get', 'head'))), |
|---|
| 141 |
array('model' => $this->options['model'], 'type' => 'list', 'method' => $this->options['model_methods']['list']) |
|---|
| 142 |
); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
protected function getRouteForNew() |
|---|
| 146 |
{ |
|---|
| 147 |
return new $this->routeClass( |
|---|
| 148 |
sprintf('%s/%s.:sf_format', $this->options['prefix_path'], $this->options['segment_names']['new']), |
|---|
| 149 |
array_merge(array('module' => $this->options['module'], 'action' => $this->getActionMethod('new'), 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 150 |
array_merge($this->options['requirements'], array('sf_method' => array('get', 'head'))), |
|---|
| 151 |
array('model' => $this->options['model'], 'type' => 'object') |
|---|
| 152 |
); |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
protected function getRouteForCreate() |
|---|
| 156 |
{ |
|---|
| 157 |
return new $this->routeClass( |
|---|
| 158 |
sprintf('%s.:sf_format', $this->options['prefix_path']), |
|---|
| 159 |
array_merge(array('module' => $this->options['module'], 'action' => $this->getActionMethod('create'), 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 160 |
array_merge($this->options['requirements'], array('sf_method' => 'post')), |
|---|
| 161 |
array('model' => $this->options['model'], 'type' => 'object') |
|---|
| 162 |
); |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
protected function getRouteForShow() |
|---|
| 166 |
{ |
|---|
| 167 |
return new $this->routeClass( |
|---|
| 168 |
sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], $this->options['column']), |
|---|
| 169 |
array_merge(array('module' => $this->options['module'], 'action' => $this->getActionMethod('show'), 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 170 |
array_merge($this->options['requirements'], array('sf_method' => array('get', 'head'))), |
|---|
| 171 |
array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object']) |
|---|
| 172 |
); |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
protected function getRouteForEdit() |
|---|
| 176 |
{ |
|---|
| 177 |
return new $this->routeClass( |
|---|
| 178 |
sprintf('%s/:%s/%s.:sf_format', $this->options['prefix_path'], $this->options['column'], $this->options['segment_names']['edit']), |
|---|
| 179 |
array_merge(array('module' => $this->options['module'], 'action' => $this->getActionMethod('edit'), 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 180 |
array_merge($this->options['requirements'], array('sf_method' => array('get', 'head'))), |
|---|
| 181 |
array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object']) |
|---|
| 182 |
); |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
protected function getRouteForUpdate() |
|---|
| 186 |
{ |
|---|
| 187 |
return new $this->routeClass( |
|---|
| 188 |
sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], $this->options['column']), |
|---|
| 189 |
array_merge(array('module' => $this->options['module'], 'action' => $this->getActionMethod('update'), 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 190 |
array_merge($this->options['requirements'], array('sf_method' => 'put')), |
|---|
| 191 |
array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object']) |
|---|
| 192 |
); |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
protected function getRouteForDelete() |
|---|
| 196 |
{ |
|---|
| 197 |
return new $this->routeClass( |
|---|
| 198 |
sprintf('%s/:%s.:sf_format', $this->options['prefix_path'], $this->options['column']), |
|---|
| 199 |
array_merge(array('module' => $this->options['module'], 'action' => $this->getActionMethod('delete'), 'sf_format' => 'html'), $this->options['default_params']), |
|---|
| 200 |
array_merge($this->options['requirements'], array('sf_method' => 'delete')), |
|---|
| 201 |
array('model' => $this->options['model'], 'type' => 'object', 'method' => $this->options['model_methods']['object']) |
|---|
| 202 |
); |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
protected function getDefaultActions() |
|---|
| 206 |
{ |
|---|
| 207 |
$actions = array('list', 'new', 'create', 'edit', 'update', 'delete'); |
|---|
| 208 |
|
|---|
| 209 |
if ($this->options['with_show']) |
|---|
| 210 |
{ |
|---|
| 211 |
$actions[] = 'show'; |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
return $actions; |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
protected function getRoute($action) |
|---|
| 218 |
{ |
|---|
| 219 |
return 'list' == $action ? $this->options['name'] : $this->options['name'].'_'.$action; |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
protected function getActionMethod($action) |
|---|
| 223 |
{ |
|---|
| 224 |
return 'list' == $action ? 'index' : $action; |
|---|
| 225 |
} |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|