Skip to content

Commit 8c04f79

Browse files
committed
Move to service, new namespace, default config
1 parent a83bd30 commit 8c04f79

File tree

8 files changed

+183
-135
lines changed

8 files changed

+183
-135
lines changed

src/Config/Alerts.php.example renamed to Alerts.php.example

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212

1313
use CodeIgniter\Config\BaseConfig;
1414

15-
class Alerts extends BaseConfig
15+
class Alerts extends \Tatter\Alerts\Config
1616
{
1717
// prefix for SESSION variables and HTML classes, to prevent collision
18-
public $prefix = "alerts-";
18+
public $prefix = "alerts-";
1919

2020
/*
2121
Template to use for HTML output
22-
Make sure there is a corresponding view file for the path/namespace provided
22+
There must be a corresponding view file for the path/namespace provided
2323
Native support for:
24-
Basic - a minimalist layout internal to this library
25-
Bootstrap (default) - [https://getbootstrap.com/docs/4.0/components/alerts/#dismissing]
26-
Foundation - [https://foundation.zurb.com/sites/docs/callout.html#making-closable]
24+
basic - a minimalist layout internal to this library
25+
bootstrap (default) - [https://getbootstrap.com/docs/4.0/components/alerts/#dismissing]
26+
foundation - [https://foundation.zurb.com/sites/docs/callout.html#making-closable]
2727
*/
28-
public $template = "Tatter\bootstrap";
28+
public $template = "Tatter\Alerts\Views\bootstrap";
2929
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"autoload": {
2828
"psr-4": {
29-
"Tatter\\": "src"
29+
"Tatter\\Alerts\\": "src"
3030
}
3131
},
3232
"scripts": {

src/Alerts.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php namespace Tatter\Alerts;
2+
3+
/***
4+
* Name: Alerts
5+
* Author: Matthew Gatner
6+
* Contact: mgatner@tattersoftware.com
7+
* Created: 2019-02-13
8+
*
9+
* Description: Lightweight user notification library for CodeIgniter 4
10+
*
11+
* Requirements:
12+
* >= PHP 7.1
13+
* >= CodeIgniter 4.0
14+
* CodeIgniter's Session Library (loaded automatically)
15+
*
16+
* Configuration:
17+
* Use Config/Alerts.php to override default behavior
18+
*
19+
* @package CodeIgniter4-Alerts
20+
* @author Matthew Gatner
21+
* @link https://github.com/tattersoftware/codeigniter4-alerts
22+
*
23+
***/
24+
25+
use CodeIgniter\Config\BaseConfig;
26+
use CodeIgniter\Config\Services;
27+
use Tatter\Alerts\Exceptions\AlertsException;
28+
29+
/*** CLASS ***/
30+
class Alerts {
31+
32+
/**
33+
* Our configuration instance.
34+
*
35+
* @var \Tatter\Alerts\Config\Alerts
36+
*/
37+
protected $config;
38+
39+
/**
40+
* The view engine to render the alerts with.
41+
*
42+
* @var RendererInterface
43+
*/
44+
protected $view;
45+
46+
/**
47+
* The active user session, for loading and storing alerts.
48+
*
49+
* @var \CodeIgniter\Session\Session
50+
*/
51+
protected $session;
52+
53+
// initiate library, check for existing session
54+
public function __construct(BaseConfig $config, RendererInterface $view = null)
55+
{
56+
// save configuration
57+
$this->config = $config;
58+
59+
// initiate the Session library
60+
$this->session = Services::session();
61+
62+
// verify renderer
63+
if ($view instanceof RendererInterface)
64+
$this->view = $view;
65+
else
66+
$this->view = Services::renderer();
67+
68+
// validations
69+
if (empty($this->config->template))
70+
throw AlertsException::forInvalidTemplate('');
71+
72+
$locator = Services::locator();
73+
if (! $locator->locateFile($this->config->template))
74+
throw AlertsException::forMissingTemplateView($this->config->template);
75+
}
76+
77+
// add a new alert to the queue
78+
public function add($class, $text)
79+
{
80+
$alert = [
81+
'class' => $class,
82+
'text' => $text
83+
];
84+
85+
// start the queue if it doesn't exist
86+
if (! $this->session->has($this->config->prefix . 'queue'))
87+
$this->session->set($this->config->prefix . 'queue', [$alert]);
88+
89+
// push onto the queue if it was already there
90+
else
91+
$this->session->push($this->config->prefix . 'queue', [$alert]);
92+
93+
return;
94+
}
95+
96+
// clears the queue and returns template formatted alerts
97+
public function display()
98+
{
99+
// get any alerts
100+
$alerts = $this->session->get($this->config->prefix . 'queue') ?? [ ];
101+
102+
// clear alerts
103+
$this->session->remove($this->config->prefix . 'queue');
104+
105+
if (empty($alerts))
106+
return;
107+
108+
// render the specified view template
109+
return $this->view->setVar('prefix', $this->config->prefix)
110+
->setVar('alerts', $alerts)
111+
->render($this->config->template);
112+
}
113+
114+
// returns default CSS as inline style sheet
115+
// should be injected into <head>
116+
public function css()
117+
{
118+
return $this->view->setVar('prefix', $this->config->prefix)->render("Tatter\Alerts\Views\css");
119+
}
120+
}

src/Config/Alerts.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php namespace Tatter\Alerts\Config;
2+
3+
use CodeIgniter\Config\BaseConfig;
4+
5+
class Alerts extends BaseConfig
6+
{
7+
// prefix for SESSION variables and HTML classes, to prevent collision
8+
public $prefix = "alerts-";
9+
10+
// Template to use for HTML output
11+
public $template = "Tatter\Alerts\Views\bootstrap";
12+
}

src/Config/Services.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php namespace Tatter\Alerts\Config;
2+
3+
use CodeIgniter\Config\BaseService;
4+
use CodeIgniter\View\RendererInterface;
5+
6+
class Services extends BaseService
7+
{
8+
public static function alerts(BaseConfig $config = null, RendererInterface $view = null, bool $getShared = true)
9+
{
10+
if ($getShared):
11+
return static::getSharedInstance('alerts', $config, $view);
12+
endif;
13+
14+
// prioritizes user config in app/Config if found
15+
if (empty($config)):
16+
if (class_exists('\Config\Alerts')):
17+
$config = new \Config\Alerts();
18+
else:
19+
$config = new \Tatter\Alerts\Config\Alerts();
20+
endif;
21+
endif;
22+
23+
return new \Tatter\Alerts\Alerts($config, $view);
24+
}
25+
}

src/Exceptions/AlertsException.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace Tatter\Alerts\Exceptions;
2+
3+
use CodeIgniter\Exceptions\ExceptionInterface;
4+
use CodeIgniter\Exceptions\FrameworkException;
5+
6+
class AlertsException extends FrameworkException implements ExceptionInterface
7+
{
8+
public static function forInvalidTemplate(string $template = null)
9+
{
10+
return new static("'{$template}' is not a valid Alerts template.");
11+
}
12+
public static function forMissingTemplateView(string $template = null)
13+
{
14+
return new static("Could not find template view file: '{$template}'.");
15+
}
16+
}

src/Helpers/alerts_helper.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,18 @@
22

33
/***
44
*
5-
* This file contains optional helper functions to make calling the library easier.
5+
* This file contains the optional wrapper helper functions to make calling the library easier.
66
* Recommended usage:
77
* 1. Load the helper with `helper("alerts")`
88
* 2. Use `alert($class, $text) to add a new user alert
9-
* 3. Call `alertsCss()` in <head> for a basic CSS layout, or provide your own
10-
* 4. Call `alertsDisplay()` wherever you want alerts (usually after main nav/banner)
119
*
1210
***/
1311

1412
if (! function_exists('alert'))
1513
{
1614
// add a new alert to the queue
1715
function alert(string $class, string $text) {
18-
$alerts = new Tatter\Libraries\Alerts();
16+
$alerts = service('alerts');
1917
$alerts->add($class, $text);
2018
}
2119
}
22-
23-
if (! function_exists('alertsCss'))
24-
{
25-
// returns pre-configured CSS as inline style sheet
26-
function alertsCss() {
27-
$alerts = new Tatter\Libraries\Alerts();
28-
return $alerts->css();
29-
}
30-
}
31-
32-
if (! function_exists('alerts'))
33-
{
34-
// returns all queue alerts and clears the queue
35-
function alerts() {
36-
$alerts = new Tatter\Libraries\Alerts();
37-
return $alerts->display();
38-
}
39-
}

src/Libraries/Alerts.php

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)