/** * Generate doctrine proxy classes for extended entities */ public function generateProxies() { $em = $this->configManager->getEntityManager(); $isAutoGenerated = $em->getConfiguration()->getAutoGenerateProxyClasses(); if (!$isAutoGenerated) { $proxyDir = $em->getConfiguration()->getProxyDir(); if (!empty($this->cacheDir) && $this->kernelCacheDir !== $this->cacheDir && strpos($proxyDir, $this->kernelCacheDir) === 0) { $proxyDir = $this->cacheDir . substr($proxyDir, strlen($this->kernelCacheDir)); } $extendConfigProvider = $this->configManager->getProvider('extend'); $extendConfigs = $extendConfigProvider->getConfigs(null, true); $metadataFactory = $em->getMetadataFactory(); $proxyFactory = $em->getProxyFactory(); foreach ($extendConfigs as $extendConfig) { if (!$extendConfig->is('is_extend')) { continue; } if ($extendConfig->in('state', [ExtendScope::STATE_NEW])) { continue; } $entityClass = $extendConfig->getId()->getClassName(); $proxyFileName = $proxyDir . DIRECTORY_SEPARATOR . '__CG__' . str_replace('\\', '', $entityClass) . '.php'; $metadata = $metadataFactory->getMetadataFor($entityClass); $proxyFactory->generateProxyClasses([$metadata], $proxyDir); clearstatcache(true, $proxyFileName); } } }
/** * @param ConfigManager $configManager */ public function __construct(ConfigManager $configManager) { $this->configManager = $configManager; $this->em = $this->configManager->getEntityManager(); $this->options = $this->em->getRepository(OptionSet::ENTITY_NAME); $this->relations = $this->em->getRepository(OptionSetRelation::ENTITY_NAME); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $configModel = $options['config_model']; $data = array(); if ($configModel instanceof FieldConfigModel) { $className = $configModel->getEntity()->getClassName(); $fieldName = $configModel->getFieldName(); $fieldType = $configModel->getType(); $configType = PropertyConfigContainer::TYPE_FIELD; } else { $className = $configModel->getClassName(); $fieldName = null; $fieldType = null; $configType = PropertyConfigContainer::TYPE_ENTITY; } foreach ($this->configManager->getProviders() as $provider) { if ($provider->getPropertyConfig()->hasForm($configType, $fieldType)) { $config = $provider->getConfig($className, $fieldName); $builder->add($provider->getScope(), new ConfigScopeType($provider->getPropertyConfig()->getFormItems($configType, $fieldType), $config, $this->configManager, $configModel), array('block_config' => (array) $provider->getPropertyConfig()->getFormBlockConfig($configType))); $data[$provider->getScope()] = $config->all(); } } if ($fieldType == 'optionSet') { $data['extend']['set_options'] = $this->configManager->getEntityManager()->getRepository(OptionSet::ENTITY_NAME)->findOptionsByField($configModel->getId()); } $builder->setData($data); $builder->addEventSubscriber(new ConfigSubscriber($this->configManager)); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $configModel = $options['config_model']; $data = array(); if ($configModel instanceof FieldConfigModel) { $className = $configModel->getEntity()->getClassName(); $fieldName = $configModel->getFieldName(); $fieldType = $configModel->getType(); $configType = PropertyConfigContainer::TYPE_FIELD; /** * Add read only field name and field type */ $builder->add('fieldName', 'text', array('label' => 'oro.entity_config.form.name.label', 'block' => 'general', 'disabled' => true, 'data' => $fieldName)); $builder->add('type', 'choice', array('label' => 'oro.entity_config.form.type.label', 'choices' => [], 'block' => 'general', 'disabled' => true, 'empty_value' => 'oro.entity_extend.form.data_type.' . $fieldType)); } else { $className = $configModel->getClassName(); $fieldName = null; $fieldType = null; $configType = PropertyConfigContainer::TYPE_ENTITY; } foreach ($this->configManager->getProviders() as $provider) { if ($provider->getPropertyConfig()->hasForm($configType, $fieldType)) { $config = $this->configManager->getConfig($provider->getId($className, $fieldName, $fieldType)); $builder->add($provider->getScope(), new ConfigScopeType($provider->getPropertyConfig()->getFormItems($configType, $fieldType), $config, $this->configManager, $configModel), array('block_config' => $this->getFormBlockConfig($provider, $configType))); $data[$provider->getScope()] = $config->all(); } } /** @deprecated since 1.4, will be removed in 2.0 */ if ($fieldType == 'optionSet') { $data['extend']['set_options'] = $this->configManager->getEntityManager()->getRepository(OptionSet::ENTITY_NAME)->findOptionsByField($configModel->getId()); } $builder->setData($data); $builder->addEventSubscriber(new ConfigSubscriber($this->configManager, $this->translator, $this->dbTranslationMetadataCache)); }
/** * * @param string $scope * @return array */ protected function getObjectName($scope = 'name') { if (empty($this->filterChoices[$scope])) { $alias = 'ce'; $qb = $this->configManager->getEntityManager()->createQueryBuilder(); $qb->select($alias)->from(EntityConfigModel::ENTITY_NAME, $alias)->add('select', $alias . '.className')->distinct($alias . '.className'); $result = $qb->getQuery()->getArrayResult(); $options = ['name' => [], 'module' => []]; foreach ((array) $result as $value) { $className = explode('\\', $value['className']); $options['name'][$value['className']] = ''; $options['module'][$value['className']] = ''; if (strpos($value['className'], 'Extend\\Entity') === false) { foreach ($className as $index => $name) { if (count($className) - 1 == $index) { $options['name'][$value['className']] = $name; } elseif (!in_array($name, array('Bundle', 'Entity'))) { $options['module'][$value['className']] .= $name; } } } else { $options['name'][$value['className']] = str_replace('Extend\\Entity\\', '', $value['className']); $options['module'][$value['className']] = 'System'; } } $this->filterChoices = $options; } return $this->filterChoices[$scope]; }
/** * @param string $entityClass * * @return string[] */ protected function getPrimaryKeyColumnNames($entityClass) { try { return $this->configManager->getEntityManager()->getClassMetadata($entityClass)->getIdentifierColumnNames(); } catch (\ReflectionException $e) { // ignore entity not found exception return ['id']; } catch (ORMMappingException $e) { return ['id']; } catch (PersistenceMappingException $e) { return ['id']; } }
/** * @param string $entityClass * * @return string[] */ protected function getPrimaryKeyColumnNames($entityClass) { try { return $this->configManager->getEntityManager()->getClassMetadata($entityClass)->getIdentifierColumnNames(); } catch (\ReflectionException $e) { // ignore entity not found exception return ['id']; } catch (ORMMappingException $e) { // ignore any doctrine mapping exceptions // it may happens if the entity has relation to deleted custom entity // or during update schema for newly created custom entity with relation return ['id']; } catch (PersistenceMappingException $e) { return ['id']; } }
/** * @param array $classMap [class_name => entity_config_id, ...] * * @return array [entity_config_id => [field_name => is_configurable, ...], ...] */ protected function loadConfigurableFields(array $classMap) { $connection = $this->configManager->getEntityManager()->getConnection(); $fieldRows = $connection->executeQuery('SELECT id, entity_id, field_name, type, mode, data FROM oro_entity_config_field'); $configurable = []; $fields = []; foreach ($fieldRows as $row) { $fieldId = (int) $row['id']; $entityId = (int) $row['entity_id']; if (!isset($classMap[$entityId])) { continue; } $className = $classMap[$entityId]; $fieldName = $row['field_name']; $fieldType = $row['type']; $isHidden = $row['mode'] === ConfigModel::MODE_HIDDEN; $data = array_merge($this->getEmptyData(), $connection->convertToPHPValue($row['data'], 'array')); $configurable[$entityId][$fieldName] = true; $fields[$className][$fieldName] = ['i' => $fieldId, 'h' => $isHidden, 't' => $fieldType]; $this->cache->saveFieldConfigValues($data, $className, $fieldName, $fieldType); } foreach ($fields as $className => $values) { $this->cache->saveFields($className, $values); } return $configurable; }
/** * @param ConfigManager $configManager * @param Translator $translator * @param DynamicTranslationMetadataCache $dbTranslationMetadataCache */ public function __construct(ConfigManager $configManager, Translator $translator, DynamicTranslationMetadataCache $dbTranslationMetadataCache) { $this->configManager = $configManager; $this->translator = $translator; $this->dbTranslationMetadataCache = $dbTranslationMetadataCache; $this->em = $configManager->getEntityManager(); }
/** * @param object $entity * @param FieldConfigId $fieldConfig * @return OptionSetRelation[] */ protected function getValueForOptionSet($entity, FieldConfigId $fieldConfig) { /** @var $optionSetRepository OptionSetRelationRepository */ $optionSetRepository = $this->configManager->getEntityManager()->getRepository(OptionSetRelation::ENTITY_NAME); $model = $this->configManager->getConfigFieldModel($fieldConfig->getClassName(), $fieldConfig->getFieldName()); $value = $optionSetRepository->findByFieldId($model->getId(), $entity->getId()); array_walk($value, function (OptionSetRelation &$item) { $item = array('title' => $item->getOption()->getLabel()); }); $value['values'] = $value; return $value; }
/** * @param FormEvent $event */ public function postSubmitData(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); /** @var FieldConfigModel $configModel */ $configModel = $form->getRoot()->getConfig()->getOptions()['config_model']; if (count($data)) { $em = $this->configManager->getEntityManager(); $optionValues = $oldOptions = $configModel->getOptions()->getValues(); $newOptions = []; array_walk_recursive($oldOptions, function (&$oldOption) { $oldOption = $oldOption->getId(); }); foreach ($data as $option) { if (is_array($option)) { $optionSet = new OptionSet(); $optionSet->setField($configModel); $optionSet->setData($option['id'], $option['priority'], $option['label'], (bool) $option['default']); } elseif (!$option->getId()) { $optionSet = $option; $optionSet->setField($configModel); } else { $optionSet = $option; } if ($optionSet->getLabel() != null) { $newOptions[] = $optionSet->getId(); } if (!in_array($optionSet, $optionValues) && $optionSet->getLabel() != null) { $em->persist($optionSet); } } $delOptions = array_diff($oldOptions, $newOptions); foreach ($delOptions as $key => $delOption) { $em->remove($configModel->getOptions()->getValues()[$key]); } $em->flush(); } }
/** * Load entity configs from annotations to a database * * @param bool $force Force overwrite config's option values * @param callable|null $filter function (ClassMetadataInfo[] $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 { /** @var ClassMetadataInfo[] $doctrineAllMetadata */ $doctrineAllMetadata = $this->configManager->getEntityManager()->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->clearCache(); $this->configManager->clearConfigurableCache(); } } catch (\Exception $ex) { $this->logger = null; throw $ex; } }
/** * Generate doctrine proxy classes for extended entities */ public function generateProxies() { $em = $this->configManager->getEntityManager(); $isAutoGenerated = $em->getConfiguration()->getAutoGenerateProxyClasses(); if (!$isAutoGenerated) { $extendConfigProvider = $this->configManager->getProvider('extend'); $entityConfigs = $extendConfigProvider->getConfigs(null, true); foreach ($entityConfigs as $entityConfig) { if (!$entityConfig->is('is_extend')) { continue; } if ($entityConfig->in('state', [ExtendScope::STATE_NEW])) { continue; } $proxyFileName = $em->getConfiguration()->getProxyDir() . DIRECTORY_SEPARATOR . '__CG__' . str_replace('\\', '', $entityConfig->getId()->getClassName()) . '.php'; if (!file_exists($proxyFileName)) { $proxyFactory = $em->getProxyFactory(); $proxyDir = $em->getConfiguration()->getProxyDir(); $meta = $em->getClassMetadata($entityConfig->getId()->getClassName()); $proxyFactory->generateProxyClasses([$meta], $proxyDir); } } } }
/** * @param $entityName * @param $entityConfig * @return void */ protected function createEntityModel($entityName, $entityConfig) { $mode = isset($entityConfig['mode']) ? $entityConfig['mode'] : ConfigModelManager::MODE_DEFAULT; $this->configManager->createConfigEntityModel($entityName, $mode); if (class_exists($entityName)) { $doctrineMetadata = $this->configManager->getEntityManager()->getClassMetadata($entityName); foreach ($doctrineMetadata->getFieldNames() as $fieldName) { $type = $doctrineMetadata->getTypeOfField($fieldName); $this->configManager->createConfigFieldModel($doctrineMetadata->getName(), $fieldName, $type); } foreach ($doctrineMetadata->getAssociationNames() as $fieldName) { $type = $doctrineMetadata->isSingleValuedAssociation($fieldName) ? 'ref-one' : 'ref-many'; $this->configManager->createConfigFieldModel($doctrineMetadata->getName(), $fieldName, $type); } } }
/** * Gets the full class name for the given entity name * * @param string $entityName * * @return string */ protected function prepareEntityName($entityName) { return $this->configManager->getEntityManager()->getClassMetadata($entityName)->getName(); }