/** * @param ConfigInterface $formConfig * @param string $class * @param string $property * * @return TypeGuess */ protected function getTypeGuess(ConfigInterface $formConfig, $class, $property) { $formType = $formConfig->get('form_type'); $formOptions = $formConfig->has('form_options') ? $formConfig->get('form_options') : array(); $formOptions = $this->addLabelOption($formOptions, $class, $property); // fallback guess from recursive call must be with low confidence return is_null($property) ? $this->createTypeGuess($formType, $formOptions, TypeGuess::LOW_CONFIDENCE) : $this->createTypeGuess($formType, $formOptions); }
/** * @param TokenInterface $token * @param ConfigInterface $config * @param object $entity */ protected function setDefaultOrganization(TokenInterface $token, ConfigInterface $config, $entity) { if ($token instanceof OrganizationContextTokenInterface && $config->has('organization_field_name')) { $accessor = PropertyAccess::createPropertyAccessor(); $fieldName = $config->get('organization_field_name'); if (!$accessor->getValue($entity, $fieldName)) { $accessor->setValue($entity, $fieldName, $token->getOrganizationContext()); } } }
/** * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * * @param ConfigInterface $extendConfig * @param array|null $aliases * @param array|null $skippedOrigins */ protected function checkSchema(ConfigInterface $extendConfig, $aliases, array $skippedOrigins = null) { $extendProvider = $this->em->getExtendConfigProvider(); $className = $extendConfig->getId()->getClassName(); $doctrine = []; $entityName = $className; if (ExtendHelper::isCustomEntity($className)) { $type = 'Custom'; $tableName = $extendConfig->get('table'); if (!$tableName) { $tableName = $this->nameGenerator->generateCustomEntityTableName($className); } $doctrine[$entityName] = ['type' => 'entity', 'table' => $tableName]; // add 'id' field only for Custom entity without inheritance if (!$extendConfig->has('inherit')) { $doctrine[$entityName]['fields'] = ['id' => ['type' => 'integer', 'id' => true, 'generator' => ['strategy' => 'AUTO']]]; } } else { $type = 'Extend'; $entityName = $extendConfig->get('extend_class'); $doctrine[$entityName] = ['type' => 'mappedSuperclass', 'fields' => []]; } $schema = $extendConfig->get('schema'); $properties = []; $relationProperties = $schema ? $schema['relation'] : []; $defaultProperties = []; $addRemoveMethods = []; $fieldConfigs = $extendProvider->filter($this->createOriginFilterCallback($skippedOrigins), $className, true); foreach ($fieldConfigs as $fieldConfig) { $this->checkFields($entityName, $fieldConfig, $relationProperties, $defaultProperties, $properties, $doctrine); $extendProvider->persist($fieldConfig); } $relations = $extendConfig->get('relation', false, []); foreach ($relations as &$relation) { if (!$relation['field_id']) { continue; } $relation['assign'] = true; if ($relation['field_id']->getFieldType() !== RelationType::MANY_TO_ONE) { $fieldName = $relation['field_id']->getFieldName(); $addRemoveMethods[$fieldName]['self'] = $fieldName; if ($relation['target_field_id']) { $addRemoveMethods[$fieldName]['target'] = $relation['target_field_id']->getFieldName(); $addRemoveMethods[$fieldName]['is_target_addremove'] = $relation['field_id']->getFieldType() === RelationType::MANY_TO_MANY; } } $this->updateRelationValues($relation['target_entity'], $relation['field_id']); } $extendConfig->set('relation', $relations); $schema = ['class' => $className, 'entity' => $entityName, 'type' => $type, 'property' => $properties, 'relation' => $relationProperties, 'default' => $defaultProperties, 'addremove' => $addRemoveMethods, 'doctrine' => $doctrine]; if ($type == 'Extend') { $parentClassName = get_parent_class($className); if ($parentClassName == $entityName) { $parentClassName = $aliases[$entityName]; } $schema['parent'] = $parentClassName; $schema['inherit'] = get_parent_class($parentClassName); } elseif ($extendConfig->has('inherit')) { $schema['inherit'] = $extendConfig->get('inherit'); } $extendConfig->set('schema', $schema); $extendProvider->persist($extendConfig); }
/** * @param ConfigInterface $extendConfig * @param ConfigProviderInterface $extendConfigProvider * * @return bool */ protected function isApplicableField(ConfigInterface $extendConfig, ConfigProviderInterface $extendConfigProvider) { return !$extendConfig->is('is_deleted') && $extendConfig->is('owner', ExtendScope::OWNER_CUSTOM) && !$extendConfig->is('state', ExtendScope::STATE_NEW) && !in_array($extendConfig->getId()->getFieldType(), ['ref-one', 'ref-many']) && (!$extendConfig->has('target_entity') || !$extendConfigProvider->getConfig($extendConfig->get('target_entity'))->is('is_deleted')); }
/** * @param ConfigInterface $fieldConfig */ protected function createSelfRelation(ConfigInterface $fieldConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $targetEntityClass = $fieldConfig->get('target_entity'); $selfFieldId = new FieldConfigId('extend', $fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType())); $selfConfig = $this->extendConfigProvider->getConfig($selfFieldId->getClassName()); $relationKey = ExtendHelper::buildRelationKey($selfFieldId->getClassName(), $selfFieldId->getFieldName(), $selfFieldId->getFieldType(), $targetEntityClass); /** * in case of oneToMany relation * automatically create target field (type: manyToOne) */ $targetFieldId = false; $owner = true; $targetOwner = false; if (in_array($selfFieldId->getFieldType(), RelationType::$toManyRelations)) { $classNameArray = explode('\\', $selfFieldId->getClassName()); $relationFieldName = strtolower(array_pop($classNameArray)) . '_' . $selfFieldId->getFieldName(); if ($selfFieldId->getFieldType() === RelationType::ONE_TO_MANY) { $owner = false; $targetOwner = true; } $targetFieldId = new FieldConfigId('extend', $targetEntityClass, $relationFieldName, ExtendHelper::getReverseRelationType($selfFieldId->getFieldType())); } $selfRelationConfig = ['assign' => false, 'field_id' => $selfFieldId, 'owner' => $owner, 'target_entity' => $targetEntityClass, 'target_field_id' => $targetFieldId]; if ($fieldConfig->has('cascade')) { $selfRelationConfig['cascade'] = $fieldConfig->get('cascade'); } $selfRelations = $selfConfig->get('relation') ?: []; $selfRelations[$relationKey] = $selfRelationConfig; $selfConfig->set('relation', $selfRelations); $this->extendConfigProvider->persist($selfConfig); $targetConfig = $this->extendConfigProvider->getConfig($targetEntityClass); $targetRelationConfig = ['assign' => false, 'field_id' => $targetFieldId, 'owner' => $targetOwner, 'target_entity' => $selfFieldId->getClassName(), 'target_field_id' => $selfFieldId]; $targetRelations = $targetConfig->get('relation') ?: []; $targetRelations[$relationKey] = $targetRelationConfig; $targetConfig->set('relation', $targetRelations); $fieldConfig->set('relation_key', $relationKey); $this->extendConfigProvider->persist($targetConfig); }
/** * Updates values of the given config based on the given default values and $force flag * * @param ConfigInterface $config * @param array $defaultValues * @param bool $force * * @return bool TRUE if at least one config value was updated; otherwise, FALSE */ protected function updateConfigValues(ConfigInterface $config, array $defaultValues, $force) { $hasChanges = false; foreach ($defaultValues as $code => $value) { if (!$config->has($code) || $force) { $config->set($code, $value); $hasChanges = true; } } return $hasChanges; }
/** * @param ConfigInterface $extendConfig * * @return bool */ protected function isApplicableField(ConfigInterface $extendConfig) { return !$extendConfig->is('is_deleted') && $extendConfig->is('owner', ExtendScope::OWNER_CUSTOM) && !$extendConfig->is('state', ExtendScope::STATE_NEW) && !in_array($extendConfig->getId()->getFieldType(), RelationType::$toAnyRelations) && (!$extendConfig->has('target_entity') || !$this->extendConfigProvider->getConfig($extendConfig->get('target_entity'))->is('is_deleted')); }
/** * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * * @param ConfigInterface $extendConfig * @param ConfigProvider $configProvider * @param array|null $aliases * @param callable|null $filter function (ConfigInterface $config) : bool */ protected function checkSchema(ConfigInterface $extendConfig, ConfigProvider $configProvider, $aliases, $filter = null) { $className = $extendConfig->getId()->getClassName(); $doctrine = []; $entityName = $className; if (ExtendHelper::isCustomEntity($className)) { $type = 'Custom'; $tableName = $extendConfig->get('table'); if (!$tableName) { $tableName = $this->nameGenerator->generateCustomEntityTableName($className); } $doctrine[$entityName] = ['type' => 'entity', 'table' => $tableName]; // add 'id' field only for Custom entity without inheritance if (!$extendConfig->has('inherit')) { $doctrine[$entityName]['fields'] = ['id' => ['type' => 'integer', 'id' => true, 'generator' => ['strategy' => 'AUTO']]]; } } else { $type = 'Extend'; $entityName = $extendConfig->get('extend_class'); $doctrine[$entityName] = ['type' => 'mappedSuperclass', 'fields' => []]; } $schema = $extendConfig->get('schema', false, []); $properties = isset($schema['property']) && null !== $filter ? $schema['property'] : []; $relationProperties = isset($schema['relation']) && null !== $filter ? $schema['relation'] : []; $defaultProperties = isset($schema['default']) && null !== $filter ? $schema['default'] : []; $addRemoveMethods = isset($schema['addremove']) && null !== $filter ? $schema['addremove'] : []; $fieldConfigs = null === $filter ? $configProvider->getConfigs($className, true) : $configProvider->filter($filter, $className, true); foreach ($fieldConfigs as $fieldConfig) { $this->updateFieldState($fieldConfig); $this->checkFieldSchema($entityName, $fieldConfig, $relationProperties, $defaultProperties, $properties, $doctrine); } $relations = $extendConfig->get('relation', false, []); foreach ($relations as $relation) { /** @var FieldConfigId $fieldId */ $fieldId = $relation['field_id']; if (!$fieldId) { continue; } $fieldName = $fieldId->getFieldName(); $isDeleted = $configProvider->hasConfig($fieldId->getClassName(), $fieldName) ? $configProvider->getConfig($fieldId->getClassName(), $fieldName)->is('is_deleted') : false; if (!isset($relationProperties[$fieldName])) { $relationProperties[$fieldName] = []; if ($isDeleted) { $relationProperties[$fieldName]['private'] = true; } } if (!$isDeleted && $fieldId->getFieldType() !== RelationType::MANY_TO_ONE) { $addRemoveMethods[$fieldName]['self'] = $fieldName; /** @var FieldConfigId $targetFieldId */ $targetFieldId = $relation['target_field_id']; if ($targetFieldId) { $fieldType = $fieldId->getFieldType(); $addRemoveMethods[$fieldName]['target'] = $targetFieldId->getFieldName(); $addRemoveMethods[$fieldName]['is_target_addremove'] = $fieldType === RelationType::MANY_TO_MANY; } } } $schema = ['class' => $className, 'entity' => $entityName, 'type' => $type, 'property' => $properties, 'relation' => $relationProperties, 'default' => $defaultProperties, 'addremove' => $addRemoveMethods, 'doctrine' => $doctrine]; if ($type === 'Extend') { $parentClassName = get_parent_class($className); if ($parentClassName === $entityName) { $parentClassName = $aliases[$entityName]; } $schema['parent'] = $parentClassName; $schema['inherit'] = get_parent_class($parentClassName); } elseif ($extendConfig->has('inherit')) { $schema['inherit'] = $extendConfig->get('inherit'); } $extendConfig->set('schema', $schema); $this->configManager->persist($extendConfig); }