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

Debug toolbar