| 1 |
lars |
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 |
require_once(dirname(__FILE__).'/sfPropelBaseTask.class.php');
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Generates a Propel admin module.
|
|
|
15 |
*
|
|
|
16 |
* @package symfony
|
|
|
17 |
* @subpackage propel
|
|
|
18 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
19 |
* @version SVN: $Id: sfPropelGenerateAdminTask.class.php 28809 2010-03-26 17:19:58Z Jonathan.Wage $
|
|
|
20 |
*/
|
|
|
21 |
class sfPropelGenerateAdminTask extends sfPropelBaseTask
|
|
|
22 |
{
|
|
|
23 |
/**
|
|
|
24 |
* @see sfTask
|
|
|
25 |
*/
|
|
|
26 |
protected function configure()
|
|
|
27 |
{
|
|
|
28 |
$this->addArguments(array(
|
|
|
29 |
new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
|
|
|
30 |
new sfCommandArgument('route_or_model', sfCommandArgument::REQUIRED, 'The route name or the model class'),
|
|
|
31 |
));
|
|
|
32 |
|
|
|
33 |
$this->addOptions(array(
|
|
|
34 |
new sfCommandOption('module', null, sfCommandOption::PARAMETER_REQUIRED, 'The module name', null),
|
|
|
35 |
new sfCommandOption('theme', null, sfCommandOption::PARAMETER_REQUIRED, 'The theme name', 'admin'),
|
|
|
36 |
new sfCommandOption('singular', null, sfCommandOption::PARAMETER_REQUIRED, 'The singular name', null),
|
|
|
37 |
new sfCommandOption('plural', null, sfCommandOption::PARAMETER_REQUIRED, 'The plural name', null),
|
|
|
38 |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
|
|
|
39 |
new sfCommandOption('actions-base-class', null, sfCommandOption::PARAMETER_REQUIRED, 'The base class for the actions', 'sfActions'),
|
|
|
40 |
));
|
|
|
41 |
|
|
|
42 |
$this->namespace = 'propel';
|
|
|
43 |
$this->name = 'generate-admin';
|
|
|
44 |
$this->briefDescription = 'Generates a Propel admin module';
|
|
|
45 |
|
|
|
46 |
$this->detailedDescription = <<<EOF
|
|
|
47 |
The [propel:generate-admin|INFO] task generates a Propel admin module:
|
|
|
48 |
|
|
|
49 |
[./symfony propel:generate-admin frontend Article|INFO]
|
|
|
50 |
|
|
|
51 |
The task creates a module in the [%frontend%|COMMENT] application for the
|
|
|
52 |
[%Article%|COMMENT] model.
|
|
|
53 |
|
|
|
54 |
The task creates a route for you in the application [routing.yml|COMMENT].
|
|
|
55 |
|
|
|
56 |
You can also generate a Propel admin module by passing a route name:
|
|
|
57 |
|
|
|
58 |
[./symfony propel:generate-admin frontend article|INFO]
|
|
|
59 |
|
|
|
60 |
The task creates a module in the [%frontend%|COMMENT] application for the
|
|
|
61 |
[%article%|COMMENT] route definition found in [routing.yml|COMMENT].
|
|
|
62 |
|
|
|
63 |
For the filters and batch actions to work properly, you need to add
|
|
|
64 |
the [with_wildcard_routes|COMMENT] option to the route:
|
|
|
65 |
|
|
|
66 |
article:
|
|
|
67 |
class: sfPropelRouteCollection
|
|
|
68 |
options:
|
|
|
69 |
model: Article
|
|
|
70 |
with_wildcard_routes: true
|
|
|
71 |
EOF;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* @see sfTask
|
|
|
76 |
*/
|
|
|
77 |
protected function execute($arguments = array(), $options = array())
|
|
|
78 |
{
|
|
|
79 |
// get configuration for the given route
|
|
|
80 |
if (false !== ($route = $this->getRouteFromName($arguments['route_or_model'])))
|
|
|
81 |
{
|
|
|
82 |
$arguments['route'] = $route;
|
|
|
83 |
$arguments['route_name'] = $arguments['route_or_model'];
|
|
|
84 |
|
|
|
85 |
return $this->generateForRoute($arguments, $options);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// is it a model class name
|
|
|
89 |
if (!class_exists($arguments['route_or_model']))
|
|
|
90 |
{
|
|
|
91 |
throw new sfCommandException(sprintf('The route "%s" does not exist and there is no "%s" class.', $arguments['route_or_model'], $arguments['route_or_model']));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$r = new ReflectionClass($arguments['route_or_model']);
|
|
|
95 |
if (!$r->isSubclassOf('BaseObject'))
|
|
|
96 |
{
|
|
|
97 |
throw new sfCommandException(sprintf('"%s" is not a Propel class.', $arguments['route_or_model']));
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
// create a route
|
|
|
101 |
$model = $arguments['route_or_model'];
|
|
|
102 |
$name = strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), '\\1_\\2', $model));
|
|
|
103 |
|
|
|
104 |
if (isset($options['module']))
|
|
|
105 |
{
|
|
|
106 |
$route = $this->getRouteFromName($name);
|
|
|
107 |
if ($route && !$this->checkRoute($route, $model, $options['module']))
|
|
|
108 |
{
|
|
|
109 |
$name .= '_'.$options['module'];
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
$routing = sfConfig::get('sf_app_config_dir').'/routing.yml';
|
|
|
114 |
$content = file_get_contents($routing);
|
|
|
115 |
$routesArray = sfYaml::load($content);
|
|
|
116 |
|
|
|
117 |
if (!isset($routesArray[$name]))
|
|
|
118 |
{
|
|
|
119 |
$primaryKey = $this->getPrimaryKey($model);
|
|
|
120 |
$module = $options['module'] ? $options['module'] : $name;
|
|
|
121 |
$content = sprintf(<<<EOF
|
|
|
122 |
%s:
|
|
|
123 |
class: sfPropelRouteCollection
|
|
|
124 |
options:
|
|
|
125 |
model: %s
|
|
|
126 |
module: %s
|
|
|
127 |
prefix_path: /%s
|
|
|
128 |
column: %s
|
|
|
129 |
with_wildcard_routes: true
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
EOF
|
|
|
133 |
, $name, $model, $module, isset($options['plural']) ? $options['plural'] : $module, $primaryKey).$content;
|
|
|
134 |
|
|
|
135 |
$this->logSection('file+', $routing);
|
|
|
136 |
|
|
|
137 |
if (false === file_put_contents($routing, $content))
|
|
|
138 |
{
|
|
|
139 |
throw new sfCommandException(sprintf('Unable to write to file, %s.', $routing));
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
$arguments['route'] = $this->getRouteFromName($name);
|
|
|
144 |
$arguments['route_name'] = $name;
|
|
|
145 |
|
|
|
146 |
return $this->generateForRoute($arguments, $options);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
protected function generateForRoute($arguments, $options)
|
|
|
150 |
{
|
|
|
151 |
$routeOptions = $arguments['route']->getOptions();
|
|
|
152 |
|
|
|
153 |
if (!$arguments['route'] instanceof sfPropelRouteCollection)
|
|
|
154 |
{
|
|
|
155 |
throw new sfCommandException(sprintf('The route "%s" is not a Propel collection route.', $arguments['route_name']));
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
$module = $routeOptions['module'];
|
|
|
159 |
$model = $routeOptions['model'];
|
|
|
160 |
|
|
|
161 |
// execute the propel:generate-module task
|
|
|
162 |
$task = new sfPropelGenerateModuleTask($this->dispatcher, $this->formatter);
|
|
|
163 |
$task->setCommandApplication($this->commandApplication);
|
|
|
164 |
$task->setConfiguration($this->configuration);
|
|
|
165 |
|
|
|
166 |
$this->logSection('app', sprintf('Generating admin module "%s" for model "%s"', $module, $model));
|
|
|
167 |
|
|
|
168 |
return $task->run(array($arguments['application'], $module, $model), array(
|
|
|
169 |
'theme' => $options['theme'],
|
|
|
170 |
'route-prefix' => $routeOptions['name'],
|
|
|
171 |
'with-propel-route' => true,
|
|
|
172 |
'generate-in-cache' => true,
|
|
|
173 |
'non-verbose-templates' => true,
|
|
|
174 |
'singular' => $options['singular'],
|
|
|
175 |
'plural' => $options['plural'],
|
|
|
176 |
'actions-base-class' => $options['actions-base-class'],
|
|
|
177 |
));
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
protected function getRouteFromName($name)
|
|
|
181 |
{
|
|
|
182 |
$config = new sfRoutingConfigHandler();
|
|
|
183 |
$routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
|
|
|
184 |
|
|
|
185 |
if (isset($routes[$name]))
|
|
|
186 |
{
|
|
|
187 |
return $routes[$name];
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
return false;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Checks whether a route references a model and module.
|
|
|
195 |
*
|
|
|
196 |
* @param mixed $route A route collection
|
|
|
197 |
* @param string $model A model name
|
|
|
198 |
* @param string $module A module name
|
|
|
199 |
*
|
|
|
200 |
* @return boolean
|
|
|
201 |
*/
|
|
|
202 |
protected function checkRoute($route, $model, $module)
|
|
|
203 |
{
|
|
|
204 |
if ($route instanceof sfPropelRouteCollection)
|
|
|
205 |
{
|
|
|
206 |
$options = $route->getOptions();
|
|
|
207 |
return $model == $options['model'] && $module == $options['module'];
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
return false;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Returns the name of the model's primary key column.
|
|
|
215 |
*
|
|
|
216 |
* @param string $model A model name
|
|
|
217 |
*
|
|
|
218 |
* @return string A column name
|
|
|
219 |
*/
|
|
|
220 |
protected function getPrimaryKey($model)
|
|
|
221 |
{
|
|
|
222 |
$peer = constant($model.'::PEER');
|
|
|
223 |
$map = call_user_func(array($peer, 'getTableMap'));
|
|
|
224 |
|
|
|
225 |
if (!$pks = $map->getPrimaryKeys())
|
|
|
226 |
{
|
|
|
227 |
return 'id';
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
$column = array_shift($pks);
|
|
|
231 |
|
|
|
232 |
return call_user_func(array($peer, 'translateFieldName'), $column->getPhpName(), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME);
|
|
|
233 |
}
|
|
|
234 |
}
|