Skip to content

Commit

Permalink
fix and amend console resolve and remove commands
Browse files Browse the repository at this point in the history
- 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
robfrawley committed Sep 9, 2017
1 parent 0595999 commit 6b166b4
Show file tree
Hide file tree
Showing 21 changed files with 1,477 additions and 903 deletions.
2 changes: 1 addition & 1 deletion Binary/Loader/FileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FileSystemLoader implements LoaderInterface
*
* @param MimeTypeGuesserInterface $mimeGuesser
* @param ExtensionGuesserInterface $extensionGuesser
* @param LocatorInterface
* @param LocatorInterface $locator
*/
public function __construct(MimeTypeGuesserInterface $mimeGuesser, ExtensionGuesserInterface $extensionGuesser, $locator)
{
Expand Down
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ This project adheres to [semantic versioning](http://semver.org/spec/v2.0.0.html
## [Unreleased](https://github.com/liip/LiipImagineBundle/tree/HEAD)

*Note: Recent developments can be tracked via the
[latest changelog](https://github.com/liip/LiipImagineBundle/compare/1.9.0...HEAD), the
[active milestone](https://github.com/liip/LiipImagineBundle/milestone/15), as well as all
[latest changelog](https://github.com/liip/LiipImagineBundle/compare/1.9.1...HEAD), the
[active milestone](https://github.com/liip/LiipImagineBundle/milestone/16), as well as all
[open milestones](https://github.com/liip/LiipImagineBundle/milestones).*


## [v1.9.1](https://github.com/liip/LiipImagineBundle/tree/1.9.1)

*Released on* 2017-09-08 *and assigned* [`1.9.1`](https://github.com/liip/LiipImagineBundle/releases/tag/1.9.1) *tag \([view verbose changelog](https://github.com/liip/LiipImagineBundle/compare/1.9.0...1.9.1)\).*

- __\[Console\]__ __\[BC BREAK\]__ The resolve command's --as-script/-s option/shortcut renamed to --machine-readable/-m \(fixes [\#988](https://github.com/liip/LiipImagineBundle/pull/988)\), its output updated to aligned with the resolve command, and the "--machine-readable/-m" option added. [\#991](https://github.com/liip/LiipImagineBundle/pull/991) *([robfrawley](https://github.com/robfrawley))*


## [v1.9.0](https://github.com/liip/LiipImagineBundle/tree/1.9.0)

*Released on* 2017-08-30 *and assigned* [`1.9.0`](https://github.com/liip/LiipImagineBundle/releases/tag/1.9.0) *tag \([view verbose changelog](https://github.com/liip/LiipImagineBundle/compare/1.8.0...1.9.0)\).*
Expand Down
242 changes: 242 additions & 0 deletions Command/AbstractCacheCommand.php
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'),
));
}
}
Loading

0 comments on commit 6b166b4

Please sign in to comment.