Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/task/sfPropelGenerateCrudTask.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/task/sfPropelGenerateCrudTask.class.php

Revision 10512, 7.0 kB (checked in by nicolas, 6 years ago)

[1.1] fixed "No connection params set for propel" PropelException thrown in Propel command line tasks under certain circumstances (refs #3323, #2702, #1915)

  • 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 CRUD module.
15  *
16  * @package    symfony
17  * @subpackage command
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfPropelGenerateCrudTask 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-atomic-actions', null, sfCommandOption::PARAMETER_NONE, 'Generate non atomic actions'),
38       new sfCommandOption('non-verbose-templates', null, sfCommandOption::PARAMETER_NONE, 'Generate non verbose templates'),
39       new sfCommandOption('with-show', null, sfCommandOption::PARAMETER_NONE, 'Generate a show method'),
40       new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
41     ));
42
43     $this->aliases = array('propel-generate-crud');
44     $this->namespace = 'propel';
45     $this->name = 'generate-crud';
46     $this->briefDescription = 'Generates a Propel CRUD module';
47
48     $this->detailedDescription = <<<EOF
49 The [propel:generate-crud|INFO] task generates a Propel CRUD module:
50
51   [./symfony propel:generate-crud frontend article Article|INFO]
52
53 The task creates a [%module%|COMMENT] module in the [%application%|COMMENT] application
54 for the model class [%model%|COMMENT].
55
56 You can also create an empty module that inherits its actions and templates from
57 a runtime generated module in [%sf_app_cache_dir%/modules/auto%module%|COMMENT] by
58 using the [--generate-in-cache|COMMENT] option:
59
60   [./symfony propel:generate-crud --generate-in-cache frontend article Article|INFO]
61
62 The generator can use a customized theme by using the [--theme|COMMENT] option:
63
64   [./symfony propel:generate-crud --theme="custom" frontend article Article|INFO]
65
66 This way, you can create your very own CRUD generator with your own conventions.
67 EOF;
68   }
69
70   /**
71    * @see sfTask
72    */
73   protected function execute($arguments = array(), $options = array())
74   {
75     $databaseManager = new sfDatabaseManager($this->configuration);
76     
77     $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
78
79     $this->constants = array(
80       'PROJECT_NAME' => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
81       'APP_NAME'     => $arguments['application'],
82       'MODULE_NAME'  => $arguments['module'],
83       'MODEL_CLASS'  => $arguments['model'],
84       'AUTHOR_NAME'  => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
85     );
86     
87     $method = $options['generate-in-cache'] ? 'executeInit' : 'executeGenerate';
88
89     $this->$method($arguments, $options);
90   }
91
92   protected function executeGenerate($arguments = array(), $options = array())
93   {
94     // generate module
95     $tmpDir = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
96     sfConfig::set('sf_module_cache_dir', $tmpDir);
97     $generatorManager = new sfGeneratorManager($this->configuration);
98     $generatorManager->generate('sfPropelCrudGenerator', array(
99       'model_class'           => $arguments['model'],
100       'moduleName'            => $arguments['module'],
101       'theme'                 => $options['theme'],
102       'non_atomic_actions'    => $options['non-atomic-actions'],
103       'non_verbose_templates' => $options['non-verbose-templates'],
104       'with_show'             => $options['with-show'],
105     ));
106
107     $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
108
109     // copy our generated module
110     $this->getFilesystem()->mirror($tmpDir.'/auto'.ucfirst($arguments['module']), $moduleDir, sfFinder::type('any'));
111
112     if (!$options['with-show'])
113     {
114       $this->getFilesystem()->remove($moduleDir.'/templates/showSuccess.php');
115     }
116
117     // change module name
118     $this->getFilesystem()->replaceTokens($moduleDir.'/actions/actions.class.php', '', '', array('auto'.ucfirst($arguments['module']) => $arguments['module']));
119
120     // customize php and yml files
121     $finder = sfFinder::type('file')->name('*.php', '*.yml');
122     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
123
124     // create basic test
125     $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').'/task/generator/skeleton/module/test/actionsTest.php', sfConfig::get('sf_test_dir').'/functional/'.$arguments['application'].'/'.$arguments['module'].'ActionsTest.php');
126
127     // customize test file
128     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').'/functional/'.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
129
130     // delete temp files
131     $this->getFilesystem()->remove(sfFinder::type('any')->in($tmpDir));
132   }
133
134   protected function executeInit($arguments = array(), $options = array())
135   {
136     $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
137
138     // create basic application structure
139     $finder = sfFinder::type('any')->discard('.sf');
140     $dirs = $this->configuration->getGeneratorSkeletonDirs('sfPropelCrud', $options['theme']);
141     foreach ($dirs as $dir)
142     {
143       if (is_dir($dir))
144       {
145         $this->getFilesystem()->mirror($dir, $moduleDir, $finder);
146         break;
147       }
148     }
149
150     // create basic test
151     $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').'/task/generator/skeleton/module/test/actionsTest.php', sfConfig::get('sf_test_dir').'/functional/'.$arguments['application'].'/'.$arguments['module'].'ActionsTest.php');
152
153     // customize test file
154     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').'/functional/'.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
155
156     // customize php and yml files
157     $finder = sfFinder::type('file')->name('*.php', '*.yml');
158     $this->constants['CONFIG'] = sprintf("    non_atomic_actions:    %s\n    non_verbose_templates: %s\n    with_show:             %s",
159       $options['non-atomic-actions'] ? 'true' : 'false',
160       $options['non-verbose-templates'] ? 'true' : 'false',
161       $options['with-show'] ? 'true' : 'false'
162     );
163     $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
164   }
165 }
166
Note: See TracBrowser for help on using the browser.