Development

/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineGenerateModuleTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineGenerateModuleTask.class.php

Revision 24637, 9.7 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3, 1.4] fixed inconsistent case in doctrine crud (closes #7698, refs #5640)

  • 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__).'/sfDoctrineBaseTask.class.php');
12
13 /**
14  * Generates a Doctrine module.
15  *
16  * @package    symfony
17  * @subpackage doctrine
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfDoctrineGenerateModuleTask extends sfDoctrineBaseTask
22 {
23   /**
24    * @see sfTask
25    */
26   protected function configure()
27   {
28     $this->addArguments(array(
29       new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
30       new sfCommandArgument('module', sfCommandArgument::REQUIRED, 'The module name'),
31       new sfCommandArgument('model', sfCommandArgument::REQUIRED, 'The model class name'),
32     ));
33
34     $this->addOptions(array(
35       new sfCommandOption('theme', null, sfCommandOption::PARAMETER_REQUIRED, 'The theme name', 'default'),
36       new sfCommandOption('generate-in-cache', null, sfCommandOption::PARAMETER_NONE, 'Generate the module in cache'),
37       new sfCommandOption('non-verbose-templates', null, sfCommandOption::PARAMETER_NONE, 'Generate non verbose templates'),
38       new sfCommandOption('with-show', null, sfCommandOption::PARAMETER_NONE, 'Generate a show method'),
39       new sfCommandOption('singular', null, sfCommandOption::PARAMETER_REQUIRED, 'The singular name', null),
40       new sfCommandOption('plural', null, sfCommandOption::PARAMETER_REQUIRED, 'The plural name', null),
41       new sfCommandOption('route-prefix', null, sfCommandOption::PARAMETER_REQUIRED, 'The route prefix', null),
42       new sfCommandOption('with-doctrine-route', null, sfCommandOption::PARAMETER_NONE, 'Whether you will use a Doctrine route'),
43       new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
44       new sfCommandOption('actions-base-class', null, sfCommandOption::PARAMETER_REQUIRED, 'The base class for the actions', 'sfActions'),
45     ));
46
47     $this->aliases = array('doctrine-generate-crud', 'doctrine:generate-crud');
48     $this->namespace = 'doctrine';
49     $this->name = 'generate-module';
50     $this->briefDescription = 'Generates a Doctrine module';
51
52     $this->detailedDescription = <<<EOF
53 The [doctrine:generate-module|INFO] task generates a Doctrine module:
54
55   [./symfony doctrine:generate-module frontend article Article|INFO]
56
57 The task creates a [%module%|COMMENT] module in the [%application%|COMMENT] application
58 for the model class [%model%|COMMENT].
59
60 You can also create an empty module that inherits its actions and templates from
61 a runtime generated module in [%sf_app_cache_dir%/modules/auto%module%|COMMENT] by
62 using the [--generate-in-cache|COMMENT] option:
63
64   [./symfony doctrine:generate-module --generate-in-cache frontend article Article|INFO]
65
66 The generator can use a customized theme by using the [--theme|COMMENT] option:
67
68   [./symfony doctrine:generate-module --theme="custom" frontend article Article|INFO]
69
70 This way, you can create your very own module generator with your own conventions.
71
72 You can also change the default actions base class (default to sfActions) of
73 the generated modules:
74
75   [./symfony doctrine:generate-module --actions-base-class="ProjectActions" frontend article Article|INFO]
76 EOF;
77   }
78
79   /**
80    * @see sfTask
81    */
82   protected function execute($arguments = array(), $options = array())
83   {
84     $databaseManager = new sfDatabaseManager($this->configuration);
85     
86     $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
87
88     $this->constants = array(
89       'PROJECT_NAME'   => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
90       'APP_NAME'       => $arguments['application'],
91       'MODULE_NAME'    => $arguments['module'],
92       'UC_MODULE_NAME' => ucfirst($arguments['module']),
93       'MODEL_CLASS'    => $arguments['model'],
94       'AUTHOR_NAME'    => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
95     );
96
97     $method = $options['generate-in-cache'] ? 'executeInit' : 'executeGenerate';
98
99     $this->$method($arguments, $options);
100   }
101
102   protected function executeGenerate($arguments = array(), $options = array())
103   {
104     // generate module
105     $tmpDir = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
106     $generatorManager = new sfGeneratorManager($this->configuration, $tmpDir);
107     $generatorManager->generate('sfDoctrineGenerator', array(
108       'model_class'           => $arguments['model'],
109       'moduleName'            => $arguments['module'],
110       'theme'                 => $options['theme'],
111       'non_verbose_templates' => $options['non-verbose-templates'],
112       'with_show'             => $options['with-show'],
113       'singular'              => $options['singular'] ? $options['singular'] : sfInflector::underscore($arguments['model']),
114       'plural'                => $options['plural'] ? $options['plural'] : sfInflector::underscore($arguments['model'].'s'),
115       'route_prefix'          => $options['route-prefix'],
116       'with_doctrine_route'   => $options['with-doctrine-route'],
117       'actions_base_class'    => $options['actions-base-class'],
118     ));
119
120     $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
121
122     // copy our generated module
123     $this->getFilesystem()->mirror($tmpDir.DIRECTORY_SEPARATOR.'auto'.ucfirst($arguments['module']), $moduleDir, sfFinder::type('any'));
124
125     if (!$options['with-show'])
126     {
127       $this->getFilesystem()->remove($moduleDir.'/templates/showSuccess.php');
128     }
129
130     // change module name
131     $finder = sfFinder::type('file')->name('*.php');
132     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '', '', array('auto'.ucfirst($arguments['module']) => $arguments['module']));
133
134     // customize php and yml files
135     $finder = sfFinder::type('file')->name('*.php', '*.yml');
136     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
137
138     // create basic test
139     $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').DIRECTORY_SEPARATOR.'task'.DIRECTORY_SEPARATOR.'generator'.DIRECTORY_SEPARATOR.'skeleton'.DIRECTORY_SEPARATOR.'module'.DIRECTORY_SEPARATOR.'test'.DIRECTORY_SEPARATOR.'actionsTest.php', sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php');
140
141     // customize test file
142     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
143
144     // delete temp files
145     $this->getFilesystem()->remove(sfFinder::type('any')->in($tmpDir));
146   }
147
148   protected function executeInit($arguments = array(), $options = array())
149   {
150     $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
151
152     // create basic application structure
153     $finder = sfFinder::type('any')->discard('.sf');
154     $dirs = $this->configuration->getGeneratorSkeletonDirs('sfDoctrineModule', $options['theme']);
155
156     foreach ($dirs as $dir)
157     {
158       if (is_dir($dir))
159       {
160         $this->getFilesystem()->mirror($dir, $moduleDir, $finder);
161         break;
162       }
163     }
164
165     // move configuration file
166     if (file_exists($config = $moduleDir.'/lib/configuration.php'))
167     {
168       if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorConfiguration.class.php'))
169       {
170         $this->getFilesystem()->remove($config);
171       }
172       else
173       {
174         $this->getFilesystem()->rename($config, $target);
175       }
176     }
177
178     // move helper file
179     if (file_exists($config = $moduleDir.'/lib/helper.php'))
180     {
181       if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorHelper.class.php'))
182       {
183         $this->getFilesystem()->remove($config);
184       }
185       else
186       {
187         $this->getFilesystem()->rename($config, $target);
188       }
189     }
190
191     // create basic test
192     $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').DIRECTORY_SEPARATOR.'task'.DIRECTORY_SEPARATOR.'generator'.DIRECTORY_SEPARATOR.'skeleton'.DIRECTORY_SEPARATOR.'module'.DIRECTORY_SEPARATOR.'test'.DIRECTORY_SEPARATOR.'actionsTest.php', sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php');
193
194     // customize test file
195     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
196
197     // customize php and yml files
198     $finder = sfFinder::type('file')->name('*.php', '*.yml');
199     $this->constants['CONFIG'] = sprintf(<<<EOF
200     model_class:           %s
201     theme:                 %s
202     non_verbose_templates: %s
203     with_show:             %s
204     singular:              %s
205     plural:                %s
206     route_prefix:          %s
207     with_doctrine_route:   %s
208     actions_base_class:    %s
209 EOF
210     ,
211       $arguments['model'],
212       $options['theme'],
213       $options['non-verbose-templates'] ? 'true' : 'false',
214       $options['with-show'] ? 'true' : 'false',
215       $options['singular'] ? $options['singular'] : '~',
216       $options['plural'] ? $options['plural'] : '~',
217       $options['route-prefix'] ? $options['route-prefix'] : '~',
218       $options['with-doctrine-route'] ? 'true' : 'false',
219       $options['actions-base-class']
220     );
221     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
222   }
223 }
224
Note: See TracBrowser for help on using the browser.