Skip to content

Commit 536fff8

Browse files
committed
ISSUE-345: add env
1 parent 4311775 commit 536fff8

File tree

6 files changed

+122
-5
lines changed

6 files changed

+122
-5
lines changed

.env.example

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This is an example .env file for phpList
2+
# Copy this file to .env and customize it for your environment
3+
4+
# Application environment (dev, test, prod)
5+
APP_ENV=dev
6+
7+
# Debug mode (1 for enabled, 0 for disabled)
8+
APP_DEBUG=1
9+
10+
# Database configuration
11+
PHPLIST_DATABASE_DRIVER=pdo_mysql
12+
PHPLIST_DATABASE_PATH=
13+
PHPLIST_DATABASE_HOST='127.0.0.1'
14+
PHPLIST_DATABASE_PORT=3306
15+
PHPLIST_DATABASE_NAME='phplistdb'
16+
PHPLIST_DATABASE_USER='phplist'
17+
PHPLIST_DATABASE_PASSWORD='phplist'
18+
19+
# Mailer configuration
20+
MAILER_DSN=smtp://user:[email protected]:25
21+
#MAILER_DSN=gmail://username:password@default
22+
#MAILER_DSN=ses://ACCESS_KEY:SECRET_KEY@default?region=eu-west-1
23+
#MAILER_DSN=sendgrid://KEY@default
24+
#MAILER_DSN=mandrill://KEY@default
25+
26+
# Secret key for security
27+
PHPLIST_SECRET=bd21d72faef90cdb9c4e99e82f5edd089bfb0c5a

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
.vagrant
1717
.phpdoc/
1818
.phpunit.result.cache
19+
/.env
20+
/.env.local

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,40 @@ this code.
6363
The phpList application is configured so that the built-in PHP web server can
6464
run in development and testing mode, while Apache can run in production mode.
6565

66-
Please first set the database credentials in `config/parameters.yml`.
66+
Please first set the database credentials in `config/parameters.yml` or in the `.env` file (see below).
67+
68+
## Environment Variables
69+
70+
phpList now supports loading environment variables from `.env` files. This allows you to configure your application without modifying any code or configuration files.
71+
72+
### Usage
73+
74+
1. Copy the `.env.example` file to `.env` in the project root:
75+
```bash
76+
cp .env.example .env
77+
```
78+
79+
2. Edit the `.env` file and customize the variables according to your environment:
80+
```
81+
# Application environment (dev, test, prod)
82+
APP_ENV=dev
83+
84+
# Debug mode (1 for enabled, 0 for disabled)
85+
APP_DEBUG=1
86+
87+
# Database configuration
88+
DATABASE_URL=mysql://db_user:db_password@localhost:3306/db_name
89+
90+
# Mailer configuration
91+
MAILER_DSN=smtp://localhost:25
92+
93+
# Secret key for security
94+
APP_SECRET=change_this_to_a_secret_value
95+
```
96+
97+
3. For local overrides, you can create a `.env.local` file which will be loaded after `.env`.
98+
99+
Note: The `.env` and `.env.local` files are excluded from version control to prevent sensitive information from being committed.
67100

68101
### Development
69102

bin/console

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,28 @@ use PhpList\Core\Core\Bootstrap;
77
use PhpList\Core\Core\Environment;
88
use Symfony\Bundle\FrameworkBundle\Console\Application;
99
use Symfony\Component\Console\Input\ArgvInput;
10+
use Symfony\Component\Dotenv\Dotenv;
1011
use Symfony\Component\ErrorHandler\ErrorHandler;
1112

1213
set_time_limit(0);
1314

1415
require __DIR__ . '/../vendor/autoload.php';
1516

17+
// Load environment variables from .env file
18+
$dotenv = new Dotenv();
19+
$rootDir = dirname(__DIR__);
20+
if (file_exists($rootDir . '/.env')) {
21+
$dotenv->load($rootDir . '/.env');
22+
// Load .env.local if it exists (for local overrides)
23+
if (file_exists($rootDir . '/.env.local')) {
24+
$dotenv->load($rootDir . '/.env.local');
25+
}
26+
}
27+
1628
$input = new ArgvInput();
17-
$environment = $input->getParameterOption(['--env', '-e'], getenv('SYMFONY_ENV') ?: Environment::DEVELOPMENT);
18-
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(['--no-debug', ''])
29+
$environment = $input->getParameterOption(['--env', '-e'], $_ENV['APP_ENV'] ?? getenv('SYMFONY_ENV') ?: Environment::DEVELOPMENT);
30+
$debug = (isset($_ENV['APP_DEBUG']) ? $_ENV['APP_DEBUG'] !== '0' : getenv('SYMFONY_DEBUG') !== '0')
31+
&& !$input->hasParameterOption(['--no-debug', ''])
1932
&& $environment !== Environment::PRODUCTION;
2033

2134
if ($debug) {

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"symfony/google-mailer": "^6.4",
6464
"symfony/amazon-mailer": "^6.4",
6565
"symfony/mailchimp-mailer": "^6.4",
66-
"symfony/sendgrid-mailer": "^6.4"
66+
"symfony/sendgrid-mailer": "^6.4",
67+
"symfony/dotenv": "^6.4"
6768
},
6869
"require-dev": {
6970
"phpunit/phpunit": "^9.5",

src/Core/Bootstrap.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ORM\EntityManagerInterface;
88
use Exception;
99
use RuntimeException;
10+
use Symfony\Component\Dotenv\Dotenv;
1011
use Symfony\Component\ErrorHandler\ErrorHandler;
1112
use Symfony\Component\DependencyInjection\ContainerInterface;
1213
use Symfony\Component\HttpFoundation\Request;
@@ -35,6 +36,7 @@ class Bootstrap
3536
private ?ApplicationKernel $applicationKernel = null;
3637
private ApplicationStructure $applicationStructure;
3738
private ErrorHandler $errorHandler;
39+
private bool $envLoaded = false;
3840

3941
/**
4042
* Protected constructor to avoid direct instantiation of this class.
@@ -45,6 +47,33 @@ protected function __construct()
4547
{
4648
$this->applicationStructure = new ApplicationStructure();
4749
$this->errorHandler = new ErrorHandler();
50+
$this->loadEnvironmentVariables();
51+
}
52+
53+
/**
54+
* Loads environment variables from .env files.
55+
*
56+
* @return void fluent interface
57+
*/
58+
private function loadEnvironmentVariables(): void
59+
{
60+
if ($this->envLoaded) {
61+
return;
62+
}
63+
64+
$dotenv = new Dotenv();
65+
$rootDir = $this->getApplicationRoot();
66+
67+
if (file_exists($rootDir . '/.env')) {
68+
$dotenv->load($rootDir . '/.env');
69+
70+
if (file_exists($rootDir . '/.env.local')) {
71+
$dotenv->load($rootDir . '/.env.local');
72+
}
73+
74+
$this->envLoaded = true;
75+
}
76+
4877
}
4978

5079
/**
@@ -89,6 +118,10 @@ public static function purgeInstance(): void
89118
*/
90119
public function setEnvironment(string $environment): Bootstrap
91120
{
121+
if ($environment === Environment::DEFAULT_ENVIRONMENT && isset($_ENV['APP_ENV'])) {
122+
$environment = $_ENV['APP_ENV'];
123+
}
124+
92125
Environment::validateEnvironment($environment);
93126
$this->environment = $environment;
94127

@@ -102,11 +135,19 @@ public function getEnvironment(): string
102135

103136
private function isSymfonyDebugModeEnabled(): bool
104137
{
138+
if (isset($_ENV['APP_DEBUG'])) {
139+
return $_ENV['APP_DEBUG'] === '1' || $_ENV['APP_DEBUG'] === 'true';
140+
}
141+
105142
return $this->environment !== Environment::PRODUCTION;
106143
}
107144

108145
private function isDebugEnabled(): bool
109146
{
147+
if (isset($_ENV['APP_DEBUG'])) {
148+
return $_ENV['APP_DEBUG'] === '1' || $_ENV['APP_DEBUG'] === 'true';
149+
}
150+
110151
return $this->environment !== Environment::PRODUCTION;
111152
}
112153

@@ -138,7 +179,7 @@ public function ensureDevelopmentOrTestingEnvironment(): self
138179
}
139180

140181
/**
141-
* Main entry point called at every request usually from global scope. Checks if everything is correct
182+
* The main entry point called at every request usually from global scope. Checks if everything is correct
142183
* and loads the configuration.
143184
*
144185
* @return Bootstrap fluent interface

0 commit comments

Comments
 (0)