Development

/branches/1.4/lib/plugins/sfPropelPlugin/lib/task/sfPropelBuildTask.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/plugins/sfPropelPlugin/lib/task/sfPropelBuildTask.class.php

Revision 23308, 7.8 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] added build of model to propel form and filter builds in propel:build task

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