src/CRUDlex/FileHandler.php
protected function shouldWriteFile(AbstractData $data, Request $request, Entity $entity, $entityName, $action)
    {
        $result = $data->getEvents()->shouldExecute($entity, 'before', $action);
        if (!$result) {
            return false;
        }
        $filesystem = $this->filesystem;
        $this->performOnFiles($entity, $entityName, function($entity, $entityName, $field) use ($filesystem, $request) {
            $file = $request->files->get($field);
            if ($file !== null && $file->isValid()) {
                $path     = $this->getPath($entityName, $entity, $field);
                $filename = $path.'/'.$file->getClientOriginalName();
                if ($filesystem->has($filename)) {
                    $filesystem->delete($filename);
                }
                $stream = fopen($file->getRealPath(), 'r+');
                $filesystem->writeStream($filename, $stream);
                fclose($stream);
            }
        });
        $data->getEvents()->shouldExecute($entity, 'after', $action);
        return true;
    }
protected function shouldWriteFile(AbstractData $data, Request $request, Entity $entity, $entityName, $action)
    {
        $result = $data->getEvents()->shouldExecute($entity, 'before', $action);
        if (!$result) {
            return false;
        }
        $filesystem = $this->filesystem;
        $this->performOnFiles($entity, $entityName, function($entity, $entityName, $field) use ($filesystem, $request) {
            $file = $request->files->get($field);
            if ($file !== null && $file->isValid()) {
                $path     = $this->getPath($entityName, $entity, $field);
                $filename = $path.'/'.$file->getClientOriginalName();
                if ($filesystem->has($filename)) {
                    $filesystem->delete($filename);
                }
                $stream = fopen($file->getRealPath(), 'r+');
                $filesystem->writeStream($filename, $stream);
                fclose($stream);
            }
        });
        $data->getEvents()->shouldExecute($entity, 'after', $action);
        return true;
    }
protected function shouldWriteFile ( AbstractData $ data , Request $ request , Entity $ entity , $ entityName , $ action ) { $ result = $ data -> getEvents ( ) -> shouldExecute ( $ entity , 'before' , $ action ) ; if ( ! $ result ) { return false ; } $ filesystem = $ this -> filesystem ; $ this -> performOnFiles ( $ entity , $ entityName , function ( $ entity , $ entityName , $ field ) use ( $ filesystem , $ request ) { $ file = $ request -> files -> get ( $ field ) ; if ( $ file !== null && $ file -> isValid ( ) ) { $ path = $ this -> getPath ( $ entityName , $ entity , $ field ) ; $ filename = $ path . '/' . $ file -> getClientOriginalName ( ) ; if ( $ filesystem -> has ( $ filename ) ) { $ filesystem -> delete ( $ filename ) ; } $ stream = fopen ( $ file -> getRealPath ( ) , 'r+' ) ; $ filesystem -> writeStream ( $ filename , $ stream ) ; fclose ( $ stream ) ; } } ) ; $ data -> getEvents ( ) -> shouldExecute ( $ entity , 'after' , $ action ) ; return true ; }
Writes the uploaded files.

@param AbstractData $data
the AbstractData instance who should receive the events
@param Request $request
the HTTP request containing the file data
@param Entity $entity
the just manipulated entity
@param string $entityName
the name of the entity as this class here is not aware of it
@param string $action
the name of the performed action

@return boolean
true if all before events passed
Writes the uploaded files .