Skip to content

Commit

Permalink
array $options change to object $context
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Jul 20, 2021
1 parent c5f42cc commit e2fe507
Show file tree
Hide file tree
Showing 30 changed files with 107 additions and 70 deletions.
12 changes: 8 additions & 4 deletions src/Bridge/Imagine/Config/ImagineConfigFilterRegistry.php
Expand Up @@ -4,9 +4,9 @@

use Contributte\Imagist\Bridge\Imagine\OperationInterface;
use Contributte\Imagist\Config\ConfigFilterStack;
use Contributte\Imagist\Context\ContextImageAware;
use Contributte\Imagist\Exceptions\UnexpectedErrorException;
use Contributte\Imagist\Filter\FilterInterface;
use Contributte\Imagist\Scope\Scope;
use Imagine\Image\ImageInterface;
use InvalidArgumentException;

Expand All @@ -29,12 +29,16 @@ public function addOperation(ImagineConfigOperationInterface $operation): void
$this->operations[$operation->getName()] = $operation;
}

public function supports(FilterInterface $filter, Scope $scope): bool
public function supports(FilterInterface $filter, ContextImageAware $context): bool
{
return isset($this->configFilterStacks[$filter->getName()]);
}

public function operate(ImageInterface $image, FilterInterface $filter): void
public function operate(
ImageInterface $image,
FilterInterface $filter,
ContextImageAware $context
): void
{
if (!isset($this->configFilterStacks[$filter->getName()])) {
throw new UnexpectedErrorException();
Expand All @@ -47,7 +51,7 @@ public function operate(ImageInterface $image, FilterInterface $filter): void

$operation = $this->operations[$configFilter->getName()];

$operation->operate($image, $filter, $configFilter->getArguments());
$operation->operate($image, $filter, $context, $configFilter->getArguments());
}
}

Expand Down
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\Imagist\Bridge\Imagine\Config;

use Contributte\Imagist\Context\ContextImageAware;
use Contributte\Imagist\Filter\FilterInterface;
use Imagine\Image\ImageInterface;

Expand All @@ -13,6 +14,11 @@ public function getName(): string;
/**
* @param mixed[] $arguments
*/
public function operate(ImageInterface $image, FilterInterface $filter, array $arguments): void;
public function operate(
ImageInterface $image,
FilterInterface $filter,
ContextImageAware $context,
array $arguments
): void;

}
Expand Up @@ -3,6 +3,7 @@
namespace Contributte\Imagist\Bridge\Imagine\Config\Operation;

use Contributte\Imagist\Bridge\Imagine\Config\ImagineConfigOperationInterface;
use Contributte\Imagist\Context\ContextImageAware;
use Contributte\Imagist\Filter\FilterInterface;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
Expand All @@ -19,7 +20,7 @@ public function getName(): string
/**
* @param mixed[] $arguments
*/
public function operate(ImageInterface $image, FilterInterface $filter, array $arguments): void
public function operate(ImageInterface $image, FilterInterface $filter, ContextImageAware $context, array $arguments): void
{
if (count($arguments) !== 2) {
throw new InvalidArgumentException('Config filter resize must have two arguments.');
Expand Down
13 changes: 7 additions & 6 deletions src/Bridge/Imagine/FilterProcessor.php
Expand Up @@ -3,6 +3,8 @@
namespace Contributte\Imagist\Bridge\Imagine;

use Contributte\Imagist\Bridge\Imagine\Exceptions\OperationNotFoundException;
use Contributte\Imagist\Context\Context;
use Contributte\Imagist\Context\ContextImageAware;
use Contributte\Imagist\File\FileInterface;
use Contributte\Imagist\Filter\FilterProcessorInterface;
use Imagine\Gd\Imagine as GdImagine;
Expand Down Expand Up @@ -42,23 +44,22 @@ protected function createImagine(): ImagineInterface
throw new RuntimeException('PHP extension not found, need imagick or gd or gmagick');
}

/**
* @param mixed[] $options
*/
public function process(FileInterface $target, FileInterface $source, array $options = []): string
public function process(FileInterface $target, FileInterface $source, Context $context): string
{
$filter = $target->getImage()->getFilter();
if (!$filter) {
return $target->getContent();
}

$operation = $this->operationRegistry->get($filter, $target->getImage()->getScope());
$context = new ContextImageAware($target->getImage(), $context);

$operation = $this->operationRegistry->get($filter, $context);

if (!$operation) {
throw new OperationNotFoundException($target->getImage());
}

$operation->operate($image = $this->createImageInstance($source), $filter);
$operation->operate($image = $this->createImageInstance($source), $filter, $context);

return $image->get($source->getMimeType()->toSuffix());
}
Expand Down
10 changes: 7 additions & 3 deletions src/Bridge/Imagine/OperationInterface.php
Expand Up @@ -2,15 +2,19 @@

namespace Contributte\Imagist\Bridge\Imagine;

use Contributte\Imagist\Context\ContextImageAware;
use Contributte\Imagist\Filter\FilterInterface;
use Contributte\Imagist\Scope\Scope;
use Imagine\Image\ImageInterface;

interface OperationInterface
{

public function supports(FilterInterface $filter, Scope $scope): bool;
public function supports(FilterInterface $filter, ContextImageAware $context): bool;

public function operate(ImageInterface $image, FilterInterface $filter): void;
public function operate(
ImageInterface $image,
FilterInterface $filter,
ContextImageAware $context
): void;

}
6 changes: 3 additions & 3 deletions src/Bridge/Imagine/OperationRegistry.php
Expand Up @@ -2,8 +2,8 @@

namespace Contributte\Imagist\Bridge\Imagine;

use Contributte\Imagist\Context\ContextImageAware;
use Contributte\Imagist\Filter\FilterInterface;
use Contributte\Imagist\Scope\Scope;

final class OperationRegistry implements OperationRegistryInterface
{
Expand All @@ -16,10 +16,10 @@ public function add(OperationInterface $operation): void
$this->operations[] = $operation;
}

public function get(FilterInterface $filter, Scope $scope): ?OperationInterface
public function get(FilterInterface $filter, ContextImageAware $context): ?OperationInterface
{
foreach ($this->operations as $operation) {
if ($operation->supports($filter, $scope)) {
if ($operation->supports($filter, $context)) {
return $operation;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Imagine/OperationRegistryInterface.php
Expand Up @@ -2,14 +2,14 @@

namespace Contributte\Imagist\Bridge\Imagine;

use Contributte\Imagist\Context\ContextImageAware;
use Contributte\Imagist\Filter\FilterInterface;
use Contributte\Imagist\Scope\Scope;

interface OperationRegistryInterface
{

public function add(OperationInterface $operation): void;

public function get(FilterInterface $filter, Scope $scope): ?OperationInterface;
public function get(FilterInterface $filter, ContextImageAware $context): ?OperationInterface;

}
3 changes: 2 additions & 1 deletion src/Bridge/Nette/Filter/NetteFilterProcessor.php
Expand Up @@ -3,6 +3,7 @@
namespace Contributte\Imagist\Bridge\Nette\Filter;

use Contributte\Imagist\Bridge\Nette\Filter\Exceptions\OperationNotFoundException;
use Contributte\Imagist\Context\Context;
use Contributte\Imagist\File\FileInterface;
use Contributte\Imagist\Filter\FilterProcessorInterface;
use Nette\Utils\Image;
Expand All @@ -17,7 +18,7 @@ public function __construct(NetteOperationRegistryInterface $operationRegistry)
$this->operationRegistry = $operationRegistry;
}

public function process(FileInterface $target, FileInterface $source, array $options = []): string
public function process(FileInterface $target, FileInterface $source, Context $context): string
{
$filter = $target->getImage()->getFilter();
if (!$filter) {
Expand Down
13 changes: 6 additions & 7 deletions src/Bridge/Nette/Latte/LatteImageProvider.php
Expand Up @@ -23,7 +23,7 @@ public function __construct(LinkGeneratorInterface $linkGenerator)
* @param mixed[] $options
* @param mixed[] $filters
*/
public function link($id, array $options, array ...$filters): ?string
public function link($id, array $options, array $filters): ?string
{
if (is_string($id)) {
$image = new PersistentImage($id);
Expand All @@ -45,14 +45,13 @@ public function link($id, array $options, array ...$filters): ?string
throw new InvalidArgumentException('Cannot use now two or more filters.');
}

foreach ($filters as $options) {
if (count($options) === 0) {
throw new InvalidArgumentException('First filter does not contain filter name.');
foreach ($filters as $name => $opts) {
if (is_numeric($name)) {
$name = $opts;
$opts = [];
}

[$name] = array_splice($options, 0, 1);

$image = $image->withFilter($name, $options);
$image = $image->withFilter($name, $opts);
}

return $this->linkGenerator->link($image, $options);
Expand Down
3 changes: 1 addition & 2 deletions src/Bridge/Nette/Macro/ImageMacro.php
Expand Up @@ -80,8 +80,7 @@ private function extractFilter(MacroNode $node, PhpWriter $writer): string
}, $node->modifiers);
} else {
$modifiers = array_map(
function (string $modifier) use ($writer): string
{
function (string $modifier) use ($writer): string {
if (!str_contains($modifier, ':')) {
return $modifier;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Database/DatabaseConverter.php
Expand Up @@ -36,7 +36,11 @@ public function convertToDatabase(?ImageInterface $image): ?string

if ($image instanceof PromisedImageInterface) {
if ($image->isPending()) {
throw new InvalidArgumentException(sprintf('Given image "%s" is still pending', PromisedImage::getSourceId($image)));
if ($image instanceof PromisedImage) {
throw new InvalidArgumentException(sprintf('Given image "%s" is still pending', PromisedImage::getSourceId($image)));
}

throw new InvalidArgumentException('Given image is still pending');
}

$image = $image->getResult();
Expand Down
6 changes: 2 additions & 4 deletions src/Filter/FilterProcessorInterface.php
Expand Up @@ -2,14 +2,12 @@

namespace Contributte\Imagist\Filter;

use Contributte\Imagist\Context\Context;
use Contributte\Imagist\File\FileInterface;

interface FilterProcessorInterface
{

/**
* @param mixed[] $options
*/
public function process(FileInterface $target, FileInterface $source, array $options = []): string;
public function process(FileInterface $target, FileInterface $source, Context $context): string;

}
3 changes: 2 additions & 1 deletion src/Filter/OriginalFilterProcessor.php
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\Imagist\Filter;

use Contributte\Imagist\Context\Context;
use Contributte\Imagist\File\FileInterface;

final class OriginalFilterProcessor implements FilterProcessorInterface
Expand All @@ -10,7 +11,7 @@ final class OriginalFilterProcessor implements FilterProcessorInterface
/**
* @inheritDoc
*/
public function process(FileInterface $target, FileInterface $source, array $options = []): string
public function process(FileInterface $target, FileInterface $source, Context $context): string
{
return $source->getContent();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Filter/VoidFilterProcessor.php
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\Imagist\Filter;

use Contributte\Imagist\Context\Context;
use Contributte\Imagist\File\FileInterface;
use LogicException;

Expand All @@ -11,7 +12,7 @@ final class VoidFilterProcessor implements FilterProcessorInterface
/**
* @inheritDoc
*/
public function process(FileInterface $target, FileInterface $source, array $options = []): string
public function process(FileInterface $target, FileInterface $source, Context $context): string
{
if ($target->getImage()->getFilter()) {
throw new LogicException(sprintf('%s does not support filters', self::class));
Expand Down
5 changes: 3 additions & 2 deletions src/ImageStorageInterface.php
Expand Up @@ -2,14 +2,15 @@

namespace Contributte\Imagist;

use Contributte\Imagist\Context\Context;
use Contributte\Imagist\Entity\ImageInterface;
use Contributte\Imagist\Entity\PersistentImageInterface;

interface ImageStorageInterface
{

public function persist(ImageInterface $image): PersistentImageInterface;
public function persist(ImageInterface $image, ?Context $context = null): PersistentImageInterface;

public function remove(PersistentImageInterface $image): PersistentImageInterface;
public function remove(PersistentImageInterface $image, ?Context $context = null): PersistentImageInterface;

}
5 changes: 3 additions & 2 deletions src/Persister/EmptyImagePersister.php
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\Imagist\Persister;

use Contributte\Imagist\Context\Context;
use Contributte\Imagist\Entity\EmptyImage;
use Contributte\Imagist\Entity\EmptyImageInterface;
use Contributte\Imagist\Entity\ImageInterface;
Expand All @@ -18,12 +19,12 @@ public function setStrict(bool $strict): void
$this->strict = $strict;
}

public function supports(ImageInterface $image): bool
public function supports(ImageInterface $image, Context $context): bool
{
return $image instanceof EmptyImageInterface;
}

public function persist(ImageInterface $image): PersistentImageInterface
public function persist(ImageInterface $image, Context $context): PersistentImageInterface
{
if ($this->strict) {
throw new InvalidArgumentException('Cannot persist empty image');
Expand Down
5 changes: 3 additions & 2 deletions src/Persister/ImagePersisterAbstract.php
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\Imagist\Persister;

use Contributte\Imagist\Context\Context;
use Contributte\Imagist\Entity\ImageInterface;
use Contributte\Imagist\File\FileFactoryInterface;
use Contributte\Imagist\Filter\FilterProcessorInterface;
Expand All @@ -19,13 +20,13 @@ public function __construct(FileFactoryInterface $fileFactory, FilterProcessorIn
$this->filterProcessor = $filterProcessor;
}

protected function save(ImageInterface $image): void
protected function save(ImageInterface $image, Context $context): void
{
$target = $this->fileFactory->create($image);
$source = $this->fileFactory->create($image->getOriginal());

$target->setContent(
$this->filterProcessor->process($target, $source)
$this->filterProcessor->process($target, $source, $context)
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/Persister/PersistentImagePersister.php
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\Imagist\Persister;

use Contributte\Imagist\Context\Context;
use Contributte\Imagist\Entity\EmptyImageInterface;
use Contributte\Imagist\Entity\ImageInterface;
use Contributte\Imagist\Entity\PersistentImageInterface;
Expand All @@ -17,12 +18,12 @@ public function setStrict(bool $strict): void
$this->strict = $strict;
}

public function supports(ImageInterface $image): bool
public function supports(ImageInterface $image, Context $context): bool
{
return $image instanceof PersistentImageInterface && !$image instanceof EmptyImageInterface;
}

public function persist(ImageInterface $image): ImageInterface
public function persist(ImageInterface $image, Context $context): ImageInterface
{
if (!$image->getFilter()) {
if ($this->strict) {
Expand All @@ -32,7 +33,7 @@ public function persist(ImageInterface $image): ImageInterface
return $image;
}

$this->save($image);
$this->save($image, $context);

return $image;
}
Expand Down

0 comments on commit e2fe507

Please sign in to comment.