New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Filters] [Config] [DI] Add Filter configuration class as public service #1098
Changes from 6 commits
6b41c3b
11c0c12
6a8cff3
add533f
95db82b
d907670
e213124
851a38b
27c8794
5c4d68f
e306612
db0a518
d6c599f
6906000
4ea4acc
ebc5744
02e4022
c4923d0
c564a62
9971029
36d7843
91bfe94
9df4d4b
be890e3
8458b37
6613b61
cdab3b8
f8cdfb7
2a397ae
f1ed641
0624a2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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\Config; | ||
|
||
final class Filter implements FilterInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $options = []; | ||
|
||
/** | ||
* @param string $name | ||
* @param array $options | ||
*/ | ||
public function __construct(string $name, array $options) | ||
{ | ||
$this->name = $name; | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?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\Config\Filter\Type; | ||
|
||
use Liip\ImagineBundle\Config\FilterInterface; | ||
|
||
final class Crop implements FilterInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $start; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $size; | ||
|
||
/** | ||
* @param string $name | ||
* @param array $start | ||
* @param array $size | ||
*/ | ||
public function __construct(string $name, array $start, array $size) | ||
{ | ||
$this->name = $name; | ||
$this->start = $start; | ||
$this->size = $size; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getStart(): array | ||
{ | ||
return $this->start; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getSize(): array | ||
{ | ||
return $this->size; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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\Config\Filter\Type; | ||
|
||
use Liip\ImagineBundle\Config\FilterInterface; | ||
|
||
final class Strip implements FilterInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @param string $name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove |
||
*/ | ||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?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\Config\Filter\Type; | ||
|
||
use Liip\ImagineBundle\Config\FilterInterface; | ||
|
||
final class Thumbnail implements FilterInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $size; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $mode; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $allowUpscale; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $filter; | ||
|
||
/** | ||
* @param string $name | ||
* @param array $size | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you specify what kind of array this is? is it a hashmap with width and height? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* @param string|null $mode | ||
* @param bool|null $allowUpscale | ||
* @param string|null $filter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove the docblock as it contains no information |
||
*/ | ||
public function __construct( | ||
string $name, | ||
array $size, | ||
string $mode = null, | ||
bool $allowUpscale = null, | ||
string $filter = null | ||
) { | ||
$this->name = $name; | ||
$this->size = $size; | ||
$this->mode = $mode; | ||
$this->allowUpscale = $allowUpscale; | ||
$this->filter = $filter; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getSize() | ||
{ | ||
return $this->size; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getMode() | ||
{ | ||
return $this->mode; | ||
} | ||
|
||
/** | ||
* @return bool|null | ||
*/ | ||
public function isAllowUpscale() | ||
{ | ||
return $this->allowUpscale; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getFilter() | ||
{ | ||
return $this->filter; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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\Config; | ||
|
||
use Liip\ImagineBundle\Exception\Config\Filter\NotFoundException; | ||
use Liip\ImagineBundle\Factory\Config\FilterFactoryInterface; | ||
|
||
final class FilterFactoryCollection implements FilterFactoryCollectionInterface | ||
{ | ||
/** | ||
* @var FilterFactoryInterface[] | ||
*/ | ||
private $filterFactories; | ||
|
||
/** | ||
* FilterCollection constructor. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd remove docblocks that don't add any information over whats in the php code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* | ||
* @param FilterFactoryInterface ...$filterFactories | ||
*/ | ||
public function __construct(FilterFactoryInterface ...$filterFactories) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variadic here looks so strange. IMHO it is better use array of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got your point, but how usage of array and additional checks of type is better ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as you loop over the parameter anyways, you could also do an instanceof check, and have the if we ever need to add an (optional) constructor argument, the current form will be a problem as we can't add anything else without a BC break. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why would we ever need to to add something to constructor of this class ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't mind much either way. thinking of it, we should mark all the factory part as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess within Symfony the pattern used most often is an but I wonder if it wouldn't be better to use a factory method instead of the constructor. ie. something like:
this keeps the options open for if we ever do need to add anything to the constructor. while keeping the convenience and more or less the overhead reduction benefits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this object has only one purpose, to store the FilterFactoryInterfaces (which are clearly its dependencies and should be specified), I dont see any valid case which would lead to altering constructor. Also, I am not sure is it a good practice to do indirect object injections (via setters). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok lets keep it as proposed. like I said, a good chance of this feeling "weird" is simply the lack of experience with variadic in user land .. and honestly this is not the most "risky" place .. if we do in the end realize we need support additional constructor parameters .. we can then still use a static factory method for that case. |
||
{ | ||
$this->filterFactories = $filterFactories; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imho we should convert this to a hashmap with name => factory so that we don't have to loop over the array in getFilterFactoryByName There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @throws NotFoundException | ||
* | ||
* @return FilterFactoryInterface | ||
*/ | ||
public function getFilterFactoryByName(string $name): FilterFactoryInterface | ||
{ | ||
foreach ($this->filterFactories as $filterFactory) { | ||
if ($filterFactory->getName() === $name) { | ||
return $filterFactory; | ||
} | ||
} | ||
|
||
throw new NotFoundException(sprintf("Filter factory with name '%s' was not found.", $name)); | ||
} | ||
|
||
/** | ||
* @return FilterFactoryInterface[] | ||
*/ | ||
public function getAll() | ||
{ | ||
return $this->filterFactories; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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\Config; | ||
|
||
use Liip\ImagineBundle\Factory\Config\FilterFactoryInterface; | ||
|
||
interface FilterFactoryCollectionInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imho there is only one way how this can be implemented and hence no point in having an interface There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
{ | ||
/** | ||
* @param string $name | ||
* | ||
* @return FilterFactoryInterface | ||
*/ | ||
public function getFilterFactoryByName(string $name): FilterFactoryInterface; | ||
|
||
/** | ||
* @return FilterFactoryInterface[] | ||
*/ | ||
public function getAll(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?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\Config; | ||
|
||
interface FilterInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would like to do a phpdoc that explains what a filter is (the definition for one image transformation operation) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dbu could you provide an example ? cause I'm not sure that I understand this comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
{ | ||
/** | ||
* @return string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should say what the name is for. is it only for debugging purposes? do we match actual filters by filter class or by filter name? i think filters should be matched by filter name, to allow having the same class represent different filters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ |
||
*/ | ||
public function getName(): string; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once you implemented all filters, this will become obsolete, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done