Skip to content

Instantly share code, notes, and snippets.

@ischenkodv
Created March 29, 2011 22:50
Show Gist options
  • Save ischenkodv/893522 to your computer and use it in GitHub Desktop.
Save ischenkodv/893522 to your computer and use it in GitHub Desktop.
pluginPaths.My_Application_Resource = "My/Application/Resource"
resources.mongodb.mappingDirs[] = APPLICATION_PATH "/../library/My/Document"
<?php
use My\Application\Container\MongoDbContainer;
/**
* Application Resource Plugin for Doctrine MongoDB and ODM
*/
class My_Application_Resource_Mongodb extends Zend_Application_Resource_ResourceAbstract
{
public function init()
{
$config = $this->getOptions();
$this->registerAutoloaders($config);
$container = new MongoDbContainer($config);
Zend_Registry::set('mongodb', $container);
return $container;
}
private function registerAutoloaders(array $config = array())
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$doctrineIncludePath = isset($config['includePath'])
? $config['includePath'] : APPLICATION_PATH . '/../library/Doctrine';
require_once $doctrineIncludePath . '/Common/ClassLoader.php';
$doctrineAutoloader = new \Doctrine\Common\ClassLoader('Doctrine');
$autoloader->pushAutoloader(array($doctrineAutoloader, 'loadClass'), 'Doctrine');
}
}
<?php
namespace My\Application\Container;
use Doctrine\Common\Annotations\AnnotationReader,
Doctrine\MongoDB\Connection,
Doctrine\ODM\MongoDB\Configuration,
Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver,
Doctrine\ODM\MongoDB\DocumentManager;
/**
* Container for Doctrine MongoDb connection and DocumentManager
*/
class MongoDbContainer
{
/**
* @var Connection
*/
private $connection;
/**
* @var DocumentManager
*/
private $documentManager;
/**
* @var Configuration
*/
private $configuration;
public function __construct(array $config = array())
{
$config = $this->prepareConfiguration($config);
$configuration = new Configuration;
$configuration->setProxyDir($config['proxy']['dir']);
$configuration->setProxyNamespace($config['proxy']['namespace']);
$configuration->setHydratorDir($config['hydrator']['dir']);
$configuration->setHydratorNamespace($config['hydrator']['namespace']);
$reader = new AnnotationReader;
$reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
$configuration->setMetadataDriverImpl(new AnnotationDriver($reader, $config['mappingDirs']));
$this->configuration = $configuration;
}
public function getConnection()
{
if (null === $this->connection) {
$this->connection = new Connection();
}
return $this->connection;
}
public function getDocumentManager()
{
if (null === $this->documentManager) {
$this->documentManager = DocumentManager::create(
$this->getConnection(),
$this->configuration
);
}
return $this->documentManager;
}
private function prepareConfiguration(array $config = array())
{
$defaultConfiguration = array(
'mappingDirs' => array(),
'proxy' => array(
'dir' => \APPLICATION_PATH . '/../data/cache/Proxy',
'namespace' => 'Proxy'
),
'hydrator' => array(
'dir' => \APPLICATION_PATH . '/../data/cache/Hydrator',
'namespace' => 'Hydrator'
)
);
return \array_replace_recursive($defaultConfiguration, $config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment