Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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
require_once(dirname(__FILE__).'/sfPropelBaseTask.class.php');
12
 
13
/**
14
 * Creates a schema.xml from an existing database.
15
 *
16
 * @package    symfony
17
 * @subpackage propel
18
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19
 * @version    SVN: $Id: sfPropelBuildSchemaTask.class.php 23922 2009-11-14 14:58:38Z fabien $
20
 */
21
class sfPropelBuildSchemaTask extends sfPropelBaseTask
22
{
23
  /**
24
   * @see sfTask
25
   */
26
  protected function configure()
27
  {
28
    $this->addOptions(array(
29
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
30
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'cli'),
31
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', null),
32
      new sfCommandOption('xml', null, sfCommandOption::PARAMETER_NONE, 'Creates an XML schema instead of a YML one'),
33
      new sfCommandOption('phing-arg', null, sfCommandOption::PARAMETER_REQUIRED | sfCommandOption::IS_ARRAY, 'Arbitrary phing argument'),
34
    ));
35
 
36
    $this->namespace = 'propel';
37
    $this->name = 'build-schema';
38
    $this->briefDescription = 'Creates a schema from an existing database';
39
 
40
    $this->detailedDescription = <<<EOF
41
The [propel:build-schema|INFO] task introspects a database to create a schema:
42
 
43
  [./symfony propel:build-schema|INFO]
44
 
45
By default, the task creates a YML file, but you can also create a XML file:
46
 
47
  [./symfony --xml propel:build-schema|INFO]
48
 
49
The XML format contains more information than the YML one.
50
EOF;
51
  }
52
 
53
  /**
54
   * @see sfTask
55
   */
56
  protected function execute($arguments = array(), $options = array())
57
  {
58
    $databaseManager = new sfDatabaseManager($this->configuration);
59
 
60
    foreach ($databaseManager->getNames() as $connection)
61
    {
62
      if (null !== $options['connection'] && $options['connection'] != $connection)
63
      {
64
        continue;
65
      }
66
 
67
      $this->reverseDatabase($databaseManager, $connection, $options);
68
    }
69
  }
70
 
71
  protected function reverseDatabase($databaseManager, $connection, $options)
72
  {
73
    $name = 'propel' == $connection ? 'schema' : $connection.'-schema';
74
 
75
    $properties = $this->getPhingPropertiesForConnection($databaseManager, $connection);
76
    $properties['propel.default.schema.basename'] = $name;
77
 
78
    $ret = $this->callPhing('reverse', self::DO_NOT_CHECK_SCHEMA, $properties);
79
 
80
    if (!$ret)
81
    {
82
      return 1;
83
    }
84
 
85
    $xmlSchemaPath = sfConfig::get('sf_config_dir').'/'.$name.'.xml';
86
    $ymlSchemaPath = sfConfig::get('sf_config_dir').'/'.$name.'.yml';
87
 
88
    // Fix database name
89
    if (file_exists($xmlSchemaPath))
90
    {
91
      $schema = file_get_contents($xmlSchemaPath);
92
      $schema = preg_replace('/<database\s+name="[^"]+"/s', '<database name="'.$connection.'" package="lib.model"', $schema);
93
      file_put_contents($xmlSchemaPath, $schema);
94
    }
95
 
96
    if (!$options['xml'])
97
    {
98
      $this->schemaToYML(self::DO_NOT_CHECK_SCHEMA, '');
99
      $this->cleanup();
100
 
101
      if (file_exists($xmlSchemaPath))
102
      {
103
        unlink($xmlSchemaPath);
104
      }
105
    }
106
    else
107
    {
108
      if (file_exists($ymlSchemaPath))
109
      {
110
        unlink($ymlSchemaPath);
111
      }
112
    }
113
  }
114
}