1. sfCommandApplicationTask.class.php
  2. /** * Base class for tasks that depends on a sfCommandApplication object. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfCommandApplicationTask.class.php 23651 2009-11-06 08:08:54Z fabien $ */
  3. abstract class sfCommandApplicationTask extends sfTask
  4. {
  5. protected
  6. $mailer = null,
  7. $routing = null,
  8. $commandApplication = null;
  9. /**
  10. * Sets the command application instance for this task.
  11. *
  12. * @param sfCommandApplication $commandApplication A sfCommandApplication instance
  13. */
  14. public function setCommandApplication(sfCommandApplication $commandApplication = null)
  15. {
  16. $this->commandApplication = $commandApplication;
  17. }
  18. /**
  19. * @see sfTask
  20. */
  21. public function log($messages)
  22. {
  23. if (null === $this->commandApplication || $this->commandApplication->isVerbose())
  24. {
  25. parent::log($messages);
  26. }
  27. }
  28. /**
  29. * @see sfTask
  30. */
  31. public function logSection($section, $message, $size = null, $style = 'INFO')
  32. {
  33. if (null === $this->commandApplication || $this->commandApplication->isVerbose())
  34. {
  35. parent::logSection($section, $message, $size, $style);
  36. }
  37. }
  38. /**
  39. * Creates a new task object.
  40. *
  41. * @param string $name The name of the task
  42. *
  43. * @return sfTask
  44. *
  45. * @throws LogicException If the current task has no command application
  46. */
  47. protected function createTask($name)
  48. {
  49. if (null === $this->commandApplication)
  50. {
  51. throw new LogicException('Unable to create a task as no command application is associated with this task yet.');
  52. }
  53. $task = $this->commandApplication->getTaskToExecute($name);
  54. if ($task instanceof sfCommandApplicationTask)
  55. {
  56. $task->setCommandApplication($this->commandApplication);
  57. }
  58. return $task;
  59. }
  60. /**
  61. * Executes another task in the context of the current one.
  62. *
  63. * @param string $name The name of the task to execute
  64. * @param array $arguments An array of arguments to pass to the task
  65. * @param array $options An array of options to pass to the task
  66. *
  67. * @return Boolean The returned value of the task run() method
  68. *
  69. * @see createTask()
  70. */
  71. protected function runTask($name, $arguments = array(), $options = array())
  72. {
  73. return $this->createTask($name)->run($arguments, $options);
  74. }
  75. /**
  76. * Returns a mailer instance.
  77. *
  78. * Notice that your task should accept an application option.
  79. * The mailer configuration is read from the current configuration
  80. * instance, which is automatically created according to the current
  81. * --application option.
  82. *
  83. * @return sfMailer A sfMailer instance
  84. */
  85. protected function getMailer()
  86. {
  87. if (!$this->mailer)
  88. {
  89. $this->mailer = $this->initializeMailer();
  90. }
  91. return $this->mailer;
  92. }
  93. protected function initializeMailer()
  94. {
  95. require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/classes/Swift.php';
  96. Swift::registerAutoload();
  97. sfMailer::initialize();
  98. $config = sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml'));
  99. return new $config['mailer']['class']($this->dispatcher, $config['mailer']['param']);
  100. }
  101. /**
  102. * Returns a routing instance.
  103. *
  104. * Notice that your task should accept an application option.
  105. * The routing configuration is read from the current configuration
  106. * instance, which is automatically created according to the current
  107. * --application option.
  108. *
  109. * @return sfRouting A sfRouting instance
  110. */
  111. protected function getRouting()
  112. {
  113. if (!$this->routing)
  114. {
  115. $this->routing = $this->initializeRouting();
  116. }
  117. return $this->routing;
  118. }
  119. protected function initializeRouting()
  120. {
  121. $config = sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml'));
  122. $params = array_merge($config['routing']['param'], array('load_configuration' => false, 'logging' => false));
  123. $handler = new sfRoutingConfigHandler();
  124. $routes = $handler->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
  125. $routing = new $config['routing']['class']($this->dispatcher, null, $params);
  126. $routing->setRoutes($routes);
  127. $this->dispatcher->notify(new sfEvent($routing, 'routing.load_configuration'));
  128. return $routing;
  129. }
  130. }

Debug toolbar