forked from FriendsOfSymfony1/symfony1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfListTask.class.php
177 lines (146 loc) · 4.56 KB
/
sfListTask.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
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Lists tasks.
*
* @package symfony
* @subpackage task
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id$
*/
class sfListTask extends sfCommandApplicationTask
{
/**
* @see sfTask
*/
protected function configure()
{
$this->addArguments(array(
new sfCommandArgument('namespace', sfCommandArgument::OPTIONAL, 'The namespace name'),
));
$this->addOptions(array(
new sfCommandOption('xml', null, sfCommandOption::PARAMETER_NONE, 'To output help as XML'),
));
$this->briefDescription = 'Lists tasks';
$this->detailedDescription = <<<EOF
The [list|INFO] task lists all tasks:
[./symfony list|INFO]
You can also display the tasks for a specific namespace:
[./symfony list test|INFO]
You can also output the information as XML by using the [--xml|COMMENT] option:
[./symfony list --xml|INFO]
EOF;
}
/**
* @see sfTask
*/
protected function execute($arguments = array(), $options = array())
{
$tasks = array();
foreach ($this->commandApplication->getTasks() as $name => $task)
{
if ($arguments['namespace'] && $arguments['namespace'] != $task->getNamespace())
{
continue;
}
if ($name != $task->getFullName())
{
// it is an alias
continue;
}
if (!$task->getNamespace())
{
$name = '_default:'.$name;
}
$tasks[$name] = $task;
}
if ($options['xml'])
{
$this->outputAsXml($arguments['namespace'], $tasks);
}
else
{
$this->outputAsText($arguments['namespace'], $tasks);
}
}
protected function outputAsText($namespace, $tasks)
{
$this->commandApplication->help();
$this->log('');
$width = 0;
foreach ($tasks as $name => $task)
{
$width = strlen($task->getName()) > $width ? strlen($task->getName()) : $width;
}
$width += strlen($this->formatter->format(' ', 'INFO'));
$messages = array();
if ($namespace)
{
$messages[] = $this->formatter->format(sprintf("Available tasks for the \"%s\" namespace:", $namespace), 'COMMENT');
}
else
{
$messages[] = $this->formatter->format('Available tasks:', 'COMMENT');
}
// display tasks
ksort($tasks);
$currentNamespace = '';
foreach ($tasks as $name => $task)
{
if (!$namespace && $currentNamespace != $task->getNamespace())
{
$currentNamespace = $task->getNamespace();
$messages[] = $this->formatter->format($task->getNamespace(), 'COMMENT');
}
$aliases = $task->getAliases() ? $this->formatter->format(' ('.implode(', ', $task->getAliases()).')', 'COMMENT') : '';
$messages[] = sprintf(" %-{$width}s %s%s", $this->formatter->format(':'.$task->getName(), 'INFO'), $task->getBriefDescription(), $aliases);
}
$this->log($messages);
}
protected function outputAsXml($namespace, $tasks)
{
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->appendChild($symfonyXML = $dom->createElement('symfony'));
$symfonyXML->appendChild($tasksXML = $dom->createElement('tasks'));
if ($namespace)
{
$tasksXML->setAttribute('namespace', $namespace);
}
else
{
$symfonyXML->appendChild($namespacesXML = $dom->createElement('namespaces'));
}
// display tasks
ksort($tasks);
$currentNamespace = 'foobar';
$namespaceArrayXML = array();
foreach ($tasks as $name => $task)
{
if (!$namespace && $currentNamespace != $task->getNamespace())
{
$currentNamespace = $task->getNamespace();
$namespacesXML->appendChild($namespaceArrayXML[$task->getNamespace()] = $dom->createElement('namespace'));
$namespaceArrayXML[$task->getNamespace()]->setAttribute('id', $task->getNamespace() ?: '_global');
}
if (!$namespace)
{
$namespaceArrayXML[$task->getNamespace()]->appendChild($taskXML = $dom->createElement('task'));
$taskXML->appendChild($dom->createTextNode($task->getName()));
}
$taskXML = new DOMDocument('1.0', 'UTF-8');
$taskXML->formatOutput = true;
$taskXML->loadXML($task->asXml());
$node = $taskXML->getElementsByTagName('task')->item(0);
$node = $dom->importNode($node, true);
$tasksXML->appendChild($node);
}
echo $dom->saveXML();
}
}