/** * 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; })); }
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); } }
/** * Insert one new record using the Entity class. */ public function execute(Mandango $mandango, $insertedEntities) { $metadata = $mandango->getMetadata($this->class); $obj = $mandango->create($this->class); foreach ($this->columnFormatters as $column => $format) { if (null !== $format) { $value = is_callable($format) ? $format($insertedEntities, $obj) : $format; if (isset($metadata['fields'][$column]) || isset($metadata['referencesOne'][$column])) { $obj->set($column, $value); } if (isset($metadata['referencesMany'][$column])) { $adder = 'add' . ucfirst($column); $obj->{$adder}($value); } } } $mandango->persist($obj); return $obj; }
/** * Hydrate $object with the provided $data. * * @param array $data * @param object $object * @return object */ public function hydrate(array $data, $object) { $fields = $this->metadata; if (isset($data['id'])) { unset($data['id']); } foreach ($data as $fieldName => &$value) { if (isset($fields['referencesMany'][$fieldName])) { $ucFieldName = ucfirst($fieldName); $setter = 'add' . $ucFieldName; $getter = 'get' . $ucFieldName; $remover = 'remove' . $ucFieldName; $refClass = $fields['referencesMany'][$fieldName]['class']; if (!method_exists($object, $setter)) { continue; } foreach ($value as $k => $v) { $value[$k] = $this->mandango->getRepository($refClass)->findOneById($v); } $object->{$remover}($object->{$getter}()->all()); $object->{$setter}($value); continue; } $setter = 'set' . ucfirst($fieldName); if (isset($fields['referencesOne'][$fieldName])) { $refClass = $fields['referencesOne'][$fieldName]['class']; $value = $this->mandango->getRepository($refClass)->findOneById($value); } if (!method_exists($object, $setter)) { continue; } $value = $this->hydrateValue($fieldName, $value); $object->{$setter}($value); } return $object; }
public function testGetRepository() { $mandango = new Mandango($this->metadataFactory, $this->cache); $articleRepository = $mandango->getRepository('Model\\Article'); $this->assertInstanceOf('Model\\ArticleRepository', $articleRepository); $this->assertSame($mandango, $articleRepository->getMandango()); $this->assertSame($articleRepository, $mandango->getRepository('Model\\Article')); $categoryRepository = $mandango->getRepository('Model\\Category'); $this->assertInstanceOf('Model\\CategoryRepository', $categoryRepository); }
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()); }