From 43033a47484fabec070f97ded4cf9b7313a6f1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sava=20Gavri=C4=87?= Date: Mon, 4 Apr 2022 11:02:39 +0200 Subject: [PATCH] Implement environment variable support to prevent hard-coded values. Issue MIDU-192 --- .gitignore | 3 +- Dockerfile | 4 +- composer.json | 3 +- composer.lock | 236 ++++++++++++++++++++++++- database.example.env | 6 + docker-compose.yaml | 11 +- src/bootstrap.php | 3 + src/data_access/DatabaseConnection.php | 10 +- 8 files changed, 256 insertions(+), 20 deletions(-) create mode 100644 database.example.env diff --git a/.gitignore b/.gitignore index 4c34ebc..01a8dcc 100755 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ assets/images /vendor/ -volumes/database/scripts/initialize_db.sql +config/initialize_db.sql /volumes/ +database.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9765202..6904c66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,4 @@ COPY index.php /var/www/html/ RUN chown -R www-data:www-data /var/www/html -RUN a2enmod rewrite - -EXPOSE 80 \ No newline at end of file +RUN a2enmod rewrite \ No newline at end of file diff --git a/composer.json b/composer.json index 1d13c60..e6c7c5f 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "require": { "php": "7.4.*", "ext-pdo": "*", - "ext-json": "*" + "ext-json": "*", + "vlucas/phpdotenv": "4.*" }, "config": { "platform": { diff --git a/composer.lock b/composer.lock index 8fe0e6b..9b107bb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,240 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0cccdd3ca8c1657d7cae8607c3426606", - "packages": [], + "content-hash": "1f73c894b5512f60c6b79147f6262b72", + "packages": [ + { + "name": "phpoption/phpoption", + "version": "1.8.1", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/php-option.git", + "reference": "eab7a0df01fe2344d172bff4cd6dbd3f8b84ad15" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/eab7a0df01fe2344d172bff4cd6dbd3f8b84ad15", + "reference": "eab7a0df01fe2344d172bff4cd6dbd3f8b84ad15", + "shasum": "" + }, + "require": { + "php": "^7.0 || ^8.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "phpunit/phpunit": "^6.5.14 || ^7.5.20 || ^8.5.19 || ^9.5.8" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.8-dev" + } + }, + "autoload": { + "psr-4": { + "PhpOption\\": "src/PhpOption/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Johannes M. Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh" + }, + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + } + ], + "description": "Option Type for PHP", + "keywords": [ + "language", + "option", + "php", + "type" + ], + "support": { + "issues": "https://github.com/schmittjoh/php-option/issues", + "source": "https://github.com/schmittjoh/php-option/tree/1.8.1" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpoption/phpoption", + "type": "tidelift" + } + ], + "time": "2021-12-04T23:24:31+00:00" + }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.25.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "30885182c981ab175d4d034db0f6f469898070ab" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/30885182c981ab175d4d034db0f6f469898070ab", + "reference": "30885182c981ab175d4d034db0f6f469898070ab", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-ctype": "*" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.23-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.25.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-10-20T20:35:02+00:00" + }, + { + "name": "vlucas/phpdotenv", + "version": "v4.2.2", + "source": { + "type": "git", + "url": "https://github.com/vlucas/phpdotenv.git", + "reference": "77e974614d2ead521f18069dccc571696f52b8dc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/77e974614d2ead521f18069dccc571696f52b8dc", + "reference": "77e974614d2ead521f18069dccc571696f52b8dc", + "shasum": "" + }, + "require": { + "php": "^5.5.9 || ^7.0 || ^8.0", + "phpoption/phpoption": "^1.7.3", + "symfony/polyfill-ctype": "^1.17" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "ext-filter": "*", + "ext-pcre": "*", + "phpunit/phpunit": "^4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.21" + }, + "suggest": { + "ext-filter": "Required to use the boolean validator.", + "ext-pcre": "Required to use most of the library." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.2-dev" + } + }, + "autoload": { + "psr-4": { + "Dotenv\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Vance Lucas", + "email": "vance@vancelucas.com", + "homepage": "https://github.com/vlucas" + } + ], + "description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.", + "keywords": [ + "dotenv", + "env", + "environment" + ], + "support": { + "issues": "https://github.com/vlucas/phpdotenv/issues", + "source": "https://github.com/vlucas/phpdotenv/tree/v4.2.2" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/vlucas/phpdotenv", + "type": "tidelift" + } + ], + "time": "2021-12-12T23:07:53+00:00" + } + ], "packages-dev": [], "aliases": [], "minimum-stability": "stable", diff --git a/database.example.env b/database.example.env new file mode 100644 index 0000000..3459738 --- /dev/null +++ b/database.example.env @@ -0,0 +1,6 @@ +SQL_DRIVER=mysql +MYSQL_ROOT_PASSWORD=root_password +MYSQL_DATABASE=database_name +MYSQL_USER=username +MYSQL_PASSWORD=password +MYSQL_HOST=database_host \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 40d3b23..2848648 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,8 +5,6 @@ services: image: bookstore ports: - "80:80" - volumes: - - ~/Documents/Projects/BookStore/volumes/logs:/var/log/apache2 depends_on: - database database: @@ -15,10 +13,7 @@ services: - "3306:3306" hostname: database volumes: - - ~/Documents/Projects/BookStore/volumes/database/scripts:/docker-entrypoint-initdb.d + - ./config/initialize_db.sql:/docker-entrypoint-initdb.d/initialize_db.sql - ~/Documents/data/mysql/bookstore:/var/lib/mysql - environment: - - MYSQL_ALLOW_EMPTY_PASSWORD=yes - - MYSQL_DATABASE=bookstore_db - - MYSQL_USER=bookstore_user - - MYSQL_PASSWORD=password + env_file: + - database.env diff --git a/src/bootstrap.php b/src/bootstrap.php index 2e76d0c..0da164e 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -6,4 +6,7 @@ use Logeecom\Bookstore\presentation\routers\Router; +$dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT'], 'database.env'); +$dotenv->load(); + (new Router())->route(); \ No newline at end of file diff --git a/src/data_access/DatabaseConnection.php b/src/data_access/DatabaseConnection.php index 008a9d5..4884747 100644 --- a/src/data_access/DatabaseConnection.php +++ b/src/data_access/DatabaseConnection.php @@ -21,15 +21,15 @@ class DatabaseConnection */ private function __construct() { - $config = include $_SERVER['DOCUMENT_ROOT'] . '/config/database.php'; - while (true) { try { + $dsn = $_ENV['SQL_DRIVER'] . ':host=' . $_ENV['MYSQL_HOST'] . ';dbname=' . $_ENV['MYSQL_DATABASE']; $this->PDOConnection = new PDO( - $config['driver'] . ':host=' . $config['host'] . ';dbname=' . $config['db_name'], - $config['username'], - $config['password'] + $dsn, + $_ENV['MYSQL_USER'], + $_ENV['MYSQL_PASSWORD'] ); + error_log("Connection success!"); return; } catch (PDOException $e) {