Skip to content

Commit

Permalink
Create .env file and use it to load DB settings using symfongy container
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Jan 9, 2018
1 parent 30f31e6 commit 3dfa340
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 216 deletions.
14 changes: 7 additions & 7 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
DATABASE_HOST="localhost"
DATABASE_PORT="3306"
DATABASE_NAME="master"
DATABASE_USER="root"
DATABASE_PASSWORD="root"
DATABASE_HOST="{{DATABASE_HOST}}"
DATABASE_PORT="{{DATABASE_PORT}}"
DATABASE_NAME="{{DATABASE_NAME}}"
DATABASE_USER="{{DATABASE_USER}}"
DATABASE_PASSWORD="{{DATABASE_PASSWORD}}"
###< doctrine/doctrine-bundle ###

###> symfony/swiftmailer-bundle ###
Expand All @@ -28,8 +28,8 @@ APP_SECRET=141af65f23c7935a37b504c422f113b0
###< symfony/framework-bundle ###

###> chamilo ###
APP_INSTALLED=0
APP_ENCRYPT_METHOD=bcrypt
APP_INSTALLED={{APP_INSTALLED}}
APP_ENCRYPT_METHOD={{APP_ENCRYPT_METHOD}}
APP_LOCALE=en
APP_URL_APPEND=''
APP_MULTIPLE_ACCESS_URL=''
Expand Down
24 changes: 20 additions & 4 deletions main/inc/global.inc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
/* For licensing terms, see /license.txt */

use Symfony\Component\Dotenv\Dotenv;

/**
* It is recommended that ALL Chamilo scripts include this important file.
* This script manages
Expand All @@ -21,14 +23,13 @@
// Include the libraries that are necessary everywhere
require_once __DIR__.'/../../vendor/autoload.php';

$kernel = new Chamilo\Kernel('', '');
$kernel = new Chamilo\Kernel('dev', true);

// Determine the directory path where this current file lies.
// This path will be useful to include the other initialisation files.
$includePath = __DIR__;

// Include the main Chamilo platform configuration file.

$alreadyInstalled = false;
if (file_exists($kernel->getConfigurationFile())) {
require_once $kernel->getConfigurationFile();
Expand Down Expand Up @@ -102,6 +103,21 @@
mkdir(_MPDF_TEMP_PATH, api_get_permissions_for_new_directories(), true);
}

if (file_exists(api_get_path(SYS_PATH).'.env')) {
(new Dotenv())->load(api_get_path(SYS_PATH).'.env');
$kernel->boot();
$doctrine = $kernel->getContainer()->get('doctrine');

$database = new \Database();
$database->setManager($doctrine->getManager());
$database->setConnection($doctrine->getConnection());
} else {
$global_error_code = 3;
// The database server is not available or credentials are invalid.
require $includePath.'/global_error_message.inc.php';
die();
}

// Connect to the server database and select the main chamilo database.
// When $_configuration['db_persistent_connection'] is set, it is expected to be a boolean type.
/*$dbPersistConnection = api_get_configuration_value('db_persistent_connection');
Expand All @@ -118,7 +134,7 @@
);*/

// Doctrine ORM configuration

/*
$dbParams = [
'driver' => 'pdo_mysql',
'host' => $_configuration['db_host'],
Expand All @@ -139,7 +155,7 @@
// The database server is not available or credentials are invalid.
require $includePath.'/global_error_message.inc.php';
die();
}
}*/

/* RETRIEVING ALL THE CHAMILO CONFIG SETTINGS FOR MULTIPLE URLs FEATURE*/
if (!empty($_configuration['multiple_access_urls'])) {
Expand Down
10 changes: 5 additions & 5 deletions main/inc/lib/database.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class Database
/**
* @param EntityManager $em
*/
public function setManager($em)
public static function setManager($em)
{
self::$em = $em;
}

/**
* @param Connection $connection
*/
public function setConnection(Connection $connection)
public static function setConnection(Connection $connection)
{
self::$connection = $connection;
}
Expand Down Expand Up @@ -143,13 +143,13 @@ public function connect(

$config->setEntityNamespaces(
[
'ChamiloPageBundle' => 'Chamilo\PageBundle\Entity',
'ChamiloUserBundle' => 'Chamilo\UserBundle\Entity',
'ChamiloCoreBundle' => 'Chamilo\CoreBundle\Entity',
'ChamiloCourseBundle' => 'Chamilo\CourseBundle\Entity',
'ChamiloPageBundle' => 'Chamilo\PageBundle\Entity',
'ChamiloPluginBundle' => 'Chamilo\PluginBundle\Entity',
'ChamiloSkillBundle' => 'Chamilo\SkillBundle\Entity',
'ChamiloTicketBundle' => 'Chamilo\TicketBundle\Entity',
'ChamiloPluginBundle' => 'Chamilo\PluginBundle\Entity'
'ChamiloUserBundle' => 'Chamilo\UserBundle\Entity'
]
);

Expand Down
3 changes: 2 additions & 1 deletion main/install/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@

$dbPort = isset($_POST['db_port']) ? $_POST['db_port'] : 3306;

$manager = connectToDatabase($dbHost, $dbUsername, $dbPass, $dbName, $dbPort);
$database = connectToDatabase($dbHost, $dbUsername, $dbPass, $dbName, $dbPort);
$manager = $database->getManager();

$db_prefix = api_get_configuration_value('db_prefix') ? api_get_configuration_value('db_prefix') : 'chamilo_';
$db_c_prefix = api_get_configuration_value('table_prefix') ? api_get_configuration_value('table_prefix') : 'crs_';
Expand Down
Loading

0 comments on commit 3dfa340

Please sign in to comment.