This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
<?php
namespace Gedmo\Translatable\Mapping\Driver;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
use Gedmo\Exception\InvalidMappingException;
/**
* This is an annotation mapping driver for Translatable
* behavioral extension. Used for extraction of extended
* metadata from Annotations specifically for Translatable
* extension.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Annotation extends AbstractAnnotationDriver
{
/**
* Annotation to identity translation entity to be used for translation storage
*/
const ENTITY_CLASS = 'Gedmo\\Mapping\\Annotation\\TranslationEntity';
/**
* Annotation to identify field as translatable
*/
const TRANSLATABLE = 'Gedmo\\Mapping\\Annotation\\Translatable';
/**
* Annotation to identify field which can store used locale or language
* alias is LANGUAGE
*/
const LOCALE = 'Gedmo\\Mapping\\Annotation\\Locale';
/**
* Annotation to identify field which can store used locale or language
* alias is LOCALE
*/
const LANGUAGE = 'Gedmo\\Mapping\\Annotation\\Language';
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$class = $this->getMetaReflectionClass($meta);
// class annotations
if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) {
if (!$cl = $this->getRelatedClassName($meta, $annot->class)) {
throw new InvalidMappingException("Translation class: {$annot->class} does not exist.");
}
$config['translationClass'] = $cl;
}
// property annotations
foreach ($class->getProperties() as $property) {
if ($meta->isMappedSuperclass && !$property->isPrivate() ||
$meta->isInheritedField($property->name) ||
isset($meta->associationMappings[$property->name]['inherited'])
) {
continue;
}
// translatable property
if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) {
$field = $property->getName();
if (!$meta->hasField($field)) {
throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}");
}
// fields cannot be overrided and throws mapping exception
$config['fields'][] = $field;
if (isset($translatable->fallback)) {
$config['fallback'][$field] = $translatable->fallback;
}
}
// locale property
if ($this->reader->getPropertyAnnotation($property, self::LOCALE)) {
$field = $property->getName();
if ($meta->hasField($field)) {
throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sense");
}
$config['locale'] = $field;
} elseif ($this->reader->getPropertyAnnotation($property, self::LANGUAGE)) {
$field = $property->getName();
if ($meta->hasField($field)) {
throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sense");
}
$config['locale'] = $field;
}
}
// Embedded entity
if (property_exists($meta, 'embeddedClasses') && $meta->embeddedClasses) {
foreach ($meta->embeddedClasses as $propertyName => $embeddedClassInfo) {
if ($meta->isInheritedEmbeddedClass($propertyName)) {
continue;
}
$embeddedClass = new \ReflectionClass($embeddedClassInfo['class']);
foreach ($embeddedClass->getProperties() as $embeddedProperty) {
if ($translatable = $this->reader->getPropertyAnnotation($embeddedProperty, self::TRANSLATABLE)) {
$field = $propertyName . '.' . $embeddedProperty->getName();
$config['fields'][] = $field;
if (isset($translatable->fallback)) {
$config['fallback'][$field] = $translatable->fallback;
}
}
}
}
}
if (!$meta->isMappedSuperclass && $config) {
if (is_array($meta->identifier) && count($meta->identifier) > 1) {
throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
}
}
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace Gedmo\Translatable\Mapping\Driver;
use Gedmo\Mapping\Driver\Xml as BaseXml;
use Gedmo\Exception\InvalidMappingException;
/**
* This is a xml mapping driver for Translatable
* behavioral extension. Used for extraction of extended
* metadata from xml specifically for Translatable
* extension.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Xml extends BaseXml
{
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
/**
* @var \SimpleXmlElement $xml
*/
$xml = $this->_getMapping($meta->name);
$xmlDoctrine = $xml;
$xml = $xml->children(self::GEDMO_NAMESPACE_URI);
if (($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass')) {
if ($xml->count() && isset($xml->translation)) {
/**
* @var \SimpleXmlElement $data
*/
$data = $xml->translation;
if ($this->_isAttributeSet($data, 'locale')) {
$config['locale'] = $this->_getAttribute($data, 'locale');
} elseif ($this->_isAttributeSet($data, 'language')) {
$config['locale'] = $this->_getAttribute($data, 'language');
}
if ($this->_isAttributeSet($data, 'entity')) {
$entity = $this->_getAttribute($data, 'entity');
if (!$cl = $this->getRelatedClassName($meta, $entity)) {
throw new InvalidMappingException("Translation entity class: {$entity} does not exist.");
}
$config['translationClass'] = $cl;
}
}
}
if (property_exists($meta, 'embeddedClasses') && $meta->embeddedClasses) {
foreach ($meta->embeddedClasses as $propertyName => $embeddedClassInfo) {
if ($meta->isInheritedEmbeddedClass($propertyName)) {
continue;
}
$xmlEmbeddedClass = $this->_getMapping($embeddedClassInfo['class']);
$this->inspectElementsForTranslatableFields($xmlEmbeddedClass, $config, $propertyName);
}
}
if ($xmlDoctrine->{'attribute-overrides'}->count() > 0) {
foreach ($xmlDoctrine->{'attribute-overrides'}->{'attribute-override'} as $overrideMapping) {
$this->buildFieldConfiguration($this->_getAttribute($overrideMapping, 'name'), $overrideMapping->field, $config);
}
}
$this->inspectElementsForTranslatableFields($xmlDoctrine, $config);
if (!$meta->isMappedSuperclass && $config) {
if (is_array($meta->identifier) && count($meta->identifier) > 1) {
throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
}
}
}
private function inspectElementsForTranslatableFields(\SimpleXMLElement $xml, array &$config, $prefix = null)
{
if (!isset($xml->field)) {
return;
}
foreach ($xml->field as $mapping) {
$mappingDoctrine = $mapping;
$fieldName = $this->_getAttribute($mappingDoctrine, 'name');
if ($prefix !== null) {
$fieldName = $prefix . '.' . $fieldName;
}
$this->buildFieldConfiguration($fieldName, $mapping, $config);
}
}
private function buildFieldConfiguration($fieldName, \SimpleXMLElement $mapping, array &$config)
{
$mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
if ($mapping->count() > 0 && isset($mapping->translatable)) {
$config['fields'][] = $fieldName;
/** @var \SimpleXmlElement $data */
$data = $mapping->translatable;
if ($this->_isAttributeSet($data, 'fallback')) {
$config['fallback'][$fieldName] = 'true' == $this->_getAttribute($data, 'fallback') ? true : false;
}
}
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace Gedmo\Translatable\Mapping\Driver;
use Gedmo\Mapping\Driver\File;
use Gedmo\Mapping\Driver;
use Gedmo\Exception\InvalidMappingException;
/**
* This is a yaml mapping driver for Translatable
* behavioral extension. Used for extraction of extended
* metadata from yaml specifically for Translatable
* extension.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Yaml extends File implements Driver
{
/**
* File extension
* @var string
*/
protected $_extension = '.dcm.yml';
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$mapping = $this->_getMapping($meta->name);
if (isset($mapping['gedmo'])) {
$classMapping = $mapping['gedmo'];
if (isset($classMapping['translation']['entity'])) {
$translationEntity = $classMapping['translation']['entity'];
if (!$cl = $this->getRelatedClassName($meta, $translationEntity)) {
throw new InvalidMappingException("Translation entity class: {$translationEntity} does not exist.");
}
$config['translationClass'] = $cl;
}
if (isset($classMapping['translation']['locale'])) {
$config['locale'] = $classMapping['translation']['locale'];
} elseif (isset($classMapping['translation']['language'])) {
$config['locale'] = $classMapping['translation']['language'];
}
}
if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $field => $fieldMapping) {
$this->buildFieldConfiguration($field, $fieldMapping, $config);
}
}
if (isset($mapping['attributeOverride'])) {
foreach ($mapping['attributeOverride'] as $field => $overrideMapping) {
$this->buildFieldConfiguration($field, $overrideMapping, $config);
}
}
if (!$meta->isMappedSuperclass && $config) {
if (is_array($meta->identifier) && count($meta->identifier) > 1) {
throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
}
}
}
/**
* {@inheritDoc}
*/
protected function _loadMappingFile($file)
{
return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
}
private function buildFieldConfiguration($field, $fieldMapping, array &$config)
{
if (is_array($fieldMapping) && isset($fieldMapping['gedmo'])) {
if (in_array('translatable', $fieldMapping['gedmo']) || isset($fieldMapping['gedmo']['translatable'])) {
// fields cannot be overrided and throws mapping exception
$config['fields'][] = $field;
if (isset($fieldMapping['gedmo']['translatable']['fallback'])) {
$config['fallback'][$field] = $fieldMapping['gedmo']['translatable']['fallback'];
}
}
}
}
}