Movatterモバイル変換


[0]ホーム

URL:


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

Commitfa4ffdf

Browse files
committed
[Env] added the component
1 parente66e6af commitfa4ffdf

File tree

13 files changed

+722
-0
lines changed

13 files changed

+722
-0
lines changed

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"symfony/debug-bundle":"self.version",
4141
"symfony/doctrine-bridge":"self.version",
4242
"symfony/dom-crawler":"self.version",
43+
"symfony/env":"self.version",
4344
"symfony/event-dispatcher":"self.version",
4445
"symfony/expression-language":"self.version",
4546
"symfony/filesystem":"self.version",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
3.3.0
5+
-----
6+
7+
* added the component
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Dotenv;
13+
14+
useSymfony\Component\Dotenv\Exception\FormatException;
15+
useSymfony\Component\Dotenv\Exception\FormatExceptionContext;
16+
useSymfony\Component\Dotenv\Exception\PathException;
17+
useSymfony\Component\Process\Exception\ExceptionInterfaceasProcessException;
18+
useSymfony\Component\Process\Process;
19+
20+
/**
21+
* Manages .env files.
22+
*
23+
* @author Fabien Potencier <fabien@symfony.com>
24+
*/
25+
finalclass Dotenv
26+
{
27+
constVARNAME_REGEX ='(?i:[A-Z][A-Z0-9_]*+)';
28+
constSTATE_VARNAME =0;
29+
constSTATE_VALUE =1;
30+
31+
private$path;
32+
private$cursor;
33+
private$lineno;
34+
private$data;
35+
private$end;
36+
private$state;
37+
private$values;
38+
39+
/**
40+
* Loads one or several .env files.
41+
*
42+
* @param ...string A list of files to load
43+
*
44+
* @throws FormatException when a file has a syntax error
45+
* @throws PathException when a file does not exist or is not readable
46+
*/
47+
publicfunctionload(/*...$paths*/)
48+
{
49+
// func_get_args() to be replaced by a variadic argument for Symfony 4.0
50+
foreach (func_get_args()as$path) {
51+
if (!is_readable($path)) {
52+
thrownewPathException($path);
53+
}
54+
55+
$this->populate($this->parse(file_get_contents($path),$path));
56+
}
57+
}
58+
59+
/**
60+
* Sets values as environment variables (via putenv, $_ENV, and $_SERVER).
61+
*
62+
* Note that existing environment variables are never overridden.
63+
*
64+
* @param array An array of env variables
65+
*/
66+
publicfunctionpopulate($values)
67+
{
68+
foreach ($valuesas$name =>$value) {
69+
if (isset($_ENV[$name]) ||false !==getenv($name)) {
70+
continue;
71+
}
72+
73+
putenv("$name=$value");
74+
$_ENV[$name] =$value;
75+
$_SERVER[$name] =$value;
76+
}
77+
}
78+
79+
/**
80+
* Parses the contents of an .env file.
81+
*
82+
* @param string $data The data to be parsed
83+
* @param string $path The original file name where data where stored (used for more meaningful error messages)
84+
*
85+
* @return array An array of env variables
86+
*
87+
* @throws FormatException when a file has a syntax error
88+
*/
89+
publicfunctionparse($data,$path ='.env')
90+
{
91+
$this->path =$path;
92+
$this->data =str_replace(array("\r\n","\r"),"\n",$data);
93+
$this->lineno =1;
94+
$this->cursor =0;
95+
$this->end =strlen($this->data);
96+
$this->state =self::STATE_VARNAME;
97+
$this->values =array();
98+
$name =$value ='';
99+
100+
$this->skipEmptyLines();
101+
102+
while ($this->cursor <$this->end) {
103+
switch ($this->state) {
104+
caseself::STATE_VARNAME:
105+
$name =$this->lexVarname();
106+
$this->state =self::STATE_VALUE;
107+
break;
108+
109+
caseself::STATE_VALUE:
110+
$this->values[$name] =$this->lexValue();
111+
$this->state =self::STATE_VARNAME;
112+
break;
113+
}
114+
}
115+
116+
if (self::STATE_VALUE ===$this->state) {
117+
$this->values[$name] ='';
118+
}
119+
120+
try {
121+
return$this->values;
122+
}finally {
123+
$this->values =array();
124+
}
125+
}
126+
127+
privatefunctionlexVarname()
128+
{
129+
// var name + optional export
130+
if (!preg_match('/(export[ \t]++)?('.self::VARNAME_REGEX.')/A',$this->data,$matches,0,$this->cursor)) {
131+
throw$this->createFormatException('Invalid character in variable name');
132+
}
133+
$this->moveCursor($matches[0]);
134+
135+
if ($this->cursor ===$this->end ||"\n" ===$this->data[$this->cursor] ||'#' ===$this->data[$this->cursor]) {
136+
if ($matches[1]) {
137+
throw$this->createFormatException('Unable to unset an environment variable');
138+
}
139+
140+
throw$this->createFormatException('Missing = in the environment variable declaration');
141+
}
142+
143+
if ('' ===$this->data[$this->cursor] ||"\t" ===$this->data[$this->cursor]) {
144+
throw$this->createFormatException('Whitespace are not supported after the variable name');
145+
}
146+
147+
if ('=' !==$this->data[$this->cursor]) {
148+
throw$this->createFormatException('Missing = in the environment variable declaration');
149+
}
150+
++$this->cursor;
151+
152+
return$matches[2];
153+
}
154+
155+
privatefunctionlexValue()
156+
{
157+
if (preg_match('/[ \t]*+(?:#.*)?$/Am',$this->data,$matches,null,$this->cursor)) {
158+
$this->moveCursor($matches[0]);
159+
$this->skipEmptyLines();
160+
161+
return'';
162+
}
163+
164+
if ('' ===$this->data[$this->cursor] ||"\t" ===$this->data[$this->cursor]) {
165+
throw$this->createFormatException('Whitespace are not supported before the value');
166+
}
167+
168+
$value ='';
169+
$singleQuoted =false;
170+
$notQuoted =false;
171+
if ("'" ===$this->data[$this->cursor]) {
172+
$singleQuoted =true;
173+
++$this->cursor;
174+
while ("\n" !==$this->data[$this->cursor]) {
175+
if ("'" ===$this->data[$this->cursor]) {
176+
if ($this->cursor +1 ===$this->end) {
177+
break;
178+
}
179+
if ("'" !==$this->data[$this->cursor +1]) {
180+
break;
181+
}
182+
183+
++$this->cursor;
184+
}
185+
$value .=$this->data[$this->cursor];
186+
++$this->cursor;
187+
188+
if ($this->cursor ===$this->end) {
189+
throw$this->createFormatException('Missing quote to end the value');
190+
}
191+
}
192+
if ("\n" ===$this->data[$this->cursor]) {
193+
throw$this->createFormatException('Missing quote to end the value');
194+
}
195+
++$this->cursor;
196+
}elseif ('"' ===$this->data[$this->cursor]) {
197+
++$this->cursor;
198+
while ('"' !==$this->data[$this->cursor] || ('\\' ===$this->data[$this->cursor -1] &&'\\' !==$this->data[$this->cursor -2])) {
199+
$value .=$this->data[$this->cursor];
200+
++$this->cursor;
201+
202+
if ($this->cursor ===$this->end) {
203+
throw$this->createFormatException('Missing quote to end the value');
204+
}
205+
}
206+
if ("\n" ===$this->data[$this->cursor]) {
207+
throw$this->createFormatException('Missing quote to end the value');
208+
}
209+
++$this->cursor;
210+
$value =str_replace(array('\\\\','\\"','\r','\n'),array('\\','"',"\r","\n"),$value);
211+
}else {
212+
$notQuoted =true;
213+
$prevChr =$this->data[$this->cursor -1];
214+
while ($this->cursor <$this->end &&"\n" !==$this->data[$this->cursor] && !(('' ===$prevChr ||"\t" ===$prevChr) &&'#' ===$this->data[$this->cursor])) {
215+
$value .=$prevChr =$this->data[$this->cursor];
216+
++$this->cursor;
217+
}
218+
$value =rtrim($value);
219+
}
220+
221+
$this->skipEmptyLines();
222+
223+
$currentValue =$value;
224+
if (!$singleQuoted) {
225+
$value =$this->resolveVariables($value);
226+
$value =$this->resolveCommands($value);
227+
}
228+
229+
if ($notQuoted &&$currentValue ==$value &&preg_match('/\s+/',$value)) {
230+
throw$this->createFormatException('A value containing spaces must be surrounded by quotes');
231+
}
232+
233+
return$value;
234+
}
235+
236+
privatefunctionskipWhitespace()
237+
{
238+
$this->cursor +=strspn($this->data,"\t",$this->cursor);
239+
}
240+
241+
privatefunctionskipEmptyLines()
242+
{
243+
if (preg_match('/(?:\s*+(?:#[^\n]*+)?+)++/A',$this->data,$match,null,$this->cursor)) {
244+
$this->moveCursor($match[0]);
245+
}
246+
}
247+
248+
privatefunctionresolveCommands($value)
249+
{
250+
if (false ===strpos($value,'$')) {
251+
return$value;
252+
}
253+
254+
$regex ='/
255+
(\\\\)? # escaped with a backslash?
256+
\$
257+
(?<cmd>
258+
\( # require opening parenthesis
259+
([^()]|\g<cmd>)+ # allow any number of non-parens, or balanced parens (by nesting the <cmd> expression recursively)
260+
\) # require closing paren
261+
)
262+
/x';
263+
264+
returnpreg_replace_callback($regex,function ($matches) {
265+
if ('\\' ===$matches[1]) {
266+
returnsubstr($matches[0],1);
267+
}
268+
269+
if ('\\' ===DIRECTORY_SEPARATOR) {
270+
thrownew \LogicException('Resolving commands is not supported on Windows.');
271+
}
272+
273+
if (!class_exists(Process::class)) {
274+
thrownew \LogicException('Resolving commands requires the Symfony Process component.');
275+
}
276+
277+
$process =newProcess('echo'.$matches[0]);
278+
$process->inheritEnvironmentVariables(true);
279+
$process->setEnv($this->values);
280+
try {
281+
$process->mustRun();
282+
}catch (ProcessException$e) {
283+
throw$this->createFormatException(sprintf('Issue expanding a command (%s)',$process->getErrorOutput()));
284+
}
285+
286+
returnpreg_replace('/[\r\n]+$/','',$process->getOutput());
287+
},$value);
288+
}
289+
290+
privatefunctionresolveVariables($value)
291+
{
292+
if (false ===strpos($value,'$')) {
293+
return$value;
294+
}
295+
296+
$regex ='/
297+
(\\\\)? # escaped with a backslash?
298+
\$
299+
(?!\() # no opening parenthesis
300+
(\{)? # optional brace
301+
('.self::VARNAME_REGEX.') # var name
302+
(\})? # optional closing brace
303+
/x';
304+
305+
$value =preg_replace_callback($regex,function ($matches) {
306+
if ('\\' ===$matches[1]) {
307+
returnsubstr($matches[0],1);
308+
}
309+
310+
if ('{' ===$matches[2] && !isset($matches[4])) {
311+
throw$this->createFormatException('Unclosed braces on variable expansion');
312+
}
313+
314+
$name =$matches[3];
315+
$value =isset($this->values[$name]) ?$this->values[$name] : (isset($_ENV[$name]) ?isset($_ENV[$name]) : (string)getenv($name));
316+
317+
if (!$matches[2] &&isset($matches[4])) {
318+
$value .='}';
319+
}
320+
321+
return$value;
322+
},$value);
323+
324+
// unescape $
325+
returnstr_replace('\\$','$',$value);
326+
}
327+
328+
privatefunctionmoveCursor($text)
329+
{
330+
$this->cursor +=strlen($text);
331+
$this->lineno +=substr_count($text,"\n");
332+
}
333+
334+
privatefunctioncreateFormatException($message)
335+
{
336+
returnnewFormatException($message,newFormatExceptionContext($this->data,$this->path,$this->lineno,$this->cursor));
337+
}
338+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Dotenv\Exception;
13+
14+
/**
15+
* Interface for exceptions.
16+
*
17+
* @author Fabien Potencier <fabien@symfony.com>
18+
*/
19+
interface ExceptionInterface
20+
{
21+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp