Integration With Symfony Components
This chapter describes how to integrate this framework with the typical Symfony components used to work with files.
To enable this feature, you need to install the package
rekalogika/file-symfony-bridge:
composer require rekalogika/file-symfony-bridge
Components Summary
- Adapters to convert HttpFoundation Fileobjects to aFileInterfaceand vice versa, with special handling forUploadedFile.
- FileResponsefor streaming a- FileInterfaceto the client web browser.
- FileTypeform that works with- FileInterfaceobjects.
- A form transformer FileTransformerthat you can add to an existing SymfonyFileTypefields so that it gives us aFileInterfaceinstead of aUploadedFileobject.
- A form extension FileTypeExtensionthat you can optionally register to automatically convert all the existing SymfonyFileTypeso they all give us aFileInterface.
- Subclassed FileValidatorandImageValidatorthat works withFileInterfaceobjects.
Adapters
Converts a HttpFoundation File (and child classes, including UploadedFile)
to a FileInterface:
use Rekalogika\File\Bridge\Symfony\HttpFoundation\FromHttpFoundationFileAdapter;
use Symfony\Component\HttpFoundation\File\File;
/** @var File $httpFoundationFile */
$file = FromHttpFoundationFileAdapter::adapt($httpFoundationFile);
However, it is more convenient to use the universal adapter instead, although the universal adapter still needs this package to be installed.
use Symfony\Component\HttpFoundation\File\File;
use Rekalogika\File\FileAdapter;
/** @var File $httpFoundationFile */
$file = FileAdapter::adapt($httpFoundationFile);
Converts a FileInterface to a HttpFoundation File:
use Rekalogika\File\Bridge\Symfony\HttpFoundation\ToHttpFoundationFileAdapter;
use Rekalogika\Contracts\File\FileInterface;
/** @var FileInterface $file */
$httpFoundationFile = ToHttpFoundationFileAdapter::adapt($file);
Streaming a FileInterface
To stream a FileInterface to the client's web browser, you can use
FileResponse.
use Rekalogika\File\Bridge\Symfony\HttpFoundation\FileResponse;
use Rekalogika\Contracts\File\FileInterface;
use Symfony\Component\HttpFoundation\Response;
class SomeController
{
    public function download(): Response
    {
        /** @var FileInterface $file */
        $file = ...;
        return new FileResponse($file);
    }
}
FileResponse accepts additional optional parameters:
- $status: HTTP status code. Default:- 200.
- $headers: Array of additional headers. Default:- [].
- $disposition: Force the first parameter of the- Content-Dispositionheader to the specified value. It can be- attachmentor- inline. The filename is automatically taken from the metadata.
Forms
You might want to use our FilePondType form type instead. See the chapter
File Upload With FilePond for more information.
We provide a FileType that works with FileInterface objects. This is
basically the same as Symfony's FileType with a transformer built-in:
use Rekalogika\File\Bridge\Symfony\Form\FileType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class SomeFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            // ...
            ->add('file', FileType::class, [
                // ...
            ])
        ;
    }
}
If for some reason you cannot change the form type, you can use
FileTransformer to transform existing fields. It should work with Symfony's
FileType and any third-party form types with a compatible behavior:
use Rekalogika\File\Bridge\Symfony\Form\FileTransformer;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class SomeFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('file', FileType::class, [
                // ...
            ]);
        $builder->get('file')->addModelTransformer(new FileTransformer());
    }
}
You can also modify all the existing Symfony's FileType fields en masse by
registering the FileTypeExtension:
services:
    Rekalogika\File\Bridge\Symfony\Form\FileTypeExtension:
        tags:
            - { name: form.type_extension }
Validators
We provide File and Image validators. They are the same as Symfony's
File and Image validators, except that they work with FileInterface
objects instead of HttpFoundation File objects:
use Rekalogika\Contracts\File\FileInterface;
use Rekalogika\File\Bridge\Symfony\Constraints\File as FileConstraint;
use Rekalogika\File\Bridge\Symfony\Constraints\Image as ImageConstraint;
class Product
{
    #[ImageConstraint(minWidth: '1000']
    private ?FileInterface $photo = null;
    #[ImageConstraint(maxSize: '10000k']
    private ?FileInterface $manual = null;
    // ...
}
Due to how the adapters work, some validator functions might not work correctly, like those that check file names.