/**
  * @param array                $configs
  * @param LoggerInterface|null $logger
  * @param bool                 $dryRun Log modifications without apply them
  * @throws \Exception
  */
 public function processConfigs(array $configs, LoggerInterface $logger = null, $dryRun = false)
 {
     $this->logger = $logger ?: new NullLogger();
     try {
         if (!empty($configs)) {
             $this->appendConfigs = $this->getAndRemoveElement($configs, self::APPEND_CONFIGS, []);
             $renameConfigs = $this->getAndRemoveElement($configs, self::RENAME_CONFIGS, []);
             $this->filterConfigs($configs);
             $hasChanges = false;
             if (!empty($configs)) {
                 foreach ($configs as $className => $entityConfigs) {
                     $this->processEntityConfigs($className, $entityConfigs);
                 }
                 $hasChanges = true;
             }
             if ($this->renameFields($renameConfigs)) {
                 $hasChanges = true;
             }
             if ($hasChanges) {
                 if ($dryRun) {
                     $this->configManager->clear();
                 } else {
                     $this->configManager->flush();
                     $this->configManager->clearCache();
                 }
             }
         }
     } catch (\Exception $ex) {
         $this->logger = null;
         throw $ex;
     }
 }
Example #2
0
 /**
  * Load entity configs from annotations to a database
  *
  * @param bool                 $force  Force overwrite config's option values
  * @param callable|null        $filter function (ClassMetadata[] $doctrineAllMetadata)
  * @param LoggerInterface|null $logger
  * @param bool                 $dryRun Log modifications without apply them
  *
  * @throws \Exception
  */
 public function load($force = false, \Closure $filter = null, LoggerInterface $logger = null, $dryRun = false)
 {
     $this->logger = $logger ?: new NullLogger();
     try {
         $entityManagers = $this->entityManagerBag->getEntityManagers();
         foreach ($entityManagers as $em) {
             /** @var ClassMetadata[] $doctrineAllMetadata */
             $doctrineAllMetadata = $em->getMetadataFactory()->getAllMetadata();
             if (null !== $filter) {
                 $doctrineAllMetadata = $filter($doctrineAllMetadata);
             }
             foreach ($doctrineAllMetadata as $metadata) {
                 $this->loadEntityConfigs($metadata, $force);
             }
         }
         if ($dryRun) {
             $this->configManager->clear();
         } else {
             $this->configManager->flush();
             $this->configManager->flushAllCaches();
         }
     } catch (\Exception $ex) {
         $this->logger = null;
         throw $ex;
     }
 }
 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());
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function write(array $items)
 {
     $translations = [];
     foreach ($items as $item) {
         $translations = array_merge($translations, $this->writeItem($item));
     }
     $this->configManager->flush();
     $this->translationHelper->saveTranslations($translations);
 }
Example #5
0
 /**
  * {@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();
         }
     });
 }
Example #6
0
 /**
  * @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();
     */
 }
 /**
  * Runs command
  *
  * @param  InputInterface  $input
  * @param  OutputInterface $output
  * @throws \InvalidArgumentException
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln($this->getDescription());
     $this->configManager = $this->getContainer()->get('oro_entity_config.config_manager');
     /** @var Kernel $kernel */
     $kernel = $this->getContainer()->get('kernel');
     foreach ($kernel->getBundles() as $bundle) {
         $path = $bundle->getPath() . '/Resources/config/entity_extend.yml';
         if (is_file($path)) {
             $config = Yaml::parse(realpath($path));
             foreach ($config as $className => $entityOptions) {
                 $className = class_exists($className) ? $className : 'Extend\\Entity\\' . $className;
                 $this->parseEntity($className, $entityOptions);
             }
             $this->configManager->flush();
             $output->writeln('Done');
         }
     }
     $this->getContainer()->get('oro_entity_extend.tools.dumper')->clear();
     $this->configManager->clearConfigurableCache();
 }
 /**
  * @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();
     }
 }
Example #9
0
 /**
  * @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 string $enumValueClassName
  * @param bool   $isPublic
  * @param bool   $doFlush
  *
  * @throws \InvalidArgumentException
  */
 public function applyEnumEntityOptions($enumValueClassName, $isPublic, $doFlush = true)
 {
     if (empty($enumValueClassName)) {
         throw new \InvalidArgumentException('$enumValueClassName must not be empty.');
     }
     $enumConfigProvider = $this->configManager->getProvider('enum');
     $enumEntityConfig = $enumConfigProvider->getConfig($enumValueClassName);
     if (!$enumEntityConfig->is('public', $isPublic)) {
         $enumEntityConfig->set('public', $isPublic);
         $this->configManager->persist($enumEntityConfig);
         if ($doFlush) {
             $this->configManager->flush();
         }
     }
 }
Example #11
0
 /**
  * Makes sure that extended entity configs are ready to be processing by other config related commands
  */
 public function checkConfig()
 {
     $hasAliases = file_exists(ExtendClassLoadingUtils::getAliasesPath($this->cacheDir));
     if ($hasAliases) {
         return;
     }
     $hasChanges = false;
     $extendConfigs = $this->configManager->getProvider('extend')->getConfigs(null, true);
     foreach ($extendConfigs as $extendConfig) {
         $schema = $extendConfig->get('schema', false, []);
         // make sure that inheritance definition for extended entities is up-to-date
         // this check is required to avoid doctrine mapping exceptions during the platform update
         // in case if a parent class is changed in a new version of a code
         if (isset($schema['type'], $schema['class'], $schema['entity']) && $schema['type'] === 'Extend') {
             $entityClassName = $schema['entity'];
             $parentClassName = get_parent_class($schema['class']);
             if ($parentClassName !== $entityClassName) {
                 $inheritClassName = get_parent_class($parentClassName);
                 $hasSchemaChanges = false;
                 if (!isset($schema['parent']) || $schema['parent'] !== $parentClassName) {
                     $hasSchemaChanges = true;
                     $schema['parent'] = $parentClassName;
                 }
                 if (!isset($schema['inherit']) || $schema['inherit'] !== $inheritClassName) {
                     $hasSchemaChanges = true;
                     $schema['inherit'] = $inheritClassName;
                 }
                 if ($hasSchemaChanges) {
                     $hasChanges = true;
                     $extendConfig->set('schema', $schema);
                     $this->configManager->persist($extendConfig);
                 }
             }
         }
     }
     if ($hasChanges) {
         $this->configManager->flush();
     }
 }
Example #12
0
 /**
  * @param ConfigInterface $entityConfig
  */
 protected function persistEntityConfig(ConfigInterface $entityConfig)
 {
     $this->configManager->persist($entityConfig);
     $this->configManager->flush();
 }
 /**
  * Flushes all changes to configuration data that have been queued up to now to the database.
  */
 public function flush()
 {
     $this->configManager->flush();
 }