-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsfCommandApplicationTask.class.php
228 lines (197 loc) · 6.62 KB
/
sfCommandApplicationTask.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Base class for tasks that depends on a sfCommandApplication object.
*
* @author Fabien Potencier <[email protected]>
*
* @property sfApplicationConfiguration $configuration
*/
abstract class sfCommandApplicationTask extends sfTask
{
/** @var sfSymfonyCommandApplication */
protected $commandApplication;
/** @var sfMailer */
private $mailer;
/** @var sfRouting */
private $routing;
/** @var sfServiceContainer */
private $serviceContainer;
private $factoryConfiguration;
/**
* Sets the command application instance for this task.
*
* @param sfCommandApplication $commandApplication A sfCommandApplication instance
*/
public function setCommandApplication(sfCommandApplication $commandApplication = null)
{
$this->commandApplication = $commandApplication;
}
/**
* @see sfTask
*/
public function log($messages)
{
if (null === $this->commandApplication || $this->commandApplication->isVerbose()) {
parent::log($messages);
}
}
/**
* @see sfTask
*
* @param mixed|null $size
*/
public function logSection($section, $message, $size = null, $style = 'INFO')
{
if (null === $this->commandApplication || $this->commandApplication->isVerbose()) {
parent::logSection($section, $message, $size, $style);
}
}
/**
* Retrieves a service from the service container.
*
* @param string $id The service identifier
*
* @return object The service instance
*/
public function getService($id)
{
return $this->getServiceContainer()->getService($id);
}
/**
* Creates a new task object.
*
* @param string $name The name of the task
*
* @return sfTask
*
* @throws LogicException If the current task has no command application
*/
protected function createTask($name)
{
if (null === $this->commandApplication) {
throw new LogicException('Unable to create a task as no command application is associated with this task yet.');
}
$task = $this->commandApplication->getTaskToExecute($name);
if ($task instanceof sfCommandApplicationTask) {
$task->setCommandApplication($this->commandApplication);
}
return $task;
}
/**
* Executes another task in the context of the current one.
*
* @param string $name The name of the task to execute
* @param array $arguments An array of arguments to pass to the task
* @param array $options An array of options to pass to the task
*
* @return bool The returned value of the task run() method
*
* @see createTask()
*/
protected function runTask($name, $arguments = array(), $options = array())
{
return $this->createTask($name)->run($arguments, $options);
}
/**
* Returns a mailer instance.
*
* Notice that your task should accept an application option.
* The mailer configuration is read from the current configuration
* instance, which is automatically created according to the current
* --application option.
*
* @return sfMailer A sfMailer instance
*/
protected function getMailer()
{
if (null === $this->mailer) {
$this->mailer = $this->initializeMailer();
}
return $this->mailer;
}
/**
* Initialize mailer.
*
* @return sfMailer A sfMailer instance
*/
protected function initializeMailer()
{
require_once sfConfig::get('sf_symfony_lib_dir').'/config/autoload/swift.php';
$config = $this->getFactoryConfiguration();
return new $config['mailer']['class']($this->dispatcher, $config['mailer']['param']);
}
/**
* Returns a routing instance.
*
* Notice that your task should accept an application option.
* The routing configuration is read from the current configuration
* instance, which is automatically created according to the current
* --application option.
*
* @return sfRouting A sfRouting instance
*/
protected function getRouting()
{
if (null === $this->routing) {
$this->routing = $this->initializeRouting();
}
return $this->routing;
}
/**
* Initialize routing.
*
* @return sfRouting A sfRouting instance
*/
protected function initializeRouting()
{
$config = $this->getFactoryConfiguration();
$params = array_merge($config['routing']['param'], array('load_configuration' => false, 'logging' => false));
$handler = new sfRoutingConfigHandler();
$routes = $handler->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
/** @var sfRouting $routing */
$routing = new $config['routing']['class']($this->dispatcher, null, $params);
$routing->setRoutes($routes);
$this->dispatcher->notify(new sfEvent($routing, 'routing.load_configuration'));
return $routing;
}
/**
* Returns the service container instance.
*
* Notice that your task should accept an application option.
* The routing configuration is read from the current configuration
* instance, which is automatically created according to the current
* --application option.
*
* @return sfServiceContainer An application service container
*/
protected function getServiceContainer()
{
if (null === $this->serviceContainer) {
$class = require $this->configuration->getConfigCache()->checkConfig('config/services.yml', true);
$this->serviceContainer = new $class();
$this->serviceContainer->setService('sf_event_dispatcher', $this->dispatcher);
$this->serviceContainer->setService('sf_formatter', $this->formatter);
$this->serviceContainer->setService('sf_routing', $this->getRouting());
}
return $this->serviceContainer;
}
/**
* Gets the factory configuration.
*
* @return array
*/
protected function getFactoryConfiguration()
{
if (null === $this->factoryConfiguration) {
$this->factoryConfiguration = sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml'));
}
return $this->factoryConfiguration;
}
}