Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Imldeployment
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
Imldeployment
Merge requests
!66
php8 only; added variable types; short array syntax; remove glyphicons
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
php8 only; added variable types; short array syntax; remove glyphicons
7359-update-php83
into
master
Overview
0
Commits
61
Pipelines
0
Changes
1
Merged
Hahn Axel (hahn)
requested to merge
7359-update-php83
into
master
10 months ago
Overview
0
Commits
61
Pipelines
0
Changes
1
0
0
Merge request reports
Viewing commit
43b5d42e
Prev
Next
Show latest version
1 file
+
37
−
26
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
43b5d42e
messenger class: php8 only; added variable types
· 43b5d42e
Hahn Axel (hahn)
authored
10 months ago
public_html/deployment/classes/messenger.class.php
+
37
−
26
View file @ 43b5d42e
Edit in single-file editor
Open in Web IDE
Show full file
@@ -4,70 +4,80 @@
* send messenger notifications
*
* @author hahn
*
* 2024-08-23 v1.1 Axel Hahn php8 only; added variable types
*/
class
messenger
{
class
messenger
{
/**
* config array for messengers
* @var
type
* @var
array
*/
protected
$_aCfg
=
array
()
;
protected
array
$_aCfg
=
[]
;
/**
* content of messagetext to send
* @var string
*/
protected
$_sMessage
=
''
;
protected
string
$_sMessage
=
''
;
/**
* Constructor
*
* @example
* $oMessenger = new messenger(
array(
* 'slack'=>
array(
* $oMessenger = new messenger(
[
* 'slack'=>
[
* 'incomingurl'=>[WebHook url],
* 'user'=>[visible username in slack],
* 'icon'=>[Slack Icon], // hm, does not seem to work
*
)
,
* 'email'=>
array(
*
]
,
* 'email'=>
[
* 'from'=>[senders e-mail]
* 'to'=>[email-address(es)] // multiple emails must be delimited with ";"
*
)
*
)
);
*
]
*
]
);
* @param array $aCfg config array for notification targets
* @return boolean
*/
public
function
__construct
(
$aCfg
)
{
public
function
__construct
(
array
$aCfg
)
{
$this
->
_aCfg
=
$aCfg
;
return
true
;
}
/**
* send an email if _aCfg['email']['to'] exists
* Send an email if _aCfg['email']['to'] exists
* @return bool
*/
private
function
_sendEmail
(){
private
function
_sendEmail
():
bool
{
if
(
isset
(
$this
->
_aCfg
[
'email'
][
'to'
])
&&
$this
->
_aCfg
[
'email'
][
'to'
])
{
preg_match
(
'/^(.*)\n/'
,
$this
->
_sMessage
,
$aTmp
);
$sSubject
=
$aTmp
[
0
];
return
mail
(
$this
->
_aCfg
[
'email'
][
'to'
],
$sSubject
,
$this
->
_sMessage
,
$this
->
_aCfg
[
'email'
][
'to'
],
$sSubject
,
$this
->
_sMessage
,
"From: "
.
$this
->
_aCfg
[
'email'
][
'from'
]
.
"
\r\n
"
.
"Reply-To: "
.
$this
->
_aCfg
[
'email'
][
'from'
]
.
"
\r\n
"
);
}
return
false
;
}
/**
* send a message to slack if _aCfg['slack']['incomingurl'] exists
* Send a message to slack if _aCfg['slack']['incomingurl'] exists
* @return bool|string
*/
private
function
_sendToSlack
(){
private
function
_sendToSlack
():
bool
|
string
{
if
(
isset
(
$this
->
_aCfg
[
'slack'
][
'incomingurl'
])
&&
$this
->
_aCfg
[
'slack'
][
'incomingurl'
])
{
require_once
(
__DIR__
.
'/../../vendor/shooker/shooker.php'
);
$shkr
=
new
Shooker
();
$shkr
->
setupIncoming
(
$this
->
_aCfg
[
'slack'
][
'incomingurl'
]);
$sUser
=
(
isset
(
$this
->
_aCfg
[
'slack'
][
'user'
])
?
$this
->
_aCfg
[
'slack'
][
'user'
]
:
false
);
$sIcon
=
(
isset
(
$this
->
_aCfg
[
'slack'
][
'icon'
])
?
$this
->
_aCfg
[
'slack'
][
'icon'
]
:
false
);
$sUser
=
(
isset
(
$this
->
_aCfg
[
'slack'
][
'user'
])
?
$this
->
_aCfg
[
'slack'
][
'user'
]
:
false
);
$sIcon
=
(
isset
(
$this
->
_aCfg
[
'slack'
][
'icon'
])
?
$this
->
_aCfg
[
'slack'
][
'icon'
]
:
false
);
// returns bool or string with response from Slack API
return
$shkr
->
sendMessage
(
$this
->
_sMessage
,
$sUser
,
$sIcon
);
}
return
false
;
@@ -77,13 +87,14 @@ class messenger {
/**
* send a message to all targets
* @param string $sMessage
* @return void
*/
public
function
sendMessage
(
$sMessage
)
{
$this
->
_sMessage
=
$sMessage
;
public
function
sendMessage
(
string
$sMessage
):
void
{
$this
->
_sMessage
=
$sMessage
;
// echo '<pre>'.print_r($this->_aCfg, 1).'</pre>'.$sMessage.'<br>';
$this
->
_sendEmail
();
$this
->
_sendToSlack
();
}
}
Loading