Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/task/sfPropelBaseTask.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/task/sfPropelBaseTask.class.php

Revision 12868, 8.4 kB (checked in by fabien, 6 years ago)

[1.1] added autoload cache to CLI (closes #4882)

  • 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 set_include_path(dirname(__FILE__).'/../vendor'.PATH_SEPARATOR.get_include_path());
12
13 /**
14  * Base class for all symfony Propel tasks.
15  *
16  * @package    symfony
17  * @subpackage command
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 abstract class sfPropelBaseTask extends sfBaseTask
22 {
23   const CHECK_SCHEMA = true;
24   const DO_NOT_CHECK_SCHEMA = false;
25
26   static protected $done = false;
27
28   public function initialize(sfEventDispatcher $dispatcher, sfFormatter $formatter)
29   {
30     parent::initialize($dispatcher, $formatter);
31
32     if (!self::$done)
33     {
34       set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).'/../vendor');
35
36       $libDir = dirname(__FILE__).'/..';
37
38       $autoloader = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
39       $autoloader->addDirectory($libDir.'/vendor/creole');
40       $autoloader->addDirectory($libDir.'/vendor/propel');
41       $autoloader->addDirectory($libDir.'/creole');
42       $autoloader->addDirectory($libDir.'/propel');
43       $autoloader->setClassPath('Propel', $libDir.'/propel/sfPropelAutoload.php');
44       $autoloader->addDirectory(sfConfig::get('sf_lib_dir').'/model');
45       $autoloader->addDirectory(sfConfig::get('sf_lib_dir').'/form');
46       $autoloader->register();
47
48       self::$done = true;
49     }
50   }
51
52   protected function schemaToYML($checkSchema = self::CHECK_SCHEMA, $prefix = '')
53   {
54     $finder = sfFinder::type('file')->name('*schema.xml')->prune('doctrine');
55
56     $schemas = array_unique(array_merge($finder->in('config'), $finder->in(glob(sfConfig::get('sf_plugins_dir').DIRECTORY_SEPARATOR.'*'.DIRECTORY_SEPARATOR.'config'))));
57     if (self::CHECK_SCHEMA === $checkSchema && !count($schemas))
58     {
59       throw new sfCommandException('You must create a schema.xml file.');
60     }
61
62     $dbSchema = new sfPropelDatabaseSchema();
63     foreach ($schemas as $schema)
64     {
65       $dbSchema->loadXML($schema);
66
67       $this->logSection('schema', sprintf('converting "%s" to YML', $schema));
68
69       $localprefix = $prefix;
70
71       // change prefix for plugins
72       if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
73       {
74         $localprefix = $prefix.$match[1].'-';
75       }
76
77       // save converted xml files in original directories
78       $yml_file_name = str_replace('.xml', '.yml', basename($schema));
79
80       $file = str_replace(basename($schema), $prefix.$yml_file_name$schema);
81       $this->logSection('schema', sprintf('putting %s', $file));
82       file_put_contents($file, $dbSchema->asYAML());
83     }
84   }
85
86   protected function schemaToXML($checkSchema = self::CHECK_SCHEMA, $prefix = '')
87   {
88     $finder = sfFinder::type('file')->name('*schema.yml')->prune('doctrine');
89     $dirs = array('config');
90     if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/config'))
91     {
92       $dirs = array_merge($dirs, $pluginDirs);
93     }
94     $schemas = $finder->in($dirs);
95     if (self::CHECK_SCHEMA === $checkSchema && !count($schemas))
96     {
97       throw new sfCommandException('You must create a schema.yml file.');
98     }
99
100     $dbSchema = new sfPropelDatabaseSchema();
101
102     foreach ($schemas as $schema)
103     {
104       $schemaArray = sfYaml::load($schema);
105       
106       if (!is_array($schemaArray))
107       {
108         continue; // No defined schema here, skipping
109       }
110
111       if (!isset($schemaArray['classes']))
112       {
113         // Old schema syntax: we convert it
114         $schemaArray = $dbSchema->convertOldToNewYaml($schemaArray);
115       }
116
117       $customSchemaFilename = str_replace(array(
118         str_replace(DIRECTORY_SEPARATOR, '/', sfConfig::get('sf_root_dir')).'/',
119         'plugins/',
120         'config/',
121         '/',
122         'schema.yml'
123       ), array('', '', '', '_', 'schema.custom.yml'), $schema);
124       $customSchemas = sfFinder::type('file')->name($customSchemaFilename)->in($dirs);
125
126       foreach ($customSchemas as $customSchema)
127       {
128         $this->logSection('schema', sprintf('found custom schema %s', $customSchema));
129
130         $customSchemaArray = sfYaml::load($customSchema);
131         if (!isset($customSchemaArray['classes']))
132         {
133           // Old schema syntax: we convert it
134           $customSchemaArray = $dbSchema->convertOldToNewYaml($customSchemaArray);
135         }
136         $schemaArray = sfToolkit::arrayDeepMerge($schemaArray, $customSchemaArray);
137       }
138
139       $dbSchema->loadArray($schemaArray);
140
141       $this->logSection('schema', sprintf('converting "%s" to XML', $schema));
142
143       $localprefix = $prefix;
144
145       // change prefix for plugins
146       if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
147       {
148         $localprefix = $prefix.$match[1].'-';
149       }
150
151       // save converted xml files in original directories
152       $xml_file_name = str_replace('.yml', '.xml', basename($schema));
153
154       $file = str_replace(basename($schema), $localprefix.$xml_file_name$schema);
155       $this->logSection('schema', sprintf('putting %s', $file));
156       file_put_contents($file, $dbSchema->asXML());
157     }
158   }
159
160   protected function copyXmlSchemaFromPlugins($prefix = '')
161   {
162     if($dirs = glob(sfConfig::get('sf_plugins_dir').DIRECTORY_SEPARATOR.'*'.DIRECTORY_SEPARATOR.'config'))
163     {
164       $schemas = sfFinder::type('file')->name('*schema.xml')->prune('doctrine')->in($dirs);
165       foreach ($schemas as $schema)
166       {
167         // reset local prefix
168         $localprefix = '';
169
170         // change prefix for plugins
171         if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
172         {
173           // if the plugin name is not in the schema filename, add it
174           if (!strstr(basename($schema), $match[1]))
175           {
176             $localprefix = $match[1].'-';
177           }
178         }
179
180         // if the prefix is not in the schema filename, add it
181         if (!strstr(basename($schema), $prefix))
182         {
183           $localprefix = $prefix.$localprefix;
184         }
185
186         $this->getFilesystem()->copy($schema, 'config'.DIRECTORY_SEPARATOR.$localprefix.basename($schema));
187         if ('' === $localprefix)
188         {
189           $this->getFilesystem()->remove($schema);
190         }
191       }
192     }
193   }
194
195   protected function cleanup()
196   {
197     $finder = sfFinder::type('file')->name('generated-*schema.xml');
198     $this->getFilesystem()->remove($finder->in(array('config', 'plugins')));
199   }
200
201   protected function callPhing($taskName, $checkSchema)
202   {
203     $schemas = sfFinder::type('file')->name('*schema.xml')->relative()->follow_link()->in('config');
204     if (self::CHECK_SCHEMA === $checkSchema && !$schemas)
205     {
206       throw new sfCommandException('You must create a schema.yml or schema.xml file.');
207     }
208
209     // Call phing targets
210     if (false === strpos('propel-generator', get_include_path()))
211     {
212       set_include_path(sfConfig::get('sf_symfony_lib_dir').'/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes'.PATH_SEPARATOR.get_include_path());
213     }
214     set_include_path(sfConfig::get('sf_root_dir').PATH_SEPARATOR.get_include_path());
215
216     $args = array();
217
218     // Needed to include the right Propel builders
219     set_include_path(sfConfig::get('sf_symfony_lib_dir').PATH_SEPARATOR.get_include_path());
220
221     $options = array(
222       'project.dir'       => sfConfig::get('sf_config_dir'),
223       'build.properties'  => 'propel.ini',
224       'propel.output.dir' => sfConfig::get('sf_root_dir'),
225     );
226     foreach ($options as $key => $value)
227     {
228       $args[] = "-D$key=$value";
229     }
230
231     // Build file
232     $args[] = '-f';
233     $args[] = realpath(sfConfig::get('sf_symfony_lib_dir').'/plugins/sfPropelPlugin/lib/vendor/propel-generator/build.xml');
234
235     if (is_null($this->commandApplication) || !$this->commandApplication->isVerbose())
236     {
237       $args[] = '-q';
238     }
239
240     if (!is_null($this->commandApplication) && $this->commandApplication->withTrace())
241     {
242       $args[] = '-debug';
243     }
244
245     // Logger
246     if (DIRECTORY_SEPARATOR != '\\' && (function_exists('posix_isatty') && @posix_isatty(STDOUT)))
247     {
248       $args[] = '-logger';
249       $args[] = 'phing.listener.AnsiColorLogger';
250     }
251
252     $args[] = $taskName;
253
254     require_once dirname(__FILE__).'/sfPhing.class.php';
255
256     Phing::startup();
257     Phing::setProperty('phing.home', getenv('PHING_HOME'));
258
259     $m = new sfPhing();
260     $m->execute($args);
261     $m->runBuild();
262
263     chdir(sfConfig::get('sf_root_dir'));
264   }
265 }
266
Note: See TracBrowser for help on using the browser.