Skip to content

Commit

Permalink
Merge pull request #11 from ARCANEDEV/rewrite-v4
Browse files Browse the repository at this point in the history
[v4] Rewriting the package
  • Loading branch information
arcanedev-maroc authored May 18, 2019
2 parents 86fb232 + d5f0095 commit 6085dd2
Show file tree
Hide file tree
Showing 18 changed files with 744 additions and 406 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ before_script:
- travis_retry composer install --prefer-source --no-interaction

script:
- composer validate
- mkdir -p build/logs
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

Expand Down
19 changes: 10 additions & 9 deletions _docs/1-Installation-and-Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ The Notify package has a few system requirements:

## Version Compatibility

| Notify | Laravel |
|:-------------------------------|:--------------------------------------------------------------------------------------------------------------------|
| ![Notify v1.x][notify_1_x] | ![Laravel v4.2][laravel_4_2] |
| ![Notify v3.2.x][notify_3_2_x] | ![Laravel v5.0][laravel_5_0] ![Laravel v5.1][laravel_5_1] ![Laravel v5.2][laravel_5_2] ![Laravel v5.3][laravel_5_3] |
| ![Notify v3.3.x][notify_3_3_x] | ![Laravel v5.4][laravel_5_4] |
| ![Notify v3.4.x][notify_3_4_x] | ![Laravel v5.5][laravel_5_5] |
| ![Notify v3.5.x][notify_3_5_x] | ![Laravel v5.6][laravel_5_6] |
| ![Notify v3.6.x][notify_3_6_x] | ![Laravel v5.7][laravel_5_7] |
| ![Notify v3.7.x][notify_3_7_x] | ![Laravel v5.8][laravel_5_8] |
| Notify | Laravel |
|:--------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------|
| ![Notify v1.x][notify_1_x] | ![Laravel v4.2][laravel_4_2] |
| ![Notify v3.2.x][notify_3_2_x] | ![Laravel v5.0][laravel_5_0] ![Laravel v5.1][laravel_5_1] ![Laravel v5.2][laravel_5_2] ![Laravel v5.3][laravel_5_3] |
| ![Notify v3.3.x][notify_3_3_x] | ![Laravel v5.4][laravel_5_4] |
| ![Notify v3.4.x][notify_3_4_x] | ![Laravel v5.5][laravel_5_5] |
| ![Notify v3.5.x][notify_3_5_x] | ![Laravel v5.6][laravel_5_6] |
| ![Notify v3.6.x][notify_3_6_x] | ![Laravel v5.7][laravel_5_7] |
| ![Notify v3.7.x][notify_3_7_x] ![Notify v4.0.x][notify_4_0_x] | ![Laravel v5.8][laravel_5_8] |

[laravel_4_2]: https://img.shields.io/badge/v4.2-supported-brightgreen.svg?style=flat-square "Laravel v4.2"
[laravel_5_0]: https://img.shields.io/badge/v5.0-supported-brightgreen.svg?style=flat-square "Laravel v5.0"
Expand All @@ -44,6 +44,7 @@ The Notify package has a few system requirements:
[notify_3_5_x]: https://img.shields.io/badge/version-3.5.*-blue.svg?style=flat-square "Notify v3.5.*"
[notify_3_6_x]: https://img.shields.io/badge/version-3.6.*-blue.svg?style=flat-square "Notify v3.6.*"
[notify_3_7_x]: https://img.shields.io/badge/version-3.7.*-blue.svg?style=flat-square "Notify v3.7.*"
[notify_4_0_x]: https://img.shields.io/badge/version-4.0.*-blue.svg?style=flat-square "Notify v4.0.*"

## Composer

Expand Down
28 changes: 23 additions & 5 deletions _docs/2-Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,34 @@
After you've published the config file `config/notify.php`, you can customize the settings :

```php
<?php

return [

/* -----------------------------------------------------------------
| Session
| Default Store
| -----------------------------------------------------------------
*/

'session' => [
'prefix' => 'notifier'

'default' => 'session',

/* -----------------------------------------------------------------
| Supported Stores
| -----------------------------------------------------------------
*/

'stores' => [

'session' => [
'driver' => Arcanedev\Notify\Stores\SessionStore::class,
'options' => [
'key' => 'notifications'
],
],

],

];
```

The `prefix` value is the session prefix name for all your flash notifications.
You can create you own store like `database` or `redis` store for your notifications.
53 changes: 17 additions & 36 deletions _docs/3-Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
2. [Configuration](2-Configuration.md)
3. [Usage](3-Usage.md)

> **NOTE:** This documentation is for `v4.0` (master branch), switch the branch if you're using an older version.
#### Basic Usage

From your application, call the flash method with a message and type.
Expand All @@ -29,58 +31,37 @@ notify()->flash('Welcome back!', 'success');
Within a view, you can now check if a flash message exists and output it.

```blade
@if (notify()->ready())
<div class="alert-box {{ notify()->type() }}">
{{ notify()->message() }}
@if (notify()->isNotEmpty())
@foreach(notify()->notifications() as $notification)
<div class="alert-box {{ $notification['type'] }}">
{{ $notification['message'] }}
</div>
@endforeach
@endif
```

> Notify is front-end framework agnostic, so you're free to easily implement the output however you choose (Like Twitter bootstrap, Zurb Foundation, Semantic UI ...).
#### Options
#### Extra Options

You can pass additional `options` to the `flash` method, which are then easily accessible within your view.
You can pass additional `extra` options to the `flash` method, which are then easily accessible within your view.

```php
notify()->flash("Great to see you again, Bruh!", 'success', [
'title' => 'Welcome back!',
'timer' => 5000,
'icon' => 'success-icon',
]);
```

Then, in your view.

```blade
@if (notify()->ready())
<script>
swal({
text: "{{ notify()->message() }}",
type: "{{ notify()->type() }}",
title: "{{ notify()->option('title') }}",
timer: {{ notify()->option('timer') }}
});
</script>
@endif
```

You can also check if an `option` exists or not like this:

```blade
@if (notify()->ready())
<script>
swal({
text: "{{ notify()->message() }}",
type: "{{ notify()->type() }}",
@if (notify()->hasOption('title'))
title: "{{ notify()->option('title') }}",
@endif
@if (notify()->hasOption('timer'))
timer: {{ notify()->option('timer') }}
@endif
});
</script>
@if (notify()->isNotEmpty())
@foreach(notify()->notifications() as $notification)
<div class="alert-box {{ $notification['type'] }}">
<h5><i class="{{ $notification['extra']['icon'] ?? 'default-icon' }}"></i> {{ $notification['extra']['title'] ?? 'Default Title' }}</h5>
<p>{{ $notification['message'] }}</p>
</div>
@endforeach
@endif
```

> The above example uses [SweetAlert](http://t4t5.github.io/sweetalert/), but the flexibily of Notify means you can easily use it with any JavaScript alert solution (For example [toastr](https://github.com/CodeSeven/toastr)).
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "arcanedev/notify",
"description": "Flexible flash notifications helper for Laravel.",
"keywords": ["arcanedev", "notify", "flash", "sweetalert", "sweet-alert", "laravel"],
"keywords": ["arcanedev", "laravel", "notify", "flash", "notifications", "alerts"],
"homepage": "https://github.com/ARCANEDEV/Notify",
"authors": [
{
Expand Down Expand Up @@ -38,10 +38,7 @@
"laravel": {
"providers": [
"Arcanedev\\Notify\\NotifyServiceProvider"
],
"aliases": {
"Notify": "Arcanedev\\Notify\\Facades\\Notify"
}
]
}
}
}
20 changes: 17 additions & 3 deletions config/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@
return [

/* -----------------------------------------------------------------
| Session
| Default Store
| -----------------------------------------------------------------
*/

'session' => [
'prefix' => 'notifier'
'default' => 'session',

/* -----------------------------------------------------------------
| Supported Stores
| -----------------------------------------------------------------
*/

'stores' => [

'session' => [
'driver' => Arcanedev\Notify\Stores\SessionStore::class,
'options' => [
'key' => 'notifications'
],
],

],

];
8 changes: 3 additions & 5 deletions helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
*
* @param string|null $message
* @param string $type
* @param array $options
* @param array $extra
*
* @return \Arcanedev\Notify\Contracts\Notify
*/
function notify($message = null, $type = 'info', array $options = [])
function notify($message = null, $type = 'info', array $extra = [])
{
/** @var Arcanedev\Notify\Contracts\Notify $notifier */
$notifier = app(Notify::class);

return is_null($message)
? $notifier
: $notifier->flash($message, $type, $options);
return is_null($message) ? $notifier : $notifier->flash($message, $type, $extra);
}
}
95 changes: 66 additions & 29 deletions src/Contracts/Notify.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Arcanedev\Notify\Contracts;

use Illuminate\Support\Collection;

/**
* Interface Notify
*
Expand All @@ -14,67 +16,102 @@ interface Notify
*/

/**
* Get the notification message.
* Get the store.
*
* @return string
* @return \Arcanedev\Notify\Contracts\Store
*/
public function message();
public function store(): Store;

/**
* Get the notification type.
* Set the store.
*
* @param \Arcanedev\Notify\Contracts\Store $store
*
* @return string
* @return \Arcanedev\Notify\Notify
*/
public function type();
public function setStore(Store $store);

/**
* Get an additional stored options.
*
* @param boolean $assoc
* Get all the notifications.
*
* @return mixed
* @return \Illuminate\Support\Collection
*/
public function notifications(): Collection;

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/
public function options($assoc = false);

/**
* Get a notification option.
* Flash an information message.
*
* @param string $key
* @param mixed|null $default
* @param string $message
* @param array $extra
*
* @return mixed
* @return \Arcanedev\Notify\Notify
*/
public function option($key, $default = null);
public function info(string $message, array $extra = []);

/**
* Check if the flash notification has an option.
* Flash a success message.
*
* @param string $key
* @param string $message
* @param array $extra
*
* @return bool
* @return $this
*/
public function hasOption($key);
public function success(string $message, array $extra = []);

/**
* If the notification is ready to be shown.
* Flash an error message.
*
* @return bool
* @param string $message
* @param array $extra
*
* @return $this
*/
public function ready();
public function error(string $message, array $extra = []);

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
/**
* Flash a warning message.
*
* @param string $message
* @param array $extra
*
* @return $this
*/
public function warning(string $message, array $extra = []);

/**
* Flash a message.
* Flash a new notification.
*
* @param string $message
* @param string|null $type
* @param array $options
* @param array $extra
*
* @return $this
*/
public function flash($message, $type = null, array $options = []);
public function flash($message, $type = 'info', array $extra = []);

/**
* Forget the notification.
*
* @return void
*/
public function forget();

/**
* Check if it has notifications.
*
* @return bool
*/
public function isEmpty(): bool;

/**
* Check if there is no notifications.
*
* @return bool
*/
public function isNotEmpty(): bool;
}
Loading

0 comments on commit 6085dd2

Please sign in to comment.