Skip to content

Commit 2287ec0

Browse files
committedOct 24, 2019
Initial commit
0 parents  commit 2287ec0

28 files changed

+650
-0
lines changed
 

‎.gitattributes

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.hack linguist-language=Hack
2+
3+
/tests export-ignore
4+
/docs export-ignore
5+
.travis.sh export-ignore
6+
.travis.yml export-ignore

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
vendor/
3+
*.hhast.parser-cache

‎.hhconfig

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
assume_php=false
2+
safe_array = true
3+
safe_vector_array = true
4+
unsafe_rx = false
5+
ignored_paths = [ "vendor/.+/tests/.+", "vendor/.+/bin/.+" ]

‎.travis.sh

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
set -ex
3+
apt update -y
4+
DEBIAN_FRONTEND=noninteractive apt install -y php-cli zip unzip
5+
hhvm --version
6+
php --version
7+
if [ ! -e .git/refs/heads/master ]; then
8+
# - Travis clones with `--branch`, then moves to a detached HEAD state
9+
# - if we're on a detached HEAD, Composer uses master to resolve branch
10+
# aliases.
11+
# So, create the master branch :p
12+
git branch master HEAD
13+
fi
14+
15+
(
16+
cd $(mktemp -d)
17+
curl https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
18+
)
19+
20+
if (hhvm --version | grep -q -- -dev); then
21+
# Doesn't exist in master, but keep it here so that we can test release
22+
# branches on nightlies too
23+
rm -f composer.lock
24+
fi
25+
26+
composer --version
27+
28+
composer install
29+
30+
composer check

‎.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
sudo: false
2+
dist: trusty
3+
language: generic
4+
services: docker
5+
env:
6+
- HHVM_VERSION=4.25-latest
7+
- HHVM_VERSION=latest
8+
- HHVM_VERSION=nightly
9+
matrix:
10+
allow_failures:
11+
- env: HHVM_VERSION=nightly
12+
install:
13+
- docker pull hhvm/hhvm:$HHVM_VERSION
14+
script:
15+
- docker run --rm -w /var/source -v $(pwd):/var/source hhvm/hhvm:$HHVM_VERSION ./.travis.sh
16+
notifications:
17+
slack:
18+
secure: fZ74Yt9xxelrIZiWBe74X6zCNV+RCbI2aD0EjB8P6d3ovIdyHc+JOe/AFDwUwU8tuKBYt1DpiMnw/rFQQwu2Y6CQnAjgGtG+ScCCVhy5OJvHqFTmMt/XMs9Hrgdylak3IofaI6D/4Du+E9ZMXHgXGVgjQQr0SNMsj1s70sSd97oiW4t4Kn5hxlAbZK7EWCs2BWwyTtVJD96UOEJrBK59lD0wQvfv0wSV948Wwnms70cPgO26Fa+pdBGsv4Ho475Dzu/y4JuO/kqMMzodZtMSm7FNDrppwqgX3qYkfGBI/foQ5IpBn5gGcG5w3RhZLXsNhLDrULcEHtF1Ptfo5PQGUArN8KPRf91Mju2CGICg3wy6GMEm+iXHdPWzUkCaQPhw4ty6ix+fm2ELatXW4BGGXANJzL6UNUGuQPh4Z2oVeX8zEFpUAA+PJRzd6FPYdQDdI3Xj8P445x/KQ+Mg4f2wCR/YKTkjWbkYKzqvjvssgrDGkbQfhXWAOr5/NgKj0/vRovT66Tra14UncjZdM6yHUOqeMq5KfDboEBXoj7+jZG1cQmtSErUoFF2CUyI/Jqva7symsJbjOYSVTKv6BAgoL+CncdLcTPGFLzavLiavkqp4Gd3ErWoeOWqFY0ZYByY4cLcnwLtL0TrY6Y9fdzFeJLmN6xhxTteuw3Ils66K/fw=

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018-2019 Saif Eddin Gmati
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 all
13+
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 THE
21+
SOFTWARE.

‎README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<p align="center"><img src="https://avatars3.githubusercontent.com/u/45311177?s=200&v=4"></p>
2+
3+
<p align="center">
4+
<a href="https://travis-ci.org/nuxed/environment"><img src="https://travis-ci.org/nuxed/environment.svg" alt="Build Status"></a>
5+
<a href="https://packagist.org/packages/nuxed/environment"><img src="https://poser.pugx.org/nuxed/environment/d/total.svg" alt="Total Downloads"></a>
6+
<a href="https://packagist.org/packages/nuxed/environment"><img src="https://poser.pugx.org/nuxed/environment/v/stable.svg" alt="Latest Stable Version"></a>
7+
<a href="https://packagist.org/packages/nuxed/environment"><img src="https://poser.pugx.org/nuxed/environment/license.svg" alt="License"></a>
8+
</p>
9+
10+
# Nuxed Environment
11+
12+
Nuxed Environment provides functions that help you manage environment variables.
13+
14+
### Installation
15+
16+
This package can be installed with [Composer](https://getcomposer.org).
17+
18+
```console
19+
$ composer require nuxed/environment
20+
```
21+
22+
### Example
23+
24+
```hack
25+
use namespace Nuxed\Environment;
26+
27+
<<__EntryPoint>>
28+
async function main(): void {
29+
await Environment\load('.env');
30+
31+
if(!Environment\contains('APP_MODE')) {
32+
Environment\put('APP_MODE', 'prod');
33+
}
34+
35+
$mode = Environment\mode();
36+
switch($mode) {
37+
case Environment\Mode::Developement:
38+
// Dev
39+
case Environment\Mode::Production:
40+
// Prod
41+
case Environment\Mode::Test:
42+
// Test
43+
}
44+
}
45+
```
46+
47+
---
48+
49+
### Security
50+
51+
For information on reporting security vulnerabilities in Nuxed Environment, see [SECURITY.md](SECURITY.md).
52+
53+
---
54+
55+
### License
56+
57+
The Nuxed Environment library is open-sourced software licensed under the MIT-licensed.

‎SECURITY.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Security
2+
3+
If you discover a security vulnerability within Nuxed Environment, please send an e-mail to Saif Eddin Gmati via azjezz@protonmail.com.
4+
5+
Please withhold public disclosure until after we have addressed the vulnerability.
6+
7+
There are no hard and fast rules to determine if a bug is worth reporting as a security issue.

‎composer.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "nuxed/environment",
3+
"type": "library",
4+
"description": "Nuxed Environment - Manage Environment variables with ease",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "azjezz",
9+
"email": "azjezz@protonmail.com"
10+
},
11+
{
12+
"name": "Nuxed Community",
13+
"homepage": "https://github.com/nuxed/environment/graphs/contributors"
14+
}
15+
],
16+
"require": {
17+
"hhvm": "^4.25",
18+
"hhvm/hsl": "^4.25",
19+
"nuxed/filesystem": "^1.0"
20+
},
21+
"require-dev": {
22+
"facebook/fbexpect": "^2.7",
23+
"hhvm/hacktest": "^1.6",
24+
"hhvm/hhast": "^4.25"
25+
},
26+
"scripts": {
27+
"check": [
28+
"@type-check",
29+
"@lint",
30+
"@test"
31+
],
32+
"lint": "hhast-lint -v src/",
33+
"test": "hacktest tests/",
34+
"type-check": "hh_client src/"
35+
}
36+
}

‎hh_autoload.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"roots": [
3+
"src/"
4+
],
5+
"devRoots": [
6+
"tests/"
7+
],
8+
"devFailureHandler": "Facebook\\AutoloadMap\\HHClientFallbackHandler"
9+
}

‎hhast-lint.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"roots": [ "src/" ],
3+
"builtinLinters": "all",
4+
"disableAllAutoFixes": false,
5+
"disabledLinters": [
6+
"Facebook\\HHAST\\Linters\\AsyncFunctionAndMethodLinter"
7+
],
8+
"overrides": []
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace Nuxed\Environment\Exception;
2+
3+
<<__Sealed(InvalidArgumentException::class)>>
4+
interface IException {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Nuxed\Environment\Exception;
2+
3+
final class InvalidArgumentException
4+
extends \InvalidArgumentException
5+
implements IException {}

‎src/Nuxed/Environment/Mode.hack

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Nuxed\Environment;
2+
3+
enum Mode: int {
4+
/**
5+
* The application is running in dev environment.
6+
*/
7+
Development = 1;
8+
9+
/**
10+
* The application is running in production.
11+
*/
12+
Production = 2;
13+
14+
/**
15+
* The application is running tests.
16+
*/
17+
Test = 3;
18+
}
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
namespace Nuxed\Environment\_Private;
2+
3+
use namespace HH\Lib\{C, Regex, Str, Vec};
4+
use namespace Nuxed\Environment\Exception;
5+
6+
final abstract class Parser {
7+
/**
8+
* Parse the given environment variable entry into a name and value.
9+
*/
10+
public static function parse(string $entry): (string, ?string) {
11+
$name = $entry;
12+
$value = null;
13+
if (Str\contains($entry, '=')) {
14+
list($name, $value) = Vec\map(
15+
Str\split($entry, '=', 2),
16+
($str) ==> Str\trim($str),
17+
);
18+
}
19+
20+
return tuple(self::parseName($name), self::parseValue($value));
21+
}
22+
23+
public static function parseName(string $name): string {
24+
if ($name === '') {
25+
throw new Exception\InvalidArgumentException(
26+
'Failed to parse environment variable name : name cannot be empty.',
27+
);
28+
}
29+
30+
$name = Str\trim(
31+
Str\replace_every($name, dict['export ' => '', '\'' => '', '"' => '']),
32+
);
33+
if (!Regex\matches($name, re"~\A[a-zA-Z0-9_.]+\z~")) {
34+
throw new Exception\InvalidArgumentException(
35+
Str\format(
36+
'Failed to parse environment variable name : an invalid name ( %s ).',
37+
$name,
38+
),
39+
);
40+
}
41+
42+
return $name;
43+
}
44+
45+
/**
46+
* Strips quotes and comments from the environment variable value.
47+
*/
48+
public static function parseValue(?string $value): ?string {
49+
if ($value === null || Str\trim($value) === '') {
50+
return null;
51+
}
52+
53+
return C\reduce(
54+
Str\chunk($value),
55+
((string, State) $data, string $char): (string, State) ==> {
56+
switch ($data[1]) {
57+
case State::INITIAL:
58+
if ($char === '"' || $char === '\'') {
59+
return tuple($data[0], State::QUOTED);
60+
} else if ($char === '#') {
61+
return tuple($data[0], State::COMMENT);
62+
} else {
63+
return tuple($data[0].$char, State::UNQUOTED);
64+
}
65+
case State::UNQUOTED:
66+
if ($char === '#') {
67+
return tuple($data[0], State::COMMENT);
68+
} else if (\ctype_space($char)) {
69+
return tuple($data[0], State::WHITESPACE);
70+
}
71+
72+
return tuple($data[0].$char, State::UNQUOTED);
73+
case State::QUOTED:
74+
if ($char === $value[0]) {
75+
return tuple($data[0], State::WHITESPACE);
76+
} else if ($char === '\\') {
77+
return tuple($data[0], State::ESCAPE);
78+
}
79+
80+
return tuple($data[0].$char, State::QUOTED);
81+
case State::ESCAPE:
82+
if ($char === $value[0] || $char === '\\') {
83+
return tuple($data[0].$char, State::QUOTED);
84+
}
85+
86+
throw new Exception\InvalidArgumentException(
87+
Str\format(
88+
'Failed to parse environment entry : an unexpected escape sequence ( %s ).',
89+
$value,
90+
),
91+
);
92+
case State::WHITESPACE:
93+
if ($char === '#') {
94+
return tuple($data[0], State::COMMENT);
95+
} else if (!\ctype_space($char)) {
96+
throw new Exception\InvalidArgumentException(
97+
Str\format(
98+
'Failed to parse environment entry : unexpected whitespace ( %s ).',
99+
$value,
100+
),
101+
);
102+
}
103+
104+
return tuple($data[0], State::WHITESPACE);
105+
case State::COMMENT:
106+
return tuple($data[0], State::COMMENT);
107+
}
108+
},
109+
tuple('', State::INITIAL),
110+
)[0];
111+
}
112+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Nuxed\Environment\_Private;
2+
3+
enum State: int {
4+
INITIAL = 0;
5+
UNQUOTED = 1;
6+
QUOTED = 2;
7+
ESCAPE = 3;
8+
WHITESPACE = 4;
9+
COMMENT = 5;
10+
}

0 commit comments

Comments
 (0)