/** * {@inheritdoc} */ public function preUpdate() { $ownershipConfigProvider = $this->configManager->getProvider('ownership'); $extendConfigProvider = $this->configManager->getProvider('extend'); $entityConfigs = $extendConfigProvider->getConfigs(); foreach ($entityConfigs as $entityConfig) { if (!$entityConfig->is('owner', ExtendScope::OWNER_CUSTOM)) { continue; } if (!$ownershipConfigProvider->hasConfig($entityConfig->getId()->getClassName())) { continue; } $ownershipConfig = $ownershipConfigProvider->getConfig($entityConfig->getId()->getClassName()); $ownerType = $ownershipConfig->get('owner_type'); if (empty($ownerType)) { continue; } $this->createOwnerRelation($entityConfig, $this->getOwnerTargetEntityClassName($ownerType), $ownershipConfig->get('owner_field_name')); if (in_array($ownerType, [OwnershipType::OWNER_TYPE_USER, OwnershipType::OWNER_TYPE_BUSINESS_UNIT])) { if (!$ownershipConfig->has('organization_field_name')) { $ownershipConfig->set('organization_field_name', 'organization'); $ownershipConfig->set('organization_column_name', 'organization_id'); $this->configManager->persist($ownershipConfig); $organizationFieldName = 'organization'; } else { $organizationFieldName = $ownershipConfig->get('organization_field_name'); } $this->createOwnerRelation($entityConfig, $this->ownershipMetadataProvider->getOrganizationClass(), $organizationFieldName); } } }
public function testFlush() { $model = new EntityConfigModel(self::ENTITY_CLASS); $entityConfigId = new EntityConfigId('entity', self::ENTITY_CLASS); $entityConfig = new Config($entityConfigId); $entityConfig->set('icon', 'test_icon'); $entityConfig->set('label', 'test_label'); $entityPropertyConfig = new PropertyConfigContainer(['entity' => ['items' => ['icon' => [], 'label' => ['options' => ['indexed' => true]]]]]); $this->entityConfigProvider->expects($this->once())->method('getPropertyConfig')->will($this->returnValue($entityPropertyConfig)); $testConfigId = new EntityConfigId('test', self::ENTITY_CLASS); $testConfig = new Config($testConfigId); $testConfig->set('attr1', 'test_attr1'); $testPropertyConfig = new PropertyConfigContainer(['entity' => ['items' => ['attr1' => []]]]); $this->testConfigProvider->expects($this->once())->method('getPropertyConfig')->will($this->returnValue($testPropertyConfig)); $this->modelManager->expects($this->once())->method('getEntityModel')->with($entityConfigId->getClassName())->will($this->returnValue($model)); $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock(); $this->modelManager->expects($this->any())->method('getEntityManager')->will($this->returnValue($em)); $this->setFlushExpectations($em, [$model]); $this->eventDispatcher->expects($this->at(2))->method('dispatch')->with(Events::PRE_FLUSH, new PreFlushConfigEvent(['entity' => $entityConfig, 'test' => $testConfig], $this->configManager)); $this->configManager->persist($entityConfig); $this->configManager->persist($testConfig); $this->configManager->flush(); $this->assertEquals(['icon' => 'test_icon', 'label' => 'test_label'], $model->toArray('entity')); $this->assertEquals(['attr1' => 'test_attr1'], $model->toArray('test')); $this->assertCount(3, $model->getIndexedValues()); $this->assertEquals('entity_config', $model->getIndexedValues()[0]->getScope()); $this->assertEquals('module_name', $model->getIndexedValues()[0]->getCode()); $this->assertEquals('entity_config', $model->getIndexedValues()[1]->getScope()); $this->assertEquals('entity_name', $model->getIndexedValues()[1]->getCode()); $this->assertEquals('entity', $model->getIndexedValues()[2]->getScope()); $this->assertEquals('label', $model->getIndexedValues()[2]->getCode()); }
/** * POST_SUBMIT event handler * * @param FormEvent $event */ public function postSubmit(FormEvent $event) { $form = $event->getForm(); $options = $form->getConfig()->getOptions(); /** @var ConfigIdInterface $configId */ $configId = $options['config_id']; if (!$form->isValid()) { return; } // change the entity state to "Requires update" if the attribute has "require_schema_update" option // and the value of the attribute was changed $configProvider = $this->configManager->getProvider($configId->getScope()); if ($configProvider->getPropertyConfig()->isSchemaUpdateRequired($form->getName(), $configId)) { $newVal = $form->getData(); $oldVal = $this->configManager->getConfig($configId)->get($form->getName()); if ($this->isSchemaUpdateRequired($newVal, $oldVal)) { $extendConfigProvider = $this->configManager->getProvider('extend'); $extendConfig = $extendConfigProvider->getConfig($configId->getClassName()); if ($extendConfig->is('state', ExtendScope::STATE_ACTIVE)) { $extendConfig->set('state', ExtendScope::STATE_UPDATE); $this->configManager->persist($extendConfig); } } } }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addEventListener(FormEvents::POST_SUBMIT, function () use($options) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $options['config_id']; $entityConfig = $this->configManager->getProvider('extend')->getConfig($fieldConfigId->getClassName()); if ($entityConfig->is('state', ExtendScope::STATE_ACTIVE) && !$this->hasRelation($entityConfig, $this->getRelationKey($fieldConfigId))) { $entityConfig->set('state', ExtendScope::STATE_UPDATE); $this->configManager->persist($entityConfig); $this->configManager->flush(); } }); }
/** * @param ConfigInterface $entityConfig * @param ConfigProvider $configProvider */ protected function updateStateValues(ConfigInterface $entityConfig, ConfigProvider $configProvider) { if ($entityConfig->is('state', ExtendScope::STATE_DELETE)) { // mark entity as deleted if (!$entityConfig->is('is_deleted')) { $entityConfig->set('is_deleted', true); $this->configManager->persist($entityConfig); } // mark all fields as deleted $fieldConfigs = $configProvider->getConfigs($entityConfig->getId()->getClassName(), true); foreach ($fieldConfigs as $fieldConfig) { if (!$fieldConfig->is('is_deleted')) { $fieldConfig->set('is_deleted', true); $this->configManager->persist($fieldConfig); } } } elseif (!$entityConfig->is('state', ExtendScope::STATE_ACTIVE)) { $hasNotActiveFields = false; $fieldConfigs = $configProvider->getConfigs($entityConfig->getId()->getClassName(), true); foreach ($fieldConfigs as $fieldConfig) { if (!$fieldConfig->in('state', [ExtendScope::STATE_ACTIVE, ExtendScope::STATE_DELETE])) { $hasNotActiveFields = true; break; } } // Set entity state to active if all fields are active or deleted if (!$hasNotActiveFields) { $entityConfig->set('state', ExtendScope::STATE_ACTIVE); $this->configManager->persist($entityConfig); } } }
/** * @param array $configs * @param string $className * @param string|null $fieldName * @return bool TRUE is any changes were made */ protected function updateConfigs(array $configs, $className, $fieldName = null) { $result = false; foreach ($configs as $scope => $values) { $config = $this->configManager->getProvider($scope)->getConfig($className, $fieldName); $hasChanges = false; foreach ($values as $key => $value) { $path = explode('.', $key); $pathLength = count($path); if ($pathLength > 1) { $code = array_shift($path); $existingVal = (array) $config->get($code); $current =& $existingVal; foreach ($path as $name) { if (!array_key_exists($name, $current)) { $current[$name] = []; } $current =& $current[$name]; } $current = $this->isAppend($scope, $key, $className, $fieldName) ? array_merge($current, (array) $value) : $value; $config->set($code, $existingVal); } elseif ($this->isAppend($scope, $key, $className, $fieldName)) { $config->set($key, array_merge((array) $config->get($key), (array) $value)); } else { $config->set($key, $value); } $hasChanges = true; } if ($hasChanges) { $this->configManager->persist($config); $result = true; } } return $result; }
/** * @param ConfigInterface $fieldConfig */ protected function setRelationKeyToFieldConfig(ConfigInterface $fieldConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $relationKey = ExtendHelper::buildRelationKey($fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType()), $fieldConfig->get('target_entity')); $fieldConfig->set('relation_key', $relationKey); $this->configManager->persist($fieldConfig); }
/** * {@inheritdoc} */ public function preUpdate() { $targetEntityConfigs = $this->configManager->getProvider('extend')->getConfigs(); foreach ($targetEntityConfigs as $targetEntityConfig) { if ($targetEntityConfig->is('is_extend')) { $indices = $targetEntityConfig->has('index') ? $targetEntityConfig->get('index') : []; if ($this->updateIndices($indices, $targetEntityConfig->getId()->getClassName())) { if (empty($indices)) { $targetEntityConfig->remove('index'); } else { $targetEntityConfig->set('index', $indices); } $this->configManager->persist($targetEntityConfig); } } } }
/** * @param string $entityClass * @throws WorkflowException */ public function generateWorkflowFields($entityClass) { if ($this->entityConnector->isWorkflowAware($entityClass)) { return; } $extendConfigProvider = $this->configManager->getProvider('extend'); $entityConfig = $extendConfigProvider->getConfig($entityClass); if (!$entityConfig || !$entityConfig->is('is_extend')) { throw new WorkflowException(sprintf('Class %s can not be extended', $entityClass)); } $workflowItemClass = 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowItem'; $workflowStepClass = 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowStep'; // add fields $hasWorkflowItemField = $this->configManager->hasConfig($entityClass, self::PROPERTY_WORKFLOW_ITEM); if (!$hasWorkflowItemField) { $this->addRelationField($entityClass, self::PROPERTY_WORKFLOW_ITEM, ConfigHelper::getTranslationKey('entity', 'label', $workflowItemClass, 'related_entity'), ConfigHelper::getTranslationKey('entity', 'description', $workflowItemClass, 'related_entity'), $workflowItemClass, 'id'); } $hasWorkflowStepField = $this->configManager->hasConfig($entityClass, self::PROPERTY_WORKFLOW_STEP); if (!$hasWorkflowStepField) { $this->addRelationField($entityClass, self::PROPERTY_WORKFLOW_STEP, ConfigHelper::getTranslationKey('entity', 'label', $workflowStepClass, 'related_entity'), ConfigHelper::getTranslationKey('entity', 'description', $workflowStepClass, 'related_entity'), $workflowStepClass, 'label'); } // update entity config $entityConfig->set('state', ExtendScope::STATE_UPDATE); $entityConfig->set('upgradeable', true); $this->configManager->persist($entityConfig); $this->configManager->flush(); // update database $this->entityProcessor->updateDatabase(); // make fields hidden // TODO: Fields can be hidden only after schema update due to a bug in DoctrineSubscriber // TODO: Should be fixed in scope of https://magecore.atlassian.net/browse/BAP-3621 // TODO: If make fields hidden then these fields will be created only for the first extended entity // TODO: Should be fixed in scope of https://magecore.atlassian.net/browse/BAP-3632 /* if (!$hasWorkflowItemField) { $this->hideRelationField($entityClass, self::PROPERTY_WORKFLOW_ITEM); } if (!$hasWorkflowStepField) { $this->hideRelationField($entityClass, self::PROPERTY_WORKFLOW_STEP); } $this->configManager->flush(); */ }
public function testPersistAndMerge() { $configId = new EntityConfigId('entity', self::ENTITY_CLASS); $config1 = $this->getConfig($configId, ['val1' => '1', 'val2' => '1']); $config2 = $this->getConfig($configId, ['val2' => '2_new', 'val3' => '3']); $expectedConfig = $this->getConfig($configId, ['val1' => '1', 'val2' => '2_new', 'val3' => '3']); $this->configManager->persist($config1); $this->configManager->merge($config2); $toBePersistedConfigs = $this->configManager->getUpdateConfig(); $this->assertEquals([$expectedConfig], $toBePersistedConfigs); }
/** * @param string $className * @param string $fieldName * @param string $state */ protected function setExtendData($className, $fieldName, $state) { $provider = $this->configManager->getProvider('extend'); if (!$provider) { return; } $config = $provider->getConfig($className, $fieldName); $data = ['owner' => ExtendScope::OWNER_CUSTOM, 'state' => $config->is('state', ExtendScope::STATE_NEW) ? ExtendScope::STATE_NEW : $state, 'origin' => ExtendScope::ORIGIN_CUSTOM, 'is_extend' => true, 'is_deleted' => false, 'is_serialized' => false]; foreach ($data as $code => $value) { $config->set($code, $value); } $this->configManager->persist($config); }
/** * @param FormEvent $event */ public function postSubmit(FormEvent $event) { $options = $event->getForm()->getConfig()->getOptions(); $configModel = $options['config_model']; if ($configModel instanceof FieldConfigModel) { $className = $configModel->getEntity()->getClassName(); $fieldName = $configModel->getFieldName(); } else { $fieldName = null; $className = $configModel->getClassName(); } $data = $event->getData(); foreach ($this->configManager->getProviders() as $provider) { if (isset($data[$provider->getScope()])) { $config = $provider->getConfig($className, $fieldName); $config->setValues($data[$provider->getScope()]); $this->configManager->persist($config); } } if ($event->getForm()->isValid()) { $this->configManager->flush(); } }
/** * @param FormEvent $event */ public function postSubmit(FormEvent $event) { $form = $event->getForm(); $configModel = $form->getConfig()->getOption('config_model'); $data = $event->getData(); $labelsToBeUpdated = []; foreach ($this->configManager->getProviders() as $provider) { $scope = $provider->getScope(); if (isset($data[$scope])) { $configId = $this->configManager->getConfigIdByModel($configModel, $scope); $config = $provider->getConfigById($configId); $translatable = $provider->getPropertyConfig()->getTranslatableValues($configId); foreach ($data[$scope] as $code => $value) { if (in_array($code, $translatable, true)) { // check if a label text was changed $labelKey = $config->get($code); if (!$configModel->getId()) { $labelsToBeUpdated[$labelKey] = $value; } elseif ($value != $this->translator->trans($labelKey)) { $labelsToBeUpdated[$labelKey] = $value; } // replace label text with label name in $value variable $value = $config->get($code); } $config->set($code, $value); } $this->configManager->persist($config); } } if ($form->isValid()) { // update changed labels if any if (!empty($labelsToBeUpdated)) { /** @var EntityManager $translationEm */ $translationEm = $this->doctrine->getManagerForClass(Translation::ENTITY_NAME); /** @var TranslationRepository $translationRepo */ $translationRepo = $translationEm->getRepository(Translation::ENTITY_NAME); $values = []; $locale = $this->translator->getLocale(); foreach ($labelsToBeUpdated as $labelKey => $labelText) { // save into translation table $values[] = $translationRepo->saveValue($labelKey, $labelText, $locale, TranslationRepository::DEFAULT_DOMAIN, Translation::SCOPE_UI); } // mark translation cache dirty $this->dbTranslationMetadataCache->updateTimestamp($locale); $translationEm->flush($values); } $this->configManager->flush(); } }
/** * @param ConfigInterface $enumFieldConfig * * @return bool */ protected function updateEnumFieldConfig(ConfigInterface $enumFieldConfig) { $hasChanges = false; $attributes = ['enum_name', 'enum_locale', 'enum_public', 'enum_options']; foreach ($attributes as $code) { if ($enumFieldConfig->get($code) !== null) { $enumFieldConfig->remove($code); $hasChanges = true; } } if ($hasChanges) { $this->configManager->persist($enumFieldConfig); } return $hasChanges; }
/** * @param string $className * @param array $options * @param string $fieldName */ protected function updateConfigs($className, $fieldName, $options) { foreach ($options as $scope => $scopeValues) { $config = $this->configManager->getProvider($scope)->getConfig($className, $fieldName); $hasChanges = false; foreach ($scopeValues as $code => $val) { if (!$config->is($code, $val)) { $config->set($code, $val); $hasChanges = true; } } if ($hasChanges) { $this->configManager->persist($config); } } }
public function testPersistAndMerge() { $configId = new EntityConfigId('entity', self::ENTITY_CLASS); $config1 = new Config($configId); $config1->set('val1', '1'); $config1->set('val2', '2'); $config2 = new Config($configId); $config2->set('val2', '2_new'); $config2->set('val3', '3'); $expectedConfig = new Config($configId); $expectedConfig->set('val1', '1'); $expectedConfig->set('val2', '2_new'); $expectedConfig->set('val3', '3'); $this->configManager->persist($config1); $this->configManager->merge($config2); $toBePersistedConfigs = $this->configManager->getUpdateConfig(); $this->assertEquals([$expectedConfig], $toBePersistedConfigs); }
/** * @param $className * @param $entityOptions * @throws \InvalidArgumentException */ protected function parseEntity($className, $entityOptions) { /** @var ExtendManager $extendManager */ $extendManager = $this->getContainer()->get('oro_entity_extend.extend.extend_manager'); $configProvider = $extendManager->getConfigProvider(); if (class_exists($className)) { $this->checkExtend($className); } if (!$this->configManager->hasConfig($className)) { $this->createEntityModel($className, $entityOptions); $this->setDefaultConfig($entityOptions, $className); $entityConfig = $configProvider->getConfig($className); $entityConfig->set('owner', ExtendManager::OWNER_SYSTEM); if (isset($entityOptions['is_extend'])) { $entityConfig->set('is_extend', $entityOptions['is_extend']); } else { $entityConfig->set('is_extend', false); } } foreach ($entityOptions['fields'] as $fieldName => $fieldConfig) { if ($this->configManager->hasConfig($className, $fieldName)) { throw new \InvalidArgumentException(sprintf('Field "%s" for Entity "%s" already added', $className, $fieldName)); } $mode = ConfigModelManager::MODE_DEFAULT; if (isset($fieldConfig['mode'])) { $mode = $fieldConfig['mode']; } $owner = ExtendManager::OWNER_SYSTEM; if (isset($fieldConfig['owner'])) { $owner = $fieldConfig['owner']; } $isExtend = false; if (isset($fieldConfig['is_extend'])) { $isExtend = $fieldConfig['is_extend']; } $extendManager->createField($className, $fieldName, $fieldConfig, $owner, $mode); $this->setDefaultConfig($entityOptions, $className, $fieldName); $config = $configProvider->getConfig($className, $fieldName); $config->set('state', ExtendManager::STATE_NEW); $config->set('is_extend', $isExtend); $this->configManager->persist($config); } }
/** * {@inheritdoc} */ public function postUpdate() { $extendConfigProvider = $this->configManager->getProvider('extend'); $entityConfigs = $extendConfigProvider->getConfigs(null, true); foreach ($entityConfigs as $entityConfig) { if ($entityConfig->is('inherit', ExtendHelper::BASE_ENUM_VALUE_CLASS)) { $entityClassName = $entityConfig->getId()->getClassName(); $schema = $entityConfig->get('schema', false, []); if (!empty($schema['doctrine'][$entityClassName]['repositoryClass'])) { continue; } $schema['doctrine'][$entityClassName]['repositoryClass'] = 'Oro\\Bundle\\EntityExtendBundle\\Entity\\Repository\\EnumValueRepository'; $schema['doctrine'][$entityClassName]['gedmo']['translation']['entity'] = 'Oro\\Bundle\\EntityExtendBundle\\Entity\\EnumValueTranslation'; $entityConfig->set('schema', $schema); $this->configManager->persist($entityConfig); } elseif ($entityConfig->is('is_extend')) { $fieldConfigs = $extendConfigProvider->getConfigs($entityConfig->getId()->getClassName()); foreach ($fieldConfigs as $fieldConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); if ($fieldConfigId->getFieldType() !== 'multiEnum') { continue; } $mappingClassName = $entityConfig->has('extend_class') ? $entityConfig->get('extend_class') : $entityConfig->getId()->getClassName(); $fieldName = $fieldConfigId->getFieldName(); $snapshotFieldName = ExtendHelper::getMultiEnumSnapshotFieldName($fieldName); $schema = $entityConfig->get('schema', false, []); if (!empty($schema['doctrine'][$mappingClassName]['fields'][$snapshotFieldName])) { continue; } $schema['property'][$snapshotFieldName] = []; if ($fieldConfig->is('is_deleted')) { $schema['property'][$snapshotFieldName]['private'] = true; } $schema['doctrine'][$mappingClassName]['fields'][$snapshotFieldName] = ['column' => $this->nameGenerator->generateMultiEnumSnapshotColumnName($fieldName), 'type' => 'string', 'nullable' => true, 'length' => ExtendHelper::MAX_ENUM_SNAPSHOT_LENGTH]; $entityConfig->set('schema', $schema); $this->configManager->persist($entityConfig); } } } }
/** * @param array $configs * @param string $className * @param string|null $fieldName * @return bool TRUE is any changes were made */ protected function updateConfigs(array $configs, $className, $fieldName = null) { $result = false; foreach ($configs as $scope => $values) { $config = $this->configManager->getProvider($scope)->getConfig($className, $fieldName); $hasChanges = false; foreach ($values as $key => $value) { if ($this->isAppend($scope, $key, $className, $fieldName)) { $config->set($key, array_merge((array) $config->get($key), (array) $value)); } else { $config->set($key, $value); } $hasChanges = true; } if ($hasChanges) { $this->configManager->persist($config); $result = true; } } return $result; }
/** * Makes sure that both source and target entities know about a reverse relation * * @param ConfigInterface $fieldConfig */ protected function ensureReverseRelationCompleted(ConfigInterface $fieldConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $relationKey = $fieldConfig->get('relation_key'); $selfConfig = $this->extendConfigProvider->getConfig($fieldConfigId->getClassName()); $selfRelations = $selfConfig->get('relation', false, []); if (isset($selfRelations[$relationKey]['field_id']) && $selfRelations[$relationKey]['field_id']) { return; } $targetConfig = $this->extendConfigProvider->getConfig($fieldConfig->get('target_entity')); $targetRelations = $targetConfig->get('relation', false, []); if (!isset($targetRelations[$relationKey])) { return; } $selfFieldId = new FieldConfigId('extend', $fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType())); $selfRelations[$relationKey]['field_id'] = $selfFieldId; $targetRelations[$relationKey]['target_field_id'] = $selfFieldId; $selfConfig->set('relation', $selfRelations); $targetConfig->set('relation', $targetRelations); $this->configManager->persist($selfConfig); $this->configManager->persist($targetConfig); }
/** * @param ConfigInterface $entityConfig */ protected function persistEntityConfig(ConfigInterface $entityConfig) { $this->configManager->persist($entityConfig); $this->configManager->flush(); }
/** * Tells the ConfigManager to make the given configuration data managed and persistent. * * @param ConfigInterface $config */ public function persist(ConfigInterface $config) { $this->configManager->persist($config); }
/** * @param ConfigManager $configManager * @param FieldConfigModel $fieldModel * @param array $options */ protected function updateFieldConfigs(ConfigManager $configManager, FieldConfigModel $fieldModel, $options) { $className = $fieldModel->getEntity()->getClassName(); $fieldName = $fieldModel->getFieldName(); foreach ($options as $scope => $scopeValues) { $configProvider = $configManager->getProvider($scope); $config = $configProvider->getConfig($className, $fieldName); $hasChanges = false; foreach ($scopeValues as $code => $val) { if (!$config->is($code, $val)) { $config->set($code, $val); $hasChanges = true; } } if ($hasChanges) { $configManager->persist($config); $indexedValues = $configProvider->getPropertyConfig()->getIndexedValues($config->getId()); $fieldModel->fromArray($config->getId()->getScope(), $config->all(), $indexedValues); } } }