Skip to content

Commit 597beff

Browse files
committed
Merge pull request #11 from mwhitneysdsu/develop
Use prefixed properties in .env files
2 parents 56cfbaf + 11b08b3 commit 597beff

File tree

6 files changed

+363
-1
lines changed

6 files changed

+363
-1
lines changed

application/config/Database.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php namespace App\Config;
2+
3+
/**
4+
* Database configuration class.
5+
*
6+
* The default application-level database configuration. Allows the application
7+
* to set a list of available connection configurations and the active/default connection
8+
* configuration to be used when no connection configuration is specified.
9+
*/
10+
class Database extends \CodeIgniter\Config\Database
11+
{
12+
/**
13+
* The name of the active connection configuration.
14+
*
15+
* This value should match a key in $availableConnections to tell the system
16+
* which connection configuration to use when no connection configuration is
17+
* specified when connecting to the database/loading the database library.
18+
*
19+
* @var string
20+
*/
21+
public $activeConnection = 'default';
22+
23+
/**
24+
* The connection configurations available to this application.
25+
*
26+
* An array of connection configuration classes with keys specifying the name
27+
* which will be used to reference the class in the application and configuration
28+
* files.
29+
*
30+
* @var array
31+
*/
32+
public $availableConnections = [
33+
'default' => "\\App\\Config\\Database\\DefaultConnection",
34+
];
35+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php namespace App\Config\Database;
2+
3+
/**
4+
* The default connection configuration for the application.
5+
*
6+
* This file will contain the settings needed to access your database.
7+
*
8+
* Mapping values from CI3 database configuration:
9+
* $active_group - not set here, see $activeConnection in the application's Database config
10+
* $query_builder - currently not supported
11+
* $db - set by a combination of $availableConnections in the application's
12+
* Database config and files like this one to set the configuration
13+
* of the individual connections.
14+
*
15+
* The following are the keys used in configuring connection groups in CI3's $db
16+
* property and their equivalent properties in CI4's Database Connection config:
17+
* 'dsn' - $dsn
18+
* 'hostname' - $hostname
19+
* 'username' - $username
20+
* 'password' - $password
21+
* 'dbdriver' - Instead of setting this value directly, extend the platform-specific
22+
* Connection Configuration class (to be added here when available):
23+
* 'mysqli' - \CodeIgniter\Config\Database\Connection\MySQLi
24+
* 'dbprefix' - currently not supported
25+
* 'pconnect' - $persistentConnectionEnabled
26+
* 'db_debug' - $debugEnabled
27+
* 'cache_on' - $cacheEnabled - may be modified to reference the cache configuration
28+
* 'cachedir' - currently not supported (to be configured elsewhere)
29+
* 'char_set' - $characterSet
30+
* 'dbcollat' - $collation (MySQLi-only)
31+
* 'swap_pre' - currently not supported
32+
* 'encrypt' - (MySQLi-only) instead of an array with the following options, set
33+
* the associated properties directly:
34+
* 'ssl_key' - $sslKey
35+
* 'ssl_cert' - $sslCert
36+
* 'ssl_ca' - $sslCA
37+
* 'ssl_capath' - $sslCAPath
38+
* 'ssl_cipher' - $sslCipher
39+
* 'ssl_verify' - $sslVerify
40+
* 'compress' - $compressionEnabled (MySQLi-only)
41+
* 'stricton' - $strictSQLEnabled
42+
* 'failover' - $failover Instead of an array containing connection configurations,
43+
* this will contain values matching keys used in the application's
44+
* database configuration's $availableConnections property
45+
* 'save_queries' - $saveStatementsEnabled
46+
* 'port' - $port
47+
*
48+
* Additional configuration options:
49+
* $deleteHack - (MySQLi-only)
50+
*
51+
* The order of the properties below has been arranged (and their values have been
52+
* set) to match the default connection group in CI3's database config.
53+
*/
54+
class DefaultConnection extends \CodeIgniter\Config\Database\Connection\MySQLi
55+
{
56+
/**
57+
* Data Source Name.
58+
*
59+
* A string which will usually contain all of the settings required to connect
60+
* to a database. Commonly used for database adapters which support multiple
61+
* database types, like PDO or ODBC.
62+
*
63+
* @var string
64+
*/
65+
public $dsn = '';
66+
67+
/** @var string The database server's hostname. */
68+
public $hostname = 'localhost';
69+
70+
/** @var string The username for the database connection. */
71+
public $username = '';
72+
73+
/** @var string The password for the database connection. */
74+
public $password = '';
75+
76+
/** @var string The name of the database. */
77+
public $database = '';
78+
79+
/** @var bool Use a persistent connection. */
80+
public $persistentConnectionEnabled = false;
81+
82+
/** @var bool Enable debug messages. */
83+
public $debugEnabled = (ENVIRONMENT !== 'production');
84+
85+
/** @var bool Enable database result caching. */
86+
public $cacheEnabled = false;
87+
88+
/** @var string The character set. */
89+
public $characterSet = 'utf8';
90+
91+
/** @var string The character collation used in communicating with the database. */
92+
public $collation = 'utf8_general_ci';
93+
94+
/** @var bool Whether client compression is enabled. */
95+
public $compressionEnabled = false;
96+
97+
/** @var bool Forces "Strict Mode" connections to enforce strict SQL. */
98+
public $strictSQLEnabled = false;
99+
100+
/**
101+
* List of connections to use if this one fails to connect.
102+
*
103+
* The values in this array should match the keys used in the application's
104+
* Database config to reference other connection configuration classes.
105+
*
106+
* @var array
107+
*/
108+
public $failover = [];
109+
110+
/**
111+
* Whether to "save" all executed SQL statements.
112+
*
113+
* Disabling this will also effectively disable application-level profiling
114+
* of SQL statements.
115+
*
116+
* Enabling this setting may cause high memory use, especially when running
117+
* a lot of SQL statements.
118+
*
119+
* @var bool
120+
*/
121+
public $saveStatementsEnabled = true;
122+
}

system/Config/BaseConfig.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ class BaseConfig
2020
public function __construct()
2121
{
2222
$properties = array_keys(get_object_vars($this));
23+
$prefix = get_class($this);
2324

2425
foreach ($properties as $property)
2526
{
26-
if ($value = getenv($property))
27+
if ($value = getenv("{$prefix}.{$property}"))
28+
{
29+
$this->{$property} = $value;
30+
}
31+
elseif ($value = getenv($property))
2732
{
2833
$this->{$property} = $value;
2934
}

system/Config/Database.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php namespace CodeIgniter\Config;
2+
3+
/**
4+
* Database configuration class.
5+
*
6+
* A base class for application-level database configuration. Allows the application
7+
* to set a list of available connection configurations and the active/default connection
8+
* configuration to be used when no connection configuration is specified.
9+
*/
10+
class Database extends \CodeIgniter\Config\BaseConfig
11+
{
12+
/** @var string The name of the active connection. */
13+
public $activeConnection;
14+
15+
/** @var array Connection names and the classes those names reference. */
16+
public $availableConnections = [];
17+
18+
/**
19+
* Allows a Database configuration to be built from a parameter array at run-time.
20+
*
21+
* @param array $params Property name/value pairs to set in the database config.
22+
*/
23+
public function __construct($params = [])
24+
{
25+
parent::__construct();
26+
27+
// Allow $params to override environment variables.
28+
if (isset($params['availableConnections']))
29+
{
30+
$this->availableConnections = $params['availableConnections'];
31+
}
32+
if (isset($params['activeConnection']))
33+
{
34+
$this->activeConnection = $params['activeConnection'];
35+
}
36+
}
37+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php namespace CodeIgniter\Config\Database;
2+
3+
/**
4+
* Database Connection configuration class.
5+
*
6+
* A base class for database connection configuration.
7+
*
8+
* This class is intended to be a base class for other system-level configuration
9+
* classes, not for application-level database connection configuration. Applications
10+
* should extend the platform-specific system-level connection configuration class
11+
* for their database.
12+
*/
13+
abstract class Connection extends \CodeIgniter\Config\BaseConfig
14+
{
15+
/** @var bool Enable database result caching. */
16+
public $cacheEnabled = false;
17+
18+
/** @var string The character set. */
19+
public $characterSet = 'utf8';
20+
21+
/** @var string The name of the database. */
22+
public $database = '';
23+
24+
/** @var bool Enable debug messages. */
25+
public $debugEnabled = false;
26+
27+
/**
28+
* Data Source Name.
29+
*
30+
* A string which will usually contain all of the settings required to connect
31+
* to a database. Commonly used for database adapters which support multiple
32+
* database types, like PDO or ODBC.
33+
*
34+
* @var string
35+
*/
36+
public $dsn = '';
37+
38+
/** @var array List of connections to use if this one fails to connect. */
39+
public $failover = [];
40+
41+
/** @var string The database server's hostname. */
42+
public $hostname = '';
43+
44+
/** @var string The password for the database connection. */
45+
public $password = '';
46+
47+
/** @var bool Use a persistent connection. */
48+
public $persistentConnectionEnabled = false;
49+
50+
/**
51+
* The port used to connect to the database server.
52+
*
53+
* This is usually not set when the server is configured to use the default
54+
* port.
55+
*
56+
* @var int
57+
*/
58+
public $port;
59+
60+
/**
61+
* Whether to "save" all executed SQL statements.
62+
*
63+
* Disabling this will also effectively disable application-level profiling
64+
* of SQL statements.
65+
*
66+
* Enabling this setting may cause high memory use, especially when running
67+
* a lot of SQL statements.
68+
*
69+
* @var bool
70+
*/
71+
public $saveStatementsEnabled = false;
72+
73+
/** @var bool Forces "Strict Mode" connections to enforce strict SQL. */
74+
public $strictSQLEnabled = false;
75+
76+
/** @var string The username for the database connection. */
77+
public $username = '';
78+
79+
/**
80+
* The name of the adapter to be used by this connection.
81+
* In most cases, this will match the name of the Connection class itself.
82+
*
83+
* This is intended to be set by the system-level platform-specific configuration
84+
* class, and should not be modified by the application.
85+
*
86+
* @var string
87+
*/
88+
protected $adapter;
89+
90+
/**
91+
* Get the name of the adapter to be used by the connection.
92+
*
93+
* @return string The name of the adapter.
94+
*/
95+
public function getAdapter()
96+
{
97+
return $this->adapter;
98+
}
99+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php namespace CodeIgniter\Config\Database\Connection;
2+
3+
/**
4+
* This is the base class to be used when configuring a connection to a MySQL database
5+
* using PHP's MySQLi extension. The application's database connection configuration
6+
* class should extend this class and configure the appropriate values to connect
7+
* to the database.
8+
*/
9+
class MySQLi extends \CodeIgniter\Config\Database\Connection
10+
{
11+
/** @var string The character collation used in communicating with the database. */
12+
public $collation = 'utf8_general_ci';
13+
14+
/** @var bool Whether client compression is enabled. */
15+
public $compressionEnabled = false;
16+
17+
/**
18+
* Whether to use the MySQL "delete hack" which allows the number of affected
19+
* rows to be shown.
20+
*
21+
* Uses a preg_replace when enabled, adding a little more processing to all
22+
* queries.
23+
*
24+
* @var bool
25+
*/
26+
public $deleteHack = true;
27+
28+
/** @var string Path to the private key file for encryption. */
29+
public $sslKey;
30+
31+
/** @var string Path to the public key certificate file for encryption. */
32+
public $sslCert;
33+
34+
/** @var string Path to the certificate authority file for encryption. */
35+
public $sslCA;
36+
37+
/**
38+
* Path to a directory containing trusted CA certificates in PEM format.
39+
*
40+
* @var string
41+
*/
42+
public $sslCAPath;
43+
44+
/**
45+
* List of *allowed* ciphers to be used for encryption, separated by colons
46+
* (':').
47+
*
48+
* @var string
49+
*/
50+
public $sslCipher;
51+
52+
/** @var bool Whether to verify the server certificate. */
53+
public $sslVerify;
54+
55+
/**
56+
* The name of the adapter to be used by this connection.
57+
* In most cases, this will match the name of the Connection class itself.
58+
*
59+
* This should not be modified/overridden by the application.
60+
*
61+
* @var string
62+
*/
63+
protected $adapter = 'MySQLi';
64+
}

0 commit comments

Comments
 (0)