-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,382 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/vendor/ | ||
/composer.lock | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -13,7 +14,7 @@ | |
/** | ||
* @author Shota Hoshino <[email protected]> | ||
*/ | ||
class EventSubscriber implements EventSubscriberInterface | ||
class EventSubscriber extends AbstractContextAwareEventSubscriber | ||
{ | ||
/** | ||
* @var AuthorizationCheckerInterface | ||
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.