1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of the sfPropel13Plugin package. |
---|
4 | * (c) 2007 Joshua May <notjosh@gmail.com> |
---|
5 | * |
---|
6 | * For the full copyright and license information, please view the LICENSE |
---|
7 | * file that was distributed with this source code. |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * @package symfony.plugins |
---|
12 | * @subpackage sfPropel13Plugin |
---|
13 | * @author Joshua May <notjosh@gmail.com> |
---|
14 | * @version SVN: $Id$ |
---|
15 | */ |
---|
16 | |
---|
17 | // ---- |
---|
18 | // Task runners |
---|
19 | // ---- |
---|
20 | function run_propel_13_build_model($task, $args) |
---|
21 | { |
---|
22 | _propel_convert_yml_schema(false, 'generated-'); |
---|
23 | _propel_copy_xml_schema_from_plugins('generated-'); |
---|
24 | _call_phing_13($task, 'om'); |
---|
25 | $finder = pakeFinder::type('file')->name('generated-*schema.xml'); |
---|
26 | pake_remove($finder, array('config', 'plugins')); |
---|
27 | } |
---|
28 | |
---|
29 | function run_propel_13_build_sql($task, $args) |
---|
30 | { |
---|
31 | _propel_convert_yml_schema(false, 'generated-'); |
---|
32 | _propel_copy_xml_schema_from_plugins('generated-'); |
---|
33 | _call_phing_13($task, 'sql'); |
---|
34 | $finder = pakeFinder::type('file')->name('generated-*schema.xml'); |
---|
35 | pake_remove($finder, 'config'); |
---|
36 | } |
---|
37 | |
---|
38 | function run_propel_13_insert_sql($task, $args) |
---|
39 | { |
---|
40 | _propel_convert_yml_schema(false, 'generated-'); |
---|
41 | _propel_copy_xml_schema_from_plugins('generated-'); |
---|
42 | _call_phing_13($task, 'insert-sql'); |
---|
43 | $finder = pakeFinder::type('file')->name('generated-*schema.xml'); |
---|
44 | pake_remove($finder, 'config'); |
---|
45 | } |
---|
46 | |
---|
47 | function run_propel_13_build_db($task, $args) |
---|
48 | { |
---|
49 | _call_phing_13($task, 'create-db', false); |
---|
50 | } |
---|
51 | |
---|
52 | function run_propel_13_build_schema($task, $args) |
---|
53 | { |
---|
54 | _call_phing_13($task, 'creole', false); |
---|
55 | |
---|
56 | // fix database name |
---|
57 | if (file_exists('config/schema.xml')) |
---|
58 | { |
---|
59 | $schema = file_get_contents('config/schema.xml'); |
---|
60 | $schema = preg_replace('/<database\s+name="[^"]+"/s', '<database name="propel"', $schema); |
---|
61 | file_put_contents('config/schema.xml', $schema); |
---|
62 | } |
---|
63 | |
---|
64 | if (!isset($args[0]) || $args[0] != 'xml') |
---|
65 | { |
---|
66 | _propel_convert_xml_schema(false, ''); |
---|
67 | $finder = pakeFinder::type('file')->name('schema.xml'); |
---|
68 | pake_remove($finder, 'config'); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | function run_propel_13_load_data($task, $args) |
---|
73 | { |
---|
74 | //_init_db_path(); |
---|
75 | //run_propel_load_data($task, $args) |
---|
76 | |
---|
77 | |
---|
78 | if (!count($args)) |
---|
79 | { |
---|
80 | throw new Exception('You must provide the app.'); |
---|
81 | } |
---|
82 | |
---|
83 | $app = $args[0]; |
---|
84 | |
---|
85 | if (!is_dir(sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.$app)) |
---|
86 | { |
---|
87 | throw new Exception('The app "'.$app.'" does not exist.'); |
---|
88 | } |
---|
89 | |
---|
90 | if (count($args) > 1 && $args[count($args) - 1] == 'append') |
---|
91 | { |
---|
92 | array_pop($args); |
---|
93 | $delete = false; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | $delete = true; |
---|
98 | } |
---|
99 | |
---|
100 | $env = empty($args[1]) ? 'dev' : $args[1]; |
---|
101 | |
---|
102 | // define constants |
---|
103 | define('SF_ROOT_DIR', sfConfig::get('sf_root_dir')); |
---|
104 | define('SF_APP', $app); |
---|
105 | define('SF_ENVIRONMENT', $env); |
---|
106 | define('SF_DEBUG', true); |
---|
107 | |
---|
108 | // get configuration |
---|
109 | require_once SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'; |
---|
110 | |
---|
111 | _init_db_path(); |
---|
112 | |
---|
113 | if (count($args) == 1) |
---|
114 | { |
---|
115 | if (!$pluginDirs = glob(sfConfig::get('sf_root_dir').'/plugins/*/data')) |
---|
116 | { |
---|
117 | $pluginDirs = array(); |
---|
118 | } |
---|
119 | $fixtures_dirs = pakeFinder::type('dir')->name('fixtures')->in(array_merge($pluginDirs, array(sfConfig::get('sf_data_dir')))); |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | $fixtures_dirs = array_slice($args, 1); |
---|
124 | } |
---|
125 | |
---|
126 | $databaseManager = new sfDatabaseManager(); |
---|
127 | $databaseManager->initialize(); |
---|
128 | |
---|
129 | $data = new sfPropel13Data(); |
---|
130 | $data->setDeleteCurrentData($delete); |
---|
131 | |
---|
132 | foreach ($fixtures_dirs as $fixtures_dir) |
---|
133 | { |
---|
134 | if (!is_readable($fixtures_dir)) |
---|
135 | { |
---|
136 | continue; |
---|
137 | } |
---|
138 | |
---|
139 | pake_echo_action('propel', sprintf('load data from "%s"', $fixtures_dir)); |
---|
140 | $data->loadData($fixtures_dir); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | function run_propel_13_dump_data($task, $args) |
---|
145 | { |
---|
146 | if (!count($args)) |
---|
147 | { |
---|
148 | throw new Exception('You must provide the app.'); |
---|
149 | } |
---|
150 | |
---|
151 | $app = $args[0]; |
---|
152 | |
---|
153 | if (!is_dir(sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.$app)) |
---|
154 | { |
---|
155 | throw new Exception('The app "'.$app.'" does not exist.'); |
---|
156 | } |
---|
157 | |
---|
158 | if (!isset($args[1])) |
---|
159 | { |
---|
160 | throw new Exception('You must provide a filename.'); |
---|
161 | } |
---|
162 | |
---|
163 | $filename = $args[1]; |
---|
164 | |
---|
165 | $env = empty($args[2]) ? 'dev' : $args[2]; |
---|
166 | |
---|
167 | // define constants |
---|
168 | define('SF_ROOT_DIR', sfConfig::get('sf_root_dir')); |
---|
169 | define('SF_APP', $app); |
---|
170 | define('SF_ENVIRONMENT', $env); |
---|
171 | define('SF_DEBUG', true); |
---|
172 | |
---|
173 | // get configuration |
---|
174 | require_once SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'; |
---|
175 | |
---|
176 | _init_db_path(); |
---|
177 | |
---|
178 | $databaseManager = new sfDatabaseManager(); |
---|
179 | $databaseManager->initialize(); |
---|
180 | |
---|
181 | if (!sfToolkit::isPathAbsolute($filename)) |
---|
182 | { |
---|
183 | $dir = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'fixtures'; |
---|
184 | pake_mkdirs($dir); |
---|
185 | $filename = $dir.DIRECTORY_SEPARATOR.$filename; |
---|
186 | } |
---|
187 | |
---|
188 | pake_echo_action('propel', sprintf('dumping data to "%s"', $filename)); |
---|
189 | |
---|
190 | $data = new sfPropel13Data(); |
---|
191 | $data->dumpData($filename); |
---|
192 | } |
---|
193 | |
---|
194 | function run_propel_13_build_all($task, $args) |
---|
195 | { |
---|
196 | run_propel_13_build_model($task, $args); |
---|
197 | run_propel_13_build_sql($task, $args); |
---|
198 | run_propel_13_insert_sql($task, $args); |
---|
199 | } |
---|
200 | |
---|
201 | function run_propel_13_build_all_load($task, $args) |
---|
202 | { |
---|
203 | run_propel_13_build_all($task, $args); |
---|
204 | run_propel_13_load_data($task, $args); |
---|
205 | } |
---|
206 | |
---|
207 | function run_propel_13_convert_xml_schema($task, $args) |
---|
208 | { |
---|
209 | _propel_convert_xml_schema(true); |
---|
210 | } |
---|
211 | |
---|
212 | function run_propel_13_convert_yml_schema($task, $args) |
---|
213 | { |
---|
214 | _propel_convert_yml_schema(true); |
---|
215 | } |
---|
216 | |
---|
217 | // ---- |
---|
218 | // Internal functions |
---|
219 | // ---- |
---|
220 | function _call_phing_13($task, $task_name, $check_schema = true) |
---|
221 | { |
---|
222 | $schemas = pakeFinder::type('file')->name('*schema.xml')->relative()->follow_link()->in('config'); |
---|
223 | if ($check_schema && !$schemas) |
---|
224 | { |
---|
225 | throw new Exception('You must create a schema.yml or schema.xml file.'); |
---|
226 | } |
---|
227 | |
---|
228 | $pluginDir = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'vendor'); |
---|
229 | if (false === strpos('propel-generator', get_include_path())) |
---|
230 | { |
---|
231 | set_include_path($pluginDir.'/propel-generator/classes'.PATH_SEPARATOR.get_include_path()); |
---|
232 | } |
---|
233 | |
---|
234 | // needed to include the right Propel builders |
---|
235 | set_include_path(sfConfig::get('sf_symfony_lib_dir').PATH_SEPARATOR.get_include_path()); |
---|
236 | set_include_path(sfConfig::get('sf_root_dir').PATH_SEPARATOR.get_include_path()); |
---|
237 | |
---|
238 | // add phing 2.3 to include_path |
---|
239 | set_include_path(realpath(dirname(__FILE__) . '/../../lib/vendor/phing/classes') . PATH_SEPARATOR . get_include_path()); |
---|
240 | |
---|
241 | // call phing targets |
---|
242 | pake_import('Phing', false); |
---|
243 | |
---|
244 | $options = array( |
---|
245 | 'project.dir' => sfConfig::get('sf_root_dir').'/config', |
---|
246 | 'build.properties' => 'propel.ini', |
---|
247 | 'propel.output.dir' => sfConfig::get('sf_root_dir'), |
---|
248 | ); |
---|
249 | pakePhingTask::call_phing($task, array($task_name), $pluginDir.'/propel-generator/build.xml', $options); |
---|
250 | |
---|
251 | chdir(sfConfig::get('sf_root_dir')); |
---|
252 | } |
---|
253 | |
---|
254 | function _init_db_path() |
---|
255 | { |
---|
256 | $vendorDir = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'vendor'); |
---|
257 | set_include_path($vendorDir.PATH_SEPARATOR.get_include_path()); |
---|
258 | } |
---|
259 | |
---|
260 | // ---- |
---|
261 | // Task definitions |
---|
262 | // ---- |
---|
263 | pake_desc('create classes for current model'); |
---|
264 | pake_task('propel-13-build-model', 'project_exists'); |
---|
265 | |
---|
266 | pake_desc('create sql for current model'); |
---|
267 | pake_task('propel-13-build-sql', 'project_exists'); |
---|
268 | |
---|
269 | pake_desc('create schema.xml from existing database'); |
---|
270 | pake_task('propel-13-build-schema', 'project_exists'); |
---|
271 | |
---|
272 | pake_desc('create schema.xml from schema.yml'); |
---|
273 | pake_task('propel-13-convert-yml-schema', 'project_exists'); |
---|
274 | |
---|
275 | pake_desc('create schema.yml from schema.xml'); |
---|
276 | pake_task('propel-13-convert-xml-schema', 'project_exists'); |
---|
277 | |
---|
278 | pake_desc('load data from fixtures directory'); |
---|
279 | pake_task('propel-13-load-data', 'project_exists'); |
---|
280 | |
---|
281 | pake_desc('dump data to fixtures directory'); |
---|
282 | pake_task('propel-13-dump-data', 'project_exists'); |
---|
283 | |
---|
284 | //pake_desc('create database for current model'); |
---|
285 | //pake_task('propel-13-build-db', 'project_exists'); |
---|
286 | |
---|
287 | pake_desc('insert sql for current model'); |
---|
288 | pake_task('propel-13-insert-sql', 'project_exists'); |
---|
289 | |
---|
290 | pake_desc('generate propel model and sql and initialize database'); |
---|
291 | pake_task('propel-13-build-all', 'project_exists'); |
---|
292 | |
---|
293 | pake_desc('generate propel model and sql and initialize database, and load data'); |
---|
294 | pake_task('propel-13-build-all-load', 'propel-13-build-all'); |
---|