Ejemplo n.º 1
0
 public function setUp()
 {
     $config = new Configuration();
     $config->setLoggerCallable(function ($msg) {
     });
     $this->conn = new Connection(null, array(), $config);
 }
Ejemplo n.º 2
0
 protected function createRepository()
 {
     $config = new Configuration();
     $config->setLoggerCallable(function ($msg) {
     });
     $this->connection = new Connection(null, array(), $config);
     $db = $this->connection->selectDatabase(self::$dbName);
     $coll = $db->createCollection('test');
     return new MongoDBRepository($coll);
 }
Ejemplo n.º 3
0
 public function testConnectionRetries()
 {
     $config = new Configuration();
     $config->setRetryConnect(1);
     $config->setRetryQuery(1);
     $conn = new ConnectionStub(null, array(), $config);
     $exception = new \MongoException('Test');
     try {
         $conn->testRetries($exception);
         $this->fail();
     } catch (\Exception $e) {
     }
     $this->assertSame($e, $exception);
     $this->assertEquals(2, $conn->numTimesTried);
 }
Ejemplo n.º 4
0
 /**
  * Conditionally retry a closure if it yields an exception.
  *
  * If the closure does not return successfully within the configured number
  * of retries, its first exception will be thrown.
  *
  * @param \Closure $retry
  * @return mixed
  */
 protected function retry(\Closure $retry)
 {
     $numRetries = $this->config->getRetryConnect();
     if ($numRetries < 1) {
         return $retry();
     }
     $firstException = null;
     for ($i = 0; $i <= $numRetries; $i++) {
         try {
             return $retry();
         } catch (\MongoException $e) {
             if ($firstException === null) {
                 $firstException = $e;
             }
             if ($i === $numRetries) {
                 throw $firstException;
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function register(Application $app)
 {
     // if true debug queries , regenerate proxies and hydrators.
     $app['odm.debug'] = function ($app) {
         return $app['debug'];
     };
     // connection config
     $app['odm.connection.configuration'] = $app->share(function ($app) {
         $config = new ConnectionConfiguration();
         if ($app['odm.debug'] === TRUE && isset($app['logger']) && isset($app['odm.connection.logger'])) {
             $config->setLoggerCallable($app['odm.connection.log_function']);
         }
         return $config;
     });
     // log function
     $app['odm.connection.log_function'] = $app->protect(function ($msg) use($app) {
         return $app['odm.connection.logger']->logQuery($msg);
     });
     //if (class_exists('Doctrine\Bundle\MongoDBBundle\Logger\Logger')) {
     //logger
     $app['odm.connection.logger'] = $app->share(function ($app) {
         return new Logger($app['logger']);
     });
     //}
     // server connection string
     $app['odm.connection.server'] = "";
     // mongo client connection options
     $app['odm.connection.options'] = array('connect' => TRUE);
     // mongo client  connection
     $app['odm.connection'] = $app->share(function ($app) {
         $conn = new Connection($app["odm.connection.server"], $app['odm.connection.options'], $app['odm.connection.configuration']);
         return $conn;
     });
     // multiple document managers
     $app['odm.manager_registry'] = $app->share(function ($app) {
         return new ManagerRegistry("manager_registry", array('default' => $app['odm.connection']), array('default' => $app['odm.dm']), "default", "default");
     });
     // default document manager
     $app['odm.dm'] = $app->share(function ($app) {
         foreach ($app['odm.driver.configs'] as $name => $options) {
             $driver = DoctrineODMMongoDBServiceProvider::getMetadataDriver($options['type'], $options['path']);
             $app['odm.chain_driver']->addDriver($driver, $options['namespace']);
             if ($name === "default") {
                 $app['odm.chain_driver']->setDefaultDriver($driver);
             }
         }
         $app['odm.config']->setMetadataDriverImpl($app['odm.chain_driver']);
         $dm = DocumentManager::create($app['odm.connection'], $app['odm.config']);
         # fix the proxy serialization bug @TODO fix it
         $proxies = glob($app['odm.proxy_dir'] . "/__CG__*.php");
         foreach ($proxies as $proxy) {
             require $proxy;
         }
         /* @var DocumentManager $dm */
         return $dm;
     });
     // proxy classes dir
     $app['odm.proxy_dir'] = function () {
         return sys_get_temp_dir();
     };
     // proxy classes namespace
     $app['odm.proxy_namespace'] = 'Proxies';
     // hydrators dir
     $app['odm.hydrator_dir'] = function () {
         return sys_get_temp_dir();
     };
     // hydrator namespace
     $app['odm.hydrator_namespace'] = 'Hydrators';
     // main document manager driver
     $app['odm.chain_driver'] = $app->share(function ($app) {
         return new MappingDriverChain();
     });
     // driver configs
     $app['odm.driver.configs'] = array();
     // main manager config
     $app['odm.config'] = $app->share(function ($app) {
         $config = new Configuration();
         $config->setProxyDir($app['odm.proxy_dir']);
         $config->setProxyNamespace($app['odm.proxy_namespace']);
         $config->setHydratorDir($app['odm.hydrator_dir']);
         $config->setHydratorNamespace($app['odm.hydrator_namespace']);
         $config->setAutoGenerateHydratorClasses($app['odm.debug']);
         $config->setAutoGenerateProxyClasses($app['odm.debug']);
         $config->setDefaultDB($app['odm.connection.dbname']);
         return $config;
     });
     // if you want to manage mongo db through the $app[console] , execute this function
     $app['odm.boot_commands'] = $app->protect(function () use($app) {
         $app["console"]->getHelperSet()->set(new DocumentManagerHelper($app["odm.dm"]));
         $app['console']->add(new InfoDoctrineODMCommand());
         $app["console"]->add(new MongoCommand\QueryCommand());
         $app["console"]->add(new MongoCommand\GenerateDocumentsCommand());
         $app["console"]->add(new MongoCommand\GenerateHydratorsCommand());
         $app["console"]->add(new MongoCommand\GenerateProxiesCommand());
         $app["console"]->add(new MongoCommand\GenerateRepositoriesCommand());
         $app["console"]->add(new MongoCommand\ClearCache\MetadataCommand());
         $app["console"]->add(new MongoCommand\Schema\CreateCommand());
         $app["console"]->add(new MongoCommand\Schema\DropCommand());
         $app["console"]->add(new MongoCommand\Schema\UpdateCommand());
     });
     // unique document validator service
     $app['doctrine_odm.mongodb.unique'] = $app->share(function ($app) {
         return new UniqueEntityValidator($app['odm.manager_registry']);
     });
 }