Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b03ddf8

Browse files
committed
Branch refactor
1 parent 07795ba commit b03ddf8

File tree

8 files changed

+132
-85
lines changed

8 files changed

+132
-85
lines changed

‎src/DotEnv.php

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace DevCoder;
44

5+
use DevCoder\Processor\AbstractProcessor;
6+
use DevCoder\Processor\BooleanProcessor;
7+
use DevCoder\Processor\QuotedProcessor;
8+
59
class DotEnv
610
{
711
/**
@@ -14,36 +18,42 @@ class DotEnv
1418
/**
1519
* Configure the options on which the parsed will act
1620
*
17-
* @var array
18-
*/
19-
protected $options = [];
20-
21-
/**
22-
* Process strings to obtain characteristics of it
23-
*
24-
* @var Processor
21+
* @var string[]
2522
*/
26-
protected $processor;
23+
protected $processors = [];
2724

28-
public function __construct(string $path, array $options = [])
25+
public function __construct(string $path, array $processors = null)
2926
{
3027
if (!file_exists($path)) {
3128
throw new \InvalidArgumentException(sprintf('%s does not exist', $path));
3229
}
3330

3431
$this->path = $path;
3532

36-
$this->processOptions($options);
37-
38-
$this->processor = new Processor();
33+
$this->setProcessors($processors);
3934
}
4035

41-
private function processOptions(array $options) : void
36+
private function setProcessors(array $processors = null) : DotEnv
4237
{
43-
$this->options = array_merge([
44-
Option::PROCESS_BOOLEANS => true,
45-
Option::PROCESS_QUOTES => true
46-
], $options);
38+
/**
39+
* Fill with default processors
40+
*/
41+
if ($processors === null){
42+
$this->processors = [
43+
BooleanProcessor::class,
44+
QuotedProcessor::class
45+
];
46+
47+
return $this;
48+
}
49+
50+
foreach ($processors as $processor) {
51+
if (is_subclass_of($processor, AbstractProcessor::class)) {
52+
$this->processors[] = $processor;
53+
}
54+
}
55+
56+
return $this;
4757
}
4858

4959
/**
@@ -75,43 +85,31 @@ public function load() : void
7585
}
7686
}
7787

88+
/**
89+
* Process the value with the configured processors
90+
*
91+
* @param string $value The value to process
92+
* @return string|bool
93+
*/
7894
private function processValue(string $value)
7995
{
8096
/**
8197
* First trim spaces and quotes if configured
8298
*/
83-
$preprocessedValue = $this->preprocessValue($value);
99+
$trimmedValue = trim($value);
84100

85-
/**
86-
* If the value is a boolean resolve it as such
87-
*/
88-
if (!empty($this->options[Option::PROCESS_BOOLEANS])) {
89-
$isBoolean = $this->processor->isBoolean($preprocessedValue);
101+
foreach ($this->processors as $processor) {
102+
/** @var AbstractProcessor $processorInstance */
103+
$processorInstance = new $processor($trimmedValue);
90104

91-
if ($isBoolean) {
92-
return $this->processor->resolveAsBoolean($preprocessedValue);
105+
if ($processorInstance->canBeProcessed()) {
106+
return $processorInstance->execute();
93107
}
94108
}
95109

96110
/**
97111
* Does not match any processor options, return as is
98112
*/
99-
return $preprocessedValue;
100-
}
101-
102-
private function preprocessValue(string $value) : string
103-
{
104-
$trimmedValue = trim($value);
105-
106-
if (!empty($this->options[Option::PROCESS_QUOTES])) {
107-
$wrappedBySingleQuotes = $this->processor->isWrappedByChar($trimmedValue, '\'');
108-
$wrappedByDoubleQuotes = $this->processor->isWrappedByChar($trimmedValue, '"');
109-
110-
if ($wrappedBySingleQuotes || $wrappedByDoubleQuotes) {
111-
return $this->processor->removeFirstAndLastChar($trimmedValue);
112-
}
113-
}
114-
115113
return $trimmedValue;
116114
}
117115
}

‎src/Processor.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

‎src/Processor/AbstractProcessor.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace DevCoder\Processor;
3+
4+
abstract class AbstractProcessor implements IProcessor
5+
{
6+
/**
7+
* The value to process
8+
* @var string
9+
*/
10+
protected $value;
11+
12+
public function __construct(string $value)
13+
{
14+
$this->value = $value;
15+
}
16+
}

‎src/Processor/BooleanProcessor.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace DevCoder\Processor;
3+
4+
class BooleanProcessor extends AbstractProcessor
5+
{
6+
public function canBeProcessed(): bool
7+
{
8+
$loweredValue = strtolower($this->value);
9+
10+
return in_array($loweredValue, ['true', 'false'], true);
11+
}
12+
13+
public function execute()
14+
{
15+
return strtolower($this->value) === 'true';
16+
}
17+
}

‎src/Processor/IProcessor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace DevCoder\Processor;
3+
4+
interface IProcessor
5+
{
6+
public function __construct(string $value);
7+
8+
public function canBeProcessed(): bool;
9+
10+
public function execute();
11+
}

‎src/Processor/QuotedProcessor.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace DevCoder\Processor;
4+
5+
class QuotedProcessor extends AbstractProcessor
6+
{
7+
public function canBeProcessed(): bool
8+
{
9+
$wrappedByDoubleQuotes = $this->isWrappedByChar($this->value, '"');
10+
11+
if ($wrappedByDoubleQuotes) {
12+
return true;
13+
}
14+
15+
return $this->isWrappedByChar($this->value, '\'');
16+
}
17+
18+
public function execute()
19+
{
20+
/**
21+
* Since this function is used for the quote removal
22+
* we don't need mb_substr
23+
*/
24+
return substr($this->value, 1, -1);
25+
}
26+
27+
public function isWrappedByChar(string $value, string $char) : bool
28+
{
29+
return $value[0] === $char && $value[-1] === $char;
30+
}
31+
}

‎tests/DotenvTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use DevCoder\DotEnv;
66
use DevCoder\Option;
7+
use DevCoder\Processor\BooleanProcessor;
8+
use DevCoder\Processor\QuotedProcessor;
79
use PHPUnit\Framework\TestCase;
810

911
class DotenvTest extends TestCase
@@ -24,20 +26,26 @@ public function testLoad() {
2426
$this->assertEquals('password', getenv('DATABASE_PASSWORD'));
2527
$this->assertFalse(getenv('GOOGLE_API'));
2628
$this->assertFalse(getenv('GOOGLE_MANAGER_KEY'));
29+
$this->assertEquals(true, getenv('BOOLEAN_LITERAL'));
30+
$this->assertEquals('true', getenv('BOOLEAN_QUOTED'));
2731

2832
$this->assertEquals('dev', $_ENV['APP_ENV']);
2933
$this->assertEquals('mysql:host=localhost;dbname=test;', $_ENV['DATABASE_DNS']);
3034
$this->assertEquals('root', $_ENV['DATABASE_USER']);
3135
$this->assertEquals('password', $_ENV['DATABASE_PASSWORD']);
3236
$this->assertFalse(array_key_exists('GOOGLE_API', $_ENV));
3337
$this->assertFalse(array_key_exists('GOOGLE_MANAGER_KEY', $_ENV));
38+
$this->assertEquals(true, $_ENV['BOOLEAN_LITERAL']);
39+
$this->assertEquals('true', $_ENV['BOOLEAN_QUOTED']);
3440

3541
$this->assertEquals('dev', $_SERVER['APP_ENV']);
3642
$this->assertEquals('mysql:host=localhost;dbname=test;', $_SERVER['DATABASE_DNS']);
3743
$this->assertEquals('root', $_SERVER['DATABASE_USER']);
3844
$this->assertEquals('password', $_SERVER['DATABASE_PASSWORD']);
3945
$this->assertFalse(array_key_exists('GOOGLE_API', $_SERVER));
4046
$this->assertFalse(array_key_exists('GOOGLE_MANAGER_KEY', $_SERVER));
47+
$this->assertEquals(true, $_SERVER['BOOLEAN_LITERAL']);
48+
$this->assertEquals('true', $_SERVER['BOOLEAN_QUOTED']);
4149
}
4250

4351
public function testFileNotExist() {
@@ -51,19 +59,19 @@ public function testFileNotExist() {
5159
public function testProcessBoolean()
5260
{
5361
(new DotEnv($this->env('.env.boolean'), [
54-
Option::PROCESS_BOOLEANS => true
62+
BooleanProcessor::class
5563
]))->load();
5664

5765
$this->assertEquals(false, $_ENV['FALSE1']);
5866
$this->assertEquals(false, $_ENV['FALSE2']);
5967
$this->assertEquals(false, $_ENV['FALSE3']);
60-
$this->assertEquals(false, $_ENV['FALSE4']);
68+
$this->assertEquals("'false'", $_ENV['FALSE4']);// Since we don't have the QuotedProcessor::class this will be the result
6169
$this->assertEquals('0', $_ENV['FALSE5']);
6270

6371
$this->assertEquals(true, $_ENV['TRUE1']);
6472
$this->assertEquals(true, $_ENV['TRUE2']);
6573
$this->assertEquals(true, $_ENV['TRUE3']);
66-
$this->assertEquals(true, $_ENV['TRUE4']);
74+
$this->assertEquals("'true'", $_ENV['TRUE4']);// Since we don't have the QuotedProcessor::class this will be the result
6775
$this->assertEquals('1', $_ENV['TRUE5']);
6876
}
6977

@@ -72,9 +80,7 @@ public function testProcessBoolean()
7280
*/
7381
public function testDontProcessBoolean()
7482
{
75-
(new DotEnv($this->env('.env.boolean'), [
76-
Option::PROCESS_BOOLEANS => false
77-
]))->load();
83+
(new DotEnv($this->env('.env.boolean'), []))->load();
7884

7985
$this->assertEquals('false', $_ENV['FALSE1']);
8086

@@ -87,7 +93,7 @@ public function testDontProcessBoolean()
8793
public function testProcessQuotes()
8894
{
8995
(new DotEnv($this->env('.env.quotes'), [
90-
Option::PROCESS_QUOTES => true
96+
QuotedProcessor::class
9197
]))->load();
9298

9399
$this->assertEquals('q1', $_ENV['QUOTED1']);
@@ -104,9 +110,7 @@ public function testProcessQuotes()
104110
*/
105111
public function testDontProcessQuotes()
106112
{
107-
(new DotEnv($this->env('.env.quotes'), [
108-
Option::PROCESS_QUOTES => false
109-
]))->load();
113+
(new DotEnv($this->env('.env.quotes'), []))->load();
110114

111115
$this->assertEquals('"q1"', $_ENV['QUOTED1']);
112116
$this->assertEquals('\'q2\'', $_ENV['QUOTED2']);

‎tests/env/.env.default

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ DATABASE_DNS=mysql:host=localhost;dbname=test;
44
DATABASE_USER=root
55
DATABASE_PASSWORD = password
66
#GOOGLE_API=DJfa7czhKaJ0Pig6j9XpSjT6NpXZUZwK
7-
# GOOGLE_MANAGER_KEY=P7RkBUQHIkPUEPy3yCTT4gGYa2DjRth8
7+
# GOOGLE_MANAGER_KEY=P7RkBUQHIkPUEPy3yCTT4gGYa2DjRth8
8+
BOOLEAN_LITERAL=true
9+
BOOLEAN_QUOTED="true"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /