Skip to content

Commit c26252a

Browse files
authored
Merge pull request #18 from tattersoftware/release
Prep for Release
2 parents 3e1517a + 70f7f2a commit c26252a

File tree

5 files changed

+163
-29
lines changed

5 files changed

+163
-29
lines changed

README.md

Lines changed: 136 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# Tatter\Alerts
22
Lightweight user alerts for CodeIgniter 4
33

4-
[![](https://github.com/tattersoftware/codeigniter4-alerts/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-alerts/actions?query=workflow%3A%22PHPUnit)
5-
[![](https://github.com/tattersoftware/codeigniter4-alerts/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-alerts/actions?query=workflow%3A%22PHPStan)
4+
[![](https://github.com/tattersoftware/codeigniter4-alerts/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-alerts/actions/workflows/test.yml)
5+
[![](https://github.com/tattersoftware/codeigniter4-alerts/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-alerts/actions/workflows/analyze.yml)
6+
[![](https://github.com/tattersoftware/codeigniter4-alerts/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-alerts/actions/workflows/inspect.yml)
67
[![Coverage Status](https://coveralls.io/repos/github/tattersoftware/codeigniter4-alerts/badge.svg?branch=develop)](https://coveralls.io/github/tattersoftware/codeigniter4-alerts?branch=develop)
78

89
![Screenshot](https://github.com/tattersoftware/codeigniter4-alerts/blob/master/img/screenshot2.png)
910

1011
## Quick Start
1112

12-
1. Run: `> composer require tatter/alerts`
13-
2. Load the helper: `helper('alerts');`
14-
2. Set an alert: `alert('success', "You did it!")`
15-
3. Add in head tag (optional): `<?= service('alerts')->css(); ?>`
16-
4. Add after banner/menu: `<?= service('alerts')->display(); ?>`
13+
1. Install with Composer: `> composer require tatter/alerts`
14+
2. Enable the `alerts` filter in **app/Config/Filters.php**
15+
3. Add the `{alerts}` token to your View Layouts
16+
4. Load the helper: `helper('alerts');`
17+
4. Set an alert with a class and message: `alert('success', 'You did it!')`
1718

1819
## Features
1920

20-
Provides out-of-the-box user alerts for CodeIgniter 4
21+
Provides integrated user alerts for CodeIgniter 4 with a variety of built-in templates
22+
and custom template support.
2123

2224
## Installation
2325

@@ -30,27 +32,141 @@ composer require tatter/alerts
3032
Or, install manually by downloading the source files and adding the directory to
3133
`app/Config/Autoload.php`.
3234

33-
> Note: The default display template expects [Bootstrap](https://getbootstrap.com) but is
34-
easily changed.
35+
> Note: The default display template expects [Bootstrap](https://getbootstrap.com) (not included)
3536
3637
## Configuration (optional)
3738

3839
The library's default behavior can be changed using its config file. Copy
3940
**examples/Alerts.php** to **app/Config/Alerts.php** and follow the instructions in the
4041
comments. If no config file is found the library will use its defaults.
4142

42-
### Styling
43+
The Config file consists of two properties.
4344

44-
By default alerts will be displayed with classes for Bootstrap (not included).
45-
However, styles can be set to use other frameworks (or you may use your own) by changing
46-
the `$template` property in the Config file.
45+
### Templates
46+
47+
The `$template` property sets the path to the View file which will be used to format your
48+
alerts. The default template has HTML tags and classes designed for use with
49+
[Bootstrap 4 Alerts](https://getbootstrap.com/docs/4.6/components/alerts/), but the library
50+
includes additional templates for you to choose:
51+
* `Tatter\Alerts\Views\Foundation`: Compatible with the Foundation CSS Framework
52+
* `Tatter\Alerts\Views\Vanilla`: A framework-free implementation, with classes available for your own CSS styling
53+
54+
And of course you can add your own. The view file will be passed an array of tuples named
55+
`$alerts`, with each tuple in the format `[string $class, string $content]`. Your view file
56+
should unpack each tuple:
57+
```php
58+
foreach ($alerst as $alert) {
59+
[$class, $content] = $alert;
60+
```
61+
... then output the alert `$content` wrapped in some appropriate HTML tags with whatever
62+
styling or classes you like based on `$class`.
63+
64+
> Note: This library *does not include* assets for Bootstrap or Foundation. Check out
65+
> [Tatter\Frontend](https://github.com/tattersoftware/codeigniter4-frontend) for an integrated solution.
66+
67+
### Classes
68+
69+
The `$classes` property is a mapping of Session keys to their CSS classes. This lets you
70+
control which Session keys are deemed "alerts" and how to designate them to your view
71+
template. The default list is a generous guess at common keys used by the framework and
72+
modules, with the addition of the Bootstrap alert classes, but in most cases you will want
73+
to slim this down or replace it altogether with your own.
74+
75+
*See **Warnings** below for some caveats to consider when auto-populating Session keys into displayable content.*
76+
77+
## Filter
78+
79+
In order to use the `AlertsFilter` you must add apply it to your target routes. The filter
80+
only applies when the token is present so it is safe to apply it globally in **app/Config/Filters.php**.
81+
See [Controller Filters](https://codeigniter.com/user_guide/incoming/filters.html) for more info.
82+
83+
> Note: The alias is predefined for you as "alerts", and only the `after()` method is relevant.
84+
85+
## Token
86+
87+
The token is the following string: `{alerts}`. Place this in your View layout where you want
88+
the alerts to appear. For example:
89+
```php
90+
<body>
91+
<aside>
92+
{alerts}
93+
</aside>
94+
<main class="wrapper">
95+
...
96+
```
4797

4898
## Usage
4999

50-
If installed correctly CodeIgniter 4 will detect and autoload the library, service, helper,
51-
and config. Initialize the helper if you want the convenience wrapper function:
52-
`helper('alerts');`
100+
If installed correctly CodeIgniter 4 will detect and autoload the library, filter, helper,
101+
and config. The filter will gather any alerts from the Session keys defined in your Config,
102+
pass them through your View template for formatting, and place them into your Response body
103+
wherever you have placed your token - you just need to set the alerts!
104+
105+
Alerts can be set directly in the Session, ideally as flashdata (so they are not repeated):
106+
```php
107+
session()->setFlashdata('success', 'Your account has been updated.');
108+
```
109+
110+
Many times your alerts will be handled during redirect, so you can take advantage of
111+
the framework's `RedirectResponse` class method `with()` to apply the flashdata directly:
112+
```php
113+
if (! $fruit = $this->getPost('fruit')) {
114+
return redirect()->back()->with('error', 'You must select a fruit!');
115+
}
116+
```
117+
118+
### Alerts Helper
119+
120+
This library also includes a helper function, which has the added benefit of merging values
121+
and checking for collision. Initialize the helper to us the convenience wrapper function:
122+
```php
123+
helper(['alerts']);
124+
alert('error', 'You must accept the terms of service to continue.');
125+
```
126+
127+
The helper adds a few features (like collision detection and alert merging) but may throw
128+
exceptions in some circumstances - read the **Collision** section below.
129+
130+
## Warnings
131+
132+
The premise of this library is to take data from `$_SESSION` and display it to visitors of
133+
your site. There are a few precautions mentioned here, but in general: use strong security
134+
practices and good sense any time you are moving data between the backend and public views.
135+
136+
### Security
137+
138+
Ideally `$_SESSION` should not contain critical information like passwords or credit card
139+
numbers. You should also not use distinguishable identifiers as Session keys, and this goes
140+
for `Alerts` as well. Keep the keys you use basic, and consider pairing down the Config
141+
file's to only those values your app and modules need.
142+
143+
For example, say you add a payment library to your project and some developer was using
144+
the following code to test credit card submission and forgot to remove it:
145+
```php
146+
$_SESSION['debug'] = (string) $user->getCreditCard();
147+
```
148+
149+
Since "debug" is a valid `Alerts` key this credit card number will now become a alert
150+
displayed visually on the user's browser window!
151+
152+
### Collision
153+
154+
Another concern is Session collision. Starting with an example this time:
155+
```php
156+
/** @var Notice $notice */
157+
$notice = model(NoticeModel::class)->first();
158+
session('notice', $notice);
159+
...
160+
161+
// Later that same day...
162+
alert('notice', 'Site statistics are currently being updated, expect longer load times.');
163+
```
164+
165+
We have just tried to set an alert for a Session key that already exists and contains
166+
something that is not another alert! There are a few ways that this can play out, but
167+
ultimately this was a mistake and you should take care to avoid it.
53168

54-
Then use the helper function `alert($class, $text)` to set an alert for the user's next
55-
view. Use class methods `$alerts->css()` and `$alerts->display()` to retrieve the styling
56-
and HTML for the alerts.
169+
To clarify the example:
170+
1. `AlertsFilter` will quietly ignore Session data that is not a string or an array of strings, so there is no problem with `session('notice', $notice);`.
171+
2. Using the Alerts Helper to set your alert includes the additional layer of collision protection, but will cause the `alert()` function to throw an exception.
172+
3. Setting Session keys yourself is a fine solution, but you must handle checks for existing keys or risk overwriting data.

UPGRADING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
> Note: This is a complete refactor! Please be sure to read the docs carefully before upgrading.
77
8-
* All handling is now routed through `AlertsFilter` - read the docs on setting up the Filter
8+
* All handling is now routed through `AlertsFilter` - read the docs on how to enable the Filter and add the token to your layouts
99
* Related, the service, exceptions, and language files have been removed
10-
* Alerts no longer use session prefixes but are matched to the Config `$classes` - beware of session collision
10+
* `Alerts` no longer uses session prefixes but matches to the Config `$classes` - beware of session collision
1111
* The view templates no longer include a wrapper; if you would like consistent behavior then put your token in an `<aside>` tag:
1212
```html
1313
<aside id="alerts-wrapper">

src/Config/Alerts.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ class Alerts extends BaseConfig
2121
* @var array<string,string>
2222
*/
2323
public $classes = [
24+
// Bootstrap classes
2425
'primary' => 'primary',
25-
'message' => 'primary',
2626
'secondary' => 'secondary',
27-
'notice' => 'secondary',
2827
'success' => 'success',
2928
'danger' => 'danger',
29+
'warning' => 'warning',
30+
'info' => 'info',
31+
32+
// Additional log levels
33+
'message' => 'primary',
34+
'notice' => 'secondary',
3035
'error' => 'danger',
31-
'errors' => 'danger',
3236
'critical' => 'danger',
3337
'emergency' => 'danger',
34-
'warning' => 'warning',
3538
'alert' => 'warning',
36-
'info' => 'info',
3739
'debug' => 'info',
40+
41+
// Common framework keys
42+
'messages' => 'primary',
43+
'errors' => 'danger',
3844
];
3945
}

src/Config/Filters.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tatter\Alerts\Config;
4+
5+
use Config\Filters;
6+
use Tatter\Alerts\Filters\AlertsFilter;
7+
8+
/**
9+
* @var Filters $filters
10+
*/
11+
$filters->aliases['alerts'] = AlertsFilter::class;

src/Helpers/alerts_helper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function alert(string $key, $content): void
2121

2222
// If no key exists the set and quit
2323
if (! $session->has($key)) {
24-
$session->set($key, $content);
24+
$session->setFlashdata($key, $content);
2525

2626
return;
2727
}
@@ -31,12 +31,13 @@ function alert(string $key, $content): void
3131

3232
if (is_array($value)) {
3333
$session->push($key, $content);
34+
$session->markAsFlashdata($key);
3435

3536
return;
3637
}
3738

3839
if (is_string($value)) {
39-
$session->set($key, array_merge([$value], $content));
40+
$session->setFlashdata($key, array_merge([$value], $content));
4041

4142
return;
4243
}

0 commit comments

Comments
 (0)