Skip to content

Commit

Permalink
bug #91 Support for Gedmo/DoctrineExtensions (pamil)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.6-dev branch.

Discussion
----------



Commits
-------

af224ea Provide failing scenario for Gedmo/DoctrineExtensions
1851697 Add support for Gedmo DoctrineExtensions
  • Loading branch information
pamil authored Jun 7, 2019
2 parents e114acd + 1851697 commit 1a35a52
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 19 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"require-dev": {
"akeneo/phpspec-skip-example-extension": "^4.0",
"doctrine/orm": "^2.5",
"gedmo/doctrine-extensions": "^2.4",
"lakion/api-test-case": "^3.1.0",
"matthiasnoback/symfony-dependency-injection-test": "^3.0",
"phpspec/phpspec": "^5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,33 @@

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Sylius\Component\Resource\Metadata\RegistryInterface;

final class ResourceMappingDriver implements MappingDriver
/**
* It needs to extend MappingDriverChain in order to be compatible with Gedmo/DoctrineExtensions.
*
* @see \Gedmo\Mapping\ExtensionMetadataFactory::getDriver()
*/
final class ResourceMappingDriverChain extends MappingDriverChain
{
/** @var MappingDriver */
private $mappingDriver;

/** @var RegistryInterface */
private $resourceRegistry;

public function __construct(MappingDriver $mappingDriver, RegistryInterface $resourceRegistry)
{
$this->mappingDriver = $mappingDriver;
$this->resourceRegistry = $resourceRegistry;

$this->setDefaultDriver($mappingDriver);
}

public function loadMetadataForClass($className, ClassMetadata $metadata): void
{
$this->mappingDriver->loadMetadataForClass($className, $metadata);
parent::loadMetadataForClass($className, $metadata);

$this->convertResourceMappedSuperclass($metadata);
}

public function getAllClassNames(): iterable
{
return $this->mappingDriver->getAllClassNames();
}

public function isTransient($className): bool
{
return $this->mappingDriver->isTransient($className);
}

private function convertResourceMappedSuperclass(ClassMetadata $metadata): void
{
if (!isset($metadata->isMappedSuperclass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<services>
<defaults public="true" />

<service id="sylius_resource.doctrine.mapping_driver"
class="Sylius\Bundle\ResourceBundle\Doctrine\ResourceMappingDriver"
<service id="sylius_resource.doctrine.mapping_driver_chain"
class="Sylius\Bundle\ResourceBundle\Doctrine\ResourceMappingDriverChain"
decorates="doctrine.orm.default_metadata_driver">
<argument type="service" id="sylius_resource.doctrine.mapping_driver.inner" />
<argument type="service" id="sylius_resource.doctrine.mapping_driver_chain.inner" />
<argument type="service" id="sylius.resource_registry" />
</service>
</services>
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/test/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function registerBundles()
new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new \winzou\Bundle\StateMachineBundle\winzouStateMachineBundle(),
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle(),
new Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle(),
new AppBundle\AppBundle(),
Expand Down
6 changes: 6 additions & 0 deletions src/Bundle/test/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ fos_rest:
rules:
- { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: true }

stof_doctrine_extensions:
default_locale: "%locale%"
orm:
default:
sortable: true

services:
test.translation_locale_provider:
class: Sylius\Component\Resource\Translation\Provider\ImmutableTranslationLocaleProvider
Expand Down
4 changes: 4 additions & 0 deletions src/Bundle/test/app/config/resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ sylius_resource:
app.comic_book:
classes:
model: AppBundle\Entity\ComicBook

app.gedmo:
classes:
model: AppBundle\Entity\GedmoExtendedExample
5 changes: 5 additions & 0 deletions src/Bundle/test/app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ app_book:
alias: app.book
type: sylius.resource_api

app_gedmo:
resource: |
alias: app.gedmo
type: sylius.resource_api

app_book_sortable_index:
path: /sortable-books/
methods: [GET]
Expand Down
40 changes: 40 additions & 0 deletions src/Bundle/test/src/AppBundle/Entity/GedmoBaseExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace AppBundle\Entity;

use Sylius\Component\Resource\Model\ResourceInterface;

class GedmoBaseExample implements ResourceInterface
{
/** @var int */
private $id;

/** @var int|null */
private $position;

public function getId(): ?int
{
return $this->id;
}

public function getPosition(): ?int
{
return $this->position;
}

public function setPosition(?int $position): void
{
$this->position = $position;
}
}
30 changes: 30 additions & 0 deletions src/Bundle/test/src/AppBundle/Entity/GedmoExtendedExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace AppBundle\Entity;

class GedmoExtendedExample extends GedmoBaseExample
{
/** @var string|null */
private $extra;

public function getExtra(): ?string
{
return $this->extra;
}

public function setExtra(?string $extra): void
{
$this->extra = $extra;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
AppBundle\Entity\GedmoBaseExample:
type: mappedSuperclass
table: gedmo
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
position:
type: integer
gedmo:
- sortablePosition
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AppBundle\Entity\GedmoExtendedExample:
type: mappedSuperclass
table: gedmo
fields:
extra:
type: string
45 changes: 45 additions & 0 deletions src/Bundle/test/src/Tests/Controller/GedmoApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AppBundle\Tests\Controller;

use Lakion\ApiTestCase\JsonApiTestCase;
use Symfony\Component\HttpFoundation\Response;

final class GedmoApiTest extends JsonApiTestCase
{
/**
* @test
*/
public function it_allows_creating_a_comic_book()
{
$data =
<<<EOT
{
"extra": "Some info"
}
EOT;

$this->client->request('POST', '/gedmos/', [], [], ['CONTENT_TYPE' => 'application/json'], $data);
$response = $this->client->getResponse();
$this->assertResponse($response, 'gedmos/create_response', Response::HTTP_CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": @integer@,
"position": 0,
"extra": "Some info"
}

0 comments on commit 1a35a52

Please sign in to comment.