[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/data/tasks/ -> sfPakePropel.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 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  pake_desc('create classes for current model');
  12  pake_task('propel-build-model', 'project_exists');
  13  
  14  pake_desc('create sql for current model');
  15  pake_task('propel-build-sql', 'project_exists');
  16  
  17  pake_desc('create schema.xml from existing database');
  18  pake_task('propel-build-schema', 'project_exists');
  19  
  20  pake_desc('create schema.xml from schema.yml');
  21  pake_task('propel-convert-yml-schema', 'project_exists');
  22  
  23  pake_desc('create schema.yml from schema.xml');
  24  pake_task('propel-convert-xml-schema', 'project_exists');
  25  
  26  pake_desc('load data from fixtures directory');
  27  pake_task('propel-load-data', 'project_exists');
  28  
  29  pake_desc('dump data to fixtures directory');
  30  pake_task('propel-dump-data', 'project_exists');
  31  
  32  pake_desc('create database for current model');
  33  pake_task('propel-build-db', 'project_exists');
  34  
  35  pake_desc('insert sql for current model');
  36  pake_task('propel-insert-sql', 'project_exists');
  37  
  38  pake_desc('generate propel model and sql and initialize database');
  39  pake_task('propel-build-all', 'project_exists');
  40  
  41  pake_desc('generate propel model and sql and initialize database, and load data');
  42  pake_task('propel-build-all-load', 'propel-build-all');
  43  
  44  function run_propel_convert_yml_schema($task, $args)
  45  {
  46    _propel_convert_yml_schema(true);
  47  }
  48  
  49  function run_propel_convert_xml_schema($task, $args)
  50  {
  51    _propel_convert_xml_schema(true);
  52  }
  53  
  54  function _propel_convert_yml_schema($check_schema = true, $prefix = '')
  55  {
  56    $finder = pakeFinder::type('file')->name('*schema.yml');
  57    $dirs = array('config');
  58    if ($pluginDirs = glob(sfConfig::get('sf_root_dir').'/plugins/*/config'))
  59    {
  60      $dirs = array_merge($dirs, $pluginDirs);
  61    }
  62    $schemas = $finder->in($dirs);
  63    if ($check_schema && !count($schemas))
  64    {
  65      throw new Exception('You must create a schema.yml file.');
  66    }
  67  
  68    $db_schema = new sfPropelDatabaseSchema();
  69    foreach ($schemas as $schema)
  70    {
  71      $db_schema->loadYAML($schema);
  72  
  73      pake_echo_action('schema', 'converting "'.$schema.'"'.' to XML');
  74  
  75      $localprefix = $prefix;
  76  
  77      // change prefix for plugins
  78      if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
  79      {
  80        $localprefix = $prefix.$match[1].'-';
  81      }
  82  
  83      // save converted xml files in original directories
  84      $xml_file_name = str_replace('.yml', '.xml', basename($schema));
  85  
  86      $file = str_replace(basename($schema), $localprefix.$xml_file_name,  $schema);
  87      pake_echo_action('schema', 'putting '.$file);    
  88      file_put_contents($file, $db_schema->asXML());
  89    }
  90  }
  91  
  92  function _propel_convert_xml_schema($check_schema = true, $prefix = '')
  93  {
  94    $finder = pakeFinder::type('file')->name('*schema.xml');
  95  
  96    $schemas = array_merge($finder->in('config'), $finder->in(glob(sfConfig::get('sf_root_dir').'/plugins/*/config')));
  97    if ($check_schema && !count($schemas))
  98    {
  99      throw new Exception('You must create a schema.xml file.');
 100    }
 101  
 102    $db_schema = new sfPropelDatabaseSchema();
 103    foreach ($schemas as $schema)
 104    {
 105      $db_schema->loadXML($schema);
 106  
 107      $localprefix = $prefix;
 108  
 109      // change prefix for plugins
 110      if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
 111      {
 112        $localprefix = $prefix.$match[1].'-';
 113      }
 114  
 115      // save converted xml files in original directories
 116      $yml_file_name = str_replace('.xml', '.yml', basename($schema));
 117      
 118      $file = str_replace(basename($schema), $prefix.$yml_file_name,  $schema);
 119      pake_echo_action('schema', 'putting '.$file);    
 120      file_put_contents($file, $db_schema->asYAML());
 121    }
 122  }
 123  
 124  function _propel_copy_xml_schema_from_plugins($prefix = '')
 125  {
 126    $schemas = pakeFinder::type('file')->name('*schema.xml')->in(glob(sfConfig::get('sf_root_dir').'/plugins/*/config'));
 127  
 128    foreach ($schemas as $schema)
 129    {
 130      // reset local prefix
 131      $localprefix = '';
 132  
 133      // change prefix for plugins
 134      if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
 135      {
 136        // if the plugin name is not in the schema filename, add it
 137        if (!strstr(basename($schema), $match[1]))
 138        {
 139          $localprefix = $match[1].'-';
 140        }
 141      }
 142  
 143      // if the prefix is not in the schema filename, add it
 144      if (!strstr(basename($schema), $prefix))
 145      {
 146        $localprefix = $prefix.$localprefix;
 147      }
 148  
 149      pake_copy($schema, 'config'.DIRECTORY_SEPARATOR.$localprefix.basename($schema));
 150      if ('' === $localprefix)
 151      {
 152        pake_remove($schema, '');
 153      }
 154    }
 155  }
 156  
 157  function run_propel_build_all($task, $args)
 158  {
 159    run_propel_build_model($task, $args);
 160    run_propel_build_sql($task, $args);
 161    run_propel_insert_sql($task, $args);
 162  }
 163  
 164  function run_propel_build_all_load($task, $args)
 165  {
 166    run_propel_build_all($task, $args);
 167    run_propel_load_data($task, $args);
 168  }
 169  
 170  function run_propel_build_model($task, $args)
 171  {
 172    _propel_convert_yml_schema(false, 'generated-');
 173    _propel_copy_xml_schema_from_plugins('generated-');
 174    _call_phing($task, 'om');
 175    $finder = pakeFinder::type('file')->name('generated-*schema.xml');
 176    pake_remove($finder, array('config', 'plugins'));
 177  }
 178  
 179  function run_propel_build_sql($task, $args)
 180  {
 181    _propel_convert_yml_schema(false, 'generated-');
 182    _propel_copy_xml_schema_from_plugins('generated-');
 183    _call_phing($task, 'sql');
 184    $finder = pakeFinder::type('file')->name('generated-*schema.xml');
 185    pake_remove($finder, 'config');
 186  }
 187  
 188  function run_propel_build_db($task, $args)
 189  {
 190    _call_phing($task, 'create-db', false);
 191  }
 192  
 193  function run_propel_insert_sql($task, $args)
 194  {
 195    _propel_convert_yml_schema(false, 'generated-');
 196    _propel_copy_xml_schema_from_plugins('generated-');
 197    _call_phing($task, 'insert-sql');
 198    $finder = pakeFinder::type('file')->name('generated-*schema.xml');
 199    pake_remove($finder, 'config');
 200  }
 201  
 202  function run_propel_build_schema($task, $args)
 203  {
 204    _call_phing($task, 'creole', false);
 205  
 206    // fix database name
 207    if (file_exists('config/schema.xml'))
 208    {
 209      $schema = file_get_contents('config/schema.xml');
 210      $schema = preg_replace('/<database\s+name="[^"]+"/s', '<database name="propel"', $schema);
 211      file_put_contents('config/schema.xml', $schema);
 212    }
 213  
 214    if (!isset($args[0]) || $args[0] != 'xml')
 215    {
 216      _propel_convert_xml_schema(false, '');
 217      $finder = pakeFinder::type('file')->name('schema.xml');
 218      pake_remove($finder, 'config');
 219    }
 220  }
 221  
 222  /**
 223   * Dumps yml database data to fixtures directory.
 224   *
 225   * @example symfony dump-data frontend data.yml
 226   * @example symfony dump-data frontend data.yml dev
 227   *
 228   * @param object $task
 229   * @param array $args
 230   */
 231  function run_propel_dump_data($task, $args)
 232  {
 233    if (!count($args))
 234    {
 235      throw new Exception('You must provide the app.');
 236    }
 237  
 238    $app = $args[0];
 239  
 240    if (!is_dir(sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.$app))
 241    {
 242      throw new Exception('The app "'.$app.'" does not exist.');
 243    }
 244  
 245    if (!isset($args[1]))
 246    {
 247      throw new Exception('You must provide a filename.');
 248    }
 249  
 250    $filename = $args[1];
 251  
 252    $env = empty($args[2]) ? 'dev' : $args[2];
 253  
 254    // define constants
 255    define('SF_ROOT_DIR',    sfConfig::get('sf_root_dir'));
 256    define('SF_APP',         $app);
 257    define('SF_ENVIRONMENT', $env);
 258    define('SF_DEBUG',       true);
 259  
 260    // get configuration
 261    require_once SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php';
 262  
 263    $databaseManager = new sfDatabaseManager();
 264    $databaseManager->initialize();
 265  
 266    if (!sfToolkit::isPathAbsolute($filename))
 267    {
 268      $dir = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'fixtures';
 269      pake_mkdirs($dir);
 270      $filename = $dir.DIRECTORY_SEPARATOR.$filename;
 271    }
 272  
 273    pake_echo_action('propel', sprintf('dumping data to "%s"', $filename));
 274  
 275    $data = new sfPropelData();
 276    $data->dumpData($filename);
 277  }
 278  
 279  /**
 280   * Loads yml data from fixtures directory and inserts into database.
 281   *
 282   * @example symfony load-data frontend
 283   * @example symfony load-data frontend dev fixtures append
 284   *
 285   * @todo replace delete argument with flag -d
 286   *
 287   * @param object $task
 288   * @param array $args
 289   */
 290  function run_propel_load_data($task, $args)
 291  {
 292    if (!count($args))
 293    {
 294      throw new Exception('You must provide the app.');
 295    }
 296  
 297    $app = $args[0];
 298  
 299    if (!is_dir(sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.$app))
 300    {
 301      throw new Exception('The app "'.$app.'" does not exist.');
 302    }
 303  
 304    if (count($args) > 1 && $args[count($args) - 1] == 'append')
 305    {
 306      array_pop($args);
 307      $delete = false;
 308    }
 309    else
 310    {
 311      $delete = true;
 312    }
 313  
 314    $env = empty($args[1]) ? 'dev' : $args[1];
 315  
 316    // define constants
 317    define('SF_ROOT_DIR',    sfConfig::get('sf_root_dir'));
 318    define('SF_APP',         $app);
 319    define('SF_ENVIRONMENT', $env);
 320    define('SF_DEBUG',       true);
 321  
 322    // get configuration
 323    require_once SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php';
 324  
 325    if (count($args) == 1)
 326    {
 327      if (!$pluginDirs = glob(sfConfig::get('sf_root_dir').'/plugins/*/data'))
 328      {
 329        $pluginDirs = array();
 330      }
 331      $fixtures_dirs = pakeFinder::type('dir')->name('fixtures')->in(array_merge($pluginDirs, array(sfConfig::get('sf_data_dir'))));
 332    }
 333    else
 334    {
 335      $fixtures_dirs = array_slice($args, 1);
 336    }
 337  
 338    $databaseManager = new sfDatabaseManager();
 339    $databaseManager->initialize();
 340  
 341    $data = new sfPropelData();
 342    $data->setDeleteCurrentData($delete);
 343  
 344    foreach ($fixtures_dirs as $fixtures_dir)
 345    {
 346      if (!is_readable($fixtures_dir))
 347      {
 348        continue;
 349      }
 350  
 351      pake_echo_action('propel', sprintf('load data from "%s"', $fixtures_dir));
 352      $data->loadData($fixtures_dir);
 353    }
 354  }
 355  
 356  function _call_phing($task, $task_name, $check_schema = true)
 357  {
 358    $schemas = pakeFinder::type('file')->name('*schema.xml')->relative()->follow_link()->in('config');
 359    if ($check_schema && !$schemas)
 360    {
 361      throw new Exception('You must create a schema.yml or schema.xml file.');
 362    }
 363  
 364    // call phing targets
 365    pake_import('Phing', false);
 366    if (false === strpos('propel-generator', get_include_path()))
 367    {
 368      set_include_path(sfConfig::get('sf_symfony_lib_dir').'/vendor/propel-generator/classes'.PATH_SEPARATOR.get_include_path());
 369    }
 370    set_include_path(sfConfig::get('sf_root_dir').PATH_SEPARATOR.get_include_path());
 371  
 372    // needed to include the right Propel builders
 373    set_include_path(sfConfig::get('sf_symfony_lib_dir').PATH_SEPARATOR.get_include_path());
 374  
 375    $options = array(
 376      'project.dir'       => sfConfig::get('sf_root_dir').'/config',
 377      'build.properties'  => 'propel.ini',
 378      'propel.output.dir' => sfConfig::get('sf_root_dir'),
 379    );
 380    pakePhingTask::call_phing($task, array($task_name), sfConfig::get('sf_symfony_lib_dir').'/vendor/propel-generator/build.xml', $options);
 381  
 382    chdir(sfConfig::get('sf_root_dir'));
 383  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7