From 31852d8689b9b106977116567ec215a71befda03 Mon Sep 17 00:00:00 2001 From: Anders Bilfeldt Date: Sun, 18 Dec 2022 01:19:06 +0100 Subject: [PATCH 1/3] Remove the spatie package skeleton and fix issues with component namespacing + remove configuration --- composer.json | 1 - config/flash-message.php | 24 -------- src/FlashMessageServiceProvider.php | 61 ++++++++----------- .../Middleware/ShareMessagesFromSession.php | 2 +- src/View/Components/Alert.php | 10 +-- src/View/Components/AlertError.php | 2 +- src/View/Components/AlertInfo.php | 2 +- src/View/Components/AlertMessage.php | 2 +- src/View/Components/AlertSuccess.php | 2 +- src/View/Components/AlertWarning.php | 2 +- src/View/Components/Errors.php | 2 +- src/View/Components/Messages.php | 2 +- src/helpers.php | 6 +- 13 files changed, 41 insertions(+), 77 deletions(-) delete mode 100644 config/flash-message.php diff --git a/composer.json b/composer.json index 7a5a686..77dcf27 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,6 @@ ], "require": { "php": "^8.0", - "spatie/laravel-package-tools": "^1.4.3", "illuminate/contracts": "^8.51 || ^9.0" }, "require-dev": { diff --git a/config/flash-message.php b/config/flash-message.php deleted file mode 100644 index 334ca32..0000000 --- a/config/flash-message.php +++ /dev/null @@ -1,24 +0,0 @@ - 'messages', - - /* - |-------------------------------------------------------------------------- - | Session variable - |-------------------------------------------------------------------------- - | - | This is the name of the field used to flash all messages into the session. - | - */ - 'session_flash' => 'messages', -]; diff --git a/src/FlashMessageServiceProvider.php b/src/FlashMessageServiceProvider.php index fcd8980..f9623b2 100644 --- a/src/FlashMessageServiceProvider.php +++ b/src/FlashMessageServiceProvider.php @@ -4,51 +4,40 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Blade; +use Illuminate\Support\ServiceProvider; use Illuminate\Support\ViewErrorBag; use Illuminate\View\Factory; use Illuminate\View\View; -use Spatie\LaravelPackageTools\Package; -use Spatie\LaravelPackageTools\PackageServiceProvider; -class FlashMessageServiceProvider extends PackageServiceProvider +class FlashMessageServiceProvider extends ServiceProvider { public const VIEW_COMPONENT_NAMESPACE = 'flash'; - public function configurePackage(Package $package): void + /** + * Bootstrap any package services. + * + * @return void + */ + public function boot() { - /* - * This class is a Package Service Provider - * - * More info: https://github.com/spatie/laravel-package-tools - */ - $package - ->name('laravel-flash-message') - ->hasConfigFile() - ->hasViews(); // required for the view component blade files to be registered - // The package does not allow for namespaces (``) but only prefixes (`` - // so we register those manually using `componentNamespace()` - //->hasViewComponents( - // self::VIEW_COMPONENT_NAMESPACE, - // Messages::class, - // Alert::class, - // AlertError::class, - // AlertInfo::class, - // AlertMessage::class, - // AlertSuccess::class, - // AlertWarning::class - //); - } + $this->loadViewsFrom(__DIR__.'/../resources/views', self::VIEW_COMPONENT_NAMESPACE); + + $this->publishes([ + __DIR__.'/../resources/views' => resource_path('views/vendor/'.self::VIEW_COMPONENT_NAMESPACE), + ]); + + $this->publishes([ + __DIR__.'/../config/flash-message.php' => config_path('flash-message.php'), + ]); - public function packageBooted() - { Blade::componentNamespace('Bilfeldt\\LaravelFlashMessage\\View\\Components', self::VIEW_COMPONENT_NAMESPACE); // This is used when adding a message from a controller: view('posts-index')->withMessage(...) View::macro('withMessage', function (Message $message, string $bag = 'default'): View { /** @var ViewFlashMessageBag $viewFlashMessageBag */ - $viewFlashMessageBag = \Illuminate\Support\Facades\View::shared(config('flash-message.view_share'), new ViewFlashMessageBag()); + $viewFlashMessageBag = \Illuminate\Support\Facades\View::shared('messages', new ViewFlashMessageBag()); - \Illuminate\Support\Facades\View::share(config('flash-message.view_share'), $viewFlashMessageBag->push($message, $bag)); + \Illuminate\Support\Facades\View::share('messages', $viewFlashMessageBag->push($message, $bag)); return $this; }); @@ -56,9 +45,9 @@ public function packageBooted() // This is used when adding a message from the View Facade: \Illuminate\Support\Facades\View::withMessage(...) Factory::macro('withMessage', function (Message $message, string $bag = 'default'): Factory { /** @var ViewFlashMessageBag $viewFlashMessageBag */ - $viewFlashMessageBag = \Illuminate\Support\Facades\View::shared(config('flash-message.view_share'), new ViewFlashMessageBag()); + $viewFlashMessageBag = \Illuminate\Support\Facades\View::shared('messages', new ViewFlashMessageBag()); - \Illuminate\Support\Facades\View::share(config('flash-message.view_share'), $viewFlashMessageBag->push($message, $bag)); + \Illuminate\Support\Facades\View::share('messages', $viewFlashMessageBag->push($message, $bag)); return $this; }); @@ -66,13 +55,13 @@ public function packageBooted() // This is used when adding messages from a controller: view('posts-index')->withMessages(...) View::macro('withMessages', function (array $messages, string $bag = 'default'): View { /** @var ViewFlashMessageBag $viewFlashMessageBag */ - $viewFlashMessageBag = \Illuminate\Support\Facades\View::shared(config('flash-message.view_share'), new ViewFlashMessageBag()); + $viewFlashMessageBag = \Illuminate\Support\Facades\View::shared('messages', new ViewFlashMessageBag()); /** @var \Bilfeldt\LaravelFlashMessage\Message $message */ foreach ($messages as $message) { $viewFlashMessageBag->push($message, $bag); } - \Illuminate\Support\Facades\View::share(config('flash-message.view_share'), $viewFlashMessageBag); + \Illuminate\Support\Facades\View::share('messages', $viewFlashMessageBag); return $this; }); @@ -80,13 +69,13 @@ public function packageBooted() // This is used when adding messages from the View Facade: \Illuminate\Support\Facades\View::withMessages(...) Factory::macro('withMessages', function (array $messages, string $bag = 'default'): Factory { /** @var ViewFlashMessageBag $viewFlashMessageBag */ - $viewFlashMessageBag = \Illuminate\Support\Facades\View::shared(config('flash-message.view_share'), new ViewFlashMessageBag()); + $viewFlashMessageBag = \Illuminate\Support\Facades\View::shared('messages', new ViewFlashMessageBag()); /** @var \Bilfeldt\LaravelFlashMessage\Message $message */ foreach ($messages as $message) { $viewFlashMessageBag->push($message, $bag); } - \Illuminate\Support\Facades\View::share(config('flash-message.view_share'), $viewFlashMessageBag); + \Illuminate\Support\Facades\View::share('messages', $viewFlashMessageBag); return $this; }); diff --git a/src/Http/Middleware/ShareMessagesFromSession.php b/src/Http/Middleware/ShareMessagesFromSession.php index bf7ec7c..262d12e 100644 --- a/src/Http/Middleware/ShareMessagesFromSession.php +++ b/src/Http/Middleware/ShareMessagesFromSession.php @@ -43,7 +43,7 @@ public function handle($request, Closure $next) // without having to bind. An empty collection is set when there aren't any messages. $this->view->share( 'messages', - ViewFlashMessageBag::make($request->session()->get(config('flash-message.session_flash')) ?: []) + ViewFlashMessageBag::make($request->session()->get('messages') ?: []) ); // Putting the messages in the view for every view allows the developer to just diff --git a/src/View/Components/Alert.php b/src/View/Components/Alert.php index fe3d7dd..8dd493d 100644 --- a/src/View/Components/Alert.php +++ b/src/View/Components/Alert.php @@ -26,15 +26,15 @@ public function render() { switch ($this->level) { case Message::LEVEL_MESSAGE: - return view('flash-message::components.alert-message'); + return view('flash::components.alert-message'); case Message::LEVEL_INFO: - return view('flash-message::components.alert-info'); + return view('flash::components.alert-info'); case Message::LEVEL_SUCCESS: - return view('flash-message::components.alert-success'); + return view('flash::components.alert-success'); case Message::LEVEL_WARNING: - return view('flash-message::components.alert-warning'); + return view('flash::components.alert-warning'); case Message::LEVEL_ERROR: - return view('flash-message::components.alert-error'); + return view('flash::components.alert-error'); default: throw new \InvalidArgumentException('Invalid alert level: '.$this->level); } diff --git a/src/View/Components/AlertError.php b/src/View/Components/AlertError.php index 261a6bd..9176805 100644 --- a/src/View/Components/AlertError.php +++ b/src/View/Components/AlertError.php @@ -6,6 +6,6 @@ class AlertError extends AbstractAlert { public function render() { - return view('flash-message::components.alert-error'); + return view('flash::components.alert-error'); } } diff --git a/src/View/Components/AlertInfo.php b/src/View/Components/AlertInfo.php index 237bbbb..438cc47 100644 --- a/src/View/Components/AlertInfo.php +++ b/src/View/Components/AlertInfo.php @@ -6,6 +6,6 @@ class AlertInfo extends AbstractAlert { public function render() { - return view('flash-message::components.alert-info'); + return view('flash::components.alert-info'); } } diff --git a/src/View/Components/AlertMessage.php b/src/View/Components/AlertMessage.php index bf0a86c..92321fe 100644 --- a/src/View/Components/AlertMessage.php +++ b/src/View/Components/AlertMessage.php @@ -6,6 +6,6 @@ class AlertMessage extends AbstractAlert { public function render() { - return view('flash-message::components.alert-message'); + return view('flash::components.alert-message'); } } diff --git a/src/View/Components/AlertSuccess.php b/src/View/Components/AlertSuccess.php index 52257a5..455464e 100644 --- a/src/View/Components/AlertSuccess.php +++ b/src/View/Components/AlertSuccess.php @@ -6,6 +6,6 @@ class AlertSuccess extends AbstractAlert { public function render() { - return view('flash-message::components.alert-success'); + return view('flash::components.alert-success'); } } diff --git a/src/View/Components/AlertWarning.php b/src/View/Components/AlertWarning.php index f483ded..0aa18d9 100644 --- a/src/View/Components/AlertWarning.php +++ b/src/View/Components/AlertWarning.php @@ -6,6 +6,6 @@ class AlertWarning extends AbstractAlert { public function render() { - return view('flash-message::components.alert-warning'); + return view('flash::components.alert-warning'); } } diff --git a/src/View/Components/Errors.php b/src/View/Components/Errors.php index d9886e9..e5b0254 100644 --- a/src/View/Components/Errors.php +++ b/src/View/Components/Errors.php @@ -18,6 +18,6 @@ public function __construct( public function render() { - return view('flash-message::components.errors'); + return view('flash::components.errors'); } } diff --git a/src/View/Components/Messages.php b/src/View/Components/Messages.php index f4f14bf..a9e788f 100644 --- a/src/View/Components/Messages.php +++ b/src/View/Components/Messages.php @@ -15,6 +15,6 @@ public function __construct(string $bag = 'default') public function render() { - return view('flash-message::components.messages'); + return view('flash::components.messages'); } } diff --git a/src/helpers.php b/src/helpers.php index 2121bfd..e12e354 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -15,7 +15,7 @@ function session_message(Message $message, string $bag = 'default') { // This is added as a helper function simply because \Illuminate\Session\Store is not Macroable - session()->flash(config('flash-message.session_flash').'.'.$bag.'.'.Str::orderedUuid(), $message); + session()->flash('messages'.'.'.$bag.'.'.Str::orderedUuid(), $message); // Note that we are using ordered uuid as keys and flashing each message individually to the storage instead of // flashing the entire ViewFlashMessageBag object. This is because flashing the entire object will require @@ -24,13 +24,13 @@ function session_message(Message $message, string $bag = 'default') // Despite this "bug" this is how Laravel does it with errors. I expect this can cause problems especially // when being used in Livewire. See https://github.com/laravel/framework/blob/master/src/Illuminate/Http/RedirectResponse.php#L131 /* - $messages = session()->get(config('flash-message.session_flash'), new \Bilfeldt\LaravelFlashMessage\ViewFlashMessageBag()); + $messages = session()->get('messages', new \Bilfeldt\LaravelFlashMessage\ViewFlashMessageBag()); if (! $messages instanceof \Bilfeldt\LaravelFlashMessage\ViewFlashMessageBag) { $messages = new \Bilfeldt\LaravelFlashMessage\ViewFlashMessageBag; } - session()->flash(config('flash-message.session_flash'), $messages->push($message, $bag)); + session()->flash('messages', $messages->push($message, $bag)); */ } } From 2edbf54d3aa49ea042f9e8c23518fc9a2504f41f Mon Sep 17 00:00:00 2001 From: Anders Bilfeldt Date: Sun, 18 Dec 2022 01:20:57 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffb50f4..6b8a7e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,3 +19,5 @@ to the new namespace syntax ```blade // new syntax ``` + +The session and view keys are also removed from the configuration, leaving the configuration with using the hardcoded value `messages` for both. From 58e0b6486e333cf5a24f1901e0a17edae5a32468 Mon Sep 17 00:00:00 2001 From: Anders Bilfeldt Date: Sun, 18 Dec 2022 01:21:54 +0100 Subject: [PATCH 3/3] Remove config --- src/FlashMessageServiceProvider.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/FlashMessageServiceProvider.php b/src/FlashMessageServiceProvider.php index f9623b2..274730d 100644 --- a/src/FlashMessageServiceProvider.php +++ b/src/FlashMessageServiceProvider.php @@ -26,10 +26,6 @@ public function boot() __DIR__.'/../resources/views' => resource_path('views/vendor/'.self::VIEW_COMPONENT_NAMESPACE), ]); - $this->publishes([ - __DIR__.'/../config/flash-message.php' => config_path('flash-message.php'), - ]); - Blade::componentNamespace('Bilfeldt\\LaravelFlashMessage\\View\\Components', self::VIEW_COMPONENT_NAMESPACE); // This is used when adding a message from a controller: view('posts-index')->withMessage(...)