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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializer refactoring #3

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b0a46c8
makes sure that explicit lists are serialized as lists
schmittjoh May 21, 2016
30e00c7
Removed handle callback feature
goetas Jul 20, 2016
34375e9
Simplify event dispatch
goetas Jul 20, 2016
bef9696
Added graph navigator factory
goetas Jul 21, 2016
77d8e76
Split graph navigator into two classes
goetas Jul 21, 2016
f24b8d5
simplified graph navigator
goetas Jul 21, 2016
e40ce46
Factory typehint
goetas Jul 21, 2016
3be1dea
converted type array as TypeDefinition Object
goetas Jul 21, 2016
cda5afc
Green test
goetas Jul 21, 2016
5d53b4c
unified primitive types and class handling at graph navigation level
goetas Jul 22, 2016
345052d
psr
goetas Jul 22, 2016
9931744
Removed deprecated getContext
goetas Jul 22, 2016
a50abfc
Fix form interface
goetas Jul 22, 2016
3e2d3ab
removed phpoption/phpoption and phpcollection/phpcollection
goetas Jul 22, 2016
387680f
Tweak some scrutinizer opt
goetas Jul 22, 2016
cc26348
Minor improvements on the context class
goetas Jul 22, 2016
f0d6f13
serialization direction is a string
goetas Jul 22, 2016
2bd9cd9
allow wildcard handlers
goetas Jul 22, 2016
c3a57ea
move metadata builder to metadata folder
goetas Jul 22, 2016
4088ce2
Moved graph classes into graph namespace
goetas Jul 22, 2016
0217323
moved visitor classes into visitor namespaces
goetas Jul 22, 2016
1ef1f48
Moved visitor util yaml class
goetas Jul 22, 2016
f6ae824
use type accessors
goetas Jul 22, 2016
d0d50a9
Unified serialization of cached metadata
goetas Jul 22, 2016
07c0f68
Fixed benchmark
goetas Jul 22, 2016
6f03227
optimized registry handler
goetas Jul 22, 2016
6d88387
hide not necessary public method
goetas Jul 22, 2016
fd7122a
do not handle propel by default
goetas Jul 22, 2016
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
14 changes: 13 additions & 1 deletion .scrutinizer.yml
@@ -1,3 +1,15 @@
tools:
external_code_coverage:
timeout: 600
timeout: 600

filter:
paths:
- 'src/'
coding_style:
php:
indentation:
general:
size: 1
spaces:
around_operators:
multiplicative: false
2 changes: 1 addition & 1 deletion UPGRADING.md
Expand Up @@ -8,7 +8,7 @@ From 0.13 to ???
From 0.11 to 0.12
=================

- GraphNavigator::detachObject has been removed, you can directly use Context::stopVisiting instead.
- Graph\GraphNavigator::detachObject has been removed, you can directly use Context::stopVisiting instead.
- VisitorInterface::getNavigator was deprecated, instead use Context::accept
- Serializer::setGroups, Serializer::setExclusionStrategy and Serializer::setVersion were removed, these settings must
now be passed as part of a new Context object.
Expand Down
5 changes: 1 addition & 4 deletions composer.json
Expand Up @@ -15,8 +15,6 @@
"php": ">=5.5.0",
"jms/metadata": "~1.1",
"jms/parser-lib": "1.*",
"phpoption/phpoption": "^1.1",
"phpcollection/phpcollection": "~0.1",
"doctrine/annotations": "^1.0",
"doctrine/instantiator": "^1.0.3"
},
Expand All @@ -29,10 +27,9 @@
"require-dev": {
"twig/twig": "~1.12|~2.0",
"doctrine/orm": "~2.1",
"doctrine/phpcr-odm": "^1.3",
"jackalope/jackalope-doctrine-dbal": "^1.1.5",
"doctrine/phpcr-odm": "^1.3",
"propel/propel1": "~1.7",
"symfony/yaml": "^2.1",
"symfony/translation": "^2.1",
"symfony/validator": "^2.2",
"symfony/form": "~2.1",
Expand Down
4 changes: 2 additions & 2 deletions src/JMS/Serializer/Annotation/ExclusionPolicy.php
Expand Up @@ -27,13 +27,13 @@
final class ExclusionPolicy
{
const NONE = 'NONE';
const ALL = 'ALL';
const ALL = 'ALL';

public $policy;

public function __construct(array $values)
{
if ( ! is_string($values['value'])) {
if (!is_string($values['value'])) {
throw new RuntimeException('"value" must be a string.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/JMS/Serializer/Annotation/SerializedName.php
Expand Up @@ -30,7 +30,7 @@ final class SerializedName

public function __construct(array $values)
{
if ( ! is_string($values['value'])) {
if (!is_string($values['value'])) {
throw new RuntimeException(sprintf('"value" must be a string.'));
}

Expand Down
2 changes: 1 addition & 1 deletion src/JMS/Serializer/Annotation/XmlElement.php
Expand Up @@ -28,7 +28,7 @@ final class XmlElement
* @var boolean
*/
public $cdata = true;

/**
* @var string
*/
Expand Down
18 changes: 9 additions & 9 deletions src/JMS/Serializer/Construction/DoctrineObjectConstructor.php
Expand Up @@ -19,9 +19,9 @@
namespace JMS\Serializer\Construction;

use Doctrine\Common\Persistence\ManagerRegistry;
use JMS\Serializer\VisitorInterface;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Visitor\VisitorInterface;

/**
* Doctrine object constructor for new (or existing) objects during deserialization.
Expand All @@ -34,24 +34,24 @@ class DoctrineObjectConstructor implements ObjectConstructorInterface
/**
* Constructor.
*
* @param ManagerRegistry $managerRegistry Manager registry
* @param ManagerRegistry $managerRegistry Manager registry
* @param ObjectConstructorInterface $fallbackConstructor Fallback object constructor
*/
public function __construct(ManagerRegistry $managerRegistry, ObjectConstructorInterface $fallbackConstructor)
{
$this->managerRegistry = $managerRegistry;
$this->managerRegistry = $managerRegistry;
$this->fallbackConstructor = $fallbackConstructor;
}

/**
* {@inheritdoc}
*/
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, TypeDefinition $type, DeserializationContext $context)
{
// Locate possible ObjectManager
$objectManager = $this->managerRegistry->getManagerForClass($metadata->name);

if ( ! $objectManager) {
if (!$objectManager) {
// No ObjectManager found, proceed with normal deserialization
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}
Expand All @@ -65,17 +65,17 @@ public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $d
}

// Managed entity, check for proxy load
if ( ! is_array($data)) {
if (!is_array($data)) {
// Single identifier, load proxy
return $objectManager->getReference($metadata->name, $data);
}

// Fallback to default constructor if missing identifier(s)
$classMetadata = $objectManager->getClassMetadata($metadata->name);
$classMetadata = $objectManager->getClassMetadata($metadata->name);
$identifierList = array();

foreach ($classMetadata->getIdentifierFieldNames() as $name) {
if ( ! array_key_exists($name, $data)) {
if (!array_key_exists($name, $data)) {
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}

Expand Down
Expand Up @@ -18,9 +18,10 @@

namespace JMS\Serializer\Construction;

use JMS\Serializer\VisitorInterface;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\TypeDefinition;
use JMS\Serializer\Visitor\VisitorInterface;

/**
* Implementations of this interface construct new objects during deserialization.
Expand All @@ -38,9 +39,9 @@ interface ObjectConstructorInterface
* @param VisitorInterface $visitor
* @param ClassMetadata $metadata
* @param mixed $data
* @param array $type ["name" => string, "params" => array]
* @param TypeDefinition $type ["name" => string, "params" => array]
*
* @return object
*/
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context);
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, TypeDefinition $type, DeserializationContext $context);
}
Expand Up @@ -19,16 +19,17 @@
namespace JMS\Serializer\Construction;

use Doctrine\Instantiator\Instantiator;
use JMS\Serializer\VisitorInterface;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\TypeDefinition;
use JMS\Serializer\Visitor\VisitorInterface;

class UnserializeObjectConstructor implements ObjectConstructorInterface
{
/** @var Instantiator */
/** @var Instantiator */
private $instantiator;

public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, TypeDefinition $type, DeserializationContext $context)
{
return $this->getInstantiator()->instantiate($metadata->name);
}
Expand Down
36 changes: 12 additions & 24 deletions src/JMS/Serializer/Context.php
Expand Up @@ -24,18 +24,16 @@
use JMS\Serializer\Exclusion\ExclusionStrategyInterface;
use JMS\Serializer\Exclusion\GroupsExclusionStrategy;
use JMS\Serializer\Exclusion\VersionExclusionStrategy;
use JMS\Serializer\Graph\GraphNavigator;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Visitor\VisitorInterface;
use Metadata\MetadataFactory;
use Metadata\MetadataFactoryInterface;
use PhpCollection\Map;

abstract class Context
{
/**
* @var \PhpCollection\Map
*/
public $attributes;
public $attributes = [];

private $format;

Expand All @@ -59,11 +57,6 @@ abstract class Context
/** @var \SplStack */
private $metadataStack;

public function __construct()
{
$this->attributes = new Map();
}

/**
* @param string $format
*/
Expand All @@ -81,11 +74,6 @@ public function initialize($format, VisitorInterface $visitor, GraphNavigator $n
$this->metadataStack = new \SplStack();
}

public function accept($data, array $type = null)
{
return $this->navigator->accept($data, $type, $this);
}

public function getMetadataFactory()
{
return $this->metadataFactory;
Expand All @@ -109,14 +97,14 @@ public function getExclusionStrategy()
public function setAttribute($key, $value)
{
$this->assertMutable();
$this->attributes->set($key, $value);
$this->attributes[$key] = $value;

return $this;
}

private function assertMutable()
{
if ( ! $this->initialized) {
if (!$this->initialized) {
return;
}

Expand Down Expand Up @@ -156,7 +144,7 @@ public function setVersion($version)
throw new \LogicException('The version must not be null.');
}

$this->attributes->set('version', $version);
$this->attributes['version'] = $version;
$this->addExclusionStrategy(new VersionExclusionStrategy($version));

return $this;
Expand All @@ -170,9 +158,9 @@ public function setGroups($groups)
if (empty($groups)) {
throw new \LogicException('The groups must not be empty.');
}

$this->attributes->set('groups', (array) $groups);
$this->addExclusionStrategy(new GroupsExclusionStrategy((array) $groups));
$this->attributes['groups'] = (array)$groups;
$this->addExclusionStrategy(new GroupsExclusionStrategy((array)$groups));

return $this;
}
Expand All @@ -186,7 +174,7 @@ public function enableMaxDepthChecks()

public function setSerializeNull($bool)
{
$this->serializeNull = (boolean) $bool;
$this->serializeNull = (boolean)$bool;

return $this;
}
Expand Down Expand Up @@ -218,7 +206,7 @@ public function popPropertyMetadata()
{
$metadata = $this->metadataStack->pop();

if ( ! $metadata instanceof PropertyMetadata) {
if (!$metadata instanceof PropertyMetadata) {
throw new RuntimeException('Context metadataStack not working well');
}
}
Expand All @@ -227,7 +215,7 @@ public function popClassMetadata()
{
$metadata = $this->metadataStack->pop();

if ( ! $metadata instanceof ClassMetadata) {
if (!$metadata instanceof ClassMetadata) {
throw new RuntimeException('Context metadataStack not working well');
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/JMS/Serializer/DeserializationContext.php
Expand Up @@ -18,6 +18,8 @@

namespace JMS\Serializer;

use JMS\Serializer\Graph\GraphNavigator;

class DeserializationContext extends Context
{
private $depth = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/JMS/Serializer/EventDispatcher/Event.php
Expand Up @@ -19,13 +19,14 @@
namespace JMS\Serializer\EventDispatcher;

use JMS\Serializer\Context;
use JMS\Serializer\TypeDefinition;

class Event
{
protected $type;
private $context;

public function __construct(Context $context, array $type)
public function __construct(Context $context, TypeDefinition $type)
{
$this->context = $context;
$this->type = $type;
Expand Down
15 changes: 8 additions & 7 deletions src/JMS/Serializer/EventDispatcher/EventDispatcher.php
Expand Up @@ -17,6 +17,7 @@
*/

namespace JMS\Serializer\EventDispatcher;

use JMS\Serializer\Exception\InvalidArgumentException;

/**
Expand All @@ -32,9 +33,9 @@ class EventDispatcher implements EventDispatcherInterface
private $listeners = array();
private $classListeners = array();

public static function getDefaultMethodName($eventName)
private static function getDefaultMethodName($eventName)
{
return 'on'.str_replace(array('_', '.'), '', $eventName);
return 'on' . str_replace(array('_', '.'), '', $eventName);
}

/**
Expand All @@ -57,7 +58,7 @@ public function addListener($eventName, $callable, $class = null, $format = null
public function addSubscriber(EventSubscriberInterface $subscriber)
{
foreach ($subscriber->getSubscribedEvents() as $eventData) {
if ( ! isset($eventData['event'])) {
if (!isset($eventData['event'])) {
throw new InvalidArgumentException(sprintf('Each event must have a "event" key.'));
}

Expand All @@ -71,12 +72,12 @@ public function addSubscriber(EventSubscriberInterface $subscriber)

public function hasListeners($eventName, $class, $format)
{
if ( ! isset($this->listeners[$eventName])) {
if (!isset($this->listeners[$eventName])) {
return false;
}

$loweredClass = strtolower($class);
if ( ! isset($this->classListeners[$eventName][$loweredClass][$format])) {
if (!isset($this->classListeners[$eventName][$loweredClass][$format])) {
$this->classListeners[$eventName][$loweredClass][$format] = $this->initializeListeners($eventName, $loweredClass, $format);
}

Expand All @@ -85,12 +86,12 @@ public function hasListeners($eventName, $class, $format)

public function dispatch($eventName, $class, $format, Event $event)
{
if ( ! isset($this->listeners[$eventName])) {
if (!isset($this->listeners[$eventName])) {
return;
}

$loweredClass = strtolower($class);
if ( ! isset($this->classListeners[$eventName][$loweredClass][$format])) {
if (!isset($this->classListeners[$eventName][$loweredClass][$format])) {
$this->classListeners[$eventName][$loweredClass][$format] = $this->initializeListeners($eventName, $loweredClass, $format);
}

Expand Down
4 changes: 3 additions & 1 deletion src/JMS/Serializer/EventDispatcher/Events.php
Expand Up @@ -25,5 +25,7 @@ abstract class Events
const PRE_DESERIALIZE = 'serializer.pre_deserialize';
const POST_DESERIALIZE = 'serializer.post_deserialize';

final private function __construct() { }
final private function __construct()
{
}
}