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

Support new doctrine ODM proxy objects #1139

Merged
merged 2 commits into from
Nov 14, 2019
Merged
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"doctrine/orm": "~2.1",
"jackalope/jackalope-doctrine-dbal": "^1.1.5",
"doctrine/phpcr-odm": "^1.3|^2.0",
"ocramius/proxy-manager": "^1.0|^2.0",
"psr/container": "^1.0",
"symfony/dependency-injection": "^3.0|^4.0",
"symfony/yaml": "^3.3|^4.0",
Expand Down
11 changes: 8 additions & 3 deletions src/EventDispatcher/Subscriber/DoctrineProxySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Doctrine\ODM\MongoDB\PersistentCollection as MongoDBPersistentCollection;
use Doctrine\ODM\PHPCR\PersistentCollection as PHPCRPersistentCollection;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Proxy\Proxy as ORMProxy;
use JMS\Serializer\EventDispatcher\EventDispatcherInterface;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
use ProxyManager\Proxy\LazyLoadingInterface;

final class DoctrineProxySubscriber implements EventSubscriberInterface
{
Expand Down Expand Up @@ -53,7 +53,7 @@ public function onPreSerialize(PreSerializeEvent $event): void
}

if (($this->skipVirtualTypeInit && $virtualType) ||
(!$object instanceof Proxy && !$object instanceof ORMProxy)
(!$object instanceof Proxy && !$object instanceof LazyLoadingInterface)
Copy link
Collaborator

@goetas goetas Nov 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is ORMProxy still considered? does this work also with ocramius/proxy-manager: 1.x ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ORMProxy I found that it implements Proxy interface and think that it could be dropped at all because if !$object instanceof Proxy then it is obviously !$object instanceof ORMProxy. So I replaced it with LazyLoadingInterface

For ocramius/proxy-manager you are right. It should work with 1.x and even with 0.1.0 version (very first). Should I lower version in composer.json?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ORMProxy I found that it implements Proxy interface and think that it could be dropped at all because if !$object instanceof Proxy then it is obviously !$object instanceof ORMProxy. So I replaced it with LazyLoadingInterface

I remember that, just if i'm not wrong wasn't so for all proxy-mamager versions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember that, just if i'm not wrong wasn't so for all proxy-mamager versions

Yes, proxy-manager has LaLoIn in all version, but I'm wondering how to reflect this in composer.json?
"ocramius/proxy-manager": "*" or "ocramius/proxy-manager": "^0.1|^1.0|^2.0"?

For ORMProxy, I have double checked with --prefer-lowest and Doctrine\ORM\Proxy\Proxy is instance of Doctrine\Common\Proxy\Proxy. Do you think it is still worth checking?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if the tests are green with #1139 (comment), then we are good to go

) {
return;
}
Expand All @@ -68,7 +68,11 @@ public function onPreSerialize(PreSerializeEvent $event): void
}
}

$object->__load();
if ($object instanceof LazyLoadingInterface) {
$object->initializeProxy();
} else {
$object->__load();
}

if (!$virtualType) {
$event->setType(get_parent_class($object), $type['params']);
Expand Down Expand Up @@ -111,6 +115,7 @@ public static function getSubscribedEvents()
['event' => 'serializer.pre_serialize', 'method' => 'onPreSerialize', 'interface' => MongoDBPersistentCollection::class],
['event' => 'serializer.pre_serialize', 'method' => 'onPreSerialize', 'interface' => PHPCRPersistentCollection::class],
['event' => 'serializer.pre_serialize', 'method' => 'onPreSerialize', 'interface' => Proxy::class],
['event' => 'serializer.pre_serialize', 'method' => 'onPreSerialize', 'interface' => LazyLoadingInterface::class],
];
}
}
69 changes: 69 additions & 0 deletions tests/Fixtures/SimpleObjectLazyLoading.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use Closure;
use ProxyManager\Proxy\LazyLoadingInterface;

class SimpleObjectLazyLoading extends SimpleObject implements LazyLoadingInterface
{
public $__isInitialized__ = false;

private $initializer;

private $baz = 'baz';

public function __load()
{
if (!$this->__isInitialized__) {
$this->camelCase = 'proxy-boo';
$this->__isInitialized__ = true;
}
}

public function __isInitialized()
{
return $this->__isInitialized__;
}

/**
* {@inheritDoc}
*/
public function setProxyInitializer(?Closure $initializer = null)
{
$this->initializer = $initializer;
}

/**
* {@inheritDoc}
*/
public function getProxyInitializer(): ?Closure
{
return $this->initializer;
}

/**
* {@inheritDoc}
*/
public function initializeProxy(): bool
{
if (!$this->__isInitialized__) {
$this->camelCase = 'proxy-boo';
$this->__isInitialized__ = true;

return !$this->initializer || call_user_func($this->initializer);
}

return true;
}

/**
* {@inheritDoc}
*/
public function isProxyInitialized(): bool
{
return $this->__isInitialized__;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Tests\Fixtures\ExclusionStrategy\AlwaysExcludeExclusionStrategy;
use JMS\Serializer\Tests\Fixtures\SimpleObject;
use JMS\Serializer\Tests\Fixtures\SimpleObjectLazyLoading;
use JMS\Serializer\Tests\Fixtures\SimpleObjectProxy;
use Metadata\MetadataFactoryInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -146,6 +147,15 @@ public function testOnPreSerializeMaintainsParams()
self::assertSame(['name' => SimpleObject::class, 'params' => ['baz']], $event->getType());
}

public function testRewritesLazyLoadingClassName()
{
$event = $this->createEvent($obj = new SimpleObjectLazyLoading('a', 'b'), ['name' => get_class($obj), 'params' => []]);
$this->subscriber->onPreSerialize($event);

self::assertEquals(['name' => get_parent_class($obj), 'params' => []], $event->getType());
self::assertTrue($obj->__isInitialized());
}

protected function setUp(): void
{
$this->subscriber = new DoctrineProxySubscriber();
Expand Down