| 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 |
* (c) Jonathan H. Wage <jonwage@gmail.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Generates code based on your schema.
|
|
|
16 |
*
|
|
|
17 |
* @package sfDoctrinePlugin
|
|
|
18 |
* @subpackage task
|
|
|
19 |
* @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
|
|
|
20 |
* @version SVN: $Id: sfDoctrineBuildTask.class.php 23156 2009-10-17 13:08:16Z Kris.Wallsmith $
|
|
|
21 |
*/
|
|
|
22 |
class sfDoctrineBuildTask extends sfDoctrineBaseTask
|
|
|
23 |
{
|
|
|
24 |
const
|
|
|
25 |
BUILD_MODEL = 1,
|
|
|
26 |
BUILD_FORMS = 2,
|
|
|
27 |
BUILD_FILTERS = 4,
|
|
|
28 |
BUILD_SQL = 8,
|
|
|
29 |
BUILD_DB = 16,
|
|
|
30 |
|
|
|
31 |
OPTION_MODEL = 1,
|
|
|
32 |
OPTION_FORMS = 3, // model, forms
|
|
|
33 |
OPTION_FILTERS = 5, // model, filters
|
|
|
34 |
OPTION_SQL = 9, // model, sql
|
|
|
35 |
OPTION_DB = 25, // model, sql, db
|
|
|
36 |
OPTION_ALL_CLASSES = 7, // model, forms, filters
|
|
|
37 |
OPTION_ALL = 31; // model, forms, filters, sql, db
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @see sfTask
|
|
|
41 |
*/
|
|
|
42 |
protected function configure()
|
|
|
43 |
{
|
|
|
44 |
$this->addOptions(array(
|
|
|
45 |
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
|
|
|
46 |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
|
|
|
47 |
new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Whether to force dropping of the database'),
|
|
|
48 |
new sfCommandOption('all', null, sfCommandOption::PARAMETER_NONE, 'Build everything and reset the database'),
|
|
|
49 |
new sfCommandOption('all-classes', null, sfCommandOption::PARAMETER_NONE, 'Build all classes'),
|
|
|
50 |
new sfCommandOption('model', null, sfCommandOption::PARAMETER_NONE, 'Build model classes'),
|
|
|
51 |
new sfCommandOption('forms', null, sfCommandOption::PARAMETER_NONE, 'Build form classes'),
|
|
|
52 |
new sfCommandOption('filters', null, sfCommandOption::PARAMETER_NONE, 'Build filter classes'),
|
|
|
53 |
new sfCommandOption('sql', null, sfCommandOption::PARAMETER_NONE, 'Build SQL'),
|
|
|
54 |
new sfCommandOption('db', null, sfCommandOption::PARAMETER_NONE, 'Drop, create, and either insert SQL or migrate the database'),
|
|
|
55 |
new sfCommandOption('and-migrate', null, sfCommandOption::PARAMETER_NONE, 'Migrate the database'),
|
|
|
56 |
new sfCommandOption('and-load', null, sfCommandOption::PARAMETER_OPTIONAL | sfCommandOption::IS_ARRAY, 'Load fixture data'),
|
|
|
57 |
new sfCommandOption('and-append', null, sfCommandOption::PARAMETER_OPTIONAL | sfCommandOption::IS_ARRAY, 'Append fixture data'),
|
|
|
58 |
));
|
|
|
59 |
|
|
|
60 |
$this->namespace = 'doctrine';
|
|
|
61 |
$this->name = 'build';
|
|
|
62 |
|
|
|
63 |
$this->briefDescription = 'Generate code based on your schema';
|
|
|
64 |
|
|
|
65 |
$this->detailedDescription = <<<EOF
|
|
|
66 |
The [doctrine:build|INFO] task generates code based on your schema:
|
|
|
67 |
|
|
|
68 |
[./symfony doctrine:build|INFO]
|
|
|
69 |
|
|
|
70 |
You must specify what you would like built. For instance, if you want model
|
|
|
71 |
and form classes built use the [--model|COMMENT] and [--forms|COMMENT] options:
|
|
|
72 |
|
|
|
73 |
[./symfony doctrine:build --model --forms|INFO]
|
|
|
74 |
|
|
|
75 |
You can use the [--all|COMMENT] shortcut option if you would like all classes and
|
|
|
76 |
SQL files generated and the database rebuilt:
|
|
|
77 |
|
|
|
78 |
[./symfony doctrine:build --all|INFO]
|
|
|
79 |
|
|
|
80 |
This is equivalent to running the following tasks:
|
|
|
81 |
|
|
|
82 |
[./symfony doctrine:drop-db|INFO]
|
|
|
83 |
[./symfony doctrine:build-db|INFO]
|
|
|
84 |
[./symfony doctrine:build-model|INFO]
|
|
|
85 |
[./symfony doctrine:build-forms|INFO]
|
|
|
86 |
[./symfony doctrine:build-filters|INFO]
|
|
|
87 |
[./symfony doctrine:build-sql|INFO]
|
|
|
88 |
[./symfony doctrine:insert-sql|INFO]
|
|
|
89 |
|
|
|
90 |
You can also generate only class files by using the [--all-classes|COMMENT] shortcut
|
|
|
91 |
option. When this option is used alone, the database will not be modified.
|
|
|
92 |
|
|
|
93 |
[./symfony doctrine:build --all-classes|INFO]
|
|
|
94 |
|
|
|
95 |
The [--and-migrate|COMMENT] option will run any pending migrations once the builds
|
|
|
96 |
are complete:
|
|
|
97 |
|
|
|
98 |
[./symfony doctrine:build --db --and-migrate|INFO]
|
|
|
99 |
|
|
|
100 |
The [--and-load|COMMENT] option will load data from the project and plugin
|
|
|
101 |
[data/fixtures/|COMMENT] directories:
|
|
|
102 |
|
|
|
103 |
[./symfony doctrine:build --db --and-migrate --and-load|INFO]
|
|
|
104 |
|
|
|
105 |
To specify what fixtures are loaded, add a parameter to the [--and-load|COMMENT] option:
|
|
|
106 |
|
|
|
107 |
[./symfony doctrine:build --all --and-load="data/fixtures/dev/"|INFO]
|
|
|
108 |
|
|
|
109 |
To append fixture data without erasing any records from the database, include
|
|
|
110 |
the [--and-append|COMMENT] option:
|
|
|
111 |
|
|
|
112 |
[./symfony doctrine:build --all --and-append|INFO]
|
|
|
113 |
EOF;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* @see sfTask
|
|
|
118 |
*/
|
|
|
119 |
protected function execute($arguments = array(), $options = array())
|
|
|
120 |
{
|
|
|
121 |
if (!$mode = $this->calculateMode($options))
|
|
|
122 |
{
|
|
|
123 |
throw new InvalidArgumentException(sprintf("You must include one or more of the following build options:\n--%s\n\nSee this task's help page for more information:\n\n php symfony help doctrine:build", join(', --', array_keys($this->getBuildOptions()))));
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
if (self::BUILD_DB == (self::BUILD_DB & $mode))
|
|
|
127 |
{
|
|
|
128 |
$task = new sfDoctrineDropDbTask($this->dispatcher, $this->formatter);
|
|
|
129 |
$task->setCommandApplication($this->commandApplication);
|
|
|
130 |
$task->setConfiguration($this->configuration);
|
|
|
131 |
$ret = $task->run(array(), array('no-confirmation' => $options['no-confirmation']));
|
|
|
132 |
|
|
|
133 |
if ($ret)
|
|
|
134 |
{
|
|
|
135 |
return $ret;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
$task = new sfDoctrineBuildDbTask($this->dispatcher, $this->formatter);
|
|
|
139 |
$task->setCommandApplication($this->commandApplication);
|
|
|
140 |
$task->setConfiguration($this->configuration);
|
|
|
141 |
$ret = $task->run();
|
|
|
142 |
|
|
|
143 |
if ($ret)
|
|
|
144 |
{
|
|
|
145 |
return $ret;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
// :insert-sql (or :migrate) will also be run, below
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
if (self::BUILD_MODEL == (self::BUILD_MODEL & $mode))
|
|
|
152 |
{
|
|
|
153 |
$task = new sfDoctrineBuildModelTask($this->dispatcher, $this->formatter);
|
|
|
154 |
$task->setCommandApplication($this->commandApplication);
|
|
|
155 |
$task->setConfiguration($this->configuration);
|
|
|
156 |
$ret = $task->run();
|
|
|
157 |
|
|
|
158 |
if ($ret)
|
|
|
159 |
{
|
|
|
160 |
return $ret;
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
if (self::BUILD_FORMS == (self::BUILD_FORMS & $mode))
|
|
|
165 |
{
|
|
|
166 |
$task = new sfDoctrineBuildFormsTask($this->dispatcher, $this->formatter);
|
|
|
167 |
$task->setCommandApplication($this->commandApplication);
|
|
|
168 |
$task->setConfiguration($this->configuration);
|
|
|
169 |
$ret = $task->run();
|
|
|
170 |
|
|
|
171 |
if ($ret)
|
|
|
172 |
{
|
|
|
173 |
return $ret;
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
if (self::BUILD_FILTERS == (self::BUILD_FILTERS & $mode))
|
|
|
178 |
{
|
|
|
179 |
$task = new sfDoctrineBuildFiltersTask($this->dispatcher, $this->formatter);
|
|
|
180 |
$task->setCommandApplication($this->commandApplication);
|
|
|
181 |
$task->setConfiguration($this->configuration);
|
|
|
182 |
$ret = $task->run();
|
|
|
183 |
|
|
|
184 |
if ($ret)
|
|
|
185 |
{
|
|
|
186 |
return $ret;
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
if (self::BUILD_SQL == (self::BUILD_SQL & $mode))
|
|
|
191 |
{
|
|
|
192 |
$task = new sfDoctrineBuildSqlTask($this->dispatcher, $this->formatter);
|
|
|
193 |
$task->setCommandApplication($this->commandApplication);
|
|
|
194 |
$task->setConfiguration($this->configuration);
|
|
|
195 |
$ret = $task->run();
|
|
|
196 |
|
|
|
197 |
if ($ret)
|
|
|
198 |
{
|
|
|
199 |
return $ret;
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
if ($options['and-migrate'])
|
|
|
204 |
{
|
|
|
205 |
$task = new sfDoctrineMigrateTask($this->dispatcher, $this->formatter);
|
|
|
206 |
$task->setCommandApplication($this->commandApplication);
|
|
|
207 |
$task->setConfiguration($this->configuration);
|
|
|
208 |
$ret = $task->run();
|
|
|
209 |
|
|
|
210 |
if ($ret)
|
|
|
211 |
{
|
|
|
212 |
return $ret;
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
else if (self::BUILD_DB == (self::BUILD_DB & $mode))
|
|
|
216 |
{
|
|
|
217 |
$task = new sfDoctrineInsertSqlTask($this->dispatcher, $this->formatter);
|
|
|
218 |
$task->setCommandApplication($this->commandApplication);
|
|
|
219 |
$task->setConfiguration($this->configuration);
|
|
|
220 |
$ret = $task->run();
|
|
|
221 |
|
|
|
222 |
if ($ret)
|
|
|
223 |
{
|
|
|
224 |
return $ret;
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
if (count($options['and-load']) || count($options['and-append']))
|
|
|
229 |
{
|
|
|
230 |
$task = new sfDoctrineDataLoadTask($this->dispatcher, $this->formatter);
|
|
|
231 |
$task->setCommandApplication($this->commandApplication);
|
|
|
232 |
$task->setConfiguration($this->configuration);
|
|
|
233 |
|
|
|
234 |
if (count($options['and-load']))
|
|
|
235 |
{
|
|
|
236 |
$ret = $task->run(array(
|
|
|
237 |
'dir_or_file' => in_array(array(), $options['and-load'], true) ? null : $options['and-load'],
|
|
|
238 |
));
|
|
|
239 |
|
|
|
240 |
if ($ret)
|
|
|
241 |
{
|
|
|
242 |
return $ret;
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
if (count($options['and-append']))
|
|
|
247 |
{
|
|
|
248 |
$ret = $task->run(array(
|
|
|
249 |
'dir_or_file' => in_array(array(), $options['and-append'], true) ? null : $options['and-append'],
|
|
|
250 |
), array(
|
|
|
251 |
'append' => true,
|
|
|
252 |
));
|
|
|
253 |
|
|
|
254 |
if ($ret)
|
|
|
255 |
{
|
|
|
256 |
return $ret;
|
|
|
257 |
}
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
/**
|
|
|
263 |
* Calculates a bit mode based on the supplied options.
|
|
|
264 |
*
|
|
|
265 |
* @param array $options
|
|
|
266 |
*
|
|
|
267 |
* @return integer
|
|
|
268 |
*/
|
|
|
269 |
protected function calculateMode($options = array())
|
|
|
270 |
{
|
|
|
271 |
$mode = 0;
|
|
|
272 |
foreach ($this->getBuildOptions() as $name => $value)
|
|
|
273 |
{
|
|
|
274 |
if (isset($options[$name]) && true === $options[$name])
|
|
|
275 |
{
|
|
|
276 |
$mode = $mode | $value;
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
return $mode;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Returns an array of valid build options.
|
|
|
285 |
*
|
|
|
286 |
* @return array An array of option names and their mode
|
|
|
287 |
*/
|
|
|
288 |
protected function getBuildOptions()
|
|
|
289 |
{
|
|
|
290 |
$options = array();
|
|
|
291 |
foreach ($this->options as $option)
|
|
|
292 |
{
|
|
|
293 |
if (defined($constant = __CLASS__.'::OPTION_'.str_replace('-', '_', strtoupper($option->getName()))))
|
|
|
294 |
{
|
|
|
295 |
$options[$option->getName()] = constant($constant);
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
return $options;
|
|
|
300 |
}
|
|
|
301 |
}
|