Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Noxette\Doctrine\ODM;
- use Nette\DI,
- Noxette,
- Kdyby\Doctrine\Cache,
- Doctrine,
- Doctrine\ODM,
- Doctrine\MongoDB,
- Doctrine\ODM\MongoDB\Mapping\Types\Type,
- Doctrine\Common\Annotations\AnnotationRegistry,
- Doctrine\Common\Annotations\AnnotationReader,
- Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
- final class Container extends Noxette\Doctrine\BaseContainer {
- const NAME = 'Noxette';
- /** @var types */
- private static $types = array(
- 'callback' => 'Noxette\Doctrine\ODM\Types\Callback',
- 'value' => 'Noxette\Doctrine\ODM\Types\Value'
- );
- private static $passed = false;
- public function __construct(DI\Container $context)
- {
- $this->addService('context', $context);
- if( self::$passed === true )
- return;
- self::registerAutoloader();
- if( ! empty(self::$types) )
- foreach(self::$types as $type => $className)
- if( ! Type::hasType($type) )
- Type::addType($type, $className);
- self::$passed = true;
- }
- /**
- * ->entityManager initiates call to createServiceEntityManager
- *
- * @return ORM\EntityManager
- */
- public function getDocumentManager()
- {
- return $this->documentManager;
- }
- /* ---------------- SERVICES ---------------------------------------- */
- /**
- * @return ODM\MongoDB\DocumentManager
- */
- public function createServiceDocumentManager()
- {
- $this->freeze();
- $mongo = new \Mongo();
- $connection = new MongoDB\Connection($mongo, array(), $this->configuration);
- return ODM\MongoDB\DocumentManager::create($connection, $this->configuration, $this->eventManager);
- }
- /**
- * @return Doctrine\Common\EventManager
- */
- protected function createServiceEventManager()
- {
- $eventManager = new Doctrine\Common\EventManager;
- return $eventManager;
- }
- /** @var Doctrine\ODM\MongoDB\Configuration */
- private $config;
- /**
- * @return Doctrine\ODM\MongoDB\Configuration
- */
- protected function createServiceConfiguration()
- {
- $config = new Doctrine\ODM\MongoDB\Configuration();
- $this->config = $config;
- // Database name
- $config->setDefaultDB($this->context->params['database']['dbname']);
- // Cache
- $config->setMetadataCacheImpl($this->cache);
- // Metadata
- $config->setMetadataDriverImpl($this->annotationDriver);
- // Hydrators
- $config->setHydratorDir($this->context->params['doctrine']['hydratorsDir']);
- $config->setHydratorNamespace('Hydrators');
- // Proxies
- $config->setProxyDir($this->context->params['proxyDir']);
- $config->setProxyNamespace($this->context->params['doctrine']['proxyNamespace']);
- if ($this->context->params['productionMode']) {
- $config->setAutoGenerateProxyClasses(false);
- $config->setAutoGenerateHydratorClasses(false);
- } else {
- $config->setAutoGenerateProxyClasses(true);
- $config->setAutoGenerateHydratorClasses(true);
- $config->setLoggerCallable(function($log) {
- if( \Nette\Diagnostics\Debugger::$maxDepth == 4 )
- \Nette\Diagnostics\Debugger::dump($log);
- });
- }
- return $config;
- }
- /**
- * @return Doctrine\ORM\Mapping\Driver\AnnotationDriver
- */
- protected function createServiceAnnotationDriver()
- {
- $driver = new AnnotationDriver($this->annotationReader, array($this->context->params['doctrine']['entityDir']));
- $chainDriverImpl = new ODM\MongoDB\Mapping\Driver\DriverChain();
- $chainDriverImpl->addDriver($driver, self::NAME);
- $chainDriverImpl->addDriver($driver, $this->context->params['projectName'] );
- return $chainDriverImpl;
- }
- /**
- * @return Doctrine\Common\Annotations\Reader
- */
- protected function createServiceAnnotationReader()
- {
- // Register the ODM Annotations in the AnnotationRegistry
- AnnotationRegistry::registerFile(LIBS_DIR. '/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php');
- AnnotationRegistry::registerFile(__DIR__. '/../Annotations.php');
- $reader = new AnnotationReader();
- $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\Annotations\\');
- $reader->setIgnoreNotImportedAnnotations(true);
- $reader->setEnableParsePhpImports(false);
- $reader = new \Doctrine\Common\Annotations\CachedReader(
- new \Doctrine\Common\Annotations\IndexedReader($reader), $this->cache
- );
- return $reader;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement