$config = new \Doctrine\DBAL\Configuration();
/**
 * Test if the requested driver is available
 */
$drivers = PDO::getAvailableDrivers();
$bDriverAvailable = false;
if (!in_array($_REQUEST["driver"], $drivers)) {
    echo json_encode(array("error" => true, "message" => "The requested driver isn't installed as PHP pdo module. Please install the PDO driver for PHP."));
    exit;
}
/**
 * Check which driver we are going to use, and set the connection parameters accordingly.
 */
try {
    $onnectionOptions = Setup::setDatabaseConfigurationFromRequest();
    $connectionOptions = PartKeepr::createConnectionOptionsFromConfig();
} catch (\Exception $e) {
    echo json_encode(array("error" => true, "message" => $e->getMessage()));
    exit;
}
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionOptions, $config);
try {
    $conn->connect();
} catch (\PDOException $e) {
    $additionalMessage = getPlatformSpecificErrorMessage($_REQUEST["driver"], $e->getCode());
    echo json_encode(array("error" => true, "message" => "There was an error connecting to the database:<br/><code>" . $e->getMessage() . "</code>" . $additionalMessage));
    exit;
} catch (\Exception $e) {
    echo json_encode(array("error" => true, "message" => "An unknown error occured. The error is: <code>" . $e->getMessage() . "</code>"));
    exit;
}
Esempio n. 2
0
 /**
  * Initializes the doctrine framework and
  * sets all required configuration options.
  * 
  * @param none
  * @return nothing
  */
 public static function initializeDoctrine()
 {
     $config = new Configuration();
     $driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__));
     $config->setMetadataDriverImpl($driverImpl);
     $connectionOptions = PartKeepr::createConnectionOptionsFromConfig();
     switch (strtolower(PartKeeprConfiguration::getOption("partkeepr.cache.implementation", "default"))) {
         case "apc":
             $cache = new \Doctrine\Common\Cache\ApcCache();
             break;
         case "xcache":
             if (php_sapi_name() !== "cli") {
                 $cache = new \Doctrine\Common\Cache\XcacheCache();
             } else {
                 // For CLI SAPIs, revert to the ArrayCache as Xcache spits out strange warnings when running in CLI.
                 $cache = new \Doctrine\Common\Cache\ArrayCache();
             }
             break;
         case "memcache":
             $memcache = new \Memcache();
             $memcache->connect(PartKeeprConfiguration::getOption("partkeepr.cache.memcache.host", "localhost"), PartKeeprConfiguration::getOption("partkeepr.cache.memcache.port", "11211"));
             $cache = new \Doctrine\Common\Cache\MemcacheCache();
             $cache->setMemcache($memcache);
             break;
         case "default":
         case "auto":
             if (extension_loaded("xcache")) {
                 $cache = new \Doctrine\Common\Cache\XcacheCache();
             } else {
                 if (extension_loaded("apc")) {
                     $cache = new \Doctrine\Common\Cache\ApcCache();
                 } else {
                     $cache = new \Doctrine\Common\Cache\ArrayCache();
                 }
             }
             break;
         case "none":
             $cache = new \Doctrine\Common\Cache\ArrayCache();
             break;
     }
     $config->setMetadataCacheImpl($cache);
     $config->setQueryCacheImpl($cache);
     $config->setProxyDir(self::getRootDirectory() . '/data/proxies');
     $config->setProxyNamespace('Proxies');
     $config->setEntityNamespaces(self::getEntityClasses());
     $config->setAutoGenerateProxyClasses(false);
     if (PartKeeprConfiguration::getOption("partkeepr.database.echo_sql_log", false) === true) {
         $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger();
         $config->setSQLLogger($logger);
     }
     self::$entityManager = EntityManager::create($connectionOptions, $config);
 }