Development

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

You must first sign up to be able to contribute.

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

Revision 12634, 9.2 kB (checked in by Jonathan.Wage, 6 years ago)

[1.2] sfDoctrinePlugin: initial entry of new admin generators for Doctrine

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: sfDoctrineGenerateModuleTask.class.php 12474 2008-10-31 10:41:27Z fabien $
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     ));
45
46     $this->aliases = array('doctrine-generate-crud', 'doctrine:generate-crud');
47     $this->namespace = 'doctrine';
48     $this->name = 'generate-module';
49     $this->briefDescription = 'Generates a Doctrine module';
50
51     $this->detailedDescription = <<<EOF
52 The [doctrine:generate-module|INFO] task generates a Doctrine module:
53
54   [./symfony doctrine: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 doctrine: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 doctrine: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 EOF;
71   }
72
73   /**
74    * @see sfTask
75    */
76   protected function execute($arguments = array(), $options = array())
77   {
78     $databaseManager = new sfDatabaseManager($this->configuration);
79     
80     $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
81
82     $this->constants = array(
83       'PROJECT_NAME'   => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
84       'APP_NAME'       => $arguments['application'],
85       'MODULE_NAME'    => $arguments['module'],
86       'UC_MODULE_NAME' => ucfirst($arguments['module']),
87       'MODEL_CLASS'    => $arguments['model'],
88       'AUTHOR_NAME'    => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
89     );
90
91     $method = $options['generate-in-cache'] ? 'executeInit' : 'executeGenerate';
92
93     $this->$method($arguments, $options);
94   }
95
96   protected function executeGenerate($arguments = array(), $options = array())
97   {
98     // generate module
99     $tmpDir = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
100     $generatorManager = new sfGeneratorManager($this->configuration, $tmpDir);
101     $generatorManager->generate('sfDoctrineGenerator', array(
102       'model_class'           => $arguments['model'],
103       'moduleName'            => $arguments['module'],
104       'theme'                 => $options['theme'],
105       'non_verbose_templates' => $options['non-verbose-templates'],
106       'with_show'             => $options['with-show'],
107       'singular'              => $options['singular'],
108       'plural'                => $options['plural'],
109       'route_prefix'          => $options['route-prefix'],
110       'with_doctrine_route'     => $options['with-doctrine-route'],
111     ));
112
113     $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
114
115     // copy our generated module
116     $this->getFilesystem()->mirror($tmpDir.DIRECTORY_SEPARATOR.'auto'.ucfirst($arguments['module']), $moduleDir, sfFinder::type('any'));
117
118     if (!$options['with-show'])
119     {
120       $this->getFilesystem()->remove($moduleDir.'/templates/showSuccess.php');
121     }
122
123     // change module name
124     $finder = sfFinder::type('file')->name('*.php');
125     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '', '', array('auto'.ucfirst($arguments['module']) => $arguments['module']));
126
127     // customize php and yml files
128     $finder = sfFinder::type('file')->name('*.php', '*.yml');
129     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
130
131     // create basic test
132     $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');
133
134     // customize test file
135     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
136
137     // delete temp files
138     $this->getFilesystem()->remove(sfFinder::type('any')->in($tmpDir));
139   }
140
141   protected function executeInit($arguments = array(), $options = array())
142   {
143     $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
144
145     // create basic application structure
146     $finder = sfFinder::type('any')->discard('.sf');
147     $dirs = $this->configuration->getGeneratorSkeletonDirs('sfDoctrineModule', $options['theme']);
148
149     foreach ($dirs as $dir)
150     {
151       if (is_dir($dir))
152       {
153         $this->getFilesystem()->mirror($dir, $moduleDir, $finder);
154         break;
155       }
156     }
157
158     // move configuration file
159     if (file_exists($config = $moduleDir.'/lib/configuration.php'))
160     {
161       if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorConfiguration.class.php'))
162       {
163         $this->getFilesystem()->remove($config);
164       }
165       else
166       {
167         $this->getFilesystem()->rename($config, $target);
168       }
169     }
170
171     // move helper file
172     if (file_exists($config = $moduleDir.'/lib/helper.php'))
173     {
174       if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorHelper.class.php'))
175       {
176         $this->getFilesystem()->remove($config);
177       }
178       else
179       {
180         $this->getFilesystem()->rename($config, $target);
181       }
182     }
183
184     // create basic test
185     $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');
186
187     // customize test file
188     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
189
190     // customize php and yml files
191     $finder = sfFinder::type('file')->name('*.php', '*.yml');
192     $this->constants['CONFIG'] = sprintf(<<<EOF
193     model_class:           %s
194     theme:                 %s
195     non_verbose_templates: %s
196     with_show:             %s
197     singular:              %s
198     plural:                %s
199     route_prefix:          %s
200     with_doctrine_route:     %s
201 EOF
202     ,
203       $arguments['model'],
204       $options['theme'],
205       $options['non-verbose-templates'] ? 'true' : 'false',
206       $options['with-show'] ? 'true' : 'false',
207       $options['singular'] ? $options['singular'] : '~',
208       $options['plural'] ? $options['plural'] : '~',
209       $options['route-prefix'] ? $options['route-prefix'] : '~',
210       $options['with-doctrine-route'] ? $options['with-doctrine-route'] : 'false'
211     );
212     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
213   }
214 }
Note: See TracBrowser for help on using the browser.