Last active
August 19, 2019 12:12
-
-
Save SamMousa/6678cb2f8d4791151da6374fba6cfeb1 to your computer and use it in GitHub Desktop.
NotificationService that can be easily decoupled from the session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ; | |
use bundles\PNotifyBundle; | |
use yii\authclient\AuthAction; | |
use yii\base\Component; | |
use yii\base\Event; | |
use yii\base\ViewEvent; | |
use yii\helpers\ArrayHelper; | |
use yii\web\Controller; | |
use yii\web\Session; | |
use yii\web\View; | |
class NotificationService extends Component | |
{ | |
private $counter = 0; | |
public function __construct(array $config = []) | |
{ | |
parent::__construct($config); | |
Event::on(View::class, View::EVENT_BEFORE_RENDER, function (ViewEvent $event) { | |
$this->renderNotifications($event); | |
}); | |
} | |
protected function getSession(): Session | |
{ | |
return \Yii::$app->get('session'); | |
} | |
protected function renderNotifications(ViewEvent $event) | |
{ | |
/** @var View $view */ | |
$view = $event->sender; | |
if (!$view->context instanceof Controller) { | |
return; | |
} | |
if (\Yii::$app->requestedAction instanceof AuthAction | |
|| !\Yii::$app->has('session', true) | |
) { | |
return; | |
} | |
PNotifyBundle::register($view); | |
$jsNotifications = []; | |
foreach ($this->getSession()->getAllFlashes(true) as $flash) { | |
$type = ArrayHelper::remove($flash, 'type'); | |
$config = json_encode($flash); | |
$jsNotifications[] = "PNotify.$type($config);"; | |
} | |
if (!empty($jsNotifications)) { | |
$view->registerJs(implode("\n", $jsNotifications)); | |
} | |
} | |
public function info(string $message, string $title = null, string $key = null) | |
{ | |
$this->getSession()->setFlash( | |
$key ?? "Message" . $this->counter++, | |
[ | |
'type' => 'info', | |
'text' => $message, | |
'title' => $title ?? \Yii::t('app', 'Info'), | |
] | |
); | |
} | |
public function success(string $message, string $title = null, string $key = null) | |
{ | |
$this->getSession()->setFlash( | |
$key ?? "Message" . $this->counter++, | |
[ | |
'type' => 'success', | |
'text' => $message, | |
'title' => $title ?? \Yii::t('app', 'Success!'), | |
] | |
); | |
} | |
public function error(string $message, string $title = null, string $key = null) | |
{ | |
$this->getSession()->setFlash( | |
$key ?? "Message" . $this->counter++, | |
[ | |
'type' => 'error', | |
'text' => $message, | |
'title' => $title ?? \Yii::t('app', 'Error!'), | |
] | |
); | |
} | |
public function warn(string $message, string $title = null, string $key = null) | |
{ | |
$this->getSession()->setFlash( | |
$key ?? "Message" . $this->counter++, | |
[ | |
'type' => 'error', | |
'text' => $message, | |
'title' => $title ?? \Yii::t('app', 'Warning!'), | |
] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment