Skip to content

Commit

Permalink
Merge branch 'add-ability-filter-images'
Browse files Browse the repository at this point in the history
  • Loading branch information
hshn committed Jan 4, 2015
2 parents 84711d8 + 9e6203c commit 8293396
Show file tree
Hide file tree
Showing 36 changed files with 1,211 additions and 116 deletions.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ HshnSerializerExtraBundle

This bundle provides some extra features for serialization.

### Exporting authorities of the classes
### Exporting authorities of objects

```yaml
# app/config.yml
Expand Down Expand Up @@ -77,3 +77,56 @@ class User
$serializer->serialize($blog, 'json'); // will export the blog authorities (depth 0)
$serializer->serialize($user, 'json'); // will NOT export the blog authorities (depth 1)
```

### Export files as URLs

> This feature require [VichUploaderBundle](https://github.com/dustin10/VichUploaderBundle)
```yaml
# app/config.yml
hshn_serializer_extra:
vich_uploader:
classes:
AcmeBundle\Entity\Blog:
files:
- { property: picture }
- { property: picture, export_to: image }
```
```php
/** @var $serializer JMS\Serializer\Serializer */
$json = $serializer->serialize($blog, 'json');
```

Generated URLs will be exported when serializing an object.

```json
{
"picture": "/url/to/picture",
"image": "/url/to/picture"
}
```

### Export images as URLs

> This feature require [LiipImagineBundle](https://github.com/liip/LiipImagineBundle)
Adding a filter name specification to a file configuration.

```yaml
# app/config.yml
hshn_serializer_extra:
vich_uploader:
classes:
AcmeBundle\Entity\Blog:
files:
- { property: picture }
- { property: picture, export_to: image, filter: thumbnail }
```
```json
{
"picture": "/url/to/picture",
"image": "/url/to/thumbnail/picture"
}
```
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
"symfony/browser-kit": "*",
"jms/serializer": "*",
"jms/serializer-bundle": "*",
"vich/uploader-bundle": "~0.10"
"vich/uploader-bundle": "~0.10",
"liip/imagine-bundle": "~1.0"
},
"suggest": {
"vich/uploader-bundle": "~0.10"
"vich/uploader-bundle": "to serialize uploaded files as an uri",
"liip/imagine-bundle": "to apply a filter to uploaded images before serializing a file as an uri"
},
"autoload": {
"psr-4": { "Hshn\\SerializerExtraBundle\\": "src" }
Expand Down
File renamed without changes.
13 changes: 4 additions & 9 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,12 @@ public function getConfigTreeBuilder()
->useAttributeAsKey('class')
->prototype('array')
->children()
->arrayNode('attributes')
->useAttributeAsKey('attribute')
->arrayNode('files')
->prototype('array')
->children()
->scalarNode('alias')->defaultNull()->end()
->end()
->beforeNormalization()
->ifString()
->then(function ($v) {
return ['alias' => $v];
})
->scalarNode('property')->cannotBeEmpty()->end()
->scalarNode('export_to')->defaultNull()->end()
->scalarNode('filter')->defaultNull()->end()
->end()
->end()
->end()
Expand Down
67 changes: 60 additions & 7 deletions src/DependencyInjection/HshnSerializerExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Hshn\SerializerExtraBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration
Expand Down Expand Up @@ -76,20 +76,73 @@ private function loadAuthority(ContainerBuilder $container, LoaderInterface $loa
*/
private function loadVichUploader(ContainerBuilder $container, LoaderInterface $loader, array $config)
{
$this->ensureBundleEnabled($container, 'VichUploaderBundle');
$loader->load('vich_uploader.xml');

$isFilterSpecified = false;
$configurations = [];
foreach ($config['classes'] as $class => $vars) {
$id = sprintf('hshn.serializer_extra.vich_uploader.configuration.%s', md5($class));
$container->setDefinition($id, $definition = new DefinitionDecorator('hshn.serializer_extra.vich_uploader.configuration'));
$definition
->addArgument($class)
->addArgument($vars['attributes'])
->addArgument($vars['max_depth']);

$definition = new DefinitionDecorator('hshn.serializer_extra.vich_uploader.configuration');
$definition->setArguments([$class, $this->createVichUploaderFileConfig($container, $id, $vars['files'], $isFilterSpecified), $vars['max_depth']]);
$container->setDefinition($id, $definition);

$configurations[] = new Reference($id);
}

$container->getDefinition('hshn.serializer_extra.vich_uploader.configuration_repository')->addArgument($configurations);

if ($isFilterSpecified) {
$this->loadLiipImagineFilter($container, $loader);
}
}

/**
* @param ContainerBuilder $container
* @param string $prefix
* @param array $files
* @param bool $isFilterSpecified
*
* @return \Symfony\Component\DependencyInjection\Reference[]
*/
private function createVichUploaderFileConfig(ContainerBuilder $container, $prefix, array $files, &$isFilterSpecified)
{
$references = [];
foreach ($files as $i => $file) {
$id = "{$prefix}.file{$i}";
$container->setDefinition($id, $definition = new DefinitionDecorator('hshn.serializer_extra.vich_uploader.configuration.file'));
$definition->setArguments([$file['property'], $file['export_to'], $file['filter']]);

$references[] = new Reference($id);
$isFilterSpecified = $isFilterSpecified || $file['filter'] !== null;
}

return $references;
}

/**
* @param ContainerBuilder $container
* @param LoaderInterface $loader
*/
private function loadLiipImagineFilter(ContainerBuilder $container, LoaderInterface $loader)
{
$this->ensureBundleEnabled($container, 'LiipImagineBundle');
$loader->load('liip_imagine.xml');

$container->setAlias('hshn.serializer_extra.vich_uploader.uri_resolver', 'hshn.serializer_extra.vich_uploader.uri_resolver.apply_filter');
}


/**
* @param ContainerBuilder $container
* @param string $bundle
*/
private function ensureBundleEnabled(ContainerBuilder $container, $bundle)
{
$bundles = $container->getParameter('kernel.bundles');
if (!isset($bundles[$bundle])) {
throw new \RuntimeException(sprintf('The HshnSerializerExtraBundle requires the %s to enable integration of it, please make sure to enable it in your AppKernel', $bundle));
}
}
}
12 changes: 12 additions & 0 deletions src/Resources/config/liip_imagine.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="hshn.serializer_extra.vich_uploader.uri_resolver.apply_filter" class="Hshn\SerializerExtraBundle\VichUploader\UriResolver\ApplyFilterResolver" public="false">
<argument type="service" id="hshn.serializer_extra.vich_uploader.uri_resolver.storage" />
<argument type="service" id="liip_imagine.cache.manager" />
</service>
</services>
</container>
7 changes: 6 additions & 1 deletion src/Resources/config/vich_uploader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
<services>
<service id="hshn.serializer_extra.vich_uploader.configuration_repository" class="Hshn\SerializerExtraBundle\VichUploader\ConfigurationRepository" />
<service id="hshn.serializer_extra.vich_uploader.configuration" class="Hshn\SerializerExtraBundle\VichUploader\Configuration" abstract="true" />
<service id="hshn.serializer_extra.vich_uploader.configuration.file" class="Hshn\SerializerExtraBundle\VichUploader\Configuration\File" abstract="true" />
<service id="hshn.serializer_extra.vich_uploader.uri_resolver.storage" class="Hshn\SerializerExtraBundle\VichUploader\UriResolver\StorageResolver" public="false">
<argument type="service" id="vich_uploader.storage" />
</service>
<service id="hshn.serializer_extra.vich_uploader.uri_resolver" alias="hshn.serializer_extra.vich_uploader.uri_resolver.storage" />
<service id="hshn.serializer_extra.vich_uploader.event_subscriber" class="Hshn\SerializerExtraBundle\VichUploader\EventSubscriber" parent="hshn.serilaizer_extra.event_subscriber.abstract_context_aware">
<argument type="service" id="vich_uploader.property_mapping_factory" />
<argument type="service" id="vich_uploader.storage" />
<argument type="service" id="hshn.serializer_extra.vich_uploader.configuration_repository" />
<argument type="service" id="hshn.serializer_extra.vich_uploader.uri_resolver" />
<tag name="jms_serializer.event_subscriber" />
</service>
</services>
Expand Down
55 changes: 30 additions & 25 deletions src/VichUploader/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Hshn\SerializerExtraBundle\VichUploader;

use Hshn\SerializerExtraBundle\VichUploader\Configuration\File;

/**
* @author Shota Hoshino <[email protected]>
*/
Expand All @@ -18,24 +20,22 @@ class Configuration
private $class;

/**
* @var array
* @var File[]
*/
private $attributes;
private $files;

/**
* @param string $class
* @param array $attributes
* @param array $files
* @param int $maxDepth
*/
public function __construct($class, array $attributes = [], $maxDepth = -1)
public function __construct($class, array $files = [], $maxDepth = -1)
{
$this->class = $class;
$this->attributes = [];
$this->files = [];
$this->maxDepth = $maxDepth;

foreach ($attributes as $attribute => $vars) {
$this->setAttribute($attribute, $vars['alias']);
}
$this->addFiles($files);
}

/**
Expand All @@ -55,37 +55,42 @@ public function getMaxDepth()
}

/**
* @param string $attribute
* @param string $alias
* @param array $files
*
* @return Configuration
* @return $this
*/
public function setAttribute($attribute, $alias = null)
public function addFiles(array $files)
{
$this->attributes[$attribute] = $alias ?: $attribute;
foreach ($files as $file) {
$this->addFile($file);
}

return $this;
}

/**
* @return array
* @param File $file
*
* @return $this
*/
public function getAttributes()
public function addFile(File $file)
{
return $this->attributes;
foreach ($this->files as $f) {
if ($f->getExportTo() === $file->getExportTo()) {
throw new \InvalidArgumentException(sprintf('File destination "%s" has already been specified by others', $f->getExportTo()));
}
}

$this->files[] = $file;

return $this;
}

/**
* @param string $attribute
*
* @return string
* @return File[]
*/
public function getAlias($attribute)
public function getFiles()
{
if (array_key_exists($attribute, $this->attributes)) {
return $this->attributes[$attribute];
}

throw new \LogicException('Invalid attribute "%s"');
return $this->files;
}
}
62 changes: 62 additions & 0 deletions src/VichUploader/Configuration/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php


namespace Hshn\SerializerExtraBundle\VichUploader\Configuration;


/**
* @author Shota Hoshino <[email protected]>
*/
class File
{
/**
* @var string
*/
private $property;

/**
* @var string
*/
private $exportTo;

/**
* @var null|string
*/
private $filter;

/**
* @param string $property
* @param string $exportTo
* @param string $filter
*/
public function __construct($property, $exportTo = null, $filter = null)
{
$this->property = $property;
$this->exportTo = $exportTo ?: $property;
$this->filter = $filter;
}

/**
* @return string
*/
public function getProperty()
{
return $this->property;
}

/**
* @return string
*/
public function getExportTo()
{
return $this->exportTo;
}

/**
* @return null|string
*/
public function getFilter()
{
return $this->filter;
}
}
Loading

0 comments on commit 8293396

Please sign in to comment.