-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix and amend console resolve and remove commands
- change --as-script/-s option name/shortcut to --machine-readable/-m to fix collision with core symfony 2.x console options (the long name was changed to provide additional clarity) - change option shortcut and name for "as-script/s" to "machine-readable/m" to fix collision with symfony-provided "shell/s" option from the 2.x series - align output formatting and internal implementation of remove command with the improvements already introduced to the resolve command in 1.9.0 - update help content for both remove and resolve commands to align with updated output formatting and better describe all available functionality of the commands - extracted common functionality between resolve and remove commands to a new shared abstract class to remove code duplication - updated CHANGELOG.md and UPGRADE.md to note BC break and detail additional changes - renamed test filter sets to more realistic names and added some additional ones too - completely rewrote remove command tests and refactored resolve command tests to enable shared fixtures and methods between the two - in test app, moved default web loader to descrete, new "web" named loader and made the "default" loader a chain loader that uses "web" as welll as many of the other ones
- Loading branch information
1 parent
0595999
commit 6b166b4
Showing
21 changed files
with
1,477 additions
and
903 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the `liip/LiipImagineBundle` project. | ||
* | ||
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Liip\ImagineBundle\Command; | ||
|
||
use Liip\ImagineBundle\Imagine\Cache\CacheManager; | ||
use Liip\ImagineBundle\Imagine\Data\DataManager; | ||
use Liip\ImagineBundle\Imagine\Filter\FilterManager; | ||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
abstract class AbstractCacheCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* @var OutputInterface | ||
*/ | ||
protected $output; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $machineReadable; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $actionFailures; | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
*/ | ||
protected function initializeInstState(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->output = $output; | ||
$this->machineReadable = $input->getOption('machine-readable'); | ||
$this->actionFailures = 0; | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* | ||
* @return array | ||
*/ | ||
protected function resolveFilters(InputInterface $input) | ||
{ | ||
$filters = $input->getOption('filter'); | ||
|
||
if (0 !== count($deprecated = $input->getOption('filters'))) { | ||
$filters = array_merge($filters, $deprecated); | ||
@trigger_error('The --filters option was deprecated in 1.9.0 and removed in 2.0.0. Use the --filter option instead.', E_USER_DEPRECATED); | ||
} | ||
|
||
if (0 === count($filters) && 0 === count($filters = array_keys($this->getFilterManager()->getFilterConfiguration()->all()))) { | ||
$this->output->writeln('<bg=red;fg=white> [ERROR] You do not have any configured filters available. </>'); | ||
} | ||
|
||
return $filters; | ||
} | ||
|
||
/** | ||
* @param string $command | ||
*/ | ||
protected function writeCommandHeading($command) | ||
{ | ||
if ($this->machineReadable) { | ||
return; | ||
} | ||
|
||
$title = sprintf('[liip/imagine-bundle] %s Image Caches', ucfirst($command)); | ||
$this->writeNewline(); | ||
$this->output->writeln(sprintf('<info>%s</info>', $title)); | ||
$this->output->writeln(str_repeat('=', strlen($title))); | ||
$this->writeNewline(); | ||
} | ||
|
||
/** | ||
* @param string[] $filters | ||
* @param string[] $targets | ||
* @param bool $glob | ||
*/ | ||
protected function writeResultSummary(array $filters, array $targets, $glob = false) | ||
{ | ||
if ($this->machineReadable) { | ||
return; | ||
} | ||
|
||
$targetCount = count($targets); | ||
$filterCount = count($filters); | ||
$actionCount = ($glob ? $filterCount : ($filterCount * $targetCount)) - $this->actionFailures; | ||
|
||
$this->writeNewline(); | ||
$this->output->writeln(vsprintf('<fg=black;bg=green> Completed %d %s (%d %s / %s %s) </>%s', array( | ||
$actionCount, | ||
$this->getPluralized($actionCount, 'operation'), | ||
$filterCount, | ||
$this->getPluralized($filterCount, 'filter'), | ||
$glob ? '?' : $targetCount, | ||
$this->getPluralized($targetCount, 'image'), | ||
$this->getResultSummaryFailureMarkup(), | ||
))); | ||
$this->writeNewline(); | ||
} | ||
|
||
/** | ||
* @param string $filter | ||
* @param string|null $target | ||
*/ | ||
protected function writeActionStart($filter, $target = null) | ||
{ | ||
if (!$this->machineReadable) { | ||
$this->output->write(' - '); | ||
} | ||
|
||
$this->output->write(sprintf('%s[%s] ', $target ?: '*', $filter)); | ||
} | ||
|
||
/** | ||
* @param string $result | ||
* @param bool $continued | ||
*/ | ||
protected function writeActionResult($result, $continued = true) | ||
{ | ||
$this->output->write($continued ? sprintf('%s: ', $result) : $result); | ||
|
||
if (!$continued) { | ||
$this->writeNewline(); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $detail | ||
*/ | ||
protected function writeActionDetail($detail) | ||
{ | ||
$this->output->write($detail); | ||
$this->writeNewline(); | ||
} | ||
|
||
/** | ||
* @param \Exception $exception | ||
*/ | ||
protected function writeActionException(\Exception $exception) | ||
{ | ||
$this->writeActionResult('failure'); | ||
$this->writeActionDetail($exception->getMessage()); | ||
++$this->actionFailures; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
protected function getReturnCode() | ||
{ | ||
return 0 === $this->actionFailures ? 0 : 255; | ||
} | ||
|
||
/** | ||
* @return CacheManager | ||
*/ | ||
protected function getCacheManager() | ||
{ | ||
static $manager; | ||
|
||
if (null === $manager) { | ||
$manager = $this->getContainer()->get('liip_imagine.cache.manager'); | ||
} | ||
|
||
return $manager; | ||
} | ||
|
||
/** | ||
* @return FilterManager | ||
*/ | ||
protected function getFilterManager() | ||
{ | ||
static $manager; | ||
|
||
if (null === $manager) { | ||
$manager = $this->getContainer()->get('liip_imagine.filter.manager'); | ||
} | ||
|
||
return $manager; | ||
} | ||
|
||
/** | ||
* @return DataManager | ||
*/ | ||
protected function getDataManager() | ||
{ | ||
static $manager; | ||
|
||
if (null === $manager) { | ||
$manager = $this->getContainer()->get('liip_imagine.data.manager'); | ||
} | ||
|
||
return $manager; | ||
} | ||
|
||
/** | ||
* @param int $count | ||
*/ | ||
private function writeNewline($count = 1) | ||
{ | ||
$this->output->write(str_repeat(PHP_EOL, $count)); | ||
} | ||
|
||
/** | ||
* @param int $size | ||
* @param string $word | ||
* | ||
* @return string | ||
*/ | ||
private function getPluralized($size, $word) | ||
{ | ||
return 1 === $size ? $word : sprintf('%ss', $word); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function getResultSummaryFailureMarkup() | ||
{ | ||
if (0 === $this->actionFailures) { | ||
return ''; | ||
} | ||
|
||
return vsprintf(' <fg=white;bg=red;options=bold> encountered %s %s </>', array( | ||
$this->actionFailures, | ||
$this->getPluralized($this->actionFailures, 'failure'), | ||
)); | ||
} | ||
} |
Oops, something went wrong.