Development

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

You must first sign up to be able to contribute.

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

Revision 23947, 9.8 kB (checked in by FabianLange, 5 years ago)

[1.4] merged changes in the 1.3 branch up to r23946

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