Development

/branches/1.3/lib/plugins/sfPropelPlugin/lib/task/sfPropelBuildSchemaTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfPropelPlugin/lib/task/sfPropelBuildSchemaTask.class.php

Revision 22278, 3.4 kB (checked in by fabien, 5 years ago)

[1.2, 1.3] fixed propel:build-schema when several connections are defined (closes #5305)

  • Property svn:mime-type set to text/x-php
  • 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) 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$
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->aliases = array('propel-build-schema');
37     $this->namespace = 'propel';
38     $this->name = 'build-schema';
39     $this->briefDescription = 'Creates a schema from an existing database';
40
41     $this->detailedDescription = <<<EOF
42 The [propel:build-schema|INFO] task introspects a database to create a schema:
43
44   [./symfony propel:build-schema|INFO]
45
46 By default, the task creates a YML file, but you can also create a XML file:
47
48   [./symfony --xml propel:build-schema|INFO]
49
50 The XML format contains more information than the YML one.
51 EOF;
52   }
53
54   /**
55    * @see sfTask
56    */
57   protected function execute($arguments = array(), $options = array())
58   {
59     $databaseManager = new sfDatabaseManager($this->configuration);
60
61     foreach ($databaseManager->getNames() as $connection)
62     {
63       if (null !== $options['connection'] && $options['connection'] != $connection)
64       {
65         continue;
66       }
67
68       $this->reverseDatabase($databaseManager, $connection, $options);
69     }
70   }
71
72   protected function reverseDatabase($databaseManager, $connection, $options)
73   {
74     $name = 'propel' == $connection ? 'schema' : $connection.'-schema';
75
76     $properties = $this->getPhingPropertiesForConnection($databaseManager, $connection);
77     $properties['propel.default.schema.basename'] = $name;
78
79     $ret = $this->callPhing('reverse', self::DO_NOT_CHECK_SCHEMA, $properties);
80
81     if (!$ret)
82     {
83       return 1;
84     }
85
86     $xmlSchemaPath = sfConfig::get('sf_config_dir').'/'.$name.'.xml';
87     $ymlSchemaPath = sfConfig::get('sf_config_dir').'/'.$name.'.yml';
88
89     // Fix database name
90     if (file_exists($xmlSchemaPath))
91     {
92       $schema = file_get_contents($xmlSchemaPath);
93       $schema = preg_replace('/<database\s+name="[^"]+"/s', '<database name="'.$connection.'" package="lib.model"', $schema);
94       file_put_contents($xmlSchemaPath, $schema);
95     }
96
97     if (!$options['xml'])
98     {
99       $this->schemaToYML(self::DO_NOT_CHECK_SCHEMA, '');
100       $this->cleanup();
101
102       if (file_exists($xmlSchemaPath))
103       {
104         unlink($xmlSchemaPath);
105       }
106     }
107     else
108     {
109       if (file_exists($ymlSchemaPath))
110       {
111         unlink($ymlSchemaPath);
112       }
113     }
114   }
115 }
116
Note: See TracBrowser for help on using the browser.