From 3972714fe833f34b5c4929d7fe0cb42301482fe3 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 12 Jun 2017 15:17:05 +0200 Subject: [PATCH 1/6] Translatable XML mapping --- doctrine-extensions-mapping.xsd | 15 ++++ .../Mapping/Driver/AbstractXmlDriver.php | 15 ++-- .../Mapping/Driver/AbstractYamlDriver.php | 4 +- .../Mapping/ExtendedMetadataFactory.php | 9 +-- .../ORM/Event/PostHydrateEventDispatcher.php | 14 ++-- .../Translatable/Mapping/Driver/Xml.php | 57 ++++++++++++++ .../Tests/Translatable/Fixture/XML/Page.php | 76 +++++++++++++++++++ .../Fixture/XML/PageTranslation.php | 68 +++++++++++++++++ ...ests.Translatable.Fixture.XML.Page.dcm.xml | 20 +++++ ...atable.Fixture.XML.PageTranslation.dcm.xml | 21 +++++ .../Tests/Translatable/XMLTest.php | 72 ++++++++++++++++++ 11 files changed, 348 insertions(+), 23 deletions(-) create mode 100644 lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Xml.php create mode 100644 tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/Page.php create mode 100644 tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/PageTranslation.php create mode 100644 tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.Page.dcm.xml create mode 100644 tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.PageTranslation.dcm.xml create mode 100644 tests/FSi/DoctrineExtensions/Tests/Translatable/XMLTest.php diff --git a/doctrine-extensions-mapping.xsd b/doctrine-extensions-mapping.xsd index 197ff81..fa2d1ff 100644 --- a/doctrine-extensions-mapping.xsd +++ b/doctrine-extensions-mapping.xsd @@ -13,4 +13,19 @@ + + + + + + + + + + + + + + + diff --git a/lib/FSi/DoctrineExtensions/Mapping/Driver/AbstractXmlDriver.php b/lib/FSi/DoctrineExtensions/Mapping/Driver/AbstractXmlDriver.php index 2f1c083..42e02c1 100644 --- a/lib/FSi/DoctrineExtensions/Mapping/Driver/AbstractXmlDriver.php +++ b/lib/FSi/DoctrineExtensions/Mapping/Driver/AbstractXmlDriver.php @@ -31,7 +31,11 @@ protected function getFileMapping(ClassMetadataInterface $extendedClassMetadata) $dom = new DOMDocument(); $dom->load($fileLocation); if (!$this->validateFile($dom)) { - throw new MappingException(sprintf('There are wrong mappings in xml mapping for class "%s" in file "%s"', $extendedClassMetadata->getClassName(), $fileLocation)); + throw new MappingException(sprintf( + 'There are wrong mappings in XML mapping for class "%s" in file "%s"', + $extendedClassMetadata->getClassName(), + $fileLocation + )); } $xmlElement = simplexml_load_file($fileLocation); @@ -40,13 +44,13 @@ protected function getFileMapping(ClassMetadataInterface $extendedClassMetadata) $className = $extendedClassMetadata->getClassName(); if (isset($xmlElement->entity)) { foreach ($xmlElement->entity as $entityElement) { - if ($this->getAttribute($entityElement, 'name') == $className) { + if ($this->getAttribute($entityElement, 'name') === $className) { return $entityElement; } } } elseif (isset($xmlElement->{'mapped-superclass'})) { foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) { - if ($this->getAttribute($mappedSuperClass, 'name') == $className) { + if ($this->getAttribute($mappedSuperClass, 'name') === $className) { return $mappedSuperClass; } } @@ -68,7 +72,7 @@ protected function getAttribute(SimpleXmlElement $node, $name) } /** - * Validatin xml file. + * Validating xml file. * * @param \DOMDocument $dom * @return bool @@ -118,8 +122,7 @@ private function validateFile(DOMDocument $dom) EOF ; - $valid = @$dom->schemaValidateSource($source); - return $valid; + return @$dom->schemaValidateSource($source); } /** diff --git a/lib/FSi/DoctrineExtensions/Mapping/Driver/AbstractYamlDriver.php b/lib/FSi/DoctrineExtensions/Mapping/Driver/AbstractYamlDriver.php index 34a3a84..e840d87 100644 --- a/lib/FSi/DoctrineExtensions/Mapping/Driver/AbstractYamlDriver.php +++ b/lib/FSi/DoctrineExtensions/Mapping/Driver/AbstractYamlDriver.php @@ -23,8 +23,8 @@ protected function getFileMapping(ClassMetadataInterface $extendedClassMetadata) $element = Yaml::parse(file_get_contents($this->findMappingFile($extendedClassMetadata))); if (isset($element[$extendedClassMetadata->getClassName()])) { return $element[$extendedClassMetadata->getClassName()]; - } else { - return []; } + + return []; } } diff --git a/lib/FSi/DoctrineExtensions/Mapping/ExtendedMetadataFactory.php b/lib/FSi/DoctrineExtensions/Mapping/ExtendedMetadataFactory.php index fd0863c..c9eef84 100644 --- a/lib/FSi/DoctrineExtensions/Mapping/ExtendedMetadataFactory.php +++ b/lib/FSi/DoctrineExtensions/Mapping/ExtendedMetadataFactory.php @@ -20,29 +20,21 @@ final class ExtendedMetadataFactory extends MetadataFactory { /** - * Object manager, entity or document. - * * @var ObjectManager */ private $objectManager; /** - * Extension namespace. - * * @var string */ private $extensionNamespace; /** - * Annotation reader. - * * @var object */ private $annotationReader; /** - * Initializes extension driver. - * * @param \Doctrine\Common\Persistence\ObjectManager $objectManager * @param string $extensionNamespace * @param object $annotationReader @@ -111,6 +103,7 @@ private function getDriver($omDriver) $driver->setAnnotationReader($this->annotationReader); } } + return $driver; } } diff --git a/lib/FSi/DoctrineExtensions/ORM/Event/PostHydrateEventDispatcher.php b/lib/FSi/DoctrineExtensions/ORM/Event/PostHydrateEventDispatcher.php index e0eded6..47d2ace 100644 --- a/lib/FSi/DoctrineExtensions/ORM/Event/PostHydrateEventDispatcher.php +++ b/lib/FSi/DoctrineExtensions/ORM/Event/PostHydrateEventDispatcher.php @@ -9,15 +9,17 @@ namespace FSi\DoctrineExtensions\ORM\Event; -use Doctrine\ORM\EntityManager; -use Doctrine\ORM\Query; +use Doctrine\Common\EventManager; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\ORM\Mapping\ClassMetadataFactory; +use Doctrine\ORM\Query; use FSi\DoctrineExtensions\ORM\Events; class PostHydrateEventDispatcher { /** - * @var EntityManager + * @var EntityManagerInterface */ private $entityManager; @@ -46,12 +48,10 @@ class PostHydrateEventDispatcher private $entities = []; /** - * Constructs a new dispatcher instance - * - * @param EntityManager $em + * @param EntityManagerInterface $em * @param array $hints */ - public function __construct(EntityManager $em, array $hints = []) + public function __construct(EntityManagerInterface $em, array $hints = []) { $this->entityManager = $em; $this->metadataFactory = $em->getMetadataFactory(); diff --git a/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Xml.php b/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Xml.php new file mode 100644 index 0000000..cac71cd --- /dev/null +++ b/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Xml.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FSi\DoctrineExtensions\Translatable\Mapping\Driver; + +use Doctrine\Common\Persistence\Mapping\ClassMetadata; +use FSi\Component\Metadata\ClassMetadataInterface; +use FSi\DoctrineExtensions\Mapping\Driver\AbstractXmlDriver; + +class Xml extends AbstractXmlDriver +{ + const LOCALE = 'locale'; + const FIELD_TRANSLATION = 'translatable'; + + /** + * {@inheritdoc} + */ + protected function loadExtendedClassMetadata(ClassMetadata $baseClassMetadata, ClassMetadataInterface $extendedClassMetadata) + { + $mapping = $this->getFileMapping($extendedClassMetadata); + // First iterate over nodes of translated entities, which are not part + // of the Doctrine fields group + $fsiMapping = $mapping->children(self::FSI_NAMESPACE_URI); + foreach ($fsiMapping as $translatableMapping) { + if ($translatableMapping->getName() === self::LOCALE) { + $extendedClassMetadata->localeProperty = $this->getAttribute($translatableMapping, 'field'); + } + + if ($translatableMapping->getName() === self::FIELD_TRANSLATION) { + $extendedClassMetadata->addTranslatableProperty( + $this->getAttribute($translatableMapping, 'mappedBy'), + $this->getAttribute($translatableMapping, 'field'), + $this->getAttribute($translatableMapping, 'targetField') + ); + } + } + + // Then iterate over fields - this is only to set the locale of the + // translation entity + if (count($fsiMapping) || !isset($mapping->field)) { + return; + } + + foreach ($mapping->field as $fieldMapping) { + $translationMapping = $fieldMapping->children(self::FSI_NAMESPACE_URI); + if (isset($translationMapping->{self::LOCALE})) { + $extendedClassMetadata->localeProperty = $this->getAttribute($translationMapping->{self::LOCALE}, 'field'); + } + } + } +} diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/Page.php b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/Page.php new file mode 100644 index 0000000..8a9d079 --- /dev/null +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/Page.php @@ -0,0 +1,76 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FSi\DoctrineExtensions\Tests\Translatable\Fixture\XML; + +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; + +class Page +{ + /** + * @var integer + */ + private $id; + + /** + * @var string + */ + private $locale; + + /** + * @var string + */ + private $content; + + /** + * @var PageTranslation[]|Collection + */ + private $translations; + + public function __construct() + { + $this->translations = new ArrayCollection(); + } + + public function getId() + { + return $this->id; + } + + public function getLocale() + { + return $this->locale; + } + + public function setLocale($locale) + { + $this->locale = $locale; + } + + public function getContent() + { + return $this->content; + } + + public function setContent($content) + { + $this->content = $content; + } + + public function getTranslation($locale) + { + return $this->translations[$locale]; + } + + public function getTranslations() + { + return $this->translations; + } +} diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/PageTranslation.php b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/PageTranslation.php new file mode 100644 index 0000000..e140a1e --- /dev/null +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/PageTranslation.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FSi\DoctrineExtensions\Tests\Translatable\Fixture\XML; + +class PageTranslation +{ + /** + * @var integer + */ + private $id; + + /** + * @var string + */ + private $locale; + + /** + * @var string + */ + private $content; + + /** + * @var Page + */ + private $page; + + public function getId() + { + return $this->id; + } + + public function getLocale() + { + return $this->locale; + } + + public function setLocale($locale) + { + $this->locale = $locale; + } + + public function getContent() + { + return $this->content; + } + + public function setContent($content) + { + $this->content = $content; + } + + public function getPage() + { + return $this->page; + } + + public function setPage(Page $page = null) + { + $this->page = $page; + } +} diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.Page.dcm.xml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.Page.dcm.xml new file mode 100644 index 0000000..c8787b6 --- /dev/null +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.Page.dcm.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.PageTranslation.dcm.xml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.PageTranslation.dcm.xml new file mode 100644 index 0000000..8f3f578 --- /dev/null +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.PageTranslation.dcm.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/XMLTest.php b/tests/FSi/DoctrineExtensions/Tests/Translatable/XMLTest.php new file mode 100644 index 0000000..d1fa426 --- /dev/null +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/XMLTest.php @@ -0,0 +1,72 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FSi\DoctrineExtensions\Tests\Translatable; + +use Doctrine\ORM\Mapping\Driver\XmlDriver; +use FSi\DoctrineExtensions\Tests\Translatable\Fixture\XML\Page; + +class XMLTest extends BaseTranslatableTest +{ + const PAGE = 'FSi\\DoctrineExtensions\\Tests\\Translatable\\Fixture\\XML\\Page'; + const PAGE_TRANSLATION = 'FSi\\DoctrineExtensions\\Tests\\Translatable\\Fixture\\XML\\PageTranslation'; + + public function testXMLMapping() + { + $this->_logger->enabled = true; + + $page = new Page(); + $page->setLocale($this->_languagePl); + $page->setContent(self::POLISH_CONTENTS_1); + $this->_translatableListener->setLocale($this->_languagePl); + $this->_em->persist($page); + $this->_em->flush(); + + $page->setLocale($this->_languageEn); + $page->setContent(self::ENGLISH_CONTENTS_1); + $this->_translatableListener->setLocale($this->_languageEn); + $this->_em->flush(); + $this->_em->refresh($page); + + $this->assertEquals( + 9, + count($this->_logger->queries), + 'Flushing executed wrong number of queries' + ); + + $translationCount = count($page->getTranslations()); + $this->assertEquals( + 2, + $translationCount, + 'Number of translations is not valid' + ); + + $this->assertAttributeEquals( + self::POLISH_CONTENTS_1, + 'content', + $page->getTranslation($this->_languagePl) + ); + + $this->assertAttributeEquals( + self::ENGLISH_CONTENTS_1, + 'content', + $page->getTranslation($this->_languageEn) + ); + } + + protected function getMetadataDriverImplementation() + { + return new XmlDriver(sprintf('%s/Fixture/XML/config', __DIR__)); + } + + protected function getUsedEntityFixtures() + { + return [self::PAGE, self::PAGE_TRANSLATION]; + } +} From f2a13ad6394dbc7fe6a464d40ff6374d5af764c2 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 12 Jun 2017 15:36:50 +0200 Subject: [PATCH 2/6] Scrutinizer config --- .scrutinizer.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .scrutinizer.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..6c325fd --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,13 @@ +--- +filter: + excluded_paths: [vendor/*, tests/*, bin/*] + +checks: + php: + code_rating: true + duplication: true + +tools: + php_cpd: true + php_pdepend: + excluded_dirs: [vendor] From b29b930d79359e28f73082d0c9f1603da3a89cc1 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 12 Jun 2017 16:51:20 +0200 Subject: [PATCH 3/6] Translatable YAML driver --- .../Translatable/Mapping/Driver/Yaml.php | 116 ++++++++++++++++++ .../Fixture/{XML => Common}/Page.php | 2 +- .../{XML => Common}/PageTranslation.php | 2 +- ....Translatable.Fixture.Common.Page.dcm.xml} | 2 +- ...le.Fixture.Common.PageTranslation.dcm.xml} | 2 +- ...s.Translatable.Fixture.Common.Page.dcm.yml | 21 ++++ ...ble.Fixture.Common.PageTranslation.dcm.yml | 18 +++ .../Tests/Translatable/XMLTest.php | 6 +- .../Tests/Translatable/YAMLTest.php | 72 +++++++++++ 9 files changed, 234 insertions(+), 7 deletions(-) create mode 100644 lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Yaml.php rename tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/{XML => Common}/Page.php (94%) rename tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/{XML => Common}/PageTranslation.php (93%) rename tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/{FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.Page.dcm.xml => FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.xml} (98%) rename tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/{FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.PageTranslation.dcm.xml => FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml} (96%) create mode 100644 tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/YAML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.yml create mode 100644 tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/YAML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.yml create mode 100644 tests/FSi/DoctrineExtensions/Tests/Translatable/YAMLTest.php diff --git a/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Yaml.php b/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Yaml.php new file mode 100644 index 0000000..b40b442 --- /dev/null +++ b/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Yaml.php @@ -0,0 +1,116 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FSi\DoctrineExtensions\Translatable\Mapping\Driver; + +use FSi\DoctrineExtensions\Mapping\Driver\AbstractYamlDriver; +use Doctrine\Common\Persistence\Mapping\ClassMetadata; +use FSi\Component\Metadata\ClassMetadataInterface; +use FSi\DoctrineExtensions\Uploadable\Exception\MappingException; + +class Yaml extends AbstractYamlDriver +{ + const LOCALE = 'locale'; + + /** + * {@inheritdoc} + */ + protected function loadExtendedClassMetadata(ClassMetadata $baseClassMetadata, ClassMetadataInterface $extendedClassMetadata) + { + $mapping = $this->getFileMapping($extendedClassMetadata); + if (!isset($mapping['type'])) { + return; + } + + // First iterate over nodes of translated entities, which are not part + // of the Doctrine fields group + if (isset($mapping['fsi']) && isset($mapping['fsi']['translatable'])) { + if (!is_array($mapping['fsi']['translatable'])) { + throw new MappingException(sprintf( + 'Key "translatable" should be an array, is "%s" for "%s" entity.', + gettype($mapping['fsi']['translatable']), + $extendedClassMetadata->getClassName() + )); + } + + $locale = isset($mapping['fsi']['translatable'][self::LOCALE]) + ? $mapping['fsi']['translatable'][self::LOCALE] + : self::LOCALE + ; + $extendedClassMetadata->localeProperty = $locale; + + if (!isset($mapping['fsi']['translatable']['fields'])) { + return; + } + + if (!is_array($mapping['fsi']['translatable']['fields'])) { + throw new MappingException(sprintf( + 'Key "fields" in group "translatable" should be an array, is "%s" for "%s" entity.', + gettype($mapping['fsi']['translatable']), + $extendedClassMetadata->getClassName() + )); + } + + foreach ($mapping['fsi']['translatable']['fields'] as $field => $options) { + $extendedClassMetadata->addTranslatableProperty( + $this->getValue($options, 'mappedBy'), + $field, + $this->getValue($options, 'targetField') + ); + } + } + + // Then iterate over fields - this is only to set the locale of the + // translation entity + if (!isset($mapping['fields']) || !is_array($mapping['fields'])) { + return; + } + + foreach ($mapping['fields'] as $field => $config) { + if (!isset($config['fsi'])) { + continue; + } + + if (!is_array($config['fsi'])) { + throw new MappingException(sprintf( + 'Key "fsi" should be an array, is "%s" for field "%s" of "%s" entity.', + gettype($mapping['fsi']['translatable']), + $field, + $extendedClassMetadata->getClassName() + )); + } + if (!isset($config['fsi']['translatable'])) { + continue; + } + if (!is_array($config['fsi']['translatable'])) { + throw new MappingException(sprintf( + 'Key "translatable" should be an array, is "%s" for "%s" entity.', + gettype($mapping['fsi']['translatable']), + $extendedClassMetadata->getClassName() + )); + } + + $translatable = $config['fsi']['translatable']; + $locale = isset($translatable[self::LOCALE]) + ? $translatable[self::LOCALE] + : self::LOCALE + ; + $extendedClassMetadata->localeProperty = $locale; + } + } + + /** + * @param array $array + * @return mixed + */ + private function getValue(array $array, $key) + { + return isset($array[$key]) ? $array[$key] : null; + } +} diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/Page.php b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/Common/Page.php similarity index 94% rename from tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/Page.php rename to tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/Common/Page.php index 8a9d079..cba5c7f 100644 --- a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/Page.php +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/Common/Page.php @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -namespace FSi\DoctrineExtensions\Tests\Translatable\Fixture\XML; +namespace FSi\DoctrineExtensions\Tests\Translatable\Fixture\Common; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/PageTranslation.php b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/Common/PageTranslation.php similarity index 93% rename from tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/PageTranslation.php rename to tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/Common/PageTranslation.php index e140a1e..1647691 100644 --- a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/PageTranslation.php +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/Common/PageTranslation.php @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -namespace FSi\DoctrineExtensions\Tests\Translatable\Fixture\XML; +namespace FSi\DoctrineExtensions\Tests\Translatable\Fixture\Common; class PageTranslation { diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.Page.dcm.xml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.xml similarity index 98% rename from tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.Page.dcm.xml rename to tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.xml index c8787b6..5374cb6 100644 --- a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.Page.dcm.xml +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.xml @@ -5,7 +5,7 @@ http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd" xmlns:fsi="http://fsi.pl/schemas/orm/doctrine-extensions-mapping"> - diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.PageTranslation.dcm.xml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml similarity index 96% rename from tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.PageTranslation.dcm.xml rename to tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml index 8f3f578..be57116 100644 --- a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.XML.PageTranslation.dcm.xml +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml @@ -5,7 +5,7 @@ http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd" xmlns:fsi="http://fsi.pl/schemas/orm/doctrine-extensions-mapping"> - + diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/YAML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.yml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/YAML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.yml new file mode 100644 index 0000000..d8539f6 --- /dev/null +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/YAML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.yml @@ -0,0 +1,21 @@ +FSi\DoctrineExtensions\Tests\Translatable\Fixture\Common\Page: + type: entity + repositoryClass: FSi\DoctrineExtensions\Translatable\Entity\Repository\TranslatableRepository + id: + id: + type: integer + generator: + strategy: AUTO + fsi: + translatable: + locale: locale + fields: + content: + mappedBy: translations + targetField: content + oneToMany: + translations: + targetEntity: FSi\DoctrineExtensions\Tests\Translatable\Fixture\Common\PageTranslation + mappedBy: page + cascade: ["persist", "remove"] + indexBy: locale diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/YAML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.yml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/YAML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.yml new file mode 100644 index 0000000..91e7403 --- /dev/null +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/YAML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.yml @@ -0,0 +1,18 @@ +FSi\DoctrineExtensions\Tests\Translatable\Fixture\Common\PageTranslation: + type: entity + id: + id: + type: integer + generator: + strategy: AUTO + fields: + locale: + type: string + length: 2 + fsi: + translatable: + locale: locale + manyToOne: + page: + targetEntity: FSi\DoctrineExtensions\Tests\Translatable\Fixture\Common\Page + inversedBy: translations diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/XMLTest.php b/tests/FSi/DoctrineExtensions/Tests/Translatable/XMLTest.php index d1fa426..212d422 100644 --- a/tests/FSi/DoctrineExtensions/Tests/Translatable/XMLTest.php +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/XMLTest.php @@ -10,12 +10,12 @@ namespace FSi\DoctrineExtensions\Tests\Translatable; use Doctrine\ORM\Mapping\Driver\XmlDriver; -use FSi\DoctrineExtensions\Tests\Translatable\Fixture\XML\Page; +use FSi\DoctrineExtensions\Tests\Translatable\Fixture\Common\Page; class XMLTest extends BaseTranslatableTest { - const PAGE = 'FSi\\DoctrineExtensions\\Tests\\Translatable\\Fixture\\XML\\Page'; - const PAGE_TRANSLATION = 'FSi\\DoctrineExtensions\\Tests\\Translatable\\Fixture\\XML\\PageTranslation'; + const PAGE = 'FSi\\DoctrineExtensions\\Tests\\Translatable\\Fixture\\Common\\Page'; + const PAGE_TRANSLATION = 'FSi\\DoctrineExtensions\\Tests\\Translatable\\Fixture\\Common\\PageTranslation'; public function testXMLMapping() { diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/YAMLTest.php b/tests/FSi/DoctrineExtensions/Tests/Translatable/YAMLTest.php new file mode 100644 index 0000000..e60461f --- /dev/null +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/YAMLTest.php @@ -0,0 +1,72 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FSi\DoctrineExtensions\Tests\Translatable; + +use Doctrine\ORM\Mapping\Driver\YamlDriver; +use FSi\DoctrineExtensions\Tests\Translatable\Fixture\Common\Page; + +class YAMLTest extends BaseTranslatableTest +{ + const PAGE = 'FSi\\DoctrineExtensions\\Tests\\Translatable\\Fixture\\Common\\Page'; + const PAGE_TRANSLATION = 'FSi\\DoctrineExtensions\\Tests\\Translatable\\Fixture\\Common\\PageTranslation'; + + public function testYAMLMapping() + { + $this->_logger->enabled = true; + + $page = new Page(); + $page->setLocale($this->_languagePl); + $page->setContent(self::POLISH_CONTENTS_1); + $this->_translatableListener->setLocale($this->_languagePl); + $this->_em->persist($page); + $this->_em->flush(); + + $page->setLocale($this->_languageEn); + $page->setContent(self::ENGLISH_CONTENTS_1); + $this->_translatableListener->setLocale($this->_languageEn); + $this->_em->flush(); + $this->_em->refresh($page); + + $this->assertEquals( + 9, + count($this->_logger->queries), + 'Flushing executed wrong number of queries' + ); + + $translationCount = count($page->getTranslations()); + $this->assertEquals( + 2, + $translationCount, + 'Number of translations is not valid' + ); + + $this->assertAttributeEquals( + self::POLISH_CONTENTS_1, + 'content', + $page->getTranslation($this->_languagePl) + ); + + $this->assertAttributeEquals( + self::ENGLISH_CONTENTS_1, + 'content', + $page->getTranslation($this->_languageEn) + ); + } + + protected function getMetadataDriverImplementation() + { + return new YamlDriver(sprintf('%s/Fixture/YAML/config', __DIR__)); + } + + protected function getUsedEntityFixtures() + { + return [self::PAGE, self::PAGE_TRANSLATION]; + } +} From 29d1e16e1b902da2e96039fc441b4e07971b2e48 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 12 Jun 2017 17:28:52 +0200 Subject: [PATCH 4/6] XML / YAML docs for translatable --- doc/translatable.md | 4 + doc/translatable/xml.md | 104 +++++++++++++++++ doc/translatable/yaml.md | 107 ++++++++++++++++++ .../Translatable/Mapping/Driver/Xml.php | 2 +- ...ble.Fixture.Common.PageTranslation.dcm.xml | 2 +- 5 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 doc/translatable/xml.md create mode 100644 doc/translatable/yaml.md diff --git a/doc/translatable.md b/doc/translatable.md index d21d145..f7a67ed 100644 --- a/doc/translatable.md +++ b/doc/translatable.md @@ -608,3 +608,7 @@ example in translation entity: */ private $locale; ``` + +## XML and YAML mapping reference ## + +See [here](translatable/xml.md) and [here](translatable/yaml.md), respectively. diff --git a/doc/translatable/xml.md b/doc/translatable/xml.md new file mode 100644 index 0000000..e76e409 --- /dev/null +++ b/doc/translatable/xml.md @@ -0,0 +1,104 @@ +# Translatable - Translatable behavioral extension for Doctrine 2 + +## XML config example + +Entity: + +```php + + + + + + + + + + + + + + +``` + +Translation: + +```php + + + + + + + + + + + + + + + + + +``` diff --git a/doc/translatable/yaml.md b/doc/translatable/yaml.md new file mode 100644 index 0000000..1ee8199 --- /dev/null +++ b/doc/translatable/yaml.md @@ -0,0 +1,107 @@ +# Translatable - Translatable behavioral extension for Doctrine 2 + +## YAML config example + +Translated entity: + +```php +field as $fieldMapping) { $translationMapping = $fieldMapping->children(self::FSI_NAMESPACE_URI); if (isset($translationMapping->{self::LOCALE})) { - $extendedClassMetadata->localeProperty = $this->getAttribute($translationMapping->{self::LOCALE}, 'field'); + $extendedClassMetadata->localeProperty = $this->getAttribute($fieldMapping, 'name'); } } } diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml index be57116..3d5a204 100644 --- a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml @@ -12,7 +12,7 @@ - + From 454f0e0d07eae6e4a7d8060292a5546ef7fc4cf8 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Wed, 14 Jun 2017 12:10:05 +0200 Subject: [PATCH 5/6] Fix Scrutinizer indendation --- .scrutinizer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 6c325fd..b7e5595 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -10,4 +10,4 @@ checks: tools: php_cpd: true php_pdepend: - excluded_dirs: [vendor] + excluded_dirs: [vendor] From 92ed8856df31d7f96d9315a885e7c6d6668d5b67 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Wed, 14 Jun 2017 14:58:34 +0200 Subject: [PATCH 6/6] Changed XML translatable field names --- doc/translatable/xml.md | 6 +++--- doctrine-extensions-mapping.xsd | 8 ++++---- .../Translatable/Mapping/Driver/Xml.php | 4 ++-- ...ensions.Tests.Translatable.Fixture.Common.Page.dcm.xml | 4 ++-- ...ts.Translatable.Fixture.Common.PageTranslation.dcm.xml | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/doc/translatable/xml.md b/doc/translatable/xml.md index e76e409..7ba39b2 100644 --- a/doc/translatable/xml.md +++ b/doc/translatable/xml.md @@ -45,8 +45,8 @@ XML for the entity: - - + + @@ -94,7 +94,7 @@ XML for the translation: - + diff --git a/doctrine-extensions-mapping.xsd b/doctrine-extensions-mapping.xsd index fa2d1ff..71a3f23 100644 --- a/doctrine-extensions-mapping.xsd +++ b/doctrine-extensions-mapping.xsd @@ -14,17 +14,17 @@ - + - + - + - + diff --git a/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Xml.php b/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Xml.php index 0c8f2ab..4228b8e 100644 --- a/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Xml.php +++ b/lib/FSi/DoctrineExtensions/Translatable/Mapping/Driver/Xml.php @@ -15,8 +15,8 @@ class Xml extends AbstractXmlDriver { - const LOCALE = 'locale'; - const FIELD_TRANSLATION = 'translatable'; + const LOCALE = 'translatable-locale'; + const FIELD_TRANSLATION = 'translatable-field'; /** * {@inheritdoc} diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.xml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.xml index 5374cb6..d9b6789 100644 --- a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.xml +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.Page.dcm.xml @@ -13,8 +13,8 @@ - - + + diff --git a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml index 3d5a204..339fb8a 100644 --- a/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml +++ b/tests/FSi/DoctrineExtensions/Tests/Translatable/Fixture/XML/config/FSi.DoctrineExtensions.Tests.Translatable.Fixture.Common.PageTranslation.dcm.xml @@ -12,7 +12,7 @@ - +