-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow database authentication using .env file
- Loading branch information
Showing
8 changed files
with
228 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DB_DRIVER=mysql | ||
DB_PORT=3306 | ||
DB_HOST=localhost | ||
DB_NAME=cypht_db | ||
DB_USER=root | ||
DB_PASS=rootroot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/** | ||
* Environment objects | ||
* @package framework | ||
* @subpackage environment | ||
*/ | ||
|
||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
class Hm_Environment { | ||
|
||
private static $instance; | ||
|
||
public static function getInstance() | ||
{ | ||
if (self::$instance === null) { | ||
self::$instance = new static(); | ||
} | ||
return self::$instance; | ||
} | ||
|
||
public function load() { | ||
$this->set_required_environment_variables(); | ||
|
||
$dotenvLoader = new Dotenv(); | ||
if (method_exists($dotenvLoader, 'usePutenv')) { | ||
$dotenvLoader->usePutenv(true); | ||
} | ||
$envDistFile = APP_PATH. '.env.dist'; | ||
if (!file_exists($envDistFile)) { | ||
Hm_Msgs::add('ERR.env.dist file not found at: "' . $envDistFile . '"'); | ||
return; | ||
} | ||
|
||
$envFile = static::get('TM_DOTENV'); | ||
$dotenvLoader->load($envDistFile); | ||
if ($envFile) { | ||
$dotenvLoader->loadEnv($envFile); | ||
} | ||
} | ||
|
||
public static function get($key, $defaultValue = null) | ||
{ | ||
$variables = self::getInstance()->get_environment_variables(); | ||
|
||
return array_key_exists($key, $variables) ? $variables[$key] : $defaultValue; | ||
} | ||
|
||
/** | ||
* Sets required environment variables that are used within .env files | ||
*/ | ||
private function set_required_environment_variables() | ||
{ | ||
$_ENV['TM_DOTENV'] = APP_PATH . '.env'; | ||
} | ||
|
||
/** | ||
* Get a merge of environment variables $_ENV and $_SERVER. | ||
* | ||
* @return array | ||
*/ | ||
protected function get_environment_variables() | ||
{ | ||
return array_merge($_ENV, $_SERVER); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* tests for Hm_Environment | ||
*/ | ||
class Hm_Test_Environment_Variable extends TestCase { | ||
|
||
public function setUp(): void { | ||
require 'bootstrap.php'; | ||
|
||
} | ||
/** | ||
* @preserveGlobalState disabled | ||
* @runInSeparateProcess | ||
*/ | ||
public function test_get() { | ||
$environment = Hm_Environment::getInstance(); | ||
$environment->load(); | ||
$tm_dotenv = $environment->get('TM_DOTENV'); | ||
$this->assertStringEndsWith(".env", $tm_dotenv); | ||
} | ||
|
||
/** | ||
* @preserveGlobalState disabled | ||
* @runInSeparateProcess | ||
*/ | ||
public function test_get_default_value() { | ||
$environment = Hm_Environment::getInstance(); | ||
$environment->load(); | ||
$undifined_env_data = $environment::get('APP_VERSION', "DEFAUL_VALUE"); | ||
$this->assertEquals('DEFAUL_VALUE', $undifined_env_data); | ||
} | ||
|
||
/** | ||
* @preserveGlobalState disabled | ||
* @runInSeparateProcess | ||
*/ | ||
public function test_get_environment_variables() { | ||
$environment = Hm_Environment::getInstance(); | ||
$reflection = new ReflectionClass($environment); | ||
$method = $reflection->getMethod('get_environment_variables'); | ||
$method->setAccessible(true); | ||
$env_vars = $method->invoke($environment); | ||
$expected = array_merge($_ENV, $_SERVER); | ||
$this->assertEquals($expected, $env_vars); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters