コード例 #1
0
ファイル: Query.php プロジェクト: kassko/data-mapper
 public function __construct(ObjectManager $objectManager, $objectClass)
 {
     $this->objectManager = $objectManager;
     $this->objectClass = $objectClass;
     $this->resultManager = $objectManager->getResultManager();
     $objectManager->registerEvents($objectClass);
 }
コード例 #2
0
 protected function prepare($object, ObjectKey $objectKey = null)
 {
     if (isset($object)) {
         if (null === $objectKey) {
             $this->metadata = $this->objectManager->getMetadata(get_class($object));
         } else {
             $this->metadata = $this->objectManager->getMetadata($objectKey->getKey());
         }
     }
     $this->doPrepare($object);
 }
コード例 #3
0
ファイル: HydratorTest.php プロジェクト: kassko/data-mapper
 private function createObjectManager()
 {
     $loader = new AnnotationLoader(new AnnotationReader());
     $cmFactory = (new ClassMetadataFactory())->setClassMetadataLoader($loader);
     $configuration = (new Configuration())->setDefaultClassMetadataResourceType('annotations')->setClassMetadataCacheConfig((new CacheConfiguration())->setCache(new ArrayCache()))->setResultCacheConfig((new CacheConfiguration())->setCache(new ArrayCache()));
     $cmConfigurator = new ClassMetadataFactoryConfigurator($configuration);
     $cmConfigurator->configure($cmFactory);
     $this->objectManager = ObjectManager::getInstance()->setConfiguration($configuration)->setClassMetadataFactory($cmFactory)->setMethodInvoker(new MagicMethodInvoker())->unsetCacheProfile();
 }
コード例 #4
0
 private function createObjectManager(array $settings)
 {
     $defaultResourceDir = isset($settings['mapping']['default_resource_dir']) ? $settings['mapping']['default_resource_dir'] : null;
     $defaultClassMetadataProviderMethod = isset($settings['mapping']['default_provider_method']) ? $settings['mapping']['default_provider_method'] : null;
     $classResolver = isset($settings['class_resolver']) ? $settings['class_resolver'] : null;
     $objectListenerResolver = isset($settings['object_listener_resolver']) ? $settings['object_listener_resolver'] : null;
     $loaders = [new AnnotationLoader(new AnnotationReader()), new YamlFileLoader(), new InnerYamlLoader(), new PhpFileLoader(), new InnerPhpLoader()];
     //Configuration
     $configuration = (new ConfigurationChain())->setClassMetadataCacheConfig((new CacheConfiguration())->setCache($settings['cache']['metadata']['instance'])->setLifeTime($settings['cache']['metadata']['life_time'])->setShared($settings['cache']['metadata']['is_shared']))->setResultCacheConfig((new CacheConfiguration())->setCache($settings['cache']['result']['instance'])->setLifeTime($settings['cache']['result']['life_time'])->setShared($settings['cache']['result']['is_shared']))->setDefaultClassMetadataResourceType($settings['mapping']['default_resource_type'])->setDefaultClassMetadataResourceDir($defaultResourceDir)->setDefaultClassMetadataProviderMethod($defaultClassMetadataProviderMethod);
     //Mapping
     foreach ($settings['mapping']['objects'] as $objectSettings) {
         if (isset($objectSettings['group'])) {
             $group = $objectSettings['group'];
             if (!isset($settings['mapping']['groups'][$group])) {
                 throw new LogicException(sprintf('The group "%s" is used in path mapping.objects.[%s] but not defined in mapping.groups', $group, $objectSettings));
             }
             $groupSettings = $settings['mapping']['groups'][$group];
             $parentClassMetadataResourceType = isset($groupSettings['resource_type']) ? $groupSettings['resource_type'] : null;
             $parentClassMetadataProviderMethod = isset($groupSettings['provider_method']) ? $groupSettings['provider_method'] : null;
             if (isset($groupSettings['resource_path'])) {
                 $parentClassMetadataResourcePath = trim($groupSettings['resource_path']);
             }
             if (isset($groupSettings['resource_dir'])) {
                 $classMetadataResourceDir = $groupSettings['resource_dir'];
             }
         }
         if (isset($objectSettings['resource_type'])) {
             $classMetadataResourceType = trim($objectSettings['resource_type']);
         }
         if (isset($objectSettings['resource_path'])) {
             $classMetadataResource = trim($objectSettings['resource_path']);
         } elseif (isset($objectSettings['resource_name'])) {
             $classMetadataResource = $classMetadataResourceDir . '/' . $objectSettings['resource_name'];
         }
         $mappingObjectClass = trim($objectSettings['class']);
         if (isset($classMetadataResourceType)) {
             $configuration->addClassMetadataResourceType($mappingObjectClass, $classMetadataResourceType);
         } elseif (isset($parentClassMetadataResourceType)) {
             $configuration->addClassMetadataResourceType($mappingObjectClass, $parentClassMetadataResourceType);
         }
         if (isset($classMetadataResource)) {
             $configuration->addClassMetadataResource($mappingObjectClass, $classMetadataResource);
         }
         if (isset($classMetadataProviderMethod)) {
             $configuration->addClassMetadataProviderMethod($mappingObjectClass, $classMetadataResource);
         } elseif (isset($parentClassMetadataProviderMethod)) {
             $configuration->addClassMetadataProviderMethod($mappingObjectClass, $parentClassMetadataProviderMethod);
         }
         if (isset($classMetadataDir)) {
             $configuration->addClassMetadataDir($mappingObjectClass, $classMetadataDir);
         } elseif (isset($parentClassMetadataDir)) {
             $configuration->addClassMetadataDir($mappingObjectClass, $parentClassMetadataDir);
         }
     }
     //ClassMetadataFactory
     $delegatingLoader = new DelegatingLoader(new LoaderResolver($loaders));
     $cmFactory = (new ClassMetadataFactory())->setClassMetadataLoader($delegatingLoader);
     $cmConfigurator = new ClassMetadataFactoryConfigurator($configuration);
     $cmConfigurator->configure($cmFactory);
     //Expressions
     $functionProviders = [new ExpressionFunctionProvider()];
     if (isset($settings['mapping']['expression']['function_providers'])) {
         foreach ($settings['mapping']['expression']['function_providers'] as $functionProvider) {
             $functionProviders[] = $functionProvider;
         }
     }
     $expressionLanguage = new ExpressionLanguage();
     $elConfigurator = new ExpressionLanguageConfigurator($functionProviders);
     $elConfigurator->configure($expressionLanguage);
     $expressionContext = new ExpressionContext();
     $expressionLanguageEvaluator = new ExpressionLanguageEvaluator($expressionLanguage, $expressionContext);
     //ObjectManager
     $objectManager = ObjectManager::getInstance()->setConfiguration($configuration)->setClassMetadataFactory($cmFactory);
     if (isset($classResolver)) {
         $objectManager->setClassResolver($classResolver);
     }
     if (isset($objectListenerResolver)) {
         $objectManager->setObjectListenerResolver($objectListenerResolver);
     }
     $objectManager->setExpressionLanguageEvaluator($expressionLanguageEvaluator);
     $objectManager->setExpressionContext($expressionContext);
     $objectManager->setMethodInvoker(new MagicMethodInvoker());
     return $objectManager;
 }