Development

/branches/1.4/lib/task/sfCommandApplicationTask.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/task/sfCommandApplicationTask.class.php

Revision 23651, 4.5 kB (checked in by fabien, 4 years ago)

[1.3] made sfTask::getRouting() lazy

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 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  * Base class for tasks that depends on a sfCommandApplication object.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 abstract class sfCommandApplicationTask extends sfTask
20 {
21   protected
22     $mailer = null,
23     $routing = null,
24     $commandApplication = null;
25
26   /**
27    * Sets the command application instance for this task.
28    *
29    * @param sfCommandApplication $commandApplication A sfCommandApplication instance
30    */
31   public function setCommandApplication(sfCommandApplication $commandApplication = null)
32   {
33     $this->commandApplication = $commandApplication;
34   }
35
36   /**
37    * @see sfTask
38    */
39   public function log($messages)
40   {
41     if (null === $this->commandApplication || $this->commandApplication->isVerbose())
42     {
43       parent::log($messages);
44     }
45   }
46
47   /**
48    * @see sfTask
49    */
50   public function logSection($section, $message, $size = null, $style = 'INFO')
51   {
52     if (null === $this->commandApplication || $this->commandApplication->isVerbose())
53     {
54       parent::logSection($section, $message, $size, $style);
55     }
56   }
57
58   /**
59    * Creates a new task object.
60    *
61    * @param  string $name The name of the task
62    *
63    * @return sfTask
64    *
65    * @throws LogicException If the current task has no command application
66    */
67   protected function createTask($name)
68   {
69     if (null === $this->commandApplication)
70     {
71       throw new LogicException('Unable to create a task as no command application is associated with this task yet.');
72     }
73
74     $task = $this->commandApplication->getTaskToExecute($name);
75
76     if ($task instanceof sfCommandApplicationTask)
77     {
78       $task->setCommandApplication($this->commandApplication);
79     }
80
81     return $task;
82   }
83
84   /**
85    * Executes another task in the context of the current one.
86    *
87    * @param  string  $name      The name of the task to execute
88    * @param  array   $arguments An array of arguments to pass to the task
89    * @param  array   $options   An array of options to pass to the task
90    *
91    * @return Boolean The returned value of the task run() method
92    *
93    * @see createTask()
94    */
95   protected function runTask($name, $arguments = array(), $options = array())
96   {
97     return $this->createTask($name)->run($arguments, $options);
98   }
99
100   /**
101    * Returns a mailer instance.
102    *
103    * Notice that your task should accept an application option.
104    * The mailer configuration is read from the current configuration
105    * instance, which is automatically created according to the current
106    * --application option.
107    *
108    * @return sfMailer A sfMailer instance
109    */
110   protected function getMailer()
111   {
112     if (!$this->mailer)
113     {
114       $this->mailer = $this->initializeMailer();
115     }
116
117     return $this->mailer;
118   }
119
120   protected function initializeMailer()
121   {
122     require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/classes/Swift.php';
123     Swift::registerAutoload();
124     sfMailer::initialize();
125
126     $config = sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml'));
127
128     return new $config['mailer']['class']($this->dispatcher, $config['mailer']['param']);
129   }
130
131   /**
132    * Returns a routing instance.
133    *
134    * Notice that your task should accept an application option.
135    * The routing configuration is read from the current configuration
136    * instance, which is automatically created according to the current
137    * --application option.
138    *
139    * @return sfRouting A sfRouting instance
140    */
141   protected function getRouting()
142   {
143     if (!$this->routing)
144     {
145       $this->routing = $this->initializeRouting();
146     }
147
148     return $this->routing;
149   }
150
151   protected function initializeRouting()
152   {
153     $config = sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml'));
154     $params = array_merge($config['routing']['param'], array('load_configuration' => false, 'logging' => false));
155
156     $handler = new sfRoutingConfigHandler();
157     $routes = $handler->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
158
159     $routing = new $config['routing']['class']($this->dispatcher, null, $params);
160     $routing->setRoutes($routes);
161
162     $this->dispatcher->notify(new sfEvent($routing, 'routing.load_configuration'));
163
164     return $routing;
165   }
166 }
167
Note: See TracBrowser for help on using the browser.