├── .github └── workflows │ ├── run-php-cs-fixer.yml │ └── run-tests.yml ├── .php-cs-fixer.dist.php ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── laraflash.php ├── example.png ├── resources └── views │ └── components │ └── skins │ ├── bootstrap │ ├── basic.blade.php │ ├── titled.blade.php │ └── traditional.blade.php │ └── tailwind │ ├── banner.blade.php │ ├── left_accent_border.blade.php │ ├── solid.blade.php │ ├── titled.blade.php │ ├── top_accent_border.blade.php │ └── traditional.blade.php └── src ├── Exceptions ├── InvalidArgumentException.php ├── InvalidDelayException.php ├── InvalidHopsAmountException.php └── SkinNotFoundException.php ├── Facades └── Laraflash.php ├── FlashMessage ├── FlashMessage.php ├── FlashMessageFactory.php ├── FlashMessageFactoryContract.php ├── FlashMessageRendererContract.php └── ViewFlashMessageRenderer.php ├── Laraflash ├── Laraflash.php ├── LaraflashPreparer.php ├── LaraflashPreparerContract.php ├── LaraflashRenderer.php └── LaraflashRendererContract.php ├── MessagesStorage ├── ArrayMessagesStorage.php ├── MessagesStorageContract.php ├── MessagesStorageManager.php └── SessionMessagesStorage.php ├── Middleware └── HandleLaraflash.php ├── Providers └── LaraflashServiceProvider.php └── helpers.php /.github/workflows/run-php-cs-fixer.yml: -------------------------------------------------------------------------------- 1 | name: Fix PHP Code Style 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | paths: 7 | - '**.php' 8 | 9 | jobs: 10 | fix-php-code-style: 11 | name: Fix PHP Code Style 12 | runs-on: ubuntu-latest 13 | permissions: 14 | contents: write 15 | steps: 16 | - uses: actions/checkout@v3 17 | with: 18 | fetch-depth: 2 19 | token: ${{ secrets.GITHUB_TOKEN }} 20 | 21 | - name: Run PHP CS Fixer 22 | uses: docker://oskarstark/php-cs-fixer-ga 23 | 24 | - name: Commit changes 25 | uses: stefanzweifel/git-auto-commit-action@v4 26 | with: 27 | commit_message: Fix Backend Code Style 28 | skip_fetch: true 29 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: Run Tests 2 | 3 | on: [push] 4 | 5 | jobs: 6 | test: 7 | 8 | runs-on: ubuntu-latest 9 | strategy: 10 | matrix: 11 | php: [8.2, 8.1] 12 | laravel: [10.*] 13 | dependency-version: [prefer-lowest, prefer-stable] 14 | include: 15 | - laravel: 10.* 16 | testbench: 8.* 17 | 18 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} 19 | 20 | 21 | steps: 22 | - name: Checkout code 23 | uses: actions/checkout@v1 24 | 25 | - name: Cache dependencies 26 | uses: actions/cache@v1 27 | with: 28 | path: ~/.composer/cache/files 29 | key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 30 | 31 | - name: Setup PHP 32 | uses: shivammathur/setup-php@v2 33 | with: 34 | php-version: ${{ matrix.php }} 35 | extensions: mbstring 36 | 37 | - name: Install dependencies 38 | run: | 39 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 40 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest 41 | 42 | - name: Execute tests 43 | run: vendor/bin/phpunit 44 | -------------------------------------------------------------------------------- /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | exclude('node_modules') 5 | ->exclude('vendor') 6 | ->in(__DIR__); 7 | 8 | $config = new PhpCsFixer\Config(); 9 | return $config->setRules([ 10 | '@PSR2' => true, 11 | ]) 12 | ->setFinder($finder); -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `coderello/laraflash` will be documented in this file 4 | 5 | ## 0.1.0 6 | 7 | - Initial release 8 | 9 | ## 0.2.0 10 | 11 | - Added the ability to specify default values for the flash message in the config file 12 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Requirements 8 | 9 | If the project maintainer has any additional requirements, you will find them listed here. 10 | 11 | 12 | - **[PSR-2 Coding Standard.](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** The easiest way to apply the conventions is to install [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer). 13 | - **Add tests!** Your patch won't be accepted if it doesn't have tests. 14 | - **Document any change in behaviour.** Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | - **Consider our release cycle.** We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 16 | - **Create feature branches.** Don't ask us to pull from your master branch. 17 | - **One pull request per feature.** If you want to do more than one thing, send multiple pull requests. 18 | - **Send coherent history.** Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 19 | 20 | **Happy coding!** 21 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Coderello 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Laraflash

2 | 3 |

Laraflash provides a handy way to work with the flash messages.

4 | 5 |

6 | Latest Version 7 | Software License 8 | StyleCI 9 | Quality Score 10 | Coverage Status 11 |

12 | 13 | ## Installation 14 | 15 | You can install this package via composer using this command: 16 | 17 | ```bash 18 | composer require coderello/laraflash 19 | ``` 20 | 21 | After that you need to register the `\Coderello\Laraflash\Middleware\HandleLaraflash::class` middleware after the `\Illuminate\Session\Middleware\StartSession::class` one in the `app\Http\Kernel.php` 22 | 23 | You can publish the config file with: 24 | 25 | ```bash 26 | php artisan vendor:publish --tag="laraflash-config" 27 | ``` 28 | 29 | ## Adding flash messages 30 | 31 | There are many syntax variations for adding flash messages, so you can choose the one you like the most. 32 | 33 | Let's take a look at some of them. 34 | 35 | ```php 36 | use Coderello\Laraflash\Facades\Laraflash; 37 | 38 | Laraflash::message()->content('Some content')->title('Some title')->type('success'); 39 | ``` 40 | 41 | > `message()` method creates and returns fresh `FlashMessage` instance which can be modified by chaining methods (all methods could be found in the `FlashMessage methods` section). 42 | 43 | ```php 44 | laraflash()->message()->content('Some content')->title('Some title')->type('success'); 45 | ``` 46 | 47 | > `Laraflash` facade can be replaced with the `laraflash()` helper as you could saw in the example above. 48 | 49 | ```php 50 | laraflash()->message('Some content', 'Some title')->success(); 51 | ``` 52 | 53 | > `message()` method accepts up to five arguments: `$content`, `$title`, `$type`, `$delay`, `$hops`. 54 | 55 | ```php 56 | laraflash('Some content', 'Some title')->success(); 57 | ``` 58 | 59 | > Arguments mentioned in the previous example can be passed directly to the `laraflash()` helper. 60 | 61 | ## Rendering flash messages 62 | 63 | Ready flash messages could be rendered using the `render()` method of the `Laraflash` instance. 64 | 65 | ```php 66 | laraflash()->render(); 67 | ``` 68 | 69 | > All methods of the `Laraflash` instance (which could be obtained by calling `laraflash()` helper without arguments being passed) could be found in the `Laraflash methods` section. 70 | 71 | > Output HTML will be generated using skin, specified in the `laraflash.skin` config. All available skins are listed in the config file. 72 | 73 | ```html 74 |
79 | ``` 80 | 81 | > Default separator between the messages is the `
`, which is specified in the `laraflash.separator` config. Feel free to change it if you need. 82 | 83 | Example of messages rendered as HTML: 84 | 85 | ![Example](example.png) 86 | 87 | ## Obtaining flash messages as an array 88 | 89 | Flash messages can be obtained as an array using the `toArray()` method. 90 | 91 | ```php 92 | laraflash()->toArray(); 93 | ``` 94 | 95 | Here is the result: 96 | 97 | ``` 98 | [ 99 | [ 100 | "title" => null, 101 | "content" => "Instant message.", 102 | "type" => "danger", 103 | "hops" => 1, 104 | "delay" => 0, 105 | ], 106 | ] 107 | ``` 108 | 109 | > You can use array representation of flash messages for your API. 110 | 111 | 112 | ## `Laraflash` methods 113 | 114 | #### `message(?string $content = null, ?string $title = null, ?string $type = null, ?int $delay = null, ?int $hops = null): FlashMessage` 115 | 116 | Creates and returns fresh `FlashMessage` instance. 117 | 118 | #### `render()` 119 | 120 | Renders ready flash messages as HTML. 121 | 122 | #### `keep(): self` 123 | 124 | Adds one more hop to each flash message. 125 | 126 | #### `clear(): self` 127 | 128 | Deletes all flash messages. 129 | 130 | #### `all(): Collection` 131 | 132 | Returns the `Collection` instance containing all flash messages. 133 | 134 | #### `ready(): Collection` 135 | 136 | Returns the `Collection` instance containing ready flash messages. 137 | 138 | #### `touch(): self` 139 | 140 | Touches all flash messages (decrements amount of hops and delay, deletes expired messages). 141 | 142 | #### `toArray()` 143 | 144 | Returns an array representation of ready flash messages. 145 | 146 | #### `toJson()` 147 | 148 | Returns JSON representation of ready flash messages. 149 | 150 | ## `FlashMessage` methods 151 | 152 | #### `content(?string $content): self` 153 | 154 | Sets the content of the flash message. 155 | 156 | #### `title(?string $title): self` 157 | 158 | Sets the title of the flash message. 159 | 160 | #### `type(?string $type): self` 161 | 162 | Sets the type of the flash message. 163 | 164 | #### `danger(): self` 165 | 166 | Sets the `danger` type for the flash message. 167 | 168 | #### `warning(): self` 169 | 170 | Sets the `warning` type for the flash message. 171 | 172 | #### `info(): self` 173 | 174 | Sets the `info` type for the flash message. 175 | 176 | #### `success(): self` 177 | 178 | Sets the `success` type for the flash message. 179 | 180 | #### `hops(int $hops): self` 181 | 182 | Sets the hops amount of the message (the number of requests in which the message will be present). 183 | > Default: 1 184 | 185 | #### `delay(int $delay): self` 186 | 187 | Sets the delay of the message (the number of requests in which the message will be waiting to receive the ready state). 188 | > Default: 1 189 | 190 | #### `now(): self` 191 | 192 | Shortcut for `->delay(0)` 193 | 194 | #### `keep(): self` 195 | 196 | Increments the amount of hops. 197 | 198 | #### `attribute(string $key, $value = null): self` 199 | 200 | Sets the custom attribute which will be present in the array representation of the message and could be obtained using the `get()` method. 201 | 202 | #### `get(string $key)` 203 | 204 | Returns the value of the attribute. 205 | 206 | #### `toArray()` 207 | 208 | Returns an array representation of the message. 209 | 210 | #### `toJson()` 211 | 212 | Returns JSON representation of the message. 213 | 214 | ## Testing 215 | 216 | You can run the tests with: 217 | 218 | ```bash 219 | composer test 220 | ``` 221 | 222 | ## Changelog 223 | 224 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 225 | 226 | ## Contributing 227 | 228 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 229 | 230 | ## 📖 License 231 | 232 | **Larflash** is open-sourced software licensed under the [MIT license](LICENSE.md). 233 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coderello/laraflash", 3 | "description": "Advanced flash messages for Laravel.", 4 | "keywords": ["laraflash", "laravel", "advanced", "flash", "messages"], 5 | "type": "library", 6 | "license": "MIT", 7 | "require": { 8 | "php": "^8.1", 9 | "laravel/framework": "^10.0", 10 | "ext-json": "*" 11 | }, 12 | "require-dev": { 13 | "orchestra/testbench": "^8.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Coderello\\Laraflash\\": "src/" 18 | }, 19 | "files": [ 20 | "src/helpers.php" 21 | ] 22 | }, 23 | "autoload-dev": { 24 | "psr-4": { 25 | "Coderello\\Laraflash\\Tests\\": "tests/" 26 | } 27 | }, 28 | "scripts": { 29 | "test": "vendor/bin/phpunit --colors=always" 30 | }, 31 | "extra": { 32 | "laravel": { 33 | "providers": [ 34 | "Coderello\\Laraflash\\Providers\\LaraflashServiceProvider" 35 | ] 36 | } 37 | }, 38 | "config": { 39 | "sort-packages": true 40 | }, 41 | "minimum-stability": "dev", 42 | "prefer-stable": true 43 | } 44 | -------------------------------------------------------------------------------- /config/laraflash.php: -------------------------------------------------------------------------------- 1 | 'laraflash_skin::tailwind.left_accent_border', 29 | 30 | /* 31 | |-------------------------------------------------------------------------- 32 | | Separator Between The Flash Messages 33 | |-------------------------------------------------------------------------- 34 | | 35 | | This value is the separator between the flash messages. 36 | | 37 | */ 38 | 'separator' => '
', 39 | 40 | /* 41 | |-------------------------------------------------------------------------- 42 | | Messages Storage 43 | |-------------------------------------------------------------------------- 44 | | 45 | | Drivers: "session", "array" 46 | | 47 | */ 48 | 'messages_storage' => 'session', 49 | 50 | ]; 51 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coderello/laraflash/d06923a3888ec80c6ab2cb17d15fcc9585a7683e/example.png -------------------------------------------------------------------------------- /resources/views/components/skins/bootstrap/basic.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/components/skins/bootstrap/titled.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/components/skins/bootstrap/traditional.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/components/skins/tailwind/banner.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/components/skins/tailwind/left_accent_border.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/components/skins/tailwind/solid.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/components/skins/tailwind/titled.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | {{ $title ?? null }} 4 |
5 |
6 |

{{ $content ?? null }}

7 |
8 |
-------------------------------------------------------------------------------- /resources/views/components/skins/tailwind/top_accent_border.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/components/skins/tailwind/traditional.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidArgumentException.php: -------------------------------------------------------------------------------- 1 | message = 'Invalid delay.'; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidHopsAmountException.php: -------------------------------------------------------------------------------- 1 | message = 'Invalid hops amount.'; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Exceptions/SkinNotFoundException.php: -------------------------------------------------------------------------------- 1 | message = sprintf('Skin [%s] not found.', $skin); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Facades/Laraflash.php: -------------------------------------------------------------------------------- 1 | content = $content; 36 | } else { 37 | unset($this->content); 38 | } 39 | 40 | return $this; 41 | } 42 | 43 | public function title(?string $title): self 44 | { 45 | if (! is_null($title)) { 46 | $this->title = $title; 47 | } else { 48 | unset($this->title); 49 | } 50 | 51 | return $this; 52 | } 53 | 54 | public function type(?string $type): self 55 | { 56 | if (! is_null($type)) { 57 | $this->type = $type; 58 | } else { 59 | unset($this->type); 60 | } 61 | 62 | return $this; 63 | } 64 | 65 | public function danger(): self 66 | { 67 | $this->type('danger'); 68 | 69 | return $this; 70 | } 71 | 72 | public function warning(): self 73 | { 74 | $this->type('warning'); 75 | 76 | return $this; 77 | } 78 | 79 | public function info(): self 80 | { 81 | $this->type('info'); 82 | 83 | return $this; 84 | } 85 | 86 | public function success(): self 87 | { 88 | $this->type('success'); 89 | 90 | return $this; 91 | } 92 | 93 | public function hops(int $hops): self 94 | { 95 | if ($hops < 1) { 96 | throw new InvalidHopsAmountException; 97 | } 98 | 99 | $this->hops = $hops; 100 | 101 | return $this; 102 | } 103 | 104 | public function delay(int $delay): self 105 | { 106 | if ($delay < 0) { 107 | throw new InvalidDelayException; 108 | } 109 | 110 | $this->delay = $delay; 111 | 112 | return $this; 113 | } 114 | 115 | public function now(): self 116 | { 117 | $this->delay(0); 118 | 119 | return $this; 120 | } 121 | 122 | public function keep(): self 123 | { 124 | $this->hops++; 125 | 126 | return $this; 127 | } 128 | 129 | public function attribute(string $key, $value = null): self 130 | { 131 | if (! is_null($value)) { 132 | $this->attributes[$key] = $value; 133 | } else { 134 | unset($this->attributes[$key]); 135 | } 136 | 137 | return $this; 138 | } 139 | 140 | public function get(string $key) 141 | { 142 | if (in_array($key, ['title', 'content', 'hops', 'delay', 'type'])) { 143 | return $this->{$key}; 144 | } 145 | 146 | return $this->attributes[$key] ?? null; 147 | } 148 | 149 | public function toArray() 150 | { 151 | return array_merge( 152 | $this->attributes, 153 | [ 154 | 'content' => $this->content, 155 | 'title' => $this->title, 156 | 'type' => $this->type, 157 | 'hops' => $this->hops, 158 | 'delay' => $this->delay, 159 | ] 160 | ); 161 | } 162 | 163 | public function toJson($options = 0) 164 | { 165 | return json_encode($this, $options); 166 | } 167 | 168 | public function jsonSerialize() 169 | { 170 | return $this->toArray(); 171 | } 172 | 173 | public function offsetExists($offset) 174 | { 175 | return isset($this->attributes[$offset]); 176 | } 177 | 178 | public function offsetGet($offset) 179 | { 180 | return $this->attributes[$offset]; 181 | } 182 | 183 | public function offsetSet($offset, $value) 184 | { 185 | $this->attribute($offset, $value); 186 | } 187 | 188 | public function offsetUnset($offset) 189 | { 190 | unset($this->attributes[$offset]); 191 | } 192 | 193 | public function __get($name) 194 | { 195 | return $this->attributes[$name] ?? null; 196 | } 197 | 198 | public function __set($name, $value) 199 | { 200 | $this->attribute($name, $value); 201 | } 202 | 203 | public function __isset($name) 204 | { 205 | return isset($this->attributes[$name]); 206 | } 207 | 208 | public function __unset($name) 209 | { 210 | unset($this->attributes[$name]); 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /src/FlashMessage/FlashMessageFactory.php: -------------------------------------------------------------------------------- 1 | container = Container::getInstance(); 14 | } 15 | 16 | public function make(): FlashMessage 17 | { 18 | return $this->container->make(FlashMessage::class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/FlashMessage/FlashMessageFactoryContract.php: -------------------------------------------------------------------------------- 1 | viewFactory = $viewFactory; 20 | 21 | $this->configRepository = $configRepository; 22 | } 23 | 24 | public function setSkin(string $skin) 25 | { 26 | $this->skin = $skin; 27 | } 28 | 29 | public function getSkin() 30 | { 31 | return $this->skin ?? $this->configRepository->get('laraflash.skin'); 32 | } 33 | 34 | public function render(FlashMessage $flashMessage): string 35 | { 36 | $skin = $this->getSkin(); 37 | 38 | if (! $this->viewFactory->exists($skin)) { 39 | throw new SkinNotFoundException($skin); 40 | } 41 | 42 | return $this->viewFactory->make($skin, $flashMessage->toArray()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Laraflash/Laraflash.php: -------------------------------------------------------------------------------- 1 | flashMessageFactory = $flashMessageFactory; 31 | 32 | $this->messagesStorage = $messagesStorage; 33 | 34 | $this->laraflashRenderer = $laraflashRenderer; 35 | 36 | $this->messages = Collection::make(); 37 | } 38 | 39 | public function load(): self 40 | { 41 | $this->messages = Collection::make($this->messagesStorage->get()) 42 | ->whereInstanceOf(FlashMessage::class); 43 | 44 | return $this; 45 | } 46 | 47 | public function save(): self 48 | { 49 | $this->messagesStorage->put($this->messages->all()); 50 | 51 | return $this; 52 | } 53 | 54 | public function message(?string $content = null, ?string $title = null, ?string $type = null, ?int $delay = null, ?int $hops = null): FlashMessage 55 | { 56 | $message = $this->flashMessageFactory->make(); 57 | 58 | if (! is_null($content)) { 59 | $message->content($content); 60 | } 61 | 62 | if (! is_null($title)) { 63 | $message->title($title); 64 | } 65 | 66 | if (! is_null($type)) { 67 | $message->type($type); 68 | } 69 | 70 | if (! is_null($delay)) { 71 | $message->delay($delay); 72 | } 73 | 74 | if (! is_null($hops)) { 75 | $message->hops($hops); 76 | } 77 | 78 | $this->messages->push($message); 79 | 80 | return $message; 81 | } 82 | 83 | public function keep(): self 84 | { 85 | $this->messages->each(function (FlashMessage $message) { 86 | $message->keep(); 87 | }); 88 | 89 | return $this; 90 | } 91 | 92 | public function clear(): self 93 | { 94 | $this->messages = Collection::make(); 95 | 96 | return $this; 97 | } 98 | 99 | public function all(): Collection 100 | { 101 | return Collection::make($this->messages); 102 | } 103 | 104 | public function ready(): Collection 105 | { 106 | return $this->messages 107 | ->filter(function (FlashMessage $message) { 108 | return $message->get('delay') === 0; 109 | }); 110 | } 111 | 112 | public function touch(): self 113 | { 114 | $this->messages = $this->messages->reject(function (FlashMessage $message) { 115 | return $message->get('hops') <= 1 && $message->get('delay') === 0; 116 | })->each(function (FlashMessage $message) { 117 | if ($message->get('hops') > 1 && $message->get('delay') === 0) { 118 | $message->hops($message->get('hops') - 1); 119 | } elseif ($message->get('delay') > 0) { 120 | $message->delay($message->get('delay') - 1); 121 | } 122 | }); 123 | 124 | return $this; 125 | } 126 | 127 | public function toArray() 128 | { 129 | return $this->ready()->map(function (FlashMessage $message) { 130 | return $message->toArray(); 131 | })->values()->all(); 132 | } 133 | 134 | public function jsonSerialize() 135 | { 136 | return $this->toArray(); 137 | } 138 | 139 | public function toJson($options = 0): string 140 | { 141 | return json_encode($this, $options); 142 | } 143 | 144 | public function offsetExists($offset) 145 | { 146 | return $this->messages->has($offset); 147 | } 148 | 149 | public function offsetGet($offset) 150 | { 151 | return $this->messages->get($offset); 152 | } 153 | 154 | public function offsetSet($offset, $value) 155 | { 156 | throw new BadMethodCallException; 157 | } 158 | 159 | public function offsetUnset($offset) 160 | { 161 | $this->messages->forget($offset); 162 | } 163 | 164 | public function toHtml() 165 | { 166 | return $this->render(); 167 | } 168 | 169 | public function render() 170 | { 171 | return $this->laraflashRenderer->render($this); 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /src/Laraflash/LaraflashPreparer.php: -------------------------------------------------------------------------------- 1 | touch(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Laraflash/LaraflashPreparerContract.php: -------------------------------------------------------------------------------- 1 | flashMessageRenderer = $flashMessageRenderer; 20 | 21 | $this->configRepository = $configRepository; 22 | } 23 | 24 | public function setSeparator(string $separator) 25 | { 26 | $this->separator = $separator; 27 | } 28 | 29 | public function getSeparator() 30 | { 31 | return $this->separator ?? $this->configRepository->get('laraflash.separator'); 32 | } 33 | 34 | public function render(Laraflash $laraflash): string 35 | { 36 | return $laraflash->ready() 37 | ->map(function (FlashMessage $message) { 38 | return $this->flashMessageRenderer->render($message); 39 | }) 40 | ->implode($this->getSeparator()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Laraflash/LaraflashRendererContract.php: -------------------------------------------------------------------------------- 1 | messages; 12 | } 13 | 14 | public function put(array $messages): void 15 | { 16 | $this->messages = $messages; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/MessagesStorage/MessagesStorageContract.php: -------------------------------------------------------------------------------- 1 | container['config']['laraflash.messages_storage']; 12 | } 13 | 14 | public function createSessionDriver() 15 | { 16 | return $this->container->make(SessionMessagesStorage::class); 17 | } 18 | 19 | public function createArrayDriver() 20 | { 21 | return $this->container->make(ArrayMessagesStorage::class); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/MessagesStorage/SessionMessagesStorage.php: -------------------------------------------------------------------------------- 1 | session = $session; 16 | } 17 | 18 | public function setKey(string $key) 19 | { 20 | $this->key = $key; 21 | } 22 | 23 | public function getKey() 24 | { 25 | return $this->key ?? 'flash_messages'; 26 | } 27 | 28 | public function get(): array 29 | { 30 | return $this->session->get($this->getKey(), []); 31 | } 32 | 33 | public function put(array $messages): void 34 | { 35 | $this->session->put($this->getKey(), $messages); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Middleware/HandleLaraflash.php: -------------------------------------------------------------------------------- 1 | laraflash = Container::getInstance()->make('laraflash'); 18 | 19 | $this->laraflashToucher = $laraflashToucher; 20 | } 21 | 22 | public function handle($request, Closure $next) 23 | { 24 | $this->laraflash->load(); 25 | 26 | $this->laraflashToucher->handle($this->laraflash, $request); 27 | 28 | $response = $next($request); 29 | 30 | $this->laraflash->save(); 31 | 32 | return $response; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Providers/LaraflashServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerResources(); 30 | 31 | $this->offerPublishing(); 32 | } 33 | 34 | /** 35 | * Register any application services. 36 | * 37 | * @return void 38 | */ 39 | public function register() 40 | { 41 | $this->registerBindings(); 42 | 43 | $this->configure(); 44 | } 45 | 46 | /** 47 | * Register the Laraflash bindings. 48 | * 49 | * @return void 50 | */ 51 | protected function registerBindings() 52 | { 53 | $this->app->bind(MessagesStorageContract::class, SessionMessagesStorage::class); 54 | 55 | $this->app->bind(LaraflashRendererContract::class, LaraflashRenderer::class); 56 | 57 | $this->app->bind(FlashMessageRendererContract::class, ViewFlashMessageRenderer::class); 58 | 59 | $this->app->bind(LaraflashPreparerContract::class, LaraflashPreparer::class); 60 | 61 | $this->app->bind(FlashMessageFactoryContract::class, FlashMessageFactory::class); 62 | 63 | $this->app->singleton(MessagesStorageManager::class, function (Application $app) { 64 | return new MessagesStorageManager($app); 65 | }); 66 | 67 | $this->app->bind(MessagesStorageContract::class, function (Application $app) { 68 | /** @var MessagesStorageManager $messagesStorageManager */ 69 | $messagesStorageManager = $app->make(MessagesStorageManager::class); 70 | 71 | return $messagesStorageManager->driver(); 72 | }); 73 | 74 | $this->app->singleton('laraflash', function (Application $app) { 75 | return $app->make(Laraflash::class); 76 | }); 77 | } 78 | 79 | /** 80 | * Setup the resource publishing groups for Laraflash. 81 | * 82 | * @return void 83 | */ 84 | protected function offerPublishing() 85 | { 86 | $this->publishes([ 87 | __DIR__.'/../../resources/views' => $this->app->resourcePath('views/vendor/laraflash'), 88 | ], 'laraflash-views'); 89 | 90 | $this->publishes([ 91 | __DIR__.'/../../config/laraflash.php' => $this->app->configPath('laraflash.php'), 92 | ], 'laraflash-config'); 93 | } 94 | 95 | /** 96 | * Register the Laraflash resources. 97 | * 98 | * @return void 99 | */ 100 | protected function registerResources() 101 | { 102 | $this->loadViewsFrom(__DIR__.'/../../resources/views/components/skins', 'laraflash_skin'); 103 | } 104 | 105 | /** 106 | * Setup the configuration for Laraflash. 107 | * 108 | * @return void 109 | */ 110 | protected function configure() 111 | { 112 | $this->mergeConfigFrom( 113 | __DIR__.'/../../config/laraflash.php', 114 | 'laraflash' 115 | ); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | make('laraflash'); 10 | 11 | if ($args) { 12 | return $laraflash->message(...$args); 13 | } 14 | 15 | return $laraflash; 16 | } 17 | } 18 | --------------------------------------------------------------------------------