Skip to content

Commit

Permalink
Merge pull request #8201 from dongilbert/GIT-8027
Browse files Browse the repository at this point in the history
M3: Updates for JMS Serializer Compatibility
  • Loading branch information
escopecz committed Dec 11, 2019
2 parents 65ad195 + 8777f80 commit 7cca968
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 62 deletions.
7 changes: 4 additions & 3 deletions app/bundles/ApiBundle/Config/config.php
Expand Up @@ -171,9 +171,10 @@
'bazinga.oauth.security.authentication.listener.class' => 'Mautic\ApiBundle\Security\OAuth1\Firewall\OAuthListener',
'bazinga.oauth.event_listener.request.class' => 'Mautic\ApiBundle\EventListener\OAuth1\OAuthRequestListener',
'fos_oauth_server.security.authentication.listener.class' => 'Mautic\ApiBundle\Security\OAuth2\Firewall\OAuthListener',
'jms_serializer.metadata.annotation_driver.class' => 'Mautic\ApiBundle\Serializer\Driver\AnnotationDriver',
'jms_serializer.metadata.php_driver.class' => 'Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver',

'jms_serializer.metadata.annotation_driver' => 'Mautic\ApiBundle\Serializer\Driver\AnnotationDriver',
'jms_serializer.metadata.api_metadata_driver' => [
'class' => 'Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver',
],
'mautic.validator.oauthcallback' => [
'class' => 'Mautic\ApiBundle\Form\Validator\Constraints\OAuthCallbackValidator',
'tag' => 'validator.constraint_validator',
Expand Down
2 changes: 1 addition & 1 deletion app/bundles/ApiBundle/Controller/ClientController.php
Expand Up @@ -125,7 +125,7 @@ public function indexAction($page = 1)
*/
public function authorizedClientsAction()
{
$me = $this->get('security.context')->getToken()->getUser();
$me = $this->get('security.token_storage')->getToken()->getUser();
$clients = $this->getModel('api.client')->getUserClients($me);

return $this->render('MauticApiBundle:Client:authorized.html.php', ['clients' => $clients]);
Expand Down
13 changes: 7 additions & 6 deletions app/bundles/ApiBundle/Controller/CommonApiController.php
Expand Up @@ -13,9 +13,10 @@

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\Pagination\Paginator;
use FOS\RestBundle\Context\Context;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use JMS\Serializer\Exclusion\ExclusionStrategyInterface;
use JMS\Serializer\SerializationContext;
use Mautic\ApiBundle\Serializer\Exclusion\ParentChildrenExclusionStrategy;
use Mautic\ApiBundle\Serializer\Exclusion\PublishDetailsExclusionStrategy;
use Mautic\CategoryBundle\Entity\Category;
Expand Down Expand Up @@ -1077,7 +1078,7 @@ protected function processBatchForm($key, $entity, $params, $method, &$errors, &
$entity
);
}
} elseif (get_class($formResponse) === get_class($entity)) {
} elseif (is_object($formResponse) && get_class($formResponse) === get_class($entity)) {
// Success
$entities[$key] = $formResponse;
} elseif (is_array($formResponse) && isset($formResponse['code'], $formResponse['message'])) {
Expand Down Expand Up @@ -1308,11 +1309,11 @@ protected function setBatchError($key, $msg, $code, &$errors, &$entities = [], $
/**
* Set serialization groups and exclusion strategies.
*
* @param \FOS\RestBundle\View\View $view
* @param View $view
*/
protected function setSerializationContext(&$view)
protected function setSerializationContext($view)
{
$context = SerializationContext::create();
$context = $view->getContext();
if (!empty($this->serializerGroups)) {
$context->setGroups($this->serializerGroups);
}
Expand All @@ -1339,7 +1340,7 @@ protected function setSerializationContext(&$view)
$context->setSerializeNull(true);
}

$view->setSerializationContext($context);
$view->setContext($context);
}

/**
Expand Down
Expand Up @@ -37,7 +37,7 @@ public function allowAction(Request $request)
$oauth_token = $request->get('oauth_token', null);
$oauth_callback = $request->get('oauth_callback', null);

$securityContext = $this->container->get('security.context');
$securityContext = $this->container->get('security.token_storage');
$tokenProvider = $this->container->get('bazinga.oauth.provider.token_provider');

$user = $securityContext->getToken()->getUser();
Expand Down
Expand Up @@ -35,7 +35,7 @@ class AuthorizeController extends \FOS\OAuthServerBundle\Controller\AuthorizeCon
*/
public function authorizeAction(Request $request)
{
$user = $this->container->get('security.context')->getToken()->getUser();
$user = $this->container->get('security.token_storage')->getToken()->getUser();

if (!$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
Expand Down
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Mautic\ApiBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class SerializerPass implements CompilerPassInterface
{
/**
* Replaces the available metadata drivers (yaml, xml, and annotation)
* with our metadata driver, as we do not use any of those. There's
* currently no other way that I can find to get our driver into the
* chain in front of the rest.
*
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('jms_serializer.metadata.chain_driver')) {
$definition = $container->getDefinition('jms_serializer.metadata.chain_driver');
$drivers = $definition->getArgument(0);

array_unshift($drivers, new Reference('jms_serializer.metadata.api_metadata_driver'));

$definition->replaceArgument(0, $drivers);
}
}
}
2 changes: 2 additions & 0 deletions app/bundles/ApiBundle/MauticApiBundle.php
Expand Up @@ -12,6 +12,7 @@
namespace Mautic\ApiBundle;

use Mautic\ApiBundle\DependencyInjection\Compiler\OAuthPass;
use Mautic\ApiBundle\DependencyInjection\Compiler\SerializerPass;
use Mautic\ApiBundle\DependencyInjection\Factory\ApiFactory;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -29,6 +30,7 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new OAuthPass());
$container->addCompilerPass(new SerializerPass());

$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new ApiFactory());
Expand Down
13 changes: 5 additions & 8 deletions app/bundles/ApiBundle/Serializer/Driver/AnnotationDriver.php
Expand Up @@ -12,17 +12,14 @@
namespace Mautic\ApiBundle\Serializer\Driver;

use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\Driver\AnnotationDriver as BaseAnnotationDriver;
use Metadata\ClassMetadata as BaseClassMetadata;

/**
* Class AnnotationDriver.
*/
class AnnotationDriver extends \JMS\Serializer\Metadata\Driver\AnnotationDriver
class AnnotationDriver extends BaseAnnotationDriver
{
public function loadMetadataForClass(\ReflectionClass $class)
public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata
{
// Overriding annotation driver to not generate annotation cache files
$classMetadata = new ClassMetadata($name = $class->name);

return $classMetadata;
return new ClassMetadata($class->getName());
}
}
42 changes: 25 additions & 17 deletions app/bundles/ApiBundle/Serializer/Driver/ApiMetadataDriver.php
Expand Up @@ -12,10 +12,13 @@
namespace Mautic\ApiBundle\Serializer\Driver;

use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\Driver\PhpDriver;
use JMS\Serializer\Metadata\PropertyMetadata;
use Metadata\ClassMetadata as BaseClassMetadata;
use Metadata\Driver\DriverInterface;
use ReflectionClass;
use ReflectionException;

class ApiMetadataDriver extends PhpDriver
class ApiMetadataDriver implements DriverInterface
{
/**
* @var ClassMetadata
Expand All @@ -40,31 +43,38 @@ class ApiMetadataDriver extends PhpDriver
/**
* @var null
*/
private $currentPropertyName = null;
private $currentPropertyName;

/**
* @param \ReflectionClass $class
* @param string $file
* @param ReflectionClass $class
*
* @return ClassMetadata
* @return \Metadata\ClassMetadata
*
* @throws ReflectionException
*/
protected function loadMetadataFromFile(\ReflectionClass $class, $file)
public function loadMetadataForClass(ReflectionClass $class): ?BaseClassMetadata
{
if ($class->hasMethod('loadApiMetadata')) {
$this->metadata = new ClassMetadata($class->getName());

$this->properties = [];
$this->defaultVersion = '1.0';
$this->groupPrefix = '';
$class->getMethod('loadApiMetadata')->invoke(null, $this);

$serializer = $class->getMethod('loadApiMetadata');
$serializer->invoke(null, $this);
$metadata = $this->metadata;

$metadata = $this->metadata;
$this->metadata = null;
$this->resetDefaults();

return $metadata;
}

return null;
}

private function resetDefaults()
{
$this->metadata = null;
$this->properties = [];
$this->defaultVersion = '1.0';
$this->groupPrefix = '';
}

/**
Expand Down Expand Up @@ -148,9 +158,7 @@ public function addProperty($name, $serializedName = null, $useGetter = false)
$this->properties[$name]->getter = 'get'.ucfirst($name);
}

if ($serializedName) {
$this->properties[$name]->serializedName = $serializedName;
}
$this->properties[$name]->serializedName = $serializedName ?? $name;

if (null !== $this->defaultVersion) {
// Set the default version
Expand Down
Expand Up @@ -55,15 +55,15 @@ public function __construct(array $fields, $level = 3, $path = null)
/**
* {@inheritdoc}
*/
public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext)
public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext): bool
{
return false;
}

/**
* {@inheritdoc}
*/
public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext)
public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext): bool
{
if ($this->path) {
$path = implode('.', $navigatorContext->getCurrentPath());
Expand Down
Expand Up @@ -55,15 +55,15 @@ public function __construct(array $fields, $level = 3, $path = null)
/**
* {@inheritdoc}
*/
public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext)
public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext): bool
{
return false;
}

/**
* {@inheritdoc}
*/
public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext)
public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext): bool
{
if ($this->path) {
$path = implode('.', $navigatorContext->getCurrentPath());
Expand Down
2 changes: 1 addition & 1 deletion app/bundles/CoreBundle/Form/RequestTrait.php
Expand Up @@ -32,7 +32,7 @@ protected function prepareParametersFromRequest(Form $form, array &$params, $ent
// Special handling of some fields
foreach ($form as $name => $child) {
if (isset($params[$name])) {
$type = $child->getConfig()->getType()->getName();
$type = get_class($child->getConfig()->getType());
switch ($type) {
case YesNoButtonGroupType::class:
if (is_object($entity)) {
Expand Down
21 changes: 10 additions & 11 deletions app/bundles/LeadBundle/Controller/Api/LeadApiController.php
Expand Up @@ -11,7 +11,6 @@

namespace Mautic\LeadBundle\Controller\Api;

use JMS\Serializer\SerializationContext;
use Mautic\ApiBundle\Controller\CommonApiController;
use Mautic\CoreBundle\Helper\ArrayHelper;
use Mautic\CoreBundle\Helper\DateTimeHelper;
Expand Down Expand Up @@ -72,8 +71,8 @@ public function getOwnersAction()
$start = $this->request->query->get('start', null);
$users = $this->model->getLookupResults('user', $filter, $limit, $start);
$view = $this->view($users, Response::HTTP_OK);
$context = SerializationContext::create()->setGroups(['userList']);
$view->setSerializationContext($context);
$context = $view->getContext()->setGroups(['userList']);
$view->setContext($context);

return $this->handleView($view);
}
Expand Down Expand Up @@ -105,8 +104,8 @@ public function getFieldsAction()
);

$view = $this->view($fields, Response::HTTP_OK);
$context = SerializationContext::create()->setGroups(['leadFieldList']);
$view->setSerializationContext($context);
$context = $view->getContext()->setGroups(['leadFieldList']);
$view->setContext($context);

return $this->handleView($view);
}
Expand Down Expand Up @@ -159,8 +158,8 @@ public function getNotesAction($id)
Response::HTTP_OK
);

$context = SerializationContext::create()->setGroups(['leadNoteDetails']);
$view->setSerializationContext($context);
$context = $view->getContext()->setGroups(['leadNoteDetails']);
$view->setContext($context);

return $this->handleView($view);
}
Expand Down Expand Up @@ -213,8 +212,8 @@ public function getDevicesAction($id)
Response::HTTP_OK
);

$context = SerializationContext::create()->setGroups(['leadDeviceDetails']);
$view->setSerializationContext($context);
$context = $view->getContext()->setGroups(['leadDeviceDetails']);
$view->setContext($context);

return $this->handleView($view);
}
Expand Down Expand Up @@ -382,8 +381,8 @@ public function getAllActivityAction($lead = null)
list($events, $serializerGroups) = $this->model->getEngagements($lead, $filters, $order, $page, $limit, false);

$view = $this->view($events);
$context = SerializationContext::create()->setGroups($serializerGroups);
$view->setSerializationContext($context);
$context = $view->getContext()->setGroups($serializerGroups);
$view->setContext($context);

return $this->handleView($view);
}
Expand Down
5 changes: 2 additions & 3 deletions app/bundles/LeadBundle/Controller/Api/ListApiController.php
Expand Up @@ -11,7 +11,6 @@

namespace Mautic\LeadBundle\Controller\Api;

use JMS\Serializer\SerializationContext;
use Mautic\ApiBundle\Controller\CommonApiController;
use Mautic\LeadBundle\Controller\LeadAccessTrait;
use Mautic\LeadBundle\Entity\LeadList;
Expand Down Expand Up @@ -42,8 +41,8 @@ public function getListsAction()
{
$lists = $this->getModel('lead.list')->getUserLists();
$view = $this->view($lists, Response::HTTP_OK);
$context = SerializationContext::create()->setGroups(['leadListList']);
$view->setSerializationContext($context);
$context = $view->getContext()->setGroups(['leadListList']);
$view->setContext($context);

return $this->handleView($view);
}
Expand Down
4 changes: 2 additions & 2 deletions app/bundles/LeadBundle/Controller/LeadController.php
Expand Up @@ -245,7 +245,7 @@ public function quickAddAction()
$quickForm = $model->createForm($model->getEntity(), $this->get('form.factory'), $action, ['fields' => $fields, 'isShortForm' => true]);

//set the default owner to the currently logged in user
$currentUser = $this->get('security.context')->getToken()->getUser();
$currentUser = $this->get('security.token_storage')->getToken()->getUser();
$quickForm->get('owner')->setData($currentUser);

return $this->delegateView(
Expand Down Expand Up @@ -526,7 +526,7 @@ public function newAction()
}
} else {
//set the default owner to the currently logged in user
$currentUser = $this->get('security.context')->getToken()->getUser();
$currentUser = $this->get('security.token_storage')->getToken()->getUser();
$form->get('owner')->setData($currentUser);
}

Expand Down

0 comments on commit 7cca968

Please sign in to comment.