Skip to content

Commit

Permalink
add pass __DIR__
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Mar 21, 2021
1 parent 144e3fc commit 1cfc88a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ _not_constant='not_constant'
foo='--"bar"--'
foo1="--\"bar\"--"
foo2="--"bar"--"
foo3="[__DIR__]/test"
20 changes: 20 additions & 0 deletions dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ public function initEnvFile()
}
}

public function replace(&$arr, $replaces, $cb)
{
array_walk_recursive($arr, function(&$item, $key) use ($replaces, $cb){
foreach ($replaces as $replaceKey) {
if (false === strpos($item, '['.$replaceKey.']')) {
continue;
}
$item = str_replace(
'['.$replaceKey.']',
$cb(["item"=>$item, "itemKey"=>$key, "replaceKey"=>$replaceKey]),
$item
);
}
});
}

public function toPMVC($file,$prefix='')
{
$arr = $this->getUnderscoreToArray($file);
Expand All @@ -64,6 +80,10 @@ public function toPMVC($file,$prefix='')
if (isset($arr['_'])) {
$this->_processConstantArray($arr);
}
$to = ["__DIR__"=>dirname(\PMVC\realpath($file))];
$this->replace($arr, ["__DIR__"], function($payload) use($to) {
return \PMVC\get($to, $payload["replaceKey"]);
});
\PMVC\option('set', $arr);
}

Expand Down
6 changes: 6 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ function testToPMVC()
$this->assertEquals('--bar--', \PMVC\getoption('foo2'), "Without escape double quot");
}

function testToPMVCWithDirName()
{
$p = PMVC\plug($this->_plug, ['./.env.example']);
$this->assertEquals(\PMVC\realPath("./").'/test', \PMVC\getoption('foo3'));
}

function testGetUnderscoreToArray()
{
# load dot file
Expand Down

0 comments on commit 1cfc88a

Please sign in to comment.