Skip to content

Commit

Permalink
Merge pull request #23 from crowded/feature/flysystem-integration
Browse files Browse the repository at this point in the history
Flysystem integration
  • Loading branch information
sroze authored Sep 10, 2016
2 parents 0a6ae60 + a1a78f4 commit 4859a3c
Show file tree
Hide file tree
Showing 58 changed files with 1,426 additions and 286 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ before_script:
- sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS restuploadbundle;'; fi"
- php Tests/Fixtures/App/app/console doctrine:schema:update --force --env=test

script: phpunit
script:
- sh -c "export TEST_FILESYSTEM="Gaufrette"; phpunit"
- sh -c "export TEST_FILESYSTEM="Flysystem"; phpunit"

notifications:
email:
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/CompilerPass/ProcessorCompilerPass.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace SRIO\RestUploadBundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down
8 changes: 6 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
* This is the class that validates and merges configuration from your app/config files.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
Expand All @@ -26,6 +26,10 @@ public function getConfigTreeBuilder()
->useAttributeAsKey('name')
->prototype('array')
->children()
->enumNode('type')
->values(array('gaufrette', 'flysystem'))
->defaultValue('gaufrette')
->end()
->scalarNode('filesystem')->isRequired()->end()
->scalarNode('naming_strategy')
->defaultValue('srio_rest_upload.naming.default_strategy')
Expand Down
21 changes: 19 additions & 2 deletions DependencyInjection/Factory/StorageFactory.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
<?php

namespace SRIO\RestUploadBundle\DependencyInjection\Factory;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;

class StorageFactory
{
/**
* Create the storage service.
*
*/
public function create(ContainerBuilder $container, $id, array $config)
{
$adapterId = $config['filesystem'].'.adapter';

if ($config['type'] === 'gaufrette') {
$adapterDefinition = new DefinitionDecorator('srio_rest_upload.storage.gaufrette_adapter');
$adapterDefinition->setPublic(false);
$adapterDefinition->replaceArgument(0, new Reference($config['filesystem']));

$container->setDefinition($adapterId, $adapterDefinition);
} elseif ($config['type'] === 'flysystem') {
$adapterDefinition = new DefinitionDecorator('srio_rest_upload.storage.flysystem_adapter');
$adapterDefinition->setPublic(false);
$adapterDefinition->replaceArgument(0, new Reference($config['filesystem']));

$container->setDefinition($adapterId, $adapterDefinition);
}

$container
->setDefinition($id, new Definition('SRIO\RestUploadBundle\Storage\FileStorage'))
->addArgument($config['name'])
->addArgument(new Reference($config['filesystem']))
->addArgument(new Reference($adapterId))
->addArgument(new Reference($config['storage_strategy']))
->addArgument(new Reference($config['naming_strategy']))
;
Expand Down
13 changes: 7 additions & 6 deletions DependencyInjection/SRIORestUploadExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

use SRIO\RestUploadBundle\DependencyInjection\Factory\StorageFactory;

/**
* This is the class that loads and manages your bundle configuration
* This is the class that loads and manages your bundle configuration.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class SRIORestUploadExtension extends Extension
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand All @@ -36,6 +35,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('processors.xml');
$loader->load('handlers.xml');
$loader->load('strategy.xml');
$loader->load('storage.xml');

$this->createStorageServices($container, $config['storages']);
}
Expand All @@ -60,10 +60,11 @@ private function createStorageServices(ContainerBuilder $container, array $stora
/**
* Create a single storage service.
*
* @param StorageFactory $factory
* @param ContainerBuilder $containerBuilder
* @param StorageFactory $factory
* @param ContainerBuilder $containerBuilder
* @param $name
* @param array $config
* @param array $config
*
* @return string
*/
private function createStorage(StorageFactory $factory, ContainerBuilder $containerBuilder, $name, array $config)
Expand Down
4 changes: 2 additions & 2 deletions Entity/ResumableUploadSession.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace SRIO\RestUploadBundle\Entity;

/**
* This model represent a resumable upload session. It is used to store
* a session ID and the related file path.
*
*/
class ResumableUploadSession
{
Expand Down Expand Up @@ -46,7 +46,7 @@ class ResumableUploadSession
/**
* Content length.
*
* @var integer
* @var int
*/
protected $contentLength;

Expand Down
7 changes: 7 additions & 0 deletions Exception/FileExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace SRIO\RestUploadBundle\Exception;

class FileExistsException extends \Exception
{
}
7 changes: 7 additions & 0 deletions Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace SRIO\RestUploadBundle\Exception;

class FileNotFoundException extends \Exception
{
}
1 change: 1 addition & 0 deletions Exception/InternalUploadProcessorException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace SRIO\RestUploadBundle\Exception;

class InternalUploadProcessorException extends UploadProcessorException
Expand Down
1 change: 1 addition & 0 deletions Exception/UploadException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace SRIO\RestUploadBundle\Exception;

class UploadException extends \Exception
Expand Down
1 change: 1 addition & 0 deletions Exception/UploadProcessorException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace SRIO\RestUploadBundle\Exception;

class UploadProcessorException extends UploadException
Expand Down
5 changes: 3 additions & 2 deletions Model/UploadableFileInterface.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace SRIO\RestUploadBundle\Model;

use SRIO\RestUploadBundle\Storage\UploadedFile;

/**
* A file object that will be uploaded with RestUploadBundle must implements
* this interface.
*
*/
interface UploadableFileInterface
{
Expand All @@ -14,5 +15,5 @@ interface UploadableFileInterface
*
* @param UploadedFile $file
*/
public function setFile (UploadedFile $file);
public function setFile(UploadedFile $file);
}
36 changes: 22 additions & 14 deletions Processor/AbstractUploadProcessor.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace SRIO\RestUploadBundle\Processor;

use SRIO\RestUploadBundle\Exception\UploadException;
use SRIO\RestUploadBundle\Exception\UploadProcessorException;
use SRIO\RestUploadBundle\Model\UploadableFileInterface;
use SRIO\RestUploadBundle\Request\RequestContentHandler;
use SRIO\RestUploadBundle\Request\RequestContentHandlerInterface;

use SRIO\RestUploadBundle\Upload\StorageHandler;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand Down Expand Up @@ -47,12 +47,13 @@ public function __construct(StorageHandler $storageHandler)
/**
* Constructor.
*
* @param Request $request
* @param FormInterface $form
* @param array $config
* @return boolean
* @param Request $request
* @param FormInterface $form
* @param array $config
*
* @return bool
*/
public function handleUpload (Request $request, FormInterface $form = null, array $config = array())
public function handleUpload(Request $request, FormInterface $form = null, array $config = array())
{
$this->form = $form;
$this->config = $config;
Expand All @@ -66,18 +67,20 @@ public function handleUpload (Request $request, FormInterface $form = null, arra
* This method return a Response object that will be sent back
* to the client or will be caught by controller.
*
* @param Request $request
* @param Request $request
*
* @return \SRIO\RestUploadBundle\Upload\UploadResult
*/
abstract public function handleRequest (Request $request);
abstract public function handleRequest(Request $request);

/**
* Create the form data that the form will be able to handle.
*
* It walk one the form and make an intersection between its keys and
* provided data.
*
* @param array $data
* @param array $data
*
* @return array
*/
protected function createFormData(array $data)
Expand All @@ -90,7 +93,8 @@ protected function createFormData(array $data)
/**
* Get keys of the form.
*
* @param FormInterface $form
* @param FormInterface $form
*
* @return array
*/
protected function getFormKeys(FormInterface $form)
Expand All @@ -106,7 +110,8 @@ protected function getFormKeys(FormInterface $form)
/**
* Get a request content handler.
*
* @param Request $request
* @param Request $request
*
* @return RequestContentHandlerInterface
*/
protected function getRequestContentHandler(Request $request)
Expand All @@ -121,8 +126,9 @@ protected function getRequestContentHandler(Request $request)
/**
* Check that needed headers are here.
*
* @param Request $request the request
* @param array $headers the headers to check
* @param Request $request the request
* @param array $headers the headers to check
*
* @throws \SRIO\RestUploadBundle\Exception\UploadException
*/
protected function checkHeaders(Request $request, array $headers)
Expand All @@ -140,8 +146,10 @@ protected function checkHeaders(Request $request, array $headers)
/**
* Set the uploaded file on the form data.
*
* @param UploadedFile $file
* @param UploadedFile $file
*
* @throws \SRIO\RestUploadBundle\Exception\UploadProcessorException
*
* @deprecated
*/
protected function setUploadedFile(UploadedFile $file)
Expand Down
16 changes: 10 additions & 6 deletions Processor/FormDataUploadProcessor.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace SRIO\RestUploadBundle\Processor;

use SRIO\RestUploadBundle\Storage\FileStorage;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;

use SRIO\RestUploadBundle\Exception\UploadException;
use SRIO\RestUploadBundle\Upload\UploadResult;

Expand All @@ -14,21 +14,23 @@ class FormDataUploadProcessor extends SimpleUploadProcessor
const KEY_FIELD_FORM = 'key_form';

/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function handleUpload (Request $request, FormInterface $form = null, array $config = array())
public function handleUpload(Request $request, FormInterface $form = null, array $config = array())
{
$config = array_merge(array(
self::KEY_FIELD_FILE => 'file',
self::KEY_FIELD_FORM => 'form'
self::KEY_FIELD_FORM => 'form',
), $config);

return parent::handleUpload($request, $form, $config);
}

/**
* @param Request $request
* @param Request $request
*
* @return \SRIO\RestUploadBundle\Upload\UploadResult
*
* @throws \Exception|\SRIO\RestUploadBundle\Exception\UploadException
*/
public function handleRequest(Request $request)
Expand Down Expand Up @@ -76,7 +78,9 @@ public function handleRequest(Request $request)
$uploadedFile = $request->files->get($this->config[self::KEY_FIELD_FILE]);
$contents = file_get_contents($uploadedFile->getPathname());
$file = $this->storageHandler->store($response, $contents, array(
FileStorage::METADATA_CONTENT_TYPE => $uploadedFile->getMimeType()
'metadata' => array(
FileStorage::METADATA_CONTENT_TYPE => $uploadedFile->getMimeType(),
),
));

$response->setFile($file);
Expand Down
Loading

0 comments on commit 4859a3c

Please sign in to comment.