Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
3.0 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Nov 17, 2019
1 parent 8484b6f commit 93c0b25
Show file tree
Hide file tree
Showing 20 changed files with 559 additions and 393 deletions.
19 changes: 19 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
filter:
excluded_paths: [tests/*]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true

4 changes: 4 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
preset: laravel

disabled:
- single_class_element_per_statement
89 changes: 50 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Quickly set one or multiple Alerts in your backend, and render them in the frontend.

Laralerts is compatible with any frontend framework, and allows you to modify the rendered HTML.
Laralerts is compatible with any frontend framework to better suit your app, not the other way around.

## Installation

Expand All @@ -28,7 +28,7 @@ And that's it. Everything works out of the box.

### Creating an Alert

To set an Alert in your frontend, you can use the `alert()` helper, or the `Alert` Facade. A good place to use them is before sending a Response or Redirect to the browser, like in your HTTP Controllers.
To set an Alert in your frontend, you can use the `alert()` helper, or the `Alert` Facade. A good place to use them is before sending a View or Redirect response to the browser, like in your HTTP Controllers.

```php
<?php
Expand All @@ -40,17 +40,6 @@ use App\Article;

class ArticleController extends Controller
{
/**
* Show the edit form for the Article
*
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function edit(Article $article)
{
return view('alert.edit')->with('article', $article);
}

/**
* Update the Article
*
Expand Down Expand Up @@ -99,7 +88,7 @@ You can also push an Alert if a condition evaluates to true or false. Just use t
alert_if(true, 'You should see this alert');
alert_unless(false, 'And this too since the condition is false!');

alert_if(false, 'No problem')->message('with chaining methods!');
alert_if(false)->message('No problem with chaining methods!');
```

When `alert_if` and `alert_unless` evaluate to false, you will still receive an Alert instance, but this won't be persisted into the session or response, avoiding unwanted errors in your backend.
Expand Down Expand Up @@ -134,7 +123,7 @@ Alert::message('We will email you a copy!')
#### Raw message

Since the `message()` method escape the text for safety, you can use the `raw()` method to do the same with the untouched string. This allows you to use HTML for more personalized messages, like adding some _style_, or even add JavaScript if you like to ride the wild side.
Since the `message()` method escapes the text for safety, you can use the `raw()` method to do output the raw string you set. This allows you to use HTML for personalized messages, like adding some _style_ or emojis like 😍.

```php
<?php
Expand All @@ -156,7 +145,7 @@ alert()->raw('But this is <strong>important</strong>.')
</div>
```

> **Warning: Don't use `raw()` to show user-generated content. You have been warned**.
**Warning: Don't use `raw()` to show user-generated content. You have been warned**.

#### Using Localization

Expand All @@ -176,7 +165,7 @@ alert()->lang('email.sent', ['email' => $email], 'es')->success();

#### Alert Type

You can use multiple fluent methods that mirror the Alert class from Bootstrap 4:
You're probably using Bootstrap 4, so in that case you can use multiple fluent methods that mirror the Alert class from Bootstrap 4, right out of the box.

| Method | Class |
| --- | --- |
Expand All @@ -202,12 +191,16 @@ alert()->message('Your message was sent!')
</div>
```

If you're not using Bootstrap 4, you [can change this list with your own](#adding-your-own-fluid-classes).

> By default, Alerts don't have any default type, so when they're rendered they will be *transparent* (without style or color). Don't worry, you can easily [set a default](#default-type).
##### Adding your own fluid classes

If you need to modify the Alert types, you can use the static method `Alert::setTypes()` with an array of accepted types of Alerts. You can do this on the boot method or register method of your `AppServiceProvider`.

The alert classes must must be keyed by the name of the fluent method, and the underlying class to attach to the alert.

```php
<?php

Expand All @@ -220,19 +213,21 @@ public function boot()
{
// ...

\DarkGhostHunter\Laralerts\Alert::setTypes(['gradient', 'popping']);
\DarkGhostHunter\Laralerts\Alert::setTypes([
'gradient' => 'gradient',
'popping' => 'bg-pop-color',
]);
}
```

```php
<?php

alert()->message('Popping colors!')
->popping();
alert()->message('Popping colors!')->popping();
```

```html
<div class="alert alert-popping" role="alert">
<div class="alert bg-pop-color" role="alert">
Popping colors!
</div>
```
Expand All @@ -242,7 +237,11 @@ The array you supply is not added, but replaced entirely. If you want to just ad
```php
<?php

\DarkGhostHunter\Laralerts\Alert::addTypes(['these', 'are', 'added']);
\DarkGhostHunter\Laralerts\Alert::addTypes([
'these' => 'class-these',
'are' => 'class-are',
'added' => 'class-added',
]);
```

#### Dismiss
Expand Down Expand Up @@ -292,18 +291,18 @@ alert()->message('Your message was sent!')

By default, in every request lifecycle (except on Redirects) you will start with an empty Alert Bag.

You can _rescue_ the old Alerts using the `withOld()` method, as long you haven't issued a new Alert before.
You can _rescue_ the old Alerts using the `reflash()` method. This will take the old alerts and put them into the Alert Bag, merging with any other alert you have set previously, but with the new ones taking precedence.

Once you create a new alert, it will be appended to the existing bag of alerts from the request or redirect made before.
Once you create a new alert, it will be appended to the existing bag of alerts from the request or redirect made before.

```php
<?php

alert()
->withOld()
->reflash()
->message('Be sure to check the other alerts.')
->warning();
```
```

```html
<div class="alert alert-success message-sent global-alert" role="alert">
Expand All @@ -317,9 +316,9 @@ alert()

### Adding Alerts to a JSON Response

This library has a convenient way to add Alerts into your JSON Responses. Just simply add the `AppendAlertsToJsonResponse` middleware into your routes or `app/Http/Kernel`, [as the documentation says](https://laravel.com/docs/middleware#registering-middleware). If you ask me, the `alerts` is a very straightforward middleware alias to use.
This library has a convenient way to add Alerts into your JSON Responses. Just simply add the `alert.json` middleware into your routes or `api` routes, [as the documentation says](https://laravel.com/docs/middleware#registering-middleware).

When you return a `JsonResponse` to the browser, the middleware will append the alert as JSON using the same Session Key defined in the configuration, which is `_alerts` by default. It also accepts the `key` parameter to use as an alternative, compatible with *dot notation*. Here is the lazy way to do it as example:
When you return a `JsonResponse` to the browser, the middleware will append the alert as JSON using the same Session Key defined in the configuration, which is `_alerts` by default. It also accepts the `key` parameter to use as an alternative, compatible with *dot notation*. Here is an example:

```php
<?php
Expand All @@ -332,7 +331,7 @@ Route::group('api', function () {
Route::post('create')->uses('UserController@create');
Route::post('update')->uses('UserController@update');

})->middleware(AppendAlertsToJsonResponse::class.':_status.alerts');
})->middleware('alert.json:_status.alerts');
```

When you receive a JSON Response, you will see the alerts appended to whichever key you issued. Using the above example, we should see the `alerts` key under the `_status` key:
Expand All @@ -357,7 +356,7 @@ When you receive a JSON Response, you will see the alerts appended to whichever
}
```

To keep good performance, the Alerts will be injected into the Session only if it has started. The `api` routes are stateless, and here the Sessions are not used.
To keep good performance, the Alerts will be injected into the Session only if it has started. For example, `api` routes are stateless and do not have exceptions, so there is no need to explicitly disable sessions in these places.

## Configuration

Expand All @@ -376,6 +375,7 @@ return [
'directive' => 'alerts',
'key' => '_alerts',
'type' => null,
'dismiss' => false,
];
```

Expand All @@ -393,25 +393,25 @@ return [
];
```

> When changing this parameter, you may want to use `php artisan view:clear` to rebuild your views with the new directive.
> When changing this parameter, you may want to use `php artisan view:clear` to rebuild your compiled views with the new directive.
### Session Key

The Alert Bag is registered into the Session by a given key, which is `_alerts` by default. If you're using this key name for other things, you should change the key name.
The Alert Bag is registered into the Session by a given key, which is `_alerts` by default. If you're using this key name for other things in your session, you should change the key name.

```php
<?php

return [
'session_key' => '_alerts',
'key' => '_alerts',
];
```

> For your ease of mind, the Alerts serialize and unserialize as `array`, so you don't have to worry about storage concerns.
> For ease of mind, the Alerts serialize and unserialize as `array`, so you don't have to worry about storage concerns.
### Default Type

The default type of the Alerts in the Application. You can use any of the [included type names](#alert-type), like `success` or `info`. You can override the default type anytime when you create an Alert manually.
The default type of the Alerts in the Application. You can use any of the [included type names](#alert-type), like `success` or `info`, or your own as long is [registered properly](#adding-your-own-fluid-classes). You can override the default type anytime when you create an Alert manually.

```php
<?php
Expand All @@ -432,15 +432,16 @@ You may be using another frontend framework different from Bootstrap 4, or you m
The Alert view receives:

* `$message`: The message to show inside the Alert.
* `$type`: The type of Alert.
* `$classes`: The classes to add to the HTML tag.
* `$type`: The type name.
* `$typeClass`: The type CSS class.
* `$classes`: The additional classes to add to the HTML block.

You can change the HTML to whatever you want, like adapting the Alert to be used with [Bulma.io Notifications](https://bulma.io/documentation/elements/notification/).

`/resources/views/vendor/laralerts/alert-dismiss.blde.php`

```blade
<div class="notification is-{{ $type }} {{ $classes }}">
<div class="notification is-{{ $typeClass }} {{ $classes }}">
<button class="delete"></button>
{!! $message !!}
</div>
Expand All @@ -467,7 +468,17 @@ When doing this, the alerts will be created using the `message`, `type`, `dismis
```php
<?php

$json = '"{"content": {"alerts": {"message":"Email delivered"} } }"';
$json = '"{
"content": {
"alerts":
{
"message": "I like trains!"
},
{
"message": "Me too!"
},
}
}"';

alert()->addManyFromJson($json, 'content.alerts');
```
Expand Down
4 changes: 2 additions & 2 deletions resources/views/alert-dismiss.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="alert {{ $classes }} alert-{{ $type }} alert-dismissible fade show" role="alert">
<div class="alert {{ $classes }} {{ $typeClass }} alert-dismissible fade show" role="alert">
{!! $message !!}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
</div>
4 changes: 2 additions & 2 deletions resources/views/alert.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="alert {{ $classes }} @if($type)alert-{{ $type }}@endif" role="alert">
<div class="alert {{ $classes }} {{ $typeClass }}" role="alert">
{!! $message !!}
</div>
</div>
Loading

0 comments on commit 93c0b25

Please sign in to comment.