public function register() { // Make both managers singletons $this->app->singleton('Concrete\\Core\\Database\\DatabaseManager'); $this->app->singleton('Concrete\\Core\\Database\\DatabaseManagerORM'); // Bind both `database` and `database/orm` to their respective classes $this->app->bind('database', 'Concrete\\Core\\Database\\DatabaseManager'); $this->app->bind('database/orm', 'Concrete\\Core\\Database\\DatabaseManagerORM'); // Bind a constructor for our DriverManager bootstrapped from config $this->app->bind('Concrete\\Core\\Database\\Driver\\DriverManager', function ($app) { $manager = new DriverManager($app); $manager->configExtensions($app->make('config')->get('database.drivers')); return $manager; }); // Bind a closure to support \Core::make('database/structure', $em); $this->app->bind('database/structure', function ($app, $em) { if (!is_array($em)) { $em = array($em); } return $app->make('Concrete\\Core\\Database\\DatabaseStructureManager', $em); }); // Bind default entity manager resolver $this->app->bind('Doctrine\\ORM\\EntityManagerInterface', function ($app) { return $app->make('Concrete\\Core\\Database\\DatabaseManagerORM')->entityManager(); }); $this->app->bind('Doctrine\\ORM\\EntityManager', 'Doctrine\\ORM\\EntityManagerInterface'); // Bind default connection resolver $this->app->bind('Concrete\\Core\\Database\\Connection\\Connection', function ($app) { return $app->make('Concrete\\Core\\Database\\DatabaseManager')->connection(); }); $this->app->bind('Doctrine\\DBAL\\Connection', 'Concrete\\Core\\Database\\Connection\\Connection'); }
public function register() { $this->app->bind('database', function ($cms) { $driver_manager = new DriverManager($cms); $driver_manager->configExtensions(\Config::get('database.drivers')); $factory = new ConnectionFactory($cms, $driver_manager); return new DatabaseManager($cms, $factory); }); $this->app->bind('database/orm', function ($cms) { return new DatabaseManagerORM($cms); }); $this->app->bind('database/structure', function ($cms, $em) { return new DatabaseStructureManager($em); }); }
public function createConnection($config) { $driver = $this->driver_manager->driver(array_get($config, 'driver', '')); if (!$driver instanceof \Doctrine\DBAL\Driver) { $driver = $this->driver_manager->driver(); } $params = $config; $params['host'] = array_get($params, 'host', array_get($config, 'server')); $params['user'] = array_get($params, 'user', array_get($config, 'username')); $params['wrapperClass'] = array_get($config, 'wrapperClass', '\\Concrete\\Core\\Database\\Connection\\Connection'); unset($params['driver']); $wrapperClass = 'Doctrine\\DBAL\\Connection'; if (isset($params['wrapperClass'])) { if (is_subclass_of($params['wrapperClass'], $wrapperClass)) { $wrapperClass = $params['wrapperClass']; } else { throw DBALException::invalidWrapperClass($params['wrapperClass']); } } return new $wrapperClass($params, $driver); }
public function register() { // Make both managers singletons $this->app->singleton('Concrete\\Core\\Database\\DatabaseManager'); $this->app->singleton('Concrete\\Core\\Database\\DatabaseManagerORM'); // Bind both `database` and `database/orm` to their respective classes $this->app->bind('database', 'Concrete\\Core\\Database\\DatabaseManager'); $this->app->bind('database/orm', 'Concrete\\Core\\Database\\DatabaseManagerORM'); // Bind a constructor for our DriverManager bootstrapped from config $this->app->bind('Concrete\\Core\\Database\\Driver\\DriverManager', function ($app) { $manager = new DriverManager($app); $manager->configExtensions($app->make('config')->get('database.drivers')); return $manager; }); // Bind default connection resolver $this->app->bind('Concrete\\Core\\Database\\Connection\\Connection', function ($app) { return $app->make('Concrete\\Core\\Database\\DatabaseManager')->connection(); }); $this->app->bind('Doctrine\\DBAL\\Connection', 'Concrete\\Core\\Database\\Connection\\Connection'); // Bind EntityManager factory $this->app->bind('Concrete\\Core\\Database\\EntityManagerConfigFactory', function ($app) { $config = $app->make('Doctrine\\ORM\\Configuration'); $configRepository = $app->make('config'); $connection = $app->make('Doctrine\\DBAL\\Connection'); return new EntityManagerConfigFactory($app, $config, $configRepository, $connection); }); $this->app->bind('Concrete\\Core\\Database\\EntityManagerConfigFactoryInterface', 'Concrete\\Core\\Database\\EntityManagerConfigFactory'); $this->app->bind('Concrete\\Core\\Database\\EntityManagerFactory', function ($app) { $configFactory = $app->make('Concrete\\Core\\Database\\EntityManagerConfigFactory'); return new EntityManagerFactory($configFactory); }); $this->app->bind('Concrete\\Core\\Database\\EntityManagerFactoryInterface', 'Concrete\\Core\\Database\\EntityManagerFactory'); // Bind default entity manager resolver $this->app->singleton('Doctrine\\ORM\\EntityManager', function ($app) { $factory = $app->make('Concrete\\Core\\Database\\EntityManagerFactory'); $entityManager = $factory->create($app->make('Concrete\\Core\\Database\\Connection\\Connection')); return $entityManager; }); $this->app->bind('Doctrine\\ORM\\EntityManagerInterface', 'Doctrine\\ORM\\EntityManager'); // ------------------------------------------ // Bind Doctrine EntityManager setup classes $this->app->bind('Doctrine\\Common\\Cache\\ArrayCache', function () { return new \Doctrine\Common\Cache\ArrayCache(); }); $this->app->bind('Doctrine\\Common\\Annotations\\AnnotationReader', function () { return new \Doctrine\Common\Annotations\AnnotationReader(); }); $this->app->bind('Doctrine\\Common\\Annotations\\SimpleAnnotationReader', function () { return new \Doctrine\Common\Annotations\SimpleAnnotationReader(); }); $this->app->bind('Doctrine\\Common\\Persistence\\Mapping\\Driver\\MappingDriverChain', function () { return new \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain(); }); // ORM Cache $this->app->bind('orm/cache', function ($app) { // Set cache based on doctrine dev mode $isDevMode = $app->make('config')->get('concrete.cache.doctrine_dev_mode'); if ($isDevMode) { $cache = $this->app->make('Doctrine\\Common\\Cache\\ArrayCache'); } else { $cache = new \Concrete\Core\Cache\Adapter\DoctrineCacheDriver('cache/expensive'); } return $cache; }); // Bind Doctrine ORM config resolver $this->app->bind('Doctrine\\ORM\\Configuration', function ($app) { $isDevMode = $app->make('config')->get('concrete.cache.doctrine_dev_mode'); $proxyDir = $app->make('config')->get('database.proxy_classes'); $cache = $app->make('orm/cache'); $config = \Doctrine\ORM\Tools\Setup::createConfiguration($isDevMode, $proxyDir, $cache); return $config; }); // Create the annotation reader used by packages and core > c5 version 8.0.0 // Accessed by PackageService and the EntityManagerConfigFactory $this->app->bind('orm/cachedAnnotationReader', function ($app) { $annotationReader = $app->make('Doctrine\\Common\\Annotations\\AnnotationReader'); return new \Doctrine\Common\Annotations\CachedReader($annotationReader, $app->make('orm/cache')); }); // Create legacy annotation reader used package requiring concrete5 // version lower than 8.0.0 // Accessed by PackageService and the EntityManagerConfigFactory $this->app->bind('orm/cachedSimpleAnnotationReader', function ($app) { $simpleAnnotationReader = $this->app->make('Doctrine\\Common\\Annotations\\SimpleAnnotationReader'); $simpleAnnotationReader->addNamespace('Doctrine\\ORM\\Mapping'); return new \Doctrine\Common\Annotations\CachedReader($simpleAnnotationReader, $app->make('orm/cache')); }); // Setup doctrine proxy autoloader if ($this->app->bound('config')) { $proxyDir = $this->app->make('config')->get('database.proxy_classes'); $proxyNamespace = "DoctrineProxies"; \Doctrine\Common\Proxy\Autoloader::register($proxyDir, $proxyNamespace); } }
public function testConfigLoad() { $this->driverManager->configExtensions(array('test' => 'DriverManagerTest')); $this->assertInstanceOf('DriverManagerTest', $this->driverManager->driver('test')); }