Esempio n. 1
0
 /**
  * @param      $scope
  * @param      $className
  * @param null $fieldName
  * @return ConfigIdInterface
  * @throws LogicException if a database is not synced
  * @throws RuntimeException if a model was not found
  */
 public function getId($scope, $className, $fieldName = null)
 {
     if ($fieldName) {
         // at first try to find a model in the local cache
         $configId = new FieldConfigId($scope, $className, $fieldName);
         $configKey = $this->buildConfigKey($configId);
         if (isset($this->localCache[$configKey])) {
             // if found, use field config id from the local cache
             $configId = $this->localCache[$configKey]->getId();
         } else {
             // next try to find a model in the cache
             $config = null !== $this->cache ? $this->cache->loadConfigFromCache($configId) : null;
             if ($config) {
                 // if found, use field config id from the cache
                 $configId = $config->getId();
                 $this->localCache[$configKey] = $config;
             } else {
                 // if a cached model was not found use the model manager to get field config id
                 if (!$this->modelManager->checkDatabase()) {
                     throw $this->createDatabaseNotSyncedException();
                 }
                 $fieldModel = $this->modelManager->getFieldModel($className, $fieldName);
                 $configId = new FieldConfigId($scope, $className, $fieldModel->getFieldName(), $fieldModel->getType());
             }
         }
         return $configId;
     } else {
         return new EntityConfigId($scope, $className);
     }
 }
 /**
  * @param ConfigIdInterface $configId
  * @throws RuntimeException
  * @throws LogicException
  * @return ConfigInterface
  */
 public function getConfig(ConfigIdInterface $configId)
 {
     if ($this->localCache->containsKey($configId->toString())) {
         return $this->localCache->get($configId->toString());
     }
     if (!$this->modelManager->checkDatabase()) {
         throw new LogicException('Database is not synced, if you use ConfigManager, when a db schema may be hasn\'t synced.' . ' check it by ConfigManager::modelManager::checkDatabase');
     }
     if (!$this->hasConfig($configId->getClassName())) {
         throw new RuntimeException(sprintf('Entity "%s" is not configurable', $configId->getClassName()));
     }
     $resultConfig = null !== $this->cache ? $this->cache->loadConfigFromCache($configId) : null;
     if (!$resultConfig) {
         $model = $this->modelManager->getModelByConfigId($configId);
         $config = new Config($this->getConfigIdByModel($model, $configId->getScope()));
         $config->setValues($model->toArray($configId->getScope()));
         if (null !== $this->cache) {
             $this->cache->putConfigInCache($config);
         }
         $resultConfig = $config;
     }
     //local cache
     $this->localCache->set($resultConfig->getId()->toString(), $resultConfig);
     //for calculate change set
     $this->originalConfigs->set($resultConfig->getId()->toString(), clone $resultConfig);
     return $resultConfig;
 }
 public function testCache()
 {
     $cacheProvider = $this->getMockBuilder('Doctrine\\Common\\Cache\\CacheProvider')->disableOriginalConstructor()->setMethods(array('fetch', 'save', 'delete', 'deleteAll'))->getMockForAbstractClass();
     $modelCacheProvider = $this->getMockBuilder('Doctrine\\Common\\Cache\\CacheProvider')->disableOriginalConstructor()->setMethods(array('fetch', 'save', 'delete', 'deleteAll'))->getMockForAbstractClass();
     $className = 'testClass';
     $scope = 'testScope';
     $configId = new EntityConfigId($className, $scope);
     $config = new Config($configId);
     $configCache = new ConfigCache($cacheProvider, $modelCacheProvider);
     $cacheProvider->expects($this->once())->method('save')->will($this->returnValue(true));
     $this->assertTrue($configCache->putConfigInCache($config));
     $cacheProvider->expects($this->once())->method('delete')->will($this->returnValue(true));
     $this->assertTrue($configCache->removeConfigFromCache($configId));
     $cacheProvider->expects($this->once())->method('deleteAll')->will($this->returnValue(true));
     $this->assertTrue($configCache->removeAll());
     $cacheProvider->expects($this->once())->method('fetch')->will($this->returnValue(serialize($config)));
     $this->assertEquals($config, $configCache->loadConfigFromCache($configId));
     $value = 'testValue';
     $modelCacheProvider->expects($this->once())->method('save')->will($this->returnValue(true));
     $this->assertTrue($configCache->setConfigurable($value, $className));
     $modelCacheProvider->expects($this->once())->method('fetch')->will($this->returnValue($value));
     $this->assertEquals($value, $configCache->getConfigurable($className));
     $modelCacheProvider->expects($this->once())->method('deleteAll')->will($this->returnValue(true));
     $this->assertTrue($configCache->removeAllConfigurable());
 }
Esempio n. 4
0
 public function testLoadConfigFromCache()
 {
     $className = 'testClass';
     $scope = 'testScope';
     $configId = new EntityConfigId($scope, $className);
     $config = new Config($configId);
     $configCache = new ConfigCache($this->cacheProvider, $this->modelCacheProvider);
     $this->cacheProvider->expects($this->once())->method('fetch')->will($this->returnValue($config));
     $this->assertEquals($config, $configCache->loadConfigFromCache($configId));
 }