From a19a344760e2e0f0ad2e455db58bb9d55a93e645 Mon Sep 17 00:00:00 2001 From: Benoit Jouhaud Date: Fri, 27 Oct 2023 12:05:50 +0200 Subject: [PATCH 1/6] Issue #96: Allow setting the download_uri manually --- README.md | 2 +- src/Form/Type/ImageType.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cbc7094..5906116 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ Available options for the `ImageType`: - `cancel_button_class` (`string`): class of the button (default: `''`) - `save_button_class` (`string`): class of the 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`) +- `download_link` (`bool`): whether the image should be rendered in the form or not (default: `true`) - `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`) diff --git a/src/Form/Type/ImageType.php b/src/Form/Type/ImageType.php index dd8804c..efd09f5 100644 --- a/src/Form/Type/ImageType.php +++ b/src/Form/Type/ImageType.php @@ -169,7 +169,7 @@ 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)) { + if ($options['download_link'] && $downloadUri = $options['download_uri'] ?? $this->generateDownloadUri($form)) { $view->vars['download_uri'] = $downloadUri; } } From 5d171879302245903491ba12ca961679f0c2534b Mon Sep 17 00:00:00 2001 From: Benoit Jouhaud Date: Fri, 27 Oct 2023 12:07:17 +0200 Subject: [PATCH 2/6] Issue #96: Introduce a new "show_image" option that will replace the "download_link" option in next major --- README.md | 3 ++- src/Form/Type/ImageType.php | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5906116..31d0cb9 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,8 @@ Available options for the `ImageType`: - `cancel_button_class` (`string`): class of the button (default: `''`) - `save_button_class` (`string`): class of the button (default: `''`) - `download_uri` (`string`): the path where the image is located (default: `null`, automatically set) -- `download_link` (`bool`): whether the image should be rendered in the form 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`) diff --git a/src/Form/Type/ImageType.php b/src/Form/Type/ImageType.php index efd09f5..29aabd5 100644 --- a/src/Form/Type/ImageType.php +++ b/src/Form/Type/ImageType.php @@ -125,8 +125,17 @@ static function (Options $options): string { ->setDefault('download_uri', null) ->setAllowedTypes('download_uri', ['string', 'null']) + ->setDefault('show_image', null) + ->setAllowedTypes('show_image', ['bool', 'null']) + ->setDefault('download_link', true) ->setAllowedTypes('download_link', ['bool']) + ->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']) @@ -169,7 +178,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 = $options['download_uri'] ?? $this->generateDownloadUri($form)) { + $showImage = $options['show_image'] ?? $options['download_link']; + if ($showImage && $downloadUri = $options['download_uri'] ?? $this->generateDownloadUri($form)) { $view->vars['download_uri'] = $downloadUri; } } From 7dfb0f91ebe1371705fb032d36f87c051e7484c7 Mon Sep 17 00:00:00 2001 From: Benoit Jouhaud Date: Fri, 27 Oct 2023 12:20:49 +0200 Subject: [PATCH 3/6] Issue #96: Auto-document the form options --- README.md | 6 +++--- src/Form/Type/ImageType.php | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 31d0cb9..a5b6f8a 100644 --- a/README.md +++ b/README.md @@ -123,9 +123,9 @@ 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) - `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** diff --git a/src/Form/Type/ImageType.php b/src/Form/Type/ImageType.php index 29aabd5..dd723b4 100644 --- a/src/Form/Type/ImageType.php +++ b/src/Form/Type/ImageType.php @@ -83,15 +83,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', @@ -102,6 +106,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', @@ -112,24 +120,34 @@ 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', @@ -139,22 +157,31 @@ static function (Options $options): string { ->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']) From 674f3a7a9cf98f650dc126b96923ea0e1483b5b7 Mon Sep 17 00:00:00 2001 From: Benoit Jouhaud Date: Fri, 27 Oct 2023 12:21:11 +0200 Subject: [PATCH 4/6] Remove unused import --- src/Form/Type/ImageType.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Form/Type/ImageType.php b/src/Form/Type/ImageType.php index dd723b4..96032f7 100644 --- a/src/Form/Type/ImageType.php +++ b/src/Form/Type/ImageType.php @@ -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; From 83b33755a3ec126da6e31e2e64657547e8fd2b2b Mon Sep 17 00:00:00 2001 From: Benoit Jouhaud Date: Fri, 27 Oct 2023 14:03:56 +0200 Subject: [PATCH 5/6] Add compatibility for vich/uploader-bundle versions --- phpstan-baseline.neon | 37 +++++++++++++++++++++++++++ phpstan.neon.dist | 3 +++ tests/Unit/Form/ImageTypeTestCase.php | 19 +++++++++----- 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 phpstan-baseline.neon diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..8defeb9 --- /dev/null +++ b/phpstan-baseline.neon @@ -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 diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 31fe6b1..3b052d8 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,3 +1,6 @@ +includes: + - 'phpstan-baseline.neon' + parameters: checkGenericClassInNonGenericObjectType: false checkMissingIterableValueType: false diff --git a/tests/Unit/Form/ImageTypeTestCase.php b/tests/Unit/Form/ImageTypeTestCase.php index abdf3bf..c825e35 100644 --- a/tests/Unit/Form/ImageTypeTestCase.php +++ b/tests/Unit/Form/ImageTypeTestCase.php @@ -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; @@ -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); } } From 2a9cebd434043c7510ede6b7a3534d13ef14a4ca Mon Sep 17 00:00:00 2001 From: Benoit Jouhaud Date: Fri, 27 Oct 2023 14:52:24 +0200 Subject: [PATCH 6/6] Bump GitHub quality and tests actions to Symfony 6.3 and PHP 8.2 --- .github/workflows/quality.yml | 4 ++-- .github/workflows/tests.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index b88b9c3..c768a09 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -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' @@ -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' diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 878cb58..dc14035 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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' @@ -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'