Advertisement
Guest User

Untitled

a guest
Sep 5th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.29 KB | None | 0 0
  1. <?php
  2. namespace Noxette\Doctrine\ODM;
  3.  
  4. use Nette\DI,
  5.     Noxette,
  6.     Kdyby\Doctrine\Cache,
  7.     Doctrine,
  8.     Doctrine\ODM,
  9.     Doctrine\MongoDB,
  10.     Doctrine\ODM\MongoDB\Mapping\Types\Type,
  11.     Doctrine\Common\Annotations\AnnotationRegistry,
  12.     Doctrine\Common\Annotations\AnnotationReader,
  13.     Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
  14.  
  15. final class Container extends Noxette\Doctrine\BaseContainer {
  16.    
  17.     const NAME = 'Noxette';
  18.    
  19.     /** @var types */
  20.     private static $types = array(
  21.         'callback' => 'Noxette\Doctrine\ODM\Types\Callback',
  22.         'value' => 'Noxette\Doctrine\ODM\Types\Value'
  23.     );
  24.    
  25.     private static $passed = false;
  26.    
  27.    
  28.     public function __construct(DI\Container $context)
  29.     {
  30.         $this->addService('context', $context);
  31.        
  32.         if( self::$passed === true )
  33.             return;
  34.        
  35.         self::registerAutoloader();
  36.        
  37.         if( ! empty(self::$types) )
  38.             foreach(self::$types as $type => $className)
  39.                 if( ! Type::hasType($type) )
  40.                     Type::addType($type, $className);
  41.                
  42.         self::$passed = true;
  43.     }
  44.    
  45.    
  46.    
  47.     /**
  48.      * ->entityManager initiates call to createServiceEntityManager
  49.      *
  50.      * @return ORM\EntityManager
  51.      */
  52.     public function getDocumentManager()
  53.     {
  54.         return $this->documentManager;
  55.     }
  56.    
  57.    
  58.    
  59.     /* ---------------- SERVICES ---------------------------------------- */
  60.    
  61.     /**
  62.      * @return ODM\MongoDB\DocumentManager
  63.      */
  64.     public function createServiceDocumentManager()
  65.     {
  66.         $this->freeze();
  67.        
  68.         $mongo = new \Mongo();
  69.         $connection = new MongoDB\Connection($mongo, array(), $this->configuration);
  70.        
  71.         return ODM\MongoDB\DocumentManager::create($connection, $this->configuration, $this->eventManager);
  72.     }
  73.    
  74.    
  75.    
  76.     /**
  77.      * @return Doctrine\Common\EventManager
  78.      */
  79.     protected function createServiceEventManager()
  80.     {
  81.         $eventManager = new Doctrine\Common\EventManager;
  82.        
  83.         return $eventManager;
  84.     }
  85.    
  86.     /** @var Doctrine\ODM\MongoDB\Configuration */
  87.     private $config;
  88.    
  89.     /**
  90.      * @return Doctrine\ODM\MongoDB\Configuration
  91.      */
  92.     protected function createServiceConfiguration()
  93.     {
  94.         $config = new Doctrine\ODM\MongoDB\Configuration();
  95.         $this->config = $config;
  96.  
  97.         // Database name
  98.         $config->setDefaultDB($this->context->params['database']['dbname']);
  99.        
  100.         // Cache
  101.         $config->setMetadataCacheImpl($this->cache);
  102.  
  103.         // Metadata
  104.         $config->setMetadataDriverImpl($this->annotationDriver);
  105.        
  106.         // Hydrators
  107.         $config->setHydratorDir($this->context->params['doctrine']['hydratorsDir']);
  108.         $config->setHydratorNamespace('Hydrators');
  109.  
  110.         // Proxies
  111.         $config->setProxyDir($this->context->params['proxyDir']);      
  112.         $config->setProxyNamespace($this->context->params['doctrine']['proxyNamespace']);
  113.         if ($this->context->params['productionMode']) {
  114.             $config->setAutoGenerateProxyClasses(false);
  115.             $config->setAutoGenerateHydratorClasses(false);
  116.         } else {
  117.             $config->setAutoGenerateProxyClasses(true);
  118.             $config->setAutoGenerateHydratorClasses(true);
  119.             $config->setLoggerCallable(function($log) {            
  120.                 if( \Nette\Diagnostics\Debugger::$maxDepth == 4 )
  121.                     \Nette\Diagnostics\Debugger::dump($log);
  122.             });
  123.         }
  124.  
  125.         return $config;
  126.     }
  127.    
  128.    
  129.    
  130.     /**
  131.      * @return Doctrine\ORM\Mapping\Driver\AnnotationDriver
  132.      */
  133.     protected function createServiceAnnotationDriver()
  134.     {
  135.         $driver =  new AnnotationDriver($this->annotationReader, array($this->context->params['doctrine']['entityDir']));
  136.        
  137.         $chainDriverImpl = new ODM\MongoDB\Mapping\Driver\DriverChain();
  138.         $chainDriverImpl->addDriver($driver, self::NAME);
  139.         $chainDriverImpl->addDriver($driver, $this->context->params['projectName'] );
  140.        
  141.         return $chainDriverImpl;
  142.     }
  143.    
  144.    
  145.    
  146.     /**
  147.      * @return Doctrine\Common\Annotations\Reader
  148.      */
  149.     protected function createServiceAnnotationReader()
  150.     {
  151.         // Register the ODM Annotations in the AnnotationRegistry
  152.         AnnotationRegistry::registerFile(LIBS_DIR. '/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php');
  153.         AnnotationRegistry::registerFile(__DIR__. '/../Annotations.php');
  154.        
  155.         $reader = new AnnotationReader();
  156.         $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\Annotations\\');
  157.         $reader->setIgnoreNotImportedAnnotations(true);
  158.         $reader->setEnableParsePhpImports(false);
  159.         $reader = new \Doctrine\Common\Annotations\CachedReader(
  160.             new \Doctrine\Common\Annotations\IndexedReader($reader), $this->cache
  161.         );
  162.        
  163.         return $reader;
  164.     }
  165.    
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement