Created
September 8, 2011 14:24
-
-
Save ratibus/1203516 to your computer and use it in GitHub Desktop.
symfony search task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class sfSearchTask extends sfCommandApplicationTask | |
{ | |
/** | |
* @see sfTask | |
*/ | |
protected function configure() | |
{ | |
$this->addArguments(array( | |
new sfCommandArgument('search', sfCommandArgument::REQUIRED, 'Search query'), | |
)); | |
$this->addOptions(array( | |
new sfCommandOption('verbose', null, sfCommandOption::PARAMETER_NONE, 'To output tasks help as well'), | |
)); | |
$this->briefDescription = 'Search task'; | |
} | |
/** | |
* @see sfTask | |
*/ | |
protected function execute($arguments = array(), $options = array()) | |
{ | |
$tasks = array(); | |
$width = 0; | |
$searchWords = explode('*', trim($arguments['search'], '*')); | |
foreach ($this->commandApplication->getTasks() as $name => $task) | |
{ | |
if ($name != $task->getFullName()) | |
{ | |
// it is an alias | |
continue; | |
} | |
$words = array( | |
$task->getNamespace(), | |
$task->getFullName(), | |
$task->getBriefDescription(), | |
); | |
$words = array_merge($words, $task->getAliases()); | |
$globalFound = true; | |
foreach ($searchWords as $searchWord) | |
{ | |
$found = false; | |
foreach ($words as $word) | |
{ | |
if (false !== stripos($word, $searchWord)) | |
{ | |
$found = true; | |
break; | |
} | |
} | |
if (!$found) | |
{ | |
$globalFound = false; | |
break; | |
} | |
} | |
if (!$globalFound) | |
{ | |
continue; | |
} | |
$width = max($width, strlen($task->getFullName())); | |
$tasks[] = $task; | |
} | |
$width += strlen($this->formatter->format(' ', 'INFO')); | |
if (count($tasks)) | |
{ | |
$withHelp = $options['verbose']; | |
$this->log($this->formatter->format(sprintf("Available tasks for the \"%s\" search:", $arguments['search']), 'COMMENT')); | |
$helpTask = new sfHelpTask($this->dispatcher, $this->formatter); | |
$helpTask->setCommandApplication($this->commandApplication); | |
foreach ($tasks as $task) | |
{ | |
$aliases = $task->getAliases() ? $this->formatter->format(' ('.implode(', ', $task->getAliases()).')', 'COMMENT') : ''; | |
$this->log(sprintf(" %-${width}s %s%s", $this->formatter->format($task->getFullName(), 'INFO'), $task->getBriefDescription(), $aliases)); | |
if ($withHelp) | |
{ | |
$helpTask->run(array($task->getFullName())); | |
} | |
} | |
return 0; | |
} | |
else | |
{ | |
$this->log('404 Not Found'); | |
return 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment