source: pro-violet-viettel/sourcecode/api.violet.vn/www/plugins/sfPropel13Plugin/data/tasks/sfPakePropel13.php

Last change on this file was 289, checked in by dungnv, 11 years ago
File size: 8.1 KB
Line 
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// ----
20function 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
29function 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
38function 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
47function run_propel_13_build_db($task, $args)
48{
49  _call_phing_13($task, 'create-db', false);
50}
51
52function 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
72function 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
144function 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
194function 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
201function 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
207function run_propel_13_convert_xml_schema($task, $args)
208{
209  _propel_convert_xml_schema(true);
210}
211
212function run_propel_13_convert_yml_schema($task, $args)
213{
214  _propel_convert_yml_schema(true);
215}
216
217// ----
218// Internal functions
219// ----
220function _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
254function _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// ----
263pake_desc('create classes for current model');
264pake_task('propel-13-build-model', 'project_exists');
265
266pake_desc('create sql for current model');
267pake_task('propel-13-build-sql', 'project_exists');
268
269pake_desc('create schema.xml from existing database');
270pake_task('propel-13-build-schema', 'project_exists');
271
272pake_desc('create schema.xml from schema.yml');
273pake_task('propel-13-convert-yml-schema', 'project_exists');
274
275pake_desc('create schema.yml from schema.xml');
276pake_task('propel-13-convert-xml-schema', 'project_exists');
277
278pake_desc('load data from fixtures directory');
279pake_task('propel-13-load-data', 'project_exists');
280
281pake_desc('dump data to fixtures directory');
282pake_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
287pake_desc('insert sql for current model');
288pake_task('propel-13-insert-sql', 'project_exists');
289
290pake_desc('generate propel model and sql and initialize database');
291pake_task('propel-13-build-all', 'project_exists');
292
293pake_desc('generate propel model and sql and initialize database, and load data');
294pake_task('propel-13-build-all-load', 'propel-13-build-all');
Note: See TracBrowser for help on using the repository browser.