Skip to content

Add a data persistence layer #1464

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

Merged
merged 2 commits into from
Oct 30, 2017
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* Automatically enable FOSUser support if the bundle is installed
* Add an `AbstractCollectionNormalizer` to help supporting custom formats
* Deprecate NelmioApiDocBundle 2 support (upgrade to v3, it has native API Platform support)
* Deprecate the `ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener` class in favor of the new `ApiPlatform\Core\EventListener\WriteListener` class.
* Delete the `api_platform.doctrine.listener.view.write` event listener service.
* Add a data persistence layer with a new `ApiPlatform\Core\DataPersister\DataPersisterInterface` interface.

## 2.1.2

Expand Down
82 changes: 82 additions & 0 deletions src/Bridge/Doctrine/Common/DataPersister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Doctrine\Common;

use ApiPlatform\Core\DataPersister\DataPersisterInterface;
use ApiPlatform\Core\Util\ClassInfoTrait;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager as DoctrineObjectManager;

/**
* Data persister for Doctrine.
*
* @author Baptiste Meyer <[email protected]>
*/
final class DataPersister implements DataPersisterInterface
{
use ClassInfoTrait;

private $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
}

/**
* {@inheritdoc}
*/
public function supports($data): bool
{
return null !== $this->getManager($data);
}

/**
* {@inheritdoc}
*/
public function persist($data)
{
if (!$manager = $this->getManager($data)) {
return;
}

$manager->persist($data);
$manager->flush();
}

/**
* {@inheritdoc}
*/
public function remove($data)
{
if (!$manager = $this->getManager($data)) {
return;
}

$manager->remove($data);
$manager->flush();
}

/**
* Gets the Doctrine object manager associated with given data.
*
* @param mixed $data
*
* @return DoctrineObjectManager|null
*/
private function getManager($data)
{
return is_object($data) ? $this->managerRegistry->getManagerForClass($this->getObjectClass($data)) : null;
}
}
3 changes: 3 additions & 0 deletions src/Bridge/Doctrine/EventListener/WriteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Core\Bridge\Doctrine\EventListener;

use ApiPlatform\Core\EventListener\WriteListener as BaseWriteListener;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -29,6 +30,8 @@ final class WriteListener

public function __construct(ManagerRegistry $managerRegistry)
{
@trigger_error(sprintf('The %s class is deprecated since version 2.2 and will be removed in 3.0. Use the %s class instead.', __CLASS__, BaseWriteListener::class), E_USER_DEPRECATED);

$this->managerRegistry = $managerRegistry;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Bridge/Symfony/Bundle/ApiPlatformBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Bridge\Symfony\Bundle;

use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\AnnotationFilterPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\DataPersisterPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\DataProviderPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\DoctrineQueryExtensionPass;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\FilterPass;
Expand All @@ -34,6 +35,7 @@ public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new DataPersisterPass());
$container->addCompilerPass(new DataProviderPass());
$container->addCompilerPass(new AnnotationFilterPass());
$container->addCompilerPass(new FilterPass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\RuntimeException;
Expand Down Expand Up @@ -86,9 +87,12 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('api.xml');
$loader->load('data_persister.xml');
$loader->load('data_provider.xml');
$loader->load('filter.xml');

$container->registerForAutoconfiguration(DataPersisterInterface::class)
->addTag('api_platform.data_persister');
$container->registerForAutoconfiguration(ItemDataProviderInterface::class)
->addTag('api_platform.item_data_provider');
$container->registerForAutoconfiguration(CollectionDataProviderInterface::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler;

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

/**
* Registers data persisters.
*
* @internal
Copy link
Member

Choose a reason for hiding this comment

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

We'll be able to drop this class when we'll upgrade to DI 3.4: http://symfony.com/blog/new-in-symfony-3-4-simpler-injection-of-tagged-services

*
* @author Baptiste Meyer <[email protected]>
*/
final class DataPersisterPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$persisters = [];
$services = $container->findTaggedServiceIds('api_platform.data_persister', true);

foreach ($services as $serviceId => $tags) {
$persisters[] = new Reference($serviceId);
}

$container->getDefinition('api_platform.data_persister')->addArgument($persisters);
}
}
6 changes: 6 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="4" />
</service>

<service id="api_platform.listener.view.write" class="ApiPlatform\Core\EventListener\WriteListener">
<argument type="service" id="api_platform.data_persister" />

<tag name="kernel.event_listener" event="kernel.view" method="onKernelView" priority="32" />
</service>

<service id="api_platform.listener.request.deserialize" class="ApiPlatform\Core\EventListener\DeserializeListener">
<argument type="service" id="api_platform.serializer" />
<argument type="service" id="api_platform.serializer.context_builder" />
Expand Down
11 changes: 11 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/data_persister.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="api_platform.data_persister" class="ApiPlatform\Core\DataPersister\ChainDataPersister" public="false" />
</services>

</container>
14 changes: 6 additions & 8 deletions src/Bridge/Symfony/Bundle/Resources/config/doctrine_orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<factory service="doctrine.orm.default_entity_manager" method="getMetadataFactory" />
</service>

<service id="api_platform.doctrine.orm.data_persister" class="ApiPlatform\Core\Bridge\Doctrine\Common\DataPersister" public="false">
<argument type="service" id="doctrine" />

<tag name="api_platform.data_persister" />
</service>

<service id="api_platform.doctrine.orm.collection_data_provider" public="false" abstract="true">
<argument type="service" id="doctrine" />
<argument type="collection" /> <!-- extensions -->
Expand Down Expand Up @@ -95,14 +101,6 @@
<argument type="service" id="api_platform.doctrine.orm.metadata.property.metadata_factory.inner" />
</service>

<!-- Event listener -->

<service id="api_platform.doctrine.listener.view.write" class="ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener">
<argument type="service" id="doctrine" />

<tag name="kernel.event_listener" event="kernel.view" method="onKernelView" priority="32" />
</service>

<!-- Doctrine Query extensions -->

<service id="api_platform.doctrine.orm.query_extension.eager_loading" class="ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\EagerLoadingExtension" public="false">
Expand Down
70 changes: 70 additions & 0 deletions src/DataPersister/ChainDataPersister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\DataPersister;

/**
* Chained data persisters.
*
* @author Baptiste Meyer <[email protected]>
*/
final class ChainDataPersister implements DataPersisterInterface
{
private $persisters;

/**
* @param DataPersisterInterface[] $persisters
*/
public function __construct(array $persisters)
{
$this->persisters = $persisters;
}

/**
* {@inheritdoc}
*/
public function supports($data): bool
{
foreach ($this->persisters as $persister) {
if ($persister->supports($data)) {
return true;
}
}

return false;
}

/**
* {@inheritdoc}
*/
public function persist($data)
{
foreach ($this->persisters as $persister) {
if ($persister->supports($data)) {
$persister->persist($data);
}
}
}

/**
* {@inheritdoc}
*/
public function remove($data)
{
foreach ($this->persisters as $persister) {
if ($persister->supports($data)) {
$persister->remove($data);
}
}
}
}
45 changes: 45 additions & 0 deletions src/DataPersister/DataPersisterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\DataPersister;

/**
* Manages data persistence.
*
* @author Baptiste Meyer <[email protected]>
*/
interface DataPersisterInterface
{
/**
* Is the data supported by the persister?
*
* @param mixed $data
*
* @return bool
*/
public function supports($data): bool;

/**
* Persists the data.
*
* @param mixed $data
*/
public function persist($data);

/**
* Removes the data.
*
* @param mixed $data
*/
public function remove($data);
}
Loading