Skip to content

Commit

Permalink
Merge branch 'vich-uploader'
Browse files Browse the repository at this point in the history
  • Loading branch information
hshn committed Dec 31, 2014
2 parents 15e9f71 + d212777 commit 15f3aa5
Show file tree
Hide file tree
Showing 36 changed files with 1,382 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
/composer.lock
/node_modules/
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
language: php

node_js:
- 0.10

php:
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- npm install
- composer install --dev --prefer-source

script: phpunit
script: gulp test
19 changes: 19 additions & 0 deletions Gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var gulp = require('gulp');
var phpunit = require('gulp-phpunit');

var paths = {
tests: './tests/**/*Test.php'
};

gulp.task('watch', function () {
gulp.watch(paths.tests).on('change', function (event) {
gulp.src('phpunit.xml.dist')
.pipe(phpunit('phpunit', {testClass: event.path}))
.on('error', console.error);
});
});

gulp.task('test', function () {
return gulp.src('phpunit.xml.dist').pipe(phpunit('phpunit'))
});

6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
"symfony/security-bundle": "~2.6",
"symfony/browser-kit": "*",
"jms/serializer": "*",
"jms/serializer-bundle": "*"
"jms/serializer-bundle": "*",
"vich/uploader-bundle": "~0.10"
},
"suggest": {
"vich/uploader-bundle": "~0.10"
},
"autoload": {
"psr-4": { "Hshn\\SerializerExtraBundle\\": "src" }
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"devDependencies": {
"gulp": "^3.8.10",
"gulp-phpunit": "^0.7.0",
"gulp-watch": "^3.0.0"
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Project Test Suite">
<directory suffix="Test.php">./tests/</directory>
Expand Down
26 changes: 26 additions & 0 deletions src/AbstractContextAwareEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


namespace Hshn\SerializerExtraBundle;

use Hshn\SerializerExtraBundle\ContextMatcher\MatcherFactory;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;

/**
* @author Shota Hoshino <[email protected]>
*/
abstract class AbstractContextAwareEventSubscriber implements EventSubscriberInterface
{
/**
* @var MatcherFactory
*/
protected $matcherFactory;

/**
* @param MatcherFactory $matcherFactory
*/
public function __construct(MatcherFactory $matcherFactory)
{
$this->matcherFactory = $matcherFactory;
}
}
24 changes: 18 additions & 6 deletions src/Authority/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Hshn\SerializerExtraBundle\Authority;


use Hshn\SerializerExtraBundle\AbstractContextAwareEventSubscriber;
use Hshn\SerializerExtraBundle\ContextMatcher\MatcherFactory;
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\JsonSerializationVisitor;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
Expand All @@ -13,7 +14,7 @@
/**
* @author Shota Hoshino <[email protected]>
*/
class EventSubscriber implements EventSubscriberInterface
class EventSubscriber extends AbstractContextAwareEventSubscriber
{
/**
* @var AuthorizationCheckerInterface
Expand All @@ -31,12 +32,15 @@ class EventSubscriber implements EventSubscriberInterface
private $exportTo;

/**
* @param MatcherFactory $matcherFactory
* @param AuthorizationCheckerInterface $authorizationChecker
* @param ConfigurationRepository $configurationRepository
* @param string $exportTo
*/
public function __construct(AuthorizationCheckerInterface $authorizationChecker, ConfigurationRepository $configurationRepository, $exportTo = '_roles')
public function __construct(MatcherFactory $matcherFactory, AuthorizationCheckerInterface $authorizationChecker, ConfigurationRepository $configurationRepository, $exportTo = '_roles')
{
parent::__construct($matcherFactory);

$this->authorizationChecker = $authorizationChecker;
$this->configurationRepository = $configurationRepository;
$this->exportTo = $exportTo;
Expand All @@ -51,10 +55,8 @@ public function onPostSerialize(ObjectEvent $event)
return;
}

$maxDepth = $configuration->getMaxDepth();
$context = $event->getContext();

if (-1 !== $maxDepth && $maxDepth < $context->getDepth()) {
if (!$this->buildContextMatcher($configuration)->matches($context)) {
return;
}

Expand Down Expand Up @@ -86,6 +88,16 @@ private function getConfiguration(array $type)
}
}

/**
* @param Configuration $configuration
*
* @return \Hshn\SerializerExtraBundle\ContextMatcher\MatcherInterface
*/
private function buildContextMatcher(Configuration $configuration)
{
return $this->matcherFactory->depth($configuration->getMaxDepth());
}

/**
* {@inheritdoc}
*/
Expand Down
34 changes: 34 additions & 0 deletions src/ContextMatcher/DepthMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php


namespace Hshn\SerializerExtraBundle\ContextMatcher;

use JMS\Serializer\Context;

/**
* @author Shota Hoshino <[email protected]>
*/
class DepthMatcher implements MatcherInterface
{
/**
* @var int
*/
private $maxDepth;

/**
* @param int $maxDepth
*/
public function __construct($maxDepth)
{
$this->maxDepth = $maxDepth;
}

/**
* {@inheritdoc}
*/
public function matches(Context $context)
{
return $this->maxDepth === -1 ? true
: $context->getDepth() <= $this->maxDepth;
}
}
31 changes: 31 additions & 0 deletions src/ContextMatcher/MatcherFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace Hshn\SerializerExtraBundle\ContextMatcher;


/**
* @author Shota Hoshino <[email protected]>
*/
class MatcherFactory
{
/**
* @param int $depth
*
* @return MatcherInterface
*/
public function depth($depth)
{
return new DepthMatcher($depth);
}

/**
* @param MatcherInterface[] $matcher
*
* @return MatcherInterface
*/
public function logicalAnd(array $matcher)
{
return new AndMatcher($matcher);
}
}
18 changes: 18 additions & 0 deletions src/ContextMatcher/MatcherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Hshn\SerializerExtraBundle\ContextMatcher;

use JMS\Serializer\Context;

/**
* @author Shota Hoshino <[email protected]>
*/
interface MatcherInterface
{
/**
* @param Context $context
*
* @return bool
*/
public function matches(Context $context);
}
26 changes: 26 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('vich_uploader')
->children()
->arrayNode('classes')
->useAttributeAsKey('class')
->prototype('array')
->children()
->arrayNode('attributes')
->useAttributeAsKey('attribute')
->prototype('array')
->children()
->scalarNode('alias')->defaultNull()->end()
->end()
->beforeNormalization()
->ifString()
->then(function ($v) {
return ['alias' => $v];
})
->end()
->end()
->end()
->integerNode('max_depth')->defaultValue(-1)->end()
->end()
->end()
->end()
->end()
->end()
->end()
;

Expand Down
29 changes: 29 additions & 0 deletions src/DependencyInjection/HshnSerializerExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('service.xml');

if (isset($config['authority'])) {
$this->loadAuthority($container, $loader, $config['authority']);
}

if (isset($config['vich_uploader'])) {
$this->loadVichUploader($container, $loader, $config['vich_uploader']);
}
}

/**
Expand Down Expand Up @@ -59,4 +64,28 @@ private function loadAuthority(ContainerBuilder $container, LoaderInterface $loa

$container->setDefinition('hshn.serializer_extra.authority.event_subscriber', $roleSubscriber);
}

/**
* @param ContainerBuilder $container
* @param LoaderInterface $loader
* @param array $config
*/
private function loadVichUploader(ContainerBuilder $container, LoaderInterface $loader, array $config)
{
$loader->load('vich_uploader.xml');

$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']);

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

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

0 comments on commit 15f3aa5

Please sign in to comment.