Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Dotenv
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Composer
library
Dotenv
Commits
6339ec8f
Commit
6339ec8f
authored
6 years ago
by
jurgenhaas
Browse files
Options
Downloads
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
c7dbe281
ccd81f17
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitlab-ci.yml
+0
-7
0 additions, 7 deletions
.gitlab-ci.yml
Dotenv.php
+67
-6
67 additions, 6 deletions
Dotenv.php
with
67 additions
and
13 deletions
.gitlab-ci.yml
deleted
100644 → 0
+
0
−
7
View file @
c7dbe281
Deploy
:
script
:
-
curl -XPOST -H'content-type:application/json' "https://packagist.org/api/update-package?username=${PACKAGIST_USERNAME}&apiToken=${PACKAGIST_API_TOKEN}" -d'{"repository":{"url":"'${CI_PROJECT_URL}'"}}'
only
:
-
tags
-
triggers
-
schedules
This diff is collapsed.
Click to expand it.
Dotenv.php
+
67
−
6
View file @
6339ec8f
...
...
@@ -76,8 +76,8 @@ final class Dotenv {
if
(
empty
(
$value
))
{
$value
=
$default
;
}
$this
->
put
(
$key
,
$value
);
}
$this
->
put
(
$key
,
$value
);
return
$value
;
}
...
...
@@ -92,19 +92,80 @@ final class Dotenv {
* The name of the environment variable.
* @param string $value
* The value of the environment variable.
* @param bool $overwrite
*/
public
function
put
(
$key
,
$value
)
{
public
function
put
(
$key
,
$value
,
$overwrite
=
FALSE
)
{
$envContent
=
file_exists
(
'.env'
)
?
file_get_contents
(
'.env'
)
:
''
;
$data
=
$this
->
dotenv
->
parse
(
$envContent
);
$key
=
strtoupper
(
$key
);
if
(
!
empty
(
getenv
(
$key
)
))
{
if
(
!
$overwrite
&&
isset
(
$data
[
$key
]
))
{
return
;
}
$env
=
file_exists
(
'.env'
)
?
file_get_contents
(
'.env'
)
:
''
;
$env
.
=
PHP_EOL
.
$key
.
'='
.
$value
.
PHP_EOL
;
file_put_contents
(
'.env'
,
$env
);
$data
[
$key
]
=
$value
;
$envContent
=
''
;
foreach
(
$data
as
$k
=>
$v
)
{
$envContent
.
=
$k
.
'='
.
$v
.
PHP_EOL
;
}
file_put_contents
(
'.env'
,
$envContent
);
$this
->
load
();
passthru
(
escapeshellcmd
(
'git -c "user.email=dotenv@lakedrops.com" ignore .env'
));
}
/**
* Replace environment variables in keys and values of a given array.
*
* Credit: https://github.com/composer/composer/pull/5837
*
* @param mixed $data
* The keyed array in which to replace environment variables.
*
* @return array
* The same data with environment variables being replaced.
*/
public
function
replaceEnvironmentVariables
(
$data
)
{
array_walk_recursive
(
$data
,
array
(
$this
,
'findEnvironmentVariables'
));
return
$data
;
}
/**
* Identify and replace environment variables in a key pair
*
* @param string $item
* @param string $key
*/
private
function
findEnvironmentVariables
(
&
$item
,
&
$key
)
{
/** @noinspection NotOptimalRegularExpressionsInspection */
$pattern
=
'@\{\$env:([A-Za-z0-9_]*)\}@i'
;
preg_match_all
(
$pattern
,
$item
,
$item_matches
);
preg_match_all
(
$pattern
,
$key
,
$key_matches
);
if
(
!
empty
(
$item_matches
[
1
]))
{
$item
=
$this
->
replaceItemEnvironmentVariables
(
$item
,
$item_matches
[
1
]);
}
if
(
!
empty
(
$key_matches
[
1
]))
{
$key
=
$this
->
replaceItemEnvironmentVariables
(
$key
,
$key_matches
[
1
]);
}
}
/**
* Replace environment variables in a string
*
* @param string $item
* @param array $matches
*
* @return string
*/
private
function
replaceItemEnvironmentVariables
(
$item
,
$matches
)
{
foreach
(
$matches
as
$var
)
{
$value
=
getenv
(
$var
);
if
(
!
$value
)
{
$value
=
''
;
}
$item
=
str_replace
(
'{$env:'
.
$var
.
'}'
,
$value
,
$item
);
}
return
$item
;
}
/**
* Load the local .env file.
*/
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment