Skip to content

Commit 58dfe52

Browse files
authored
Merge pull request #1779 from mkosiedowski/loggable-yaml-embedded
Loggable YAML support for embeddables
2 parents 88203f1 + ec9f522 commit 58dfe52

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

lib/Gedmo/Loggable/Mapping/Driver/Yaml.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ public function readExtendedMetadata($meta, array &$config)
100100
}
101101
}
102102

103+
if (isset($mapping['embedded'])) {
104+
foreach ($mapping['embedded'] as $field => $fieldMapping) {
105+
if (isset($fieldMapping['gedmo'])) {
106+
if (in_array('versioned', $fieldMapping['gedmo'])) {
107+
if ($meta->isCollectionValuedAssociation($field)) {
108+
throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
109+
}
110+
// fields cannot be overrided and throws mapping exception
111+
$mapping = $this->_getMapping($fieldMapping['class']);
112+
$this->inspectEmbeddedForVersioned($field, $mapping, $config);
113+
}
114+
}
115+
}
116+
}
117+
103118
if (!$meta->isMappedSuperclass && $config) {
104119
if (is_array($meta->identifier) && count($meta->identifier) > 1) {
105120
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
@@ -117,4 +132,18 @@ protected function _loadMappingFile($file)
117132
{
118133
return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
119134
}
135+
136+
/**
137+
* @param string $field
138+
* @param array $mapping
139+
* @param array $config
140+
*/
141+
private function inspectEmbeddedForVersioned($field, array $mapping, array &$config)
142+
{
143+
if (isset($mapping['fields'])) {
144+
foreach ($mapping['fields'] as $property => $fieldMapping) {
145+
$config['versioned'][] = $field . '.' . $property;
146+
}
147+
}
148+
}
120149
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
Mapping\Fixture\Yaml\Embedded:
3+
type: embeddable
4+
fields:
5+
subtitle:
6+
type: string
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
Mapping\Fixture\Yaml\LoggableWithEmbedded:
3+
type: entity
4+
table: loggable_with_embedded
5+
gedmo:
6+
loggable:
7+
logEntryClass: Gedmo\Loggable\Entity\LogEntry
8+
id:
9+
id:
10+
type: integer
11+
generator:
12+
strategy: AUTO
13+
fields:
14+
title:
15+
type: string
16+
gedmo:
17+
- versioned
18+
embedded:
19+
embedded:
20+
class: Mapping\Fixture\Yaml\Embedded
21+
gedmo:
22+
- versioned
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Mapping\Fixture\Yaml;
4+
5+
/**
6+
* Class Embedded
7+
* @package Mapping\Fixture\Yaml
8+
* @author Fabian Sabau <[email protected]>
9+
*/
10+
class Embedded
11+
{
12+
private $subtitle;
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Mapping\Fixture\Yaml;
4+
5+
class LoggableWithEmbedded
6+
{
7+
private $id;
8+
9+
private $title;
10+
11+
private $embedded;
12+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Gedmo\Mapping\Yaml;
4+
5+
use Doctrine\Common\EventManager;
6+
use Doctrine\Common\Annotations\AnnotationReader;
7+
use Doctrine\ORM\Mapping\Driver\DriverChain;
8+
use Doctrine\ORM\Mapping\Driver\YamlDriver;
9+
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
10+
use Gedmo\Loggable\LoggableListener;
11+
use Tool\BaseTestCaseOM;
12+
13+
/**
14+
* These are mapping extension tests
15+
*
16+
* @author Gediminas Morkevicius <[email protected]>
17+
* @link http://www.gediminasm.org
18+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19+
*/
20+
class LoggableMappingTest extends BaseTestCaseOM
21+
{
22+
/**
23+
* @var Doctrine\ORM\EntityManager
24+
*/
25+
private $em;
26+
27+
/**
28+
* @var Gedmo\Loggable\LoggableListener
29+
*/
30+
private $loggable;
31+
32+
public function setUp()
33+
{
34+
parent::setUp();
35+
36+
$reader = new AnnotationReader();
37+
$annotationDriver = new AnnotationDriver($reader);
38+
39+
$yamlDriver = new YamlDriver(__DIR__.'/../Driver/Yaml');
40+
41+
$chain = new DriverChain();
42+
$chain->addDriver($annotationDriver, 'Gedmo\Loggable');
43+
$chain->addDriver($yamlDriver, 'Mapping\Fixture\Yaml');
44+
45+
$this->loggable = new LoggableListener();
46+
$this->evm = new EventManager();
47+
$this->evm->addEventSubscriber($this->loggable);
48+
49+
$this->em = $this->getMockSqliteEntityManager(array(
50+
'Gedmo\Loggable\Entity\LogEntry',
51+
'Mapping\Fixture\Yaml\LoggableWithEmbedded',
52+
'Mapping\Fixture\Yaml\Embedded',
53+
), $chain);
54+
}
55+
56+
public function testLoggableMetadataWithEmbedded()
57+
{
58+
$meta = $this->em->getClassMetadata('Mapping\Fixture\Yaml\LoggableWithEmbedded');
59+
$config = $this->loggable->getConfiguration($this->em, $meta->name);
60+
61+
$this->assertArrayHasKey('logEntryClass', $config);
62+
$this->assertEquals('Gedmo\Loggable\Entity\LogEntry', $config['logEntryClass']);
63+
$this->assertArrayHasKey('loggable', $config);
64+
$this->assertTrue($config['loggable']);
65+
66+
$this->assertArrayHasKey('versioned', $config);
67+
$this->assertCount(2, $config['versioned']);
68+
$this->assertContains('title', $config['versioned']);
69+
$this->assertContains('embedded.subtitle', $config['versioned']);
70+
}
71+
}

0 commit comments

Comments
 (0)