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
/**
12
 * Creates a task skeleton
13
 *
14
 * @package    symfony
15
 * @subpackage task
16
 * @author     Francois Zaninotto <francois.zaninotto@symfony-project.com>
17
 */
18
class sfGenerateTaskTask extends sfBaseTask
19
{
20
  /**
21
   * @see sfTask
22
   */
23
  protected function configure()
24
  {
25
    $this->addArguments(array(
26
      new sfCommandArgument('task_name', sfCommandArgument::REQUIRED, 'The task name (can contain namespace)'),
27
    ));
28
 
29
    $this->addOptions(array(
30
      new sfCommandOption('dir', null, sfCommandOption::PARAMETER_REQUIRED, 'The directory to create the task in', 'lib/task'),
31
      new sfCommandOption('use-database', null, sfCommandOption::PARAMETER_REQUIRED, 'Whether the task needs model initialization to access database', sfConfig::get('sf_orm')),
32
      new sfCommandOption('brief-description', null, sfCommandOption::PARAMETER_REQUIRED, 'A brief task description (appears in task list)'),
33
    ));
34
 
35
    $this->namespace = 'generate';
36
    $this->name = 'task';
37
    $this->briefDescription = 'Creates a skeleton class for a new task';
38
 
39
    $this->detailedDescription = <<<EOF
40
The [generate:task|INFO] creates a new sfTask class based on the name passed as
41
argument:
42
 
43
  [./symfony generate:task namespace:name|INFO]
44
 
45
The [namespaceNameTask.class.php|COMMENT] skeleton task is created under the [lib/task/|COMMENT]
46
directory. Note that the namespace is optional.
47
 
48
If you want to create the file in another directory (relative to the project
49
root folder), pass it in the [--dir|COMMENT] option. This directory will be created
50
if it does not already exist.
51
 
52
  [./symfony generate:task namespace:name --dir=plugins/myPlugin/lib/task|INFO]
53
 
54
If you want the task to default to a connection other than [doctrine|COMMENT], provide
55
the name of this connection with the [--use-database|COMMENT] option:
56
 
57
  [./symfony generate:task namespace:name --use-database=main|INFO]
58
 
59
The [--use-database|COMMENT] option can also be used to disable database
60
initialization in the generated task:
61
 
62
  [./symfony generate:task namespace:name --use-database=false|INFO]
63
 
64
You can also specify a description:
65
 
66
  [./symfony generate:task namespace:name --brief-description="Does interesting things"|INFO]
67
EOF;
68
  }
69
 
70
  /**
71
   * @see sfTask
72
   */
73
  protected function execute($arguments = array(), $options = array())
74
  {
75
    $taskName = $arguments['task_name'];
76
    $taskNameComponents = explode(':', $taskName);
77
    $namespace = isset($taskNameComponents[1]) ? $taskNameComponents[0] : '';
78
    $name = isset($taskNameComponents[1]) ? $taskNameComponents[1] : $taskNameComponents[0];
79
    $taskClassName = str_replace('-', '', ($namespace ? $namespace.ucfirst($name) : $name)).'Task';
80
 
81
    // Validate the class name
82
    if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $taskClassName))
83
    {
84
      throw new sfCommandException(sprintf('The task class name "%s" is invalid.', $taskClassName));
85
    }
86
 
87
    $briefDescription = $options['brief-description'];
88
    $detailedDescription = <<<HED
89
The [$taskName|INFO] task does things.
90
Call it with:
91
 
92
  [php symfony $taskName|INFO]
93
HED;
94
 
95
    $useDatabase = sfToolkit::literalize($options['use-database']);
96
    $defaultConnection = is_string($useDatabase) ? $useDatabase : sfConfig::get('sf_orm');
97
 
98
    if ($useDatabase)
99
    {
100
      $content = <<<HED
101
<?php
102
 
103
class $taskClassName extends sfBaseTask
104
{
105
  protected function configure()
106
  {
107
    // // add your own arguments here
108
    // \$this->addArguments(array(
109
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
110
    // ));
111
 
112
    \$this->addOptions(array(
113
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
114
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
115
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', '$defaultConnection'),
116
      // add your own options here
117
    ));
118
 
119
    \$this->namespace        = '$namespace';
120
    \$this->name             = '$name';
121
    \$this->briefDescription = '$briefDescription';
122
    \$this->detailedDescription = <<<EOF
123
$detailedDescription
124
EOF;
125
  }
126
 
127
  protected function execute(\$arguments = array(), \$options = array())
128
  {
129
    // initialize the database connection
130
    \$databaseManager = new sfDatabaseManager(\$this->configuration);
131
    \$connection = \$databaseManager->getDatabase(\$options['connection'])->getConnection();
132
 
133
    // add your code here
134
  }
135
}
136
 
137
HED;
138
    }
139
    else
140
    {
141
      $content = <<<HED
142
<?php
143
 
144
class $taskClassName extends sfBaseTask
145
{
146
  protected function configure()
147
  {
148
    // // add your own arguments here
149
    // \$this->addArguments(array(
150
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
151
    // ));
152
 
153
    // // add your own options here
154
    // \$this->addOptions(array(
155
    //   new sfCommandOption('my_option', null, sfCommandOption::PARAMETER_REQUIRED, 'My option'),
156
    // ));
157
 
158
    \$this->namespace        = '$namespace';
159
    \$this->name             = '$name';
160
    \$this->briefDescription = '$briefDescription';
161
    \$this->detailedDescription = <<<EOF
162
$detailedDescription
163
EOF;
164
  }
165
 
166
  protected function execute(\$arguments = array(), \$options = array())
167
  {
168
    // add your code here
169
  }
170
}
171
 
172
HED;
173
    }
174
 
175
    // check that the task directory exists and that the task file doesn't exist
176
    if (!is_readable(sfConfig::get('sf_root_dir').'/'.$options['dir']))
177
    {
178
      $this->getFilesystem()->mkdirs($options['dir']);
179
    }
180
 
181
    $taskFile = sfConfig::get('sf_root_dir').'/'.$options['dir'].'/'.$taskClassName.'.class.php';
182
    if (is_readable($taskFile))
183
    {
184
      throw new sfCommandException(sprintf('A "%s" task already exists in "%s".', $taskName, $taskFile));
185
    }
186
 
187
    $this->logSection('task', sprintf('Creating "%s" task file', $taskFile));
188
    file_put_contents($taskFile, $content);
189
  }
190
}