public static function entityManager($name = 'default')
    {
        if (isset(self::$entityManagers[$name])) {
            return self::$entityManagers[$name];
        }
        $databaseConnection = self::connection($name)->getDbalConnection();

        if (Configuration::get('app.env') == 'development') {
            $cache = new ArrayCache();
        } else { // @codeCoverageIgnore
            $cache = new FilesystemCache(Configuration::get('app.cache')); // @codeCoverageIgnore
        }

        $config = new \Doctrine\ORM\Configuration();
        $config->setMetadataCacheImpl($cache);
        $driver = $config->newDefaultAnnotationDriver([APPPATH . 'Entity'], false);

        $config->setMetadataDriverImpl($driver);
        $config->setQueryCacheImpl($cache);

        $proxyDir = APPPATH . 'Proxy';
        $proxyNamespace = 'App\Proxy';

        $config->setProxyDir($proxyDir);
        $config->setProxyNamespace($proxyNamespace);
        Autoloader::register($proxyDir, $proxyNamespace);

        $loggerChain = new LoggerChain();
        if (Configuration::get('app.env') == 'development') {
            $config->setAutoGenerateProxyClasses(true);

            $loggerChain->addLogger(new DoctrineLogBridge(\Logger::getInstance()->getMonologInstance()));
            $loggerChain->addLogger(DebugBarHelper::getInstance()->getDebugStack());
        } else { // @codeCoverageIgnore
            $config->setAutoGenerateProxyClasses(false); // @codeCoverageIgnore
        }
        $em = EntityManager::create($databaseConnection, $config);

        $em->getConnection()->getConfiguration()->setSQLLogger($loggerChain);
        $em->getConfiguration()->setSQLLogger($loggerChain);

        self::$entityManagers[$name] = $em;
        return $em;
    }