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 4 commits
Commits
File filter

Filter by extension

Filter by extension

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

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\HttpKernel\Kernel;

/**
* This is the class that validates and merges configuration from your app/config files
Expand All @@ -30,6 +31,10 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->booleanNode('deletable_formsubmissions')->defaultFalse()->end()
->scalarNode('web_root')
->defaultValue(version_compare(Kernel::VERSION_ID, 40000, '<') ? 'web' : 'public')
->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)
{
// 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) {
$url = \sprintf('%s%s/%s', $webDir, $uuid, $safeFileName);
$content = \file_get_contents($this->file->getPathname());
$filesystem->write($url, $content);
} else {
@trigger_error(
sprintf('Not passing a valid value for the "$filesystem" argument of "%s" is deprecated since KunstmaanFormBundle 5.8 and will be required in KunstmaanFormBundle 6.0.', __METHOD__),
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;
if ($container->has('kunstmaan_form.filesystem')) {
$filesystem = $container->get('kunstmaan_form.filesystem');
}

$this->upload($uploadDir, $webDir, $filesystem);
}

/**
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,59 @@ 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);
}

/**
* @group legacy
* @expectedDeprecation Not passing a valid value for the "$filesystem" argument of "Kunstmaan\FormBundle\Entity\FormSubmissionFieldTypes\FileFormSubmissionField::upload" is deprecated since KunstmaanFormBundle 5.8 and will be required in KunstmaanFormBundle 6.0.
*/
public function testUploadWithoutFilesystemService()
{
$object = $this->object;
$this->assertNull($object->upload('..', '..'));
Expand Down Expand Up @@ -112,6 +166,10 @@ public function testUpload()
->method('getParameter')
->willReturn('whatever');

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

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