Skip to content

Instantly share code, notes, and snippets.

@ratibus
Created September 8, 2011 14:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ratibus/1203516 to your computer and use it in GitHub Desktop.
Save ratibus/1203516 to your computer and use it in GitHub Desktop.
symfony search task
<?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