Skip to content

Commit

Permalink
Merge pull request #97 from prestaconcept/issue-96
Browse files Browse the repository at this point in the history
Issue 96
  • Loading branch information
J-Ben87 authored Oct 30, 2023
2 parents c45ddb5 + 2a9cebd commit 947d1fb
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2']
symfony-require: ['5.4.*', '6.0.*', '6.1.*', '6.2.*']
symfony-require: ['5.4.*', '6.0.*', '6.1.*', '6.2.*', '6.3.*']

steps:
- name: 'Setup PHP'
Expand All @@ -41,7 +41,7 @@ jobs:
strategy:
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2']
symfony-require: ['5.4.*', '6.0.*', '6.1.*', '6.2.*']
symfony-require: ['5.4.*', '6.0.*', '6.1.*', '6.2.*', '6.3.*']

steps:
- name: 'Setup PHP'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2']
symfony-require: ['5.4.*', '6.0.*', '6.1.*', '6.2.*']
symfony-require: ['5.4.*', '6.0.*', '6.1.*', '6.2.*', '6.3.*']

steps:
- name: 'Setup PHP'
Expand All @@ -36,7 +36,7 @@ jobs:
name: 'Code coverage'
runs-on: 'ubuntu-latest'
env:
SYMFONY_REQUIRE: '6.2.*'
SYMFONY_REQUIRE: '6.3.*'

steps:
- name: 'Setup PHP'
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ Available options for the `ImageType`:
- `max_height` (`int`): the max height of the cropped image send to server (default: `180`)
- `preview_width` (`string`): the max width to use when displaying the image preview - can be in px, % or other css value (default: `'320px'`)
- `preview_height` (`string`): the max height to use when displaying the image preview - can be in px, % or other css value (default: `'180px'`)
- `upload_button_class` (`string`): class of the button (default: `''`)
- `cancel_button_class` (`string`): class of the button (default: `''`)
- `save_button_class` (`string`): class of the button (default: `''`)
- `upload_button_class` (`string`): CSS class of the "upload" button (default: `''`)
- `cancel_button_class` (`string`): CSS class of the "cancel" button (default: `''`)
- `save_button_class` (`string`): CSS class of the "save" button (default: `''`)
- `download_uri` (`string`): the path where the image is located (default: `null`, automatically set)
- `download_link` (`bool`): whether the end user should be able to add a remote image by URL or not (default: `true`)
- `show_image` (`bool`): whether the image should be rendered in the form or not (default: `null`, will default to `true` in next major)
- ~~`download_link` (`bool`): whether the image should be rendered in the form or not (default: `true`)~~ **Deprecated, will be removed (replaced by `show_image`) in next major**
- `file_upload_enabled` (`bool`): whether to enable the file upload widget or not (default: `true`)
- `remote_url_enabled` (`bool`): whether to enable the remote url widget or not (default: `true`)
- `rotation_enabled` (`bool`): whether to enable the rotation or not (default: `false`)
Expand Down
37 changes: 37 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
parameters:
ignoreErrors:
-
message: "#^Class Vich\\\\UploaderBundle\\\\Mapping\\\\PropertyMappingFactory constructor invoked with 2 parameters, 3\\-4 required\\.$#"
count: 1
path: tests/Unit/Form/ImageTypeTestCase.php
reportUnmatched: false

-
message: "#^Parameter \\#1 \\$container of class Vich\\\\UploaderBundle\\\\Mapping\\\\PropertyMappingFactory constructor expects Symfony\\\\Component\\\\DependencyInjection\\\\ContainerInterface, Vich\\\\UploaderBundle\\\\Metadata\\\\MetadataReader given\\.$#"
count: 1
path: tests/Unit/Form/ImageTypeTestCase.php
reportUnmatched: false

-
message: "#^Parameter \\#2 \\$metadata of class Vich\\\\UploaderBundle\\\\Mapping\\\\PropertyMappingFactory constructor expects Vich\\\\UploaderBundle\\\\Metadata\\\\MetadataReader, Vich\\\\UploaderBundle\\\\Mapping\\\\PropertyMappingResolver given\\.$#"
count: 1
path: tests/Unit/Form/ImageTypeTestCase.php
reportUnmatched: false

-
message: "#^Class Vich\\\\UploaderBundle\\\\Mapping\\\\PropertyMappingFactory constructor invoked with 3 parameters, 2 required\\.$#"
count: 1
path: tests/Unit/Form/ImageTypeTestCase.php
reportUnmatched: false

-
message: "#^Parameter \\#1 \\$metadata of class Vich\\\\UploaderBundle\\\\Mapping\\\\PropertyMappingFactory constructor expects Vich\\\\UploaderBundle\\\\Metadata\\\\MetadataReader, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Symfony\\\\Component\\\\DependencyInjection\\\\ContainerInterface given\\.$#"
count: 1
path: tests/Unit/Form/ImageTypeTestCase.php
reportUnmatched: false

-
message: "#^Parameter \\#2 \\$resolver of class Vich\\\\UploaderBundle\\\\Mapping\\\\PropertyMappingFactory constructor expects Vich\\\\UploaderBundle\\\\Mapping\\\\PropertyMappingResolverInterface, Vich\\\\UploaderBundle\\\\Metadata\\\\MetadataReader given\\.$#"
count: 1
path: tests/Unit/Form/ImageTypeTestCase.php
reportUnmatched: false
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes:
- 'phpstan-baseline.neon'

parameters:
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
Expand Down
40 changes: 38 additions & 2 deletions src/Form/Type/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Presta\ImageBundle\Form\Type;

use Presta\ImageBundle\Exception\UnexpectedTypeException;
use Presta\ImageBundle\Form\DataTransformer\Base64ToImageTransformer;
use Presta\ImageBundle\Form\EventListener\ImageType\AddDeleteCheckboxListener;
use Presta\ImageBundle\Form\EventListener\ImageType\ClearBase64OnDeleteListener;
Expand Down Expand Up @@ -83,15 +82,19 @@ public function configureOptions(OptionsResolver $resolver): void
]
)
->setAllowedTypes('aspect_ratios', ['array'])
->setInfo('aspect_ratios', 'A list of aspect ratio to apply when resizing an image.')

->setDefault('cropper_options', ['autoCropArea' => 1])
->setAllowedTypes('cropper_options', ['array'])
->setInfo('cropper_options', 'A list of options supported by cropper.')

->setDefault('max_width', 320)
->setAllowedTypes('max_width', ['int'])
->setInfo('max_width', 'The max width of the cropped image send to server.')

->setDefault('max_height', 180)
->setAllowedTypes('max_height', ['int'])
->setInfo('max_height', 'The max height of the cropped image send to server.')

->setDefault(
'preview_width',
Expand All @@ -102,6 +105,10 @@ static function (Options $options): string {
}
)
->setAllowedTypes('preview_width', ['string'])
->setInfo(
'preview_width',
'The max width to use when displaying the image preview. Can be in px, % or other css value.'
)

->setDefault(
'preview_height',
Expand All @@ -112,40 +119,68 @@ static function (Options $options): string {
}
)
->setAllowedTypes('preview_height', ['string'])
->setInfo(
'preview_height',
'The max height to use when displaying the image preview. Can be in px, % or other css value.'
)

->setDefault('upload_button_class', '')
->setAllowedTypes('upload_button_class', ['string'])
->setInfo('upload_button_class', 'CSS class of the "upload" button.')

->setDefault('cancel_button_class', '')
->setAllowedTypes('cancel_button_class', ['string'])
->setInfo('cancel_button_class', 'CSS class of the "cancel" button.')

->setDefault('save_button_class', '')
->setAllowedTypes('save_button_class', ['string'])
->setInfo('save_button_class', 'CSS class of the "save" button.')

->setDefault('download_uri', null)
->setAllowedTypes('download_uri', ['string', 'null'])
->setInfo('download_uri', 'The path where the image is located.')

->setDefault('show_image', null)
->setAllowedTypes('show_image', ['bool', 'null'])
->setInfo('show_image', 'Whether the image should be rendered in the form or not.')

->setDefault('download_link', true)
->setAllowedTypes('download_link', ['bool'])
->setInfo('download_link', 'Whether the image should be rendered in the form or not.')
->setDeprecated(
'download_link',
'presta/image-bundle',
'2.6.0',
'The option "download_link" is deprecated, use "show_image" instead.',
)

->setDefault('file_upload_enabled', true)
->setAllowedTypes('file_upload_enabled', ['bool'])
->setInfo('file_upload_enabled', 'Whether to enable the file upload widget or not.')

->setDefault('remote_url_enabled', true)
->setAllowedTypes('remote_url_enabled', ['bool'])
->setInfo('remote_url_enabled', 'Whether to enable the remote url widget or not.')

->setDefault('rotation_enabled', false)
->setAllowedTypes('rotation_enabled', ['bool'])
->setInfo('rotation_enabled', 'Whether to enable the rotation or not.')

->setDefault('translation_domain', 'PrestaImageBundle')
->setAllowedTypes('translation_domain', ['string'])

->setDefault('upload_mimetype', 'image/png')
->setAllowedTypes('upload_mimetype', ['string'])
->setInfo(
'upload_mimetype',
'Format of the image to be uploaded. Note: If the chosen mimetype is not supported by the browser, '
. 'it will silently fall back to `image/png`.'
)

// default value: https://developer.mozilla.org/de/docs/Web/API/HTMLCanvasElement/toDataURL
->setDefault('upload_quality', 0.92)
->setAllowedTypes('upload_quality', ['float'])
->setInfo('upload_quality', 'Quality (0..1) of uploaded image for lossy imageformats (eg. `image/jpeg`).')

->setDefault('error_bubbling', false)
->setAllowedTypes('error_bubbling', ['bool'])
Expand All @@ -169,7 +204,8 @@ public function buildView(FormView $view, FormInterface $form, array $options):
$view->vars['upload_mimetype'] = $options['upload_mimetype'];
$view->vars['upload_quality'] = $options['upload_quality'];

if ($options['download_link'] && $downloadUri = $this->generateDownloadUri($form)) {
$showImage = $options['show_image'] ?? $options['download_link'];
if ($showImage && $downloadUri = $options['download_uri'] ?? $this->generateDownloadUri($form)) {
$view->vars['download_uri'] = $downloadUri;
}
}
Expand Down
19 changes: 12 additions & 7 deletions tests/Unit/Form/ImageTypeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Vich\UploaderBundle\Handler\UploadHandler;
use Vich\UploaderBundle\Injector\FileInjectorInterface;
use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
use Vich\UploaderBundle\Mapping\PropertyMappingResolver;
use Vich\UploaderBundle\Metadata\ClassMetadata;
use Vich\UploaderBundle\Metadata\MetadataReader;
use Vich\UploaderBundle\Storage\StorageInterface;
Expand Down Expand Up @@ -79,15 +80,19 @@ protected function getExtensions(): array

protected function createUploadHandler(): UploadHandler
{
return new UploadHandler(
new PropertyMappingFactory(
if (\class_exists('Vich\UploaderBundle\Mapping\PropertyMappingResolver')) {
$factory = new PropertyMappingFactory(
new MetadataReader($this->advancedMetadataFactory),
new PropertyMappingResolver($this->container, ['default' => []])
);
} else {
$factory = new PropertyMappingFactory(
$this->container,
new MetadataReader($this->advancedMetadataFactory),
['default' => []]
),
$this->storage,
$this->fileInjector,
$this->eventDispatcher
);
);
}

return new UploadHandler($factory, $this->storage, $this->fileInjector, $this->eventDispatcher);
}
}

0 comments on commit 947d1fb

Please sign in to comment.