-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.php
More file actions
executable file
·150 lines (133 loc) · 4.05 KB
/
function.php
File metadata and controls
executable file
·150 lines (133 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
function search_include_class($path, $class = '') {
foreach (scandir($path) as $dir) {
if ($dir == ".." || $dir == '.') continue;
$dir = $path . "/" . $dir;
if (is_dir($dir)) search_include_class($dir, $class);
if (is_readable($dir)) {
$file = file($dir);
foreach ($file as $row)
if (str_contains($row, "class " . $class)) require_once($dir);
}
}
}
function env($constans = null, $default = null) : ?string
{
if(!$constans) return null;
if (!file_exists(ROOT . DS. ENV)) {
throw new Pet\Errors\AppException("There is no .env file in the project root");
}
$env = file(ROOT . DS . ENV);
foreach ($env as $str) {
if (str_contains(trim($str), '#') && strpos(trim($str), "#") == 0) continue;
if (!str_contains($str, '=')) continue;
$param = explode('=', $str);
if (trim($param[0]) == trim($constans)) {
$param = trim(str_replace([';', '"', "'",], '', $param[1]));
return $param == '' ? $default : $param;
}
}
return $default;
}
function setConstantEnv($ROOT): void
{
if (!file_exists($ROOT . DS . ENV)) {
throw new Pet\Errors\AppException("There is no .env file in the project root");
}
$env = file($ROOT . DS . ENV);
foreach ($env as $str) {
if (str_contains(trim($str), '#') && strpos(trim($str), "#") == 0) continue;
if (str_contains($str, '=')) {
$param = explode('=', $str);
$value = trim(str_replace([';', '"', "'",], '', $param[1]));
$value = serializeenv($value);
if (!defined(trim($param[0]))) {
define(trim($param[0]), ($value == '' ? null : $value));
}
}
}
}
function serializeenv(string $valueEnv){
preg_match_all("/\[[\w]{1,}\]/", $valueEnv, $mathes);
$complect = $mathes[0];
if (!empty($complect)) {
$res = $valueEnv;
foreach ($complect as $str) {
$nameConst = str_replace(['[', ']'], '', $str);
if (defined($nameConst)) {
$valueConst = constant($nameConst);
$res = str_replace('['.$nameConst.']', $valueConst, $res);
}
}
return $res;
}
return $valueEnv;
}
/**
* debaging
* dd
* @param mixed $vars
* @return void
*/
function dd(...$vars)
{
echo '<pre style="background: #0c0c0c; padding:20px; color: #067706;">';
foreach ($vars as $var) {
var_dump($var);
}
echo '</pre>';
die();
}
function svg($name): void
{
if (empty($name)) return;
$name = str_replace(".", DS, $name);
$name = SVG.DS.$name.'.svg';
if (!file_exists($name)){
throw new Pet\Errors\AppException("Not search file svg path: $name");
}
include $name;
}
function img($name, $ext = 'png'): string
{
if (empty($name)) return '';
$name = str_replace(".", DS, $name);
$name = IMG_RELAT.DS.$name.".$ext";
return $name;
}
function uploads($name, $ext = 'png'): string
{
if (empty($name)) return '';
$name = str_replace(".", DS, $name);
$name = UPLOADS.DS.$name.".$ext";
if (!file_exists($name)){
throw new Pet\Errors\AppException("Not search file png path: $name", 500);
}
return $name;
}
/**
* dirEach
*
* @param string $path
* @param mixed $callDir
* @param mixed $callFile
* @param string $error
* @return bool
*/
function dirEach(string $path, $callDir = null, $callFile = null, &$error = null) : bool
{
if (!is_dir($path)) {
$error = 'is not folder';
return false;
}
foreach (scandir($path) as $entity) {
if ($entity == '..' || $entity == '.') continue;
if (file_exists($path . DS . $entity) && $callFile != null) {
$callFile($entity);
}
if (is_dir($path . DS . $entity) && $callDir != null) {
$callDir($entity);
}
}
return true;
}