Development

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

You must first sign up to be able to contribute.

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

Revision 23930, 9.9 kB (checked in by FabianLange, 5 years ago)

[1.3] fixed defaults for singular and plural name for generate crud tasks in case none are specified (refs #5640)

  • 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  * Generates a Propel module.
15  *
16  * @package    symfony
17  * @subpackage propel
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfPropelGenerateModuleTask extends sfPropelBaseTask
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-propel-route', null, sfCommandOption::PARAMETER_NONE, 'Whether you will use a Propel 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('propel-generate-crud', 'propel:generate-crud');
48     $this->namespace = 'propel';
49     $this->name = 'generate-module';
50     $this->briefDescription = 'Generates a Propel module';
51
52     $this->detailedDescription = <<<EOF
53 The [propel:generate-module|INFO] task generates a Propel module:
54
55   [./symfony propel: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 propel: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 propel: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 propel: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     // for backwarads compatibility symfony uses the model name as singular and plural form if none specified (#5640)
100     $options['singular']  = $options['singular'] ? $options['singular'] : $arguments['model'];
101     $options['plural']  = $options['plural'] ? $options['plural'] : $arguments['model'].'s';
102
103     $this->$method($arguments, $options);
104   }
105
106   protected function executeGenerate($arguments = array(), $options = array())
107   {
108     // generate module
109     $tmpDir = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
110     $generatorManager = new sfGeneratorManager($this->configuration, $tmpDir);
111     $generatorManager->generate('sfPropelGenerator', array(
112       'model_class'           => $arguments['model'],
113       'moduleName'            => $arguments['module'],
114       'theme'                 => $options['theme'],
115       'non_verbose_templates' => $options['non-verbose-templates'],
116       'with_show'             => $options['with-show'],
117       'singular'              => $options['singular'],
118       'plural'                => $options['plural'],
119       'route_prefix'          => $options['route-prefix'],
120       'with_propel_route'     => $options['with-propel-route'],
121       'actions_base_class'    => $options['actions-base-class'],
122     ));
123
124     $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
125
126     // copy our generated module
127     $this->getFilesystem()->mirror($tmpDir.DIRECTORY_SEPARATOR.'auto'.ucfirst($arguments['module']), $moduleDir, sfFinder::type('any'));
128
129     if (!$options['with-show'])
130     {
131       $this->getFilesystem()->remove($moduleDir.'/templates/showSuccess.php');
132     }
133
134     // change module name
135     $finder = sfFinder::type('file')->name('*.php');
136     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '', '', array('auto'.ucfirst($arguments['module']) => $arguments['module']));
137
138     // customize php and yml files
139     $finder = sfFinder::type('file')->name('*.php', '*.yml');
140     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
141
142     // create basic test
143     $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');
144
145     // customize test file
146     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
147
148     // delete temp files
149     $this->getFilesystem()->remove(sfFinder::type('any')->in($tmpDir));
150   }
151
152   protected function executeInit($arguments = array(), $options = array())
153   {
154     $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
155
156     // create basic application structure
157     $finder = sfFinder::type('any')->discard('.sf');
158     $dirs = $this->configuration->getGeneratorSkeletonDirs('sfPropelModule', $options['theme']);
159
160     foreach ($dirs as $dir)
161     {
162       if (is_dir($dir))
163       {
164         $this->getFilesystem()->mirror($dir, $moduleDir, $finder);
165         break;
166       }
167     }
168
169     // move configuration file
170     if (file_exists($config = $moduleDir.'/lib/configuration.php'))
171     {
172       if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorConfiguration.class.php'))
173       {
174         $this->getFilesystem()->remove($config);
175       }
176       else
177       {
178         $this->getFilesystem()->rename($config, $target);
179       }
180     }
181
182     // move helper file
183     if (file_exists($config = $moduleDir.'/lib/helper.php'))
184     {
185       if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorHelper.class.php'))
186       {
187         $this->getFilesystem()->remove($config);
188       }
189       else
190       {
191         $this->getFilesystem()->rename($config, $target);
192       }
193     }
194
195     // create basic test
196     $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');
197
198     // customize test file
199     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
200
201     // customize php and yml files
202     $finder = sfFinder::type('file')->name('*.php', '*.yml');
203     $this->constants['CONFIG'] = sprintf(<<<EOF
204     model_class:           %s
205     theme:                 %s
206     non_verbose_templates: %s
207     with_show:             %s
208     singular:              %s
209     plural:                %s
210     route_prefix:          %s
211     with_propel_route:     %s
212     actions_base_class:    %s
213 EOF
214     ,
215       $arguments['model'],
216       $options['theme'],
217       $options['non-verbose-templates'] ? 'true' : 'false',
218       $options['with-show'] ? 'true' : 'false',
219       $options['singular'] ? $options['singular'] : '~',
220       $options['plural'] ? $options['plural'] : '~',
221       $options['route-prefix'] ? $options['route-prefix'] : '~',
222       $options['with-propel-route'] ? $options['with-propel-route'] : 'false',
223       $options['actions-base-class']
224     );
225     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
226   }
227 }
228
Note: See TracBrowser for help on using the browser.