-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.php
205 lines (178 loc) · 5.8 KB
/
core.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
declare(strict_types=1);
namespace Codefy\Framework\Helpers;
use Codefy\Framework\Application;
use Codefy\Framework\Codefy;
use Codefy\Framework\Factory\FileLoggerFactory;
use Codefy\Framework\Support\CodefyMailer;
use Qubus\Config\Collection;
use Qubus\Dbal\Connection;
use Qubus\Exception\Exception;
use Qubus\Expressive\OrmBuilder;
use ReflectionException;
use function dirname;
use function getcwd;
use function is_int;
use function Qubus\Security\Helpers\__observer;
use function Qubus\Support\Helpers\is_false__;
use function Qubus\Support\Helpers\is_null__;
use function file_exists;
use function in_array;
use function is_string;
use function realpath;
use function rtrim;
use function sprintf;
use function substr_count;
use function ucfirst;
/**
* Get the available container instance.
*
* @param string|null $name
* @param array $args
* @return mixed
*/
function app(?string $name = null, array $args = []): mixed
{
/** @var Application $app */
$app = get_fresh_bootstrap();
if (is_null__(var: $name)) {
return $app->getContainer();
}
return $app->getContainer()->make($name, $args);
}
/**
* Get the available config instance.
*
* @param string $key
* @param array|bool $set
* @return mixed
*/
function config(string $key, array|bool $set = false): mixed
{
if (!is_false__(var: $set)) {
app(name: Collection::class)->setConfigKey($key, $set);
return app(name: Collection::class)->getConfigKey($key);
}
return app(name: Collection::class)->getConfigKey($key);
}
/**
* Retrieve a fresh instance of the bootstrap.
*
* @return mixed
*/
function get_fresh_bootstrap(): mixed
{
if(file_exists(filename: $file = getcwd() . '/bootstrap/app.php')) {
return require(realpath(path: $file));
} elseif(file_exists(filename: $file = dirname(path: getcwd()) . '/bootstrap/app.php')) {
return require(realpath(path: $file));
} else {
return require(realpath(path: dirname(path: getcwd()) . '/bootstrap/app.php'));
}
}
/**
* Gets the value of an environment variable.
*
* @param string $key
* @param mixed|null $default
* @return mixed|null
*/
function env(string $key, mixed $default = null): mixed
{
return $_ENV[$key] ?? $default;
}
/**
* OrmBuilder database instance.
*
* @return OrmBuilder|null
* @throws Exception
*/
function orm(): ?OrmBuilder
{
return Codefy::$PHP->getDB();
}
/**
* Dbal database instance.
*
* @return Connection
* @throws Exception
*/
function dbal(): Connection
{
return Codefy::$PHP->getDbConnection();
}
/**
* Alternative to PHP's native mail function with SMTP support.
*
* This is a simple mail function to see for testing or for
* sending simple email messages.
*
* @param string|array $to Recipient(s)
* @param string $subject Subject of the email.
* @param string $message The email body.
* @param array $headers An array of headers.
* @param array $attachments An array of attachments.
* @return bool
* @throws Exception|ReflectionException|\PHPMailer\PHPMailer\Exception
*/
function mail(string|array $to, string $subject, string $message, array $headers = [], array $attachments = []): bool
{
// Instantiate CodefyMailer.
$instance = new CodefyMailer(config: app(name: 'codefy.config'));
// Set the mailer transport.
$func = sprintf('with%s', ucfirst(config(key: 'mailer.mail_transport')));
$instance = $instance->{$func}();
// Detect HTML markdown.
if (substr_count(haystack: $message, needle: '</') >= 1) {
$instance = $instance->withHtml(isHtml: true);
}
// Build recipient(s).
$instance = $instance->withTo(address: $to);
// Set from name and from email from environment variables.
$fromName = __observer()->filter->applyFilter('mail.from.name', env(key: 'MAILER_FROM_NAME'));
$fromEmail = __observer()->filter->applyFilter('mail.from.email', env(key: 'MAILER_FROM_EMAIL'));
// Set charset
$charset = __observer()->filter->applyFilter('mail.charset', 'utf-8');
// Set email subject and body.
$instance = $instance->withSubject(subject: $subject)->withBody(data: $message);
// Check for other headers and loop through them.
if (!empty($headers)) {
foreach ($headers as $name => $content) {
if ($name === 'cc') {
$instance = $instance->withCc(address: $content);
}
if ($name === 'bcc') {
$instance = $instance->withBcc(address: $content);
}
if ($name === 'replyTo') {
$instance = $instance->withReplyTo(address: $content);
}
if (
! in_array(needle: $name, haystack: ['MIME-Version','to','cc','bcc','replyTo'], strict: true)
&& !is_int($name)
) {
$instance = $instance->withCustomHeader(name: (string) $name, value: $content);
}
}
}
// Set X-Mailer header
$xMailer = __observer()->filter->applyFilter('mail.xmailer', sprintf('CodefyPHP Framework %s', Application::APP_VERSION));
$instance = $instance->withXMailer(xmailer: $xMailer);
// Set email charset
$instance = $instance->withCharset(charset: $charset ?: 'utf-8');
// Check if there are attachments and loop through them.
if (!empty($attachments)) {
foreach ($attachments as $filename => $filepath) {
$filename = is_string(value: $filename) ? $filename : '';
$instance = $instance->withAttachment(path: $filepath, name: $filename);
}
}
// Set sender.
$instance = $instance->withFrom(address: $fromEmail, name: $fromName ?: '');
try {
return $instance->send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
FileLoggerFactory::getLogger()->error($e->getMessage(), ['function' => '\Codefy\Framework\Helpers\mail']);
return false;
}
}