Development

/branches/1.4/lib/task/help/sfListTask.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/task/help/sfListTask.class.php

Revision 21428, 4.6 kB (checked in by fabien, 5 years ago)

[1.3] added an --xml option to the list and help tasks

  • 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) 2004-2006 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  * Lists tasks.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfListTask extends sfCommandApplicationTask
20 {
21   /**
22    * @see sfTask
23    */
24   protected function configure()
25   {
26     $this->addArguments(array(
27       new sfCommandArgument('namespace', sfCommandArgument::OPTIONAL, 'The namespace name'),
28     ));
29
30     $this->addOptions(array(
31       new sfCommandOption('xml', null, sfCommandOption::PARAMETER_NONE, 'To output help as XML'),
32     ));
33
34     $this->briefDescription = 'Lists tasks';
35
36     $this->detailedDescription = <<<EOF
37 The [list|INFO] task lists all tasks:
38
39   [./symfony list|INFO]
40
41 You can also display the tasks for a specific namespace:
42
43   [./symfony list test|INFO]
44
45 You can also output the information as XML by using the [--xml|COMMENT] option:
46
47   [./symfony list --xml|INFO]
48 EOF;
49   }
50
51   /**
52    * @see sfTask
53    */
54   protected function execute($arguments = array(), $options = array())
55   {
56     $tasks = array();
57     foreach ($this->commandApplication->getTasks() as $name => $task)
58     {
59       if ($arguments['namespace'] && $arguments['namespace'] != $task->getNamespace())
60       {
61         continue;
62       }
63
64       if ($name != $task->getFullName())
65       {
66         // it is an alias
67         continue;
68       }
69
70       if (!$task->getNamespace())
71       {
72         $name = '_default:'.$name;
73       }
74
75       $tasks[$name] = $task;
76     }
77
78     if ($options['xml'])
79     {
80       $this->outputAsXml($arguments['namespace'], $tasks);
81     }
82     else
83     {
84       $this->outputAsText($arguments['namespace'], $tasks);
85     }
86   }
87
88   protected function outputAsText($namespace, $tasks)
89   {
90     $this->commandApplication->help();
91     $this->log('');
92
93     $width = 0;
94     foreach ($tasks as $name => $task)
95     {
96       $width = strlen($task->getName()) > $width ? strlen($task->getName()) : $width;
97     }
98     $width += strlen($this->formatter->format('  ', 'INFO'));
99
100     $messages = array();
101     if ($namespace)
102     {
103       $messages[] = $this->formatter->format(sprintf("Available tasks for the \"%s\" namespace:", $namespace), 'COMMENT');
104     }
105     else
106     {
107       $messages[] = $this->formatter->format('Available tasks:', 'COMMENT');
108     }
109
110     // display tasks
111     ksort($tasks);
112     $currentNamespace = '';
113     foreach ($tasks as $name => $task)
114     {
115       if (!$namespace && $currentNamespace != $task->getNamespace())
116       {
117         $currentNamespace = $task->getNamespace();
118         $messages[] = $this->formatter->format($task->getNamespace(), 'COMMENT');
119       }
120
121       $aliases = $task->getAliases() ? $this->formatter->format(' ('.implode(', ', $task->getAliases()).')', 'COMMENT') : '';
122
123       $messages[] = sprintf("  %-${width}s %s%s", $this->formatter->format(':'.$task->getName(), 'INFO'), $task->getBriefDescription(), $aliases);
124     }
125
126     $this->log($messages);
127   }
128
129   protected function outputAsXml($namespace, $tasks)
130   {
131     $dom = new DOMDocument('1.0', 'UTF-8');
132     $dom->formatOutput = true;
133     $dom->appendChild($symfonyXML = $dom->createElement('symfony'));
134
135     $symfonyXML->appendChild($tasksXML = $dom->createElement('tasks'));
136
137     if ($namespace)
138     {
139       $tasksXML->setAttribute('namespace', $namespace);
140     }
141     else
142     {
143       $symfonyXML->appendChild($namespacesXML = $dom->createElement('namespaces'));
144     }
145
146     // display tasks
147     ksort($tasks);
148     $currentNamespace = 'foobar';
149     $namespaceArrayXML = array();
150     foreach ($tasks as $name => $task)
151     {
152       if (!$namespace && $currentNamespace != $task->getNamespace())
153       {
154         $currentNamespace = $task->getNamespace();
155         $namespacesXML->appendChild($namespaceArrayXML[$task->getNamespace()] = $dom->createElement('namespace'));
156
157         $namespaceArrayXML[$task->getNamespace()]->setAttribute('id', $task->getNamespace() ? $task->getNamespace() : '_global');
158       }
159
160       if (!$namespace)
161       {
162         $namespaceArrayXML[$task->getNamespace()]->appendChild($taskXML = $dom->createElement('task'));
163         $taskXML->appendChild($dom->createTextNode($task->getName()));
164       }
165
166       $taskXML = new DOMDocument('1.0', 'UTF-8');
167       $taskXML->formatOutput = true;
168       $taskXML->loadXML($task->asXml());
169       $node = $taskXML->getElementsByTagName('task')->item(0);
170       $node = $dom->importNode($node, true);
171
172       $tasksXML->appendChild($node);
173     }
174
175     echo $dom->saveXml();
176   }
177 }
178
Note: See TracBrowser for help on using the browser.