Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 24637 2009-12-01 05:06:21Z Kris.Wallsmith $
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->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
 
71
You can also change the default actions base class (default to sfActions) of
72
the generated modules:
73
 
74
  [./symfony doctrine: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
    $this->$method($arguments, $options);
99
  }
100
 
101
  protected function executeGenerate($arguments = array(), $options = array())
102
  {
103
    // generate module
104
    $tmpDir = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
105
    $generatorManager = new sfGeneratorManager($this->configuration, $tmpDir);
106
    $generatorManager->generate('sfDoctrineGenerator', array(
107
      'model_class'           => $arguments['model'],
108
      'moduleName'            => $arguments['module'],
109
      'theme'                 => $options['theme'],
110
      'non_verbose_templates' => $options['non-verbose-templates'],
111
      'with_show'             => $options['with-show'],
112
      'singular'              => $options['singular'] ? $options['singular'] : sfInflector::underscore($arguments['model']),
113
      'plural'                => $options['plural'] ? $options['plural'] : sfInflector::underscore($arguments['model'].'s'),
114
      'route_prefix'          => $options['route-prefix'],
115
      'with_doctrine_route'   => $options['with-doctrine-route'],
116
      'actions_base_class'    => $options['actions-base-class'],
117
    ));
118
 
119
    $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
120
 
121
    // copy our generated module
122
    $this->getFilesystem()->mirror($tmpDir.DIRECTORY_SEPARATOR.'auto'.ucfirst($arguments['module']), $moduleDir, sfFinder::type('any'));
123
 
124
    if (!$options['with-show'])
125
    {
126
      $this->getFilesystem()->remove($moduleDir.'/templates/showSuccess.php');
127
    }
128
 
129
    // change module name
130
    $finder = sfFinder::type('file')->name('*.php');
131
    $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '', '', array('auto'.ucfirst($arguments['module']) => $arguments['module']));
132
 
133
    // customize php and yml files
134
    $finder = sfFinder::type('file')->name('*.php', '*.yml');
135
    $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
136
 
137
    // create basic test
138
    $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');
139
 
140
    // customize test file
141
    $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
142
 
143
    // delete temp files
144
    $this->getFilesystem()->remove(sfFinder::type('any')->in($tmpDir));
145
  }
146
 
147
  protected function executeInit($arguments = array(), $options = array())
148
  {
149
    $moduleDir = sfConfig::get('sf_app_module_dir').'/'.$arguments['module'];
150
 
151
    // create basic application structure
152
    $finder = sfFinder::type('any')->discard('.sf');
153
    $dirs = $this->configuration->getGeneratorSkeletonDirs('sfDoctrineModule', $options['theme']);
154
 
155
    foreach ($dirs as $dir)
156
    {
157
      if (is_dir($dir))
158
      {
159
        $this->getFilesystem()->mirror($dir, $moduleDir, $finder);
160
        break;
161
      }
162
    }
163
 
164
    // move configuration file
165
    if (file_exists($config = $moduleDir.'/lib/configuration.php'))
166
    {
167
      if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorConfiguration.class.php'))
168
      {
169
        $this->getFilesystem()->remove($config);
170
      }
171
      else
172
      {
173
        $this->getFilesystem()->rename($config, $target);
174
      }
175
    }
176
 
177
    // move helper file
178
    if (file_exists($config = $moduleDir.'/lib/helper.php'))
179
    {
180
      if (file_exists($target = $moduleDir.'/lib/'.$arguments['module'].'GeneratorHelper.class.php'))
181
      {
182
        $this->getFilesystem()->remove($config);
183
      }
184
      else
185
      {
186
        $this->getFilesystem()->rename($config, $target);
187
      }
188
    }
189
 
190
    // create basic test
191
    $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');
192
 
193
    // customize test file
194
    $this->getFilesystem()->replaceTokens(sfConfig::get('sf_test_dir').DIRECTORY_SEPARATOR.'functional'.DIRECTORY_SEPARATOR.$arguments['application'].DIRECTORY_SEPARATOR.$arguments['module'].'ActionsTest.php', '##', '##', $this->constants);
195
 
196
    // customize php and yml files
197
    $finder = sfFinder::type('file')->name('*.php', '*.yml');
198
    $this->constants['CONFIG'] = sprintf(<<<EOF
199
    model_class:           %s
200
    theme:                 %s
201
    non_verbose_templates: %s
202
    with_show:             %s
203
    singular:              %s
204
    plural:                %s
205
    route_prefix:          %s
206
    with_doctrine_route:   %s
207
    actions_base_class:    %s
208
EOF
209
    ,
210
      $arguments['model'],
211
      $options['theme'],
212
      $options['non-verbose-templates'] ? 'true' : 'false',
213
      $options['with-show'] ? 'true' : 'false',
214
      $options['singular'] ? $options['singular'] : '~',
215
      $options['plural'] ? $options['plural'] : '~',
216
      $options['route-prefix'] ? $options['route-prefix'] : '~',
217
      $options['with-doctrine-route'] ? 'true' : 'false',
218
      $options['actions-base-class']
219
    );
220
    $this->getFilesystem()->replaceTokens($finder->in($moduleDir), '##', '##', $this->constants);
221
  }
222
}