Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBuildModelTask.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBuildModelTask.class.php

Revision 14213, 4.3 kB (checked in by Jonathan.Wage, 5 years ago)

[1.2] sfDoctrinePlugin: fixes issue with application option in tasks (closes #5238)

  • Property svn:keywords set to Id Date Author Revision HeadURL
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) Jonathan H. Wage <jonwage@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
13
14 /**
15  * Create classes for the current model.
16  *
17  * @package    symfony
18  * @subpackage doctrine
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Jonathan H. Wage <jonwage@gmail.com>
21  * @version    SVN: $Id$
22  */
23 class sfDoctrineBuildModelTask extends sfDoctrineBaseTask
24 {
25   /**
26    * @see sfTask
27    */
28   protected function configure()
29   {
30     $this->addOptions(array(
31       new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
32       new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
33     ));
34
35     $this->aliases = array('doctrine-build-model');
36     $this->namespace = 'doctrine';
37     $this->name = 'build-model';
38     $this->briefDescription = 'Creates classes for the current model';
39
40     $this->detailedDescription = <<<EOF
41 The [doctrine:build-model|INFO] task creates model classes from the schema:
42
43   [./symfony doctrine:build-model|INFO]
44
45 The task read the schema information in [config/doctrine/*.yml|COMMENT]
46 from the project and all installed plugins.
47
48 The model classes files are created in [lib/model/doctrine|COMMENT].
49
50 This task never overrides custom classes in [lib/model/doctrine|COMMENT].
51 It only replaces files in [lib/model/doctrine/base|COMMENT].
52 EOF;
53   }
54
55   /**
56    * @see sfTask
57    */
58   protected function execute($arguments = array(), $options = array())
59   {
60     $this->logSection('doctrine', 'generating model classes');
61
62     $config = $this->getCliConfig();
63
64     $this->_checkForPackageParameter($config['yaml_schema_path']);
65
66     $tmpPath = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp';
67
68     if (!file_exists($tmpPath))
69     {
70       Doctrine_Lib::makeDirectories($tmpPath);
71     }
72
73     $plugins = $this->configuration->getPlugins();
74     foreach ($this->configuration->getAllPluginPaths() as $plugin => $path)
75     {
76       if (!in_array($plugin, $plugins))
77       {
78         continue;
79       }
80       $schemas = sfFinder::type('file')->name('*.yml')->in($path.'/config/doctrine');
81       foreach ($schemas as $schema)
82       {
83         $tmpSchemaPath = $tmpPath.DIRECTORY_SEPARATOR.$plugin.'-'.basename($schema);
84
85         $models = Doctrine_Parser::load($schema, 'yml');
86         if (!isset($models['package']))
87         {
88           $models['package'] = $plugin.'.lib.model.doctrine';
89           $models['package_custom_path'] = $path.'/lib/model/doctrine';
90         }
91         Doctrine_Parser::dump($models, 'yml', $tmpSchemaPath);
92       }
93     }
94
95     $options = array('generateBaseClasses'  => true,
96                      'generateTableClasses' => true,
97                      'packagesPath'         => sfConfig::get('sf_plugins_dir'),
98                      'packagesPrefix'       => 'Plugin',
99                      'suffix'               => '.class.php',
100                      'baseClassesDirectory' => 'base',
101                      'baseClassName'        => 'sfDoctrineRecord');
102     $options = array_merge($options, sfConfig::get('doctrine_model_builder_options', array()));
103
104     $import = new Doctrine_Import_Schema();
105     $import->setOptions($options);
106     $import->importSchema(array($tmpPath, $config['yaml_schema_path']), 'yml', $config['models_path']);
107   }
108
109   /**
110    * Check for package parameter in main schema files.
111    * sfDoctrinePlugin uses the package feature of Doctrine
112    * for plugins and cannot be used by the user
113    *
114    * @param string $path
115    * @return void
116    */
117   protected function _checkForPackageParameter($path)
118   {
119     $files = sfFinder::type('file')->name('*.yml')->in($path);
120     foreach ($files as $file)
121     {
122       $array = sfYaml::load($file);
123       if (is_array($array) AND !empty($array))
124       {
125         foreach ($array as $key => $value)
126         {
127           if ($key == 'package' || (is_array($value) && isset($value['package'])))
128           {
129             throw new sfDoctrineException(
130               sprintf('Cannot use package parameter in symfony Doctrine schema files. Found in "%s"', $file)
131             );
132           }
133         }
134       }
135     }
136   }
137 }
Note: See TracBrowser for help on using the browser.