Author: Benjamin Eberlei (kontakt@beberlei.de)
Example #1
0
 /**
  * @param ModuleEvent $e
  */
 public function onPostLoadModules(ModuleEvent $e)
 {
     $configListener = $e->getConfigListener();
     $config = $configListener->getMergedConfig(false);
     if (isset($config['doctrine']['configuration']['orm_default']['proxy_dir']) && isset($config['doctrine']['configuration']['orm_default']['proxy_namespace'])) {
         // We need to register here manualy. Please see http://www.doctrine-project.org/jira/browse/DDC-1698
         ProxyAutoloader::register($config['doctrine']['configuration']['orm_default']['proxy_dir'], $config['doctrine']['configuration']['orm_default']['proxy_namespace']);
     }
 }
 /**
  * Registers and returns autoloader callback for the given proxy dir and namespace.
  *
  * @param string        $proxyDir
  * @param string        $proxyNamespace
  * @param callable|null $notFoundCallback Invoked when the proxy file is not found.
  *
  * @return \Closure
  *
  * @throws InvalidArgumentException
  */
 public static function register($proxyDir, $proxyNamespace, $notFoundCallback = null)
 {
     $proxyNamespace = ltrim($proxyNamespace, '\\');
     if (!(null === $notFoundCallback || is_callable($notFoundCallback))) {
         throw InvalidArgumentException::invalidClassNotFoundCallback($notFoundCallback);
     }
     $autoloader = function ($className) use($proxyDir, $proxyNamespace, $notFoundCallback) {
         if (0 === strpos($className, $proxyNamespace)) {
             $file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className);
             if ($notFoundCallback && !file_exists($file)) {
                 call_user_func($notFoundCallback, $proxyDir, $proxyNamespace, $className);
             }
             require $file;
         }
     };
     spl_autoload_register($autoloader);
     return $autoloader;
 }
 public function loadDoctrineConfiguration(Application $app)
 {
     $app['db.orm.config'] = $app->share(function () use($app) {
         $cache = $app['db.orm.cache'];
         $config = new ORMConfiguration();
         $config->setMetadataCacheImpl($cache);
         $config->setQueryCacheImpl($cache);
         $chain = new MappingDriverChain();
         foreach ((array) $app['db.orm.entities'] as $entity) {
             switch ($entity['type']) {
                 case 'default':
                 case 'annotation':
                     $driver = $config->newDefaultAnnotationDriver((array) $entity['path'], false);
                     $chain->addDriver($driver, $entity['namespace']);
                     break;
                 case 'yml':
                     $driver = new YamlDriver((array) $entity['path']);
                     $driver->setFileExtension('.yml');
                     $chain->addDriver($driver, $entity['namespace']);
                     break;
                 case 'xml':
                     $driver = new XmlDriver((array) $entity['path'], $entity['namespace']);
                     $driver->setFileExtension('.xml');
                     $chain->addDriver($driver, $entity['namespace']);
                     break;
                 default:
                     throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type']));
                     break;
             }
         }
         $config->setMetadataDriverImpl($chain);
         $config->setProxyDir($app['db.orm.proxies_dir']);
         $config->setProxyNamespace($app['db.orm.proxies_namespace']);
         $config->setAutoGenerateProxyClasses($app['db.orm.auto_generate_proxies']);
         $config->addCustomHydrationMode('SimpleArrayHydrator', 'App\\Provider\\SimpleArrayHydrator');
         // автолоад прокси
         $proxyDir = $app['db.orm.proxies_dir'];
         $proxyNamespace = $app['db.orm.proxies_namespace'];
         Autoloader::register($proxyDir, $proxyNamespace);
         return $config;
     });
 }
 public function testRegisterWithInvalidCallback()
 {
     $this->setExpectedException('Doctrine\\Common\\Proxy\\Exception\\InvalidArgumentException', 'Invalid \\$notFoundCallback given: must be a callable, "stdClass" given');
     Autoloader::register('', '', new \stdClass());
 }
 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);
     }
 }