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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the support for class inheritance and skipping handlers #177

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/JMS/Serializer/Exception/SkipStepException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* Copyright 2013 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Exception;

/**
* @author Kinn Coelho Julião <[email protected]>
* @author Juti Noppornpitak <[email protected]>
*/
class SkipStepException extends RuntimeException
{
}
9 changes: 6 additions & 3 deletions src/JMS/Serializer/GraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use JMS\Serializer\Metadata\ClassMetadata;
use Metadata\MetadataFactoryInterface;
use JMS\Serializer\Exception\InvalidArgumentException;
use JMS\Serializer\Exception\SkipStepException;

/**
* Handles traversal along the object graph.
Expand Down Expand Up @@ -169,10 +170,12 @@ public function accept($data, array $type = null, Context $context)
// before loading metadata because the type name might not be a class, but
// could also simply be an artifical type.
if (null !== $handler = $this->handlerRegistry->getHandler($context->getDirection(), $type['name'], $context->getFormat())) {
$rs = call_user_func($handler, $visitor, $data, $type, $context);
$this->leaveScope($context, $data);
try {
$rs = call_user_func($handler, $visitor, $data, $type, $context);
$this->leaveScope($context, $data);

return $rs;
return $rs;
} catch (SkipStepException $e) {}
}

$exclusionStrategy = $context->getExclusionStrategy();
Expand Down
93 changes: 92 additions & 1 deletion src/JMS/Serializer/Handler/LazyHandlerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ public function getHandler($direction, $typeName, $format)
}

if ( ! isset($this->handlers[$direction][$typeName][$format])) {
$matchedParent = $this->matchParent($typeName, $direction, $format);

if (false !== $matchedParent) {
$matchedParent[0] = $this->container->get($matchedParent[0]);

return $this->initializedHandlers[$direction][$typeName][$format] = $matchedParent;
}

$matchedInterface = $this->matchInterface($typeName, $direction, $format);

if (false !== $matchedInterface) {
$matchedInterface[0] = $this->container->get($matchedInterface[0]);

return $this->initializedHandlers[$direction][$typeName][$format] = $matchedInterface;
}

return null;
}

Expand All @@ -54,4 +70,79 @@ public function getHandler($direction, $typeName, $format)

return $this->initializedHandlers[$direction][$typeName][$format] = $handler;
}
}

/**
* Match a parent class
*
* @param string $typeName
* @param string $direction
* @param string $format
*
* @return mixed
*/
protected function matchParent($typeName, $direction, $format)
{
try {
$class = new \ReflectionClass($typeName);
$parents = array();

while ($parent = $class->getParentClass()) {
$parentClassName = $parent->getName();

foreach ($this->handlers[$direction] as $knownTypeName => $formatMap) {
if ($knownTypeName === $parentClassName) {

return $formatMap[$format];
}
}

$matchedInterface = $this->matchInterface($parentClassName, $direction, $format);

if (false !== $matchedInterface) {
return $matchedInterface;
}

$class = $parent;
}
} catch (\ReflectionException $e) {}

return false;
}

/**
* Match an interface
*
* @param string $typeName
* @param string $direction
* @param string $format
*
* @return mixed
*/
protected function matchInterface($typeName, $direction, $format)
{
try {
$class = new \ReflectionClass($typeName);
$parents = array();
$interfaceList = $class->getInterfaces();

foreach ($interfaceList as $reflectedClass) {
$interfaceName = $reflectedClass->getName();

$matchedParent = $this->matchParent($interfaceName, $direction, $format);

if (false !== $matchedParent) {
return $matchedParent;
}

foreach ($this->handlers[$direction] as $knownTypeName => $formatMap) {
if ($knownTypeName === $interfaceName) {

return $formatMap[$format];
}
}
}
} catch (\ReflectionException $e) {}

return false;
}
}