Skip to content

Commit 1bf9cfd

Browse files
committed
Initial commit
0 parents  commit 1bf9cfd

File tree

13 files changed

+536
-0
lines changed

13 files changed

+536
-0
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = false
5+
6+
[*]
7+
indent_style = tab
8+
indent_size = 2
9+
charset = "utf-8"
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.yml]
15+
indent_style = space
16+
indent_size = 2

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
composer.lock
3+
.DS_Store

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
8+
before_script:
9+
- composer self-update
10+
- composer install
11+
- pear install --alldeps PHP_CodeSniffer
12+
- phpenv rehash
13+
14+
script:
15+
- phpcs --standard=psr2 src/
16+
- phpunit --coverage-text

CONTRIBUTING.markdown

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# How to contribute
2+
3+
Dotenv loves to welcome your contributions. There are several ways to help out:
4+
5+
* Create a ticket in GitHub, if you have found a bug
6+
* Write testcases for open bug tickets
7+
* Write patches for open bug/feature tickets, preferably with testcases included
8+
* Contribute to the [documentation](https://github.com/josegonzalez/php-dotenv/tree/gh-pages)
9+
10+
There are a few guidelines that we need contributors to follow so that we have a
11+
chance of keeping on top of things.
12+
13+
## Getting Started
14+
15+
* Make sure you have a [GitHub account](https://github.com/signup/free)
16+
* Submit a ticket for your issue, assuming one does not already exist.
17+
* Clearly describe the issue including steps to reproduce when it is a bug.
18+
* Make sure you fill in the earliest version that you know has the issue.
19+
* Fork the repository on GitHub.
20+
21+
## Making Changes
22+
23+
* Create a topic branch from where you want to base your work.
24+
* This is usually the develop branch
25+
* To quickly create a topic branch based on master; `git branch
26+
master/my_contribution master` then checkout the new branch with `git
27+
checkout master/my_contribution`. Better avoid working directly on the
28+
`master` branch, to avoid conflicts if you pull in updates from origin.
29+
* Make commits of logical units.
30+
* Check for unnecessary whitespace with `git diff --check` before committing.
31+
* Use descriptive commit messages and reference the #ticket number
32+
* Core testcases should continue to pass. You can run tests locally or enable
33+
[travis-ci](https://travis-ci.org/) for your fork, so all tests and codesniffs
34+
will be executed.
35+
36+
## Which branch to base the work
37+
38+
* Bugfix branches will be based on develop branch.
39+
* New features that are backwards compatible will be based on develop branch
40+
* New features or other non-BC changes will go in the next major release branch.
41+
42+
## Submitting Changes
43+
44+
* Push your changes to a topic branch in your fork of the repository.
45+
* Submit a pull request to the repository with the correct target branch.
46+
47+
## Testcases and codesniffer
48+
49+
Dotenv tests requires [PHPUnit](http://www.phpunit.de/manual/current/en/installation.html)
50+
3.5 or higher. To run the testcases locally use the following command:
51+
52+
phpunit --coverage-text
53+
54+
To run the sniffs for PSR2 coding standards
55+
56+
phpcs --standard=psr2 src/
57+
58+
# Additional Resources
59+
60+
* [Bug tracker](https://github.com/josegonzalez/php-dotenv/issues)
61+
* [General GitHub documentation](https://help.github.com/)
62+
* [GitHub pull request documentation](https://help.github.com/send-pull-requests/)

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Jose Diaz-Gonzalez <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "josegonzalez/dotenv",
3+
"type": "library",
4+
"description": "dotenv file parsing for PHP",
5+
"keywords": ["php", "dotenv", "configuration"],
6+
"homepage": "https://github.com/josegonzalez/php-dotenv",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Jose Diaz-Gonzalez",
11+
"email": "[email protected]",
12+
"homepage": "http://josediazgonzalez.com",
13+
"role": "Maintainer"
14+
}
15+
],
16+
"require": {
17+
"php": ">=5.3.0"
18+
},
19+
"autoload": {
20+
"psr-0": {
21+
"josegonzalez\\Dotenv": ["src", "tests"]
22+
}
23+
}
24+
}

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true" bootstrap="vendor/autoload.php">
3+
<testsuites>
4+
<testsuite name="ColorExtractor Test Suite">
5+
<directory>tests/josegonzalez/Dotenv</directory>
6+
</testsuite>
7+
</testsuites>
8+
<filter>
9+
<whitelist>
10+
<directory suffix=".php">src/josegonzalez/Dotenv</directory>
11+
</whitelist>
12+
</filter>
13+
</phpunit>

src/josegonzalez/Dotenv/Load.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
namespace josegonzalez\Dotenv;
4+
5+
use InvalidArgumentException;
6+
use LogicException;
7+
use JsonSerializable;
8+
use RuntimeException;
9+
10+
class Load implements JsonSerializable {
11+
12+
protected $_filepath = null;
13+
14+
protected $_environment = null;
15+
16+
public function __construct($filepath = null) {
17+
$this->setFilepath($filepath);
18+
return $this;
19+
}
20+
21+
public function filepath() {
22+
return $this->_filepath;
23+
}
24+
25+
public function setFilepath($filepath = null) {
26+
if ($filepath == null) {
27+
$filepath = __DIR__ . DIRECTORY_SEPARATOR . '.env';
28+
}
29+
$this->_filepath = $filepath;
30+
return $this;
31+
}
32+
33+
public static function load($options = null) {
34+
$filepath = null;
35+
if (is_string($options)) {
36+
$filepath = $options;
37+
$options = array();
38+
}
39+
40+
$dotenv = new \josegonzalez\Dotenv\Load($filepath);
41+
$dotenv->parse();
42+
if (array_key_exists('expect', $options)) {
43+
$dotenv->expect($options['expect']);
44+
}
45+
46+
if (array_key_exists('define', $options)) {
47+
$dotenv->define();
48+
}
49+
50+
if (array_key_exists('toEnv', $options)) {
51+
$dotenv->toEnv($options['toEnv']);
52+
}
53+
54+
if (array_key_exists('toServer', $options)) {
55+
$dotenv->toServer($options['toServer']);
56+
}
57+
58+
return $dotenv;
59+
}
60+
61+
public function parse() {
62+
if (!file_exists($this->_filepath)) {
63+
throw new InvalidArgumentException(sprintf("Environment file '%s' is not found.", $this->_filepath));
64+
}
65+
66+
if (!is_readable($this->_filepath)) {
67+
throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath));
68+
}
69+
70+
$fc = file_get_contents($this->_filepath);
71+
if ($fc === false) {
72+
throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath));
73+
}
74+
75+
$lines = explode(PHP_EOL, $fc);
76+
77+
$this->_environment = array();
78+
foreach ($lines as $line) {
79+
if (empty($line)) {
80+
continue;
81+
}
82+
83+
if (!preg_match('/(?:export )?([a-zA-Z_][a-zA-Z0-9_]*)=(.*)/', $line, $matches)) {
84+
continue;
85+
}
86+
87+
$key = $matches[1];
88+
$value = $matches[2];
89+
if (preg_match('/^\'(.*)\'$/', $value, $matches)) {
90+
$value = $matches[1];
91+
} elseif (preg_match('/^"(.*)"$/', $value, $matches)) {
92+
$value = $matches[1];
93+
}
94+
95+
$this->_environment[$key] = $value;
96+
}
97+
98+
return $this;
99+
}
100+
101+
public function expect() {
102+
$this->requireParse('expect');
103+
104+
$args = func_get_args();
105+
if (count($args) == 0) {
106+
throw new InvalidArgumentException("No arguments were passed to expect()");
107+
}
108+
109+
if (isset($args[0]) && is_array($args[0])) {
110+
$args = $args[0];
111+
}
112+
113+
$keys = (array) $args;
114+
$missingEnvs = array();
115+
116+
foreach ($keys as $key) {
117+
if (!isset($this->_environment[$key])) {
118+
$missingEnvs[] = $key;
119+
}
120+
}
121+
122+
if (!empty($missingEnvs)) {
123+
throw new RuntimeException(sprintf("Required ENV vars missing: ['%s']", implode("', '", $missingEnvs)));
124+
}
125+
126+
return $this;
127+
}
128+
129+
public function define() {
130+
$this->requireParse('define');
131+
foreach ($this->_environment as $key => $value) {
132+
if (defined($key)) {
133+
throw new LogicException(sprintf('Key "%s" has already been defined', $key));
134+
}
135+
136+
define($key, $value);
137+
}
138+
139+
return $this;
140+
}
141+
142+
public function toEnv($overwrite = false) {
143+
$this->requireParse('toEnv');
144+
foreach ($this->_environment as $key => $value) {
145+
if (isset($_ENV[$key]) && !$overwrite) {
146+
throw new LogicException(sprintf('Key "%s" has already been defined in $_ENV', $key));
147+
}
148+
149+
$_ENV[$key] = $value;
150+
}
151+
152+
return $this;
153+
}
154+
155+
public function toServer($overwrite = false) {
156+
$this->requireParse('toServer');
157+
foreach ($this->_environment as $key => $value) {
158+
if (isset($_SERVER[$key]) && !$overwrite) {
159+
throw new LogicException(sprintf('Key "%s" has already been defined in $_SERVER', $key));
160+
}
161+
162+
$_SERVER[$key] = $value;
163+
}
164+
165+
return $this;
166+
}
167+
168+
public function toArray() {
169+
$this->requireParse('environment');
170+
return $this->_environment;
171+
}
172+
173+
public function jsonSerialize() {
174+
return $this->toArray();
175+
}
176+
177+
public function __toString() {
178+
return json_encode($this, JSON_PRETTY_PRINT);
179+
}
180+
181+
protected function requireParse($method) {
182+
if (!is_array($this->_environment)) {
183+
throw new LogicException(sprintf('Environment must be parsed before calling %s', $method));
184+
}
185+
}
186+
187+
}

0 commit comments

Comments
 (0)