Exemplo n.º 1
0
 /**
  * Expected to return \Zend\ServiceManager\Config object or array to
  * seed such an object.
  *
  * @return array|\Zend\ServiceManager\Config
  */
 public function getServiceConfig()
 {
     return array('factories' => array('Skar\\Mandango\\FilesystemCache' => function ($sm) {
         return new FilesystemCache('data/cache/mandango');
     }, 'mandango' => function ($sm) {
         // Config
         $config = $sm->get('Config');
         $configMandango = $config['skar']['mandango'];
         // Metadata factory
         $metadataFactory = new $configMandango['metadata_factory_class']();
         // Set caching
         $cache = $sm->get($configMandango['cache']);
         // Init Mandango
         $mandango = new Mandango($metadataFactory, $cache);
         // Set connections
         $connections = array();
         foreach ($configMandango['connections'] as $name => $connection) {
             $connections[$name] = new Connection($connection['server'], $connection['database']);
         }
         $mandango->setConnections($connections);
         // Set default connection
         $mandango->setDefaultConnectionName($configMandango['default_connection']);
         return $mandango;
     }));
 }
Exemplo n.º 2
0
 public function register(Application $app)
 {
     $configuration = $app['mandango.configuration'];
     if (!isset($configuration['metadata_factory_class'])) {
         throw new \Exception("Missing param 'metadata_factory_class' in 'mandango.configuration'");
     }
     if (!isset($configuration['metadata_factory_output'])) {
         throw new \Exception("Missing param 'metadata_factory_output' in 'mandango.configuration'");
     }
     if (!isset($configuration['default_output'])) {
         throw new \Exception("Missing param 'default_output' in 'mandango.configuration'");
     }
     if (!isset($configuration['schema_file'])) {
         throw new \Exception("Missing param 'schema_file' in 'mandango.configuration'");
     }
     if (!isset($app['mandango.token'])) {
         throw new \Exception("Missing param 'mandango.token'");
     }
     $app['mandango'] = $app->share(function () use($app) {
         $connections = isset($app['mandango.connections']) ? $app['mandango.connections'] : array('local' => array('host' => 'mongodb://localhost:27017', 'database' => 'test'));
         $default = isset($app['mandango.default_connection']) ? $app['mandango.default_connection'] : 'local';
         $logging = isset($app['mandango.logging']) ? (bool) $app['mandango.logging'] : false;
         $cache_dir = isset($app['mandango.cache_dir']) ? $app['mandango.cache_dir'] : sys_get_tempd_dir();
         $metadata = new $app['mandango.configuration']['metadata_factory_class']();
         $mandango = new Mandango($metadata, new FilesystemCache($cache_dir));
         foreach ($connections as $name => $config) {
             $connections[$name] = new Connection($config['host'], $config['database']);
         }
         $mandango->setConnections($connections);
         $mandango->setDefaultConnectionName($default);
         return $mandango;
     });
     $app['mandango.mondator'] = $app->share(function () use($app, $configuration) {
         $mondator = new Mondator();
         $mondator->setConfigClasses(require $configuration['schema_file']);
         $mondator->setExtensions(array(new \Mandango\Extension\Core(array('metadata_factory_class' => $configuration['metadata_factory_class'], 'metadata_factory_output' => $configuration['metadata_factory_output'], 'default_output' => $configuration['default_output']))));
         return $mondator;
     });
     $app->get('/_mandango/generate/{token}', function ($token) use($app) {
         if ($token !== $app['mandango.token']) {
             return 'Access Denied!';
         }
         $app['mandango.mondator']->process();
         return 'Successfull generated models';
     });
     // autoloading Mandango library
     if (isset($app['mandango.class_path'])) {
         $app['autoloader']->registerNamespace('Mandango', $app['mandango.class_path']);
         $app['autoloader']->registerNamespace('Mandango\\Mondator', $app['mandango.class_path'] . '/../vendor/mondator/src');
     }
     if (isset($configuration['default_output'])) {
         $outputDir = rtrim($configuration['default_output'], DIRECTORY_SEPARATOR);
         $outputDir = dirname($outputDir);
         $app['autoloader']->registerNamespace(isset($configuration['model_namespace']) ? $configuration['model_namespace'] : 'Model', $outputDir);
     }
 }
Exemplo n.º 3
0
 public function testDefaultConnectionName()
 {
     $mandango = new Mandango($this->metadataFactory, $this->cache);
     $this->assertNull($mandango->getDefaultConnectionName());
     $mandango->setDefaultConnectionName('mandango_connection');
     $this->assertSame('mandango_connection', $mandango->getDefaultConnectionName());
 }
Exemplo n.º 4
0
 public function testCollection()
 {
     $mandango = new Mandango($this->metadataFactory, $this->cache);
     $connection = new Connection($this->uri, $this->dbName . '_collection');
     $mandango->setConnection('default', $connection);
     $mandango->setDefaultConnectionName('default');
     $collection = $mandango->getRepository('Model\\Article')->getCollection();
     $this->assertEquals($connection->getDatabase()->selectCollection('articles'), $collection);
     $this->assertSame($collection, $mandango->getRepository('Model\\Article')->getCollection());
 }