Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit 3172d73

Browse files
committed
class move ..
1 parent 7b1b8be commit 3172d73

File tree

2 files changed

+131
-29
lines changed

2 files changed

+131
-29
lines changed

src/PhpDotEnv.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/6/17
6+
* Time: 上午10:11
7+
*/
8+
9+
namespace Toolkit\PhpUtil;
10+
11+
/**
12+
* Class PhpDotEnv - local env read
13+
* @package Toolkit\PhpUtil
14+
*
15+
* in local config file `.env` (must is 'ini' format):
16+
* ```ini
17+
* APP_ENV=dev
18+
* DEBUG=true
19+
* ... ...
20+
* ```
21+
*
22+
* IN CODE:
23+
*
24+
* ```php
25+
* PhpDotEnv::load(__DIE__);
26+
* env('DEBUG', false);
27+
* env('APP_ENV', 'pdt');
28+
* ```
29+
*/
30+
final class PhpDotEnv
31+
{
32+
const FULL_KEY = 'PHP_DOTENV_VARS';
33+
34+
/**
35+
* @param string $fileDir
36+
* @param string $fileName
37+
* @return static
38+
*/
39+
public static function load(string $fileDir, string $fileName = '.env')
40+
{
41+
return new self($fileDir, $fileName);
42+
}
43+
44+
/**
45+
* constructor.
46+
* @param string $fileDir
47+
* @param string $fileName
48+
*/
49+
public function __construct(string $fileDir, string $fileName = '.env')
50+
{
51+
$file = $fileDir . DIRECTORY_SEPARATOR . ($fileName ?: '.env');
52+
53+
$this->add($file);
54+
}
55+
56+
/**
57+
* @param string $file
58+
*/
59+
public function add(string $file)
60+
{
61+
if (\is_file($file) && \is_readable($file)) {
62+
$this->settingEnv(\parse_ini_file($file));
63+
}
64+
}
65+
66+
/**
67+
* setting env data
68+
* @param array $data
69+
*/
70+
private function settingEnv(array $data)
71+
{
72+
$loadedVars = \array_flip(\explode(',', \getenv(self::FULL_KEY)));
73+
unset($loadedVars['']);
74+
75+
foreach ($data as $name => $value) {
76+
if (\is_int($name) || !\is_string($value)) {
77+
continue;
78+
}
79+
80+
$name = \strtoupper($name);
81+
$notHttpName = 0 !== \strpos($name, 'HTTP_');
82+
83+
// don't check existence with getenv() because of thread safety issues
84+
if ((isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName)) && !isset($loadedVars[$name])) {
85+
continue;
86+
}
87+
88+
// is a constant var
89+
if ($value && \defined($value)) {
90+
$value = \constant($value);
91+
}
92+
93+
// eg: "FOO=BAR"
94+
\putenv("$name=$value");
95+
$_ENV[$name] = $value;
96+
97+
if ($notHttpName) {
98+
$_SERVER[$name] = $value;
99+
}
100+
101+
$loadedVars[$name] = true;
102+
}
103+
104+
if ($loadedVars) {
105+
$loadedVars = \implode(',', \array_keys($loadedVars));
106+
\putenv(self::FULL_KEY . "=$loadedVars");
107+
$_ENV[self::FULL_KEY] = $loadedVars;
108+
$_SERVER[self::FULL_KEY] = $loadedVars;
109+
}
110+
}
111+
}

src/PhpHelper.php

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@
55

66
namespace Toolkit\PhpUtil;
77

8-
use Inhere\Exceptions\ExtensionMissException;
98

109
/**
1110
* Class PhpHelper
1211
* @package Toolkit\PhpUtil
1312
*/
1413
class PhpHelper
1514
{
15+
/**
16+
* get $_SERVER value
17+
* @param string $name
18+
* @param string $default
19+
* @return mixed
20+
*/
21+
public static function serverParam(string $name, $default = '')
22+
{
23+
$name = \strtoupper($name);
24+
25+
return $_SERVER[$name] ?? $default;
26+
}
27+
1628
/**
1729
* @param callable|mixed $cb
1830
* @param array ...$args
@@ -22,13 +34,13 @@ public static function call($cb, ...$args)
2234
{
2335
if (\is_string($cb)) {
2436
// function
25-
if (strpos($cb, '::') === false) {
37+
if (\strpos($cb, '::') === false) {
2638
return $cb(...$args);
2739
}
2840

2941
// ClassName::method
30-
$cb = explode('::', $cb, 2);
31-
} elseif (\is_object($cb) && method_exists($cb, '__invoke')) {
42+
$cb = \explode('::', $cb, 2);
43+
} elseif (\is_object($cb) && \method_exists($cb, '__invoke')) {
3244
return $cb(...$args);
3345
}
3446

@@ -81,31 +93,12 @@ public static function runtime($startTime, $startMem, array $info = [], $realUsa
8193
return $info;
8294
}
8395

84-
/**
85-
* 根据服务器设置得到文件上传大小的最大值
86-
* @param int $max_size optional max file size
87-
* @return int max file size in bytes
88-
*/
89-
public static function getMaxUploadSize($max_size = 0): int
90-
{
91-
$post_max_size = FormatHelper::convertBytes(ini_get('post_max_size'));
92-
$upload_max_fileSize = FormatHelper::convertBytes(ini_get('upload_max_filesize'));
93-
94-
if ($max_size > 0) {
95-
$result = min($post_max_size, $upload_max_fileSize, $max_size);
96-
} else {
97-
$result = min($post_max_size, $upload_max_fileSize);
98-
}
99-
100-
return $result;
101-
}
102-
10396
/**
10497
* @return array
10598
*/
10699
public static function getUserConstants(): array
107100
{
108-
$const = get_defined_constants(true);
101+
$const = \get_defined_constants(true);
109102

110103
return $const['user'] ?? [];
111104
}
@@ -121,7 +114,7 @@ public static function dumpVars(...$args): string
121114
var_dump(...$args);
122115
$string = ob_get_clean();
123116

124-
return preg_replace("/=>\n\s+/", '=> ', $string);
117+
return \preg_replace("/=>\n\s+/", '=> ', $string);
125118
}
126119

127120
/**
@@ -137,7 +130,7 @@ public static function printVars(...$args): string
137130
$string .= print_r($arg, 1) . PHP_EOL;
138131
}
139132

140-
return preg_replace("/Array\n\s+\(/", 'Array (', $string);
133+
return \preg_replace("/Array\n\s+\(/", 'Array (', $string);
141134
}
142135

143136
/**
@@ -146,9 +139,7 @@ public static function printVars(...$args): string
146139
*/
147140
public static function exportVar($var)
148141
{
149-
return var_export($var, true);
142+
return \var_export($var, true);
150143
}
151144

152-
153-
154145
}

0 commit comments

Comments
 (0)