sfGenerateTaskTask.class.php 6.0 KB

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