Skip to content
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

[FormBundle] Adding Filesystem to FileFormSubmissionField #2786

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kunstmaan\FormBundle\DependencyInjection;

use Kunstmaan\MediaBundle\Utils\SymfonyVersion;
Autsider666 marked this conversation as resolved.
Show resolved Hide resolved
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand Down Expand Up @@ -30,6 +31,10 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->booleanNode('deletable_formsubmissions')->defaultFalse()->end()
->scalarNode('web_root')
->defaultValue(SymfonyVersion::getRootWebPath())
->cannotBeEmpty()
->end()
->end()
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('kunstmaan_form.deletable_formsubmissions', $config['deletable_formsubmissions']);
$container->setParameter('kunstmaan_form.web_root', $config['web_root']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Behat\Transliterator\Transliterator;
use Doctrine\ORM\Mapping as ORM;
use Gaufrette\Filesystem;
use Kunstmaan\FormBundle\Entity\FormSubmissionField;
use Kunstmaan\FormBundle\Form\FileFormSubmissionType;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -79,10 +80,8 @@ public function isNull()

/**
* Move the file to the given uploadDir and save the filename
*
* @param string $uploadDir
*/
public function upload($uploadDir, $webDir)
public function upload($uploadDir, $webDir, Filesystem $fileSystem = null)
Autsider666 marked this conversation as resolved.
Show resolved Hide resolved
{
// the file property can be empty if the field is not required
if (null === $this->file) {
Expand All @@ -95,14 +94,26 @@ public function upload($uploadDir, $webDir)
$uuid = uniqid();
$this->setUuid($uuid);

// move takes the target directory and then the target filename to move to
$this->file->move(sprintf('%s/%s', $uploadDir, $uuid), $safeFileName);
if ($fileSystem) {
Autsider666 marked this conversation as resolved.
Show resolved Hide resolved
$url = \sprintf('%s%s/%s', $webDir, $uuid, $safeFileName);
$content = \file_get_contents($this->file->getPathname());
$fileSystem->write($url, $content);
acrobat marked this conversation as resolved.
Show resolved Hide resolved
} else {
@trigger_error(
'Not passing the filesystem as the third argument of upload is deprecated since KunstmaanMediaBundle 5.7 and will be required in KunstmaanMediaBundle 6.0.',
Autsider666 marked this conversation as resolved.
Show resolved Hide resolved
E_USER_DEPRECATED
);

// move takes the target directory and then the target filename to move to
$this->file->move(sprintf('%s/%s', $uploadDir, $uuid), $safeFileName);
$url = sprintf('%s%s/', $webDir, $uuid) . $safeFileName;
}

// set the path property to the filename where you'ved saved the file
$this->fileName = $safeFileName;

// set the url to the uuid directory inside the web dir
$this->setUrl(sprintf('%s%s/', $webDir, $uuid) . $safeFileName);
$this->setUrl($url);

// clean up the file property as you won't need it anymore
$this->file = null;
Expand All @@ -120,7 +131,13 @@ public function onValidPost(Form $form, FormBuilderInterface $formBuilder, Reque
{
$uploadDir = $container->getParameter('form_submission_rootdir');
$webDir = $container->getParameter('form_submission_webdir');
$this->upload($uploadDir, $webDir);

$fileSystem = null;
Autsider666 marked this conversation as resolved.
Show resolved Hide resolved
if ($container->has('kunstmaan_form.filesystem')) {
$fileSystem = $container->get('kunstmaan_form.filesystem');
acrobat marked this conversation as resolved.
Show resolved Hide resolved
}

$this->upload($uploadDir, $webDir, $fileSystem);
Autsider666 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Kunstmaan/FormBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ services:
arguments: ['@kunstmaan_form.form_mailer']
tags:
- { name: 'kernel.event_listener', event: 'kunstmaan_form.add_submission', method: 'onSubmission' }

kunstmaan_form.filesystem_adapter:
class: Gaufrette\Adapter\Local
arguments:
- '%kunstmaan_form.web_root%'
- true

kunstmaan_form.filesystem:
class: Gaufrette\Filesystem
arguments:
- '@kunstmaan_form.filesystem_adapter'
public: true
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kunstmaan\FormBundle\Tests\Entity\FormSubmissionFieldTypes;

use Gaufrette\Filesystem;
use Kunstmaan\FormBundle\Entity\FormSubmissionFieldTypes\FileFormSubmissionField;
use Kunstmaan\FormBundle\Form\FileFormSubmissionType;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -79,6 +80,55 @@ public function testGetSubmissionTemplate()
}

public function testUpload()
{
$object = $this->object;

$file = $this->getMockBuilder(UploadedFile::class)
->disableOriginalConstructor()
->getMock();

$file->expects($this->any())
->method('getPathname')
->willReturn(__FILE__);

$object->file = $file;

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();

$builder = $this->getMockBuilder(FormBuilder::class)
->disableOriginalConstructor()
->getMock();

$request = new Request();

$container = $this->getMockBuilder(Container::class)
->disableOriginalConstructor()
->getMock();

$container->expects($this->any())
->method('getParameter')
->willReturn('whatever');

$filesystem = $this->getMockBuilder(Filesystem::class)
->disableOriginalConstructor()
->getMock();

$container->expects($this->any())
->method('has')
->willReturn(true);

$container->expects($this->any())
->method('get')
->willReturn($filesystem);

$object->onValidPost($form, $builder, $request, $container);
$this->assertNull($object->upload('...', '..', $filesystem));
$object->upload(__DIR__ . '/../../Resources/assets/', __DIR__ . '/../../Resources/assets/', $filesystem);
}

public function testUploadBC()
Autsider666 marked this conversation as resolved.
Show resolved Hide resolved
Autsider666 marked this conversation as resolved.
Show resolved Hide resolved
{
$object = $this->object;
$this->assertNull($object->upload('..', '..'));
Expand Down Expand Up @@ -112,6 +162,10 @@ public function testUpload()
->method('getParameter')
->willReturn('whatever');

$container->expects($this->any())
->method('has')
->willReturn(false);

$object->onValidPost($form, $builder, $request, $container);
}
}