From 2a1ac4873c5c98d380196983a58834b452a0c082 Mon Sep 17 00:00:00 2001 From: Toon Van den Bos Date: Thu, 21 Dec 2023 14:47:53 +0100 Subject: [PATCH 1/4] Added CookiesServiceProvider with stub --- src/CookiesServiceProvider.php | 31 +++++++++++++++++++++++++++++++ stubs/CookiesServiceProvider.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/CookiesServiceProvider.php create mode 100644 stubs/CookiesServiceProvider.php diff --git a/src/CookiesServiceProvider.php b/src/CookiesServiceProvider.php new file mode 100644 index 0000000..218c191 --- /dev/null +++ b/src/CookiesServiceProvider.php @@ -0,0 +1,31 @@ +booted(function () { + $this->registerCookies(); + }); + } + + /** + * Define the cookies users should be aware of. + */ + abstract protected function registerCookies(): void; + + /** + * Bootstrap any application services. + */ + public function boot() + { + // + } +} \ No newline at end of file diff --git a/stubs/CookiesServiceProvider.php b/stubs/CookiesServiceProvider.php new file mode 100644 index 0000000..ce58c0d --- /dev/null +++ b/stubs/CookiesServiceProvider.php @@ -0,0 +1,32 @@ +session() + ->csrf(); + + // Register all Analytics cookies at once using one single shorthand method: + // Cookies::analytics() + // ->google(env('GOOGLE_ANALYTICS_ID')); + + // Register custom cookies under the pre-existing "optional" category: + // Cookies::optional() + // ->name('darkmode_enabled') + // ->description('This cookie helps us remember your preferences regarding the interface\'s brightness.') + // ->duration(120); + // ->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue())); + } +} From 77e13ad3bffde9595a45708fdfb75efc4adc2324 Mon Sep 17 00:00:00 2001 From: Toon Van den Bos Date: Thu, 21 Dec 2023 15:13:23 +0100 Subject: [PATCH 2/4] Added CookiesServiceProvider publication & updated README accordingly --- README.md | 68 +++++++++++++++++++++++++---------------- src/ServiceProvider.php | 4 +++ 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 9a34fc0..f0a98aa 100644 --- a/README.md +++ b/README.md @@ -59,41 +59,57 @@ This package will auto-register its service provider. First, publish the package's files: -1. Publish the configuration file: `php artisan vendor:publish --tag=laravel-cookie-consent-config` -2. Publish the customizable views: `php artisan vendor:publish --tag=laravel-cookie-consent-views` -3. Publish the translation files: `php artisan vendor:publish --tag=laravel-cookie-consent-lang` +1. Publish the `CookiesServiceProvider` file: `php artisan vendor:publish --tag=laravel-cookie-consent-service-provider` +2. Add the Service Provider to the `providers` array in `config/app.php`: + ```php + 'providers' => ServiceProvider::defaultProviders()->merge([ + // ... + App\Providers\RouteServiceProvider::class, + // Add the line below AFTER "App\Providers\RouteServiceProvider::class," + App\Providers\CookiesServiceProvider::class, + ])->toArray(), + ``` +3. Publish the configuration file: `php artisan vendor:publish --tag=laravel-cookie-consent-config` + +If you want to customize the consent modal's views: + +1. Publish the customizable views: `php artisan vendor:publish --tag=laravel-cookie-consent-views` +2. Publish the translation files: `php artisan vendor:publish --tag=laravel-cookie-consent-lang` More on [customization](#customization) below. -Now, we'll have to register and configure the used cookies. A good place to do so is in the `App\Providers\AppServiceProvider`'s `boot` method, but feel free to create your own `CookiesServiceProvider`. +Now, we'll have to register and configure the used cookies in the freshly published `App\Providers\CookiesServiceProvider`: ```php +namespace App\Providers; + use Whitecube\LaravelCookieConsent\Consent; use Whitecube\LaravelCookieConsent\Facades\Cookies; +use Whitecube\LaravelCookieConsent\CookiesServiceProvider as ServiceProvider; -public function boot() +class CookiesServiceProvider extends ServiceProvider { - // Register Laravel's base cookies under the "required" cookies section: - Cookies::essentials() - ->session() - ->csrf(); - - // Register all Analytics cookies at once using one single shorthand method: - Cookies::analytics() - ->google(env('GOOGLE_ANALYTICS_ID')); - - // Register custom cookies under the pre-existing "optional" category: - Cookies::optional() - ->name('darkmode_enabled') - ->description('This cookie helps us remember your preferences regarding the interface\'s brightness.') - ->duration(120) - ->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue())); - - // Register custom cookies under a custom "accessibility" category: - Cookies::accessibility() - ->name('high_contrast_enabled') - ->description('This cookie helps us remember your preferences regarding color contrast.') - ->duration(60 * 24 * 365); + /** + * Define the cookies users should be aware of. + */ + protected function registerCookies(): void + { + // Register Laravel's base cookies under the "required" cookies section: + Cookies::essentials() + ->session() + ->csrf(); + + // Register all Analytics cookies at once using one single shorthand method: + Cookies::analytics() + ->google(env('GOOGLE_ANALYTICS_ID')); + + // Register custom cookies under the pre-existing "optional" category: + Cookies::optional() + ->name('darkmode_enabled') + ->description('This cookie helps us remember your preferences regarding the interface\'s brightness.') + ->duration(120); + ->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue())); + } } ``` diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 1cb5c21..f947274 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -31,6 +31,10 @@ public function register() */ public function boot() { + $this->publishes([ + LCC_ROOT.'/stubs/CookiesServiceProvider.php' => app_path('Providers/CookiesServiceProvider.php'), + ], 'laravel-cookie-consent-service-provider'); + $this->publishes([ LCC_ROOT.'/config/cookieconsent.php' => config_path('cookieconsent.php'), ], 'laravel-cookie-consent-config'); From 4f6ae52f97b940df60fdc8544c03992541611a63 Mon Sep 17 00:00:00 2001 From: Toon Van den Bos Date: Thu, 21 Dec 2023 15:27:16 +0100 Subject: [PATCH 3/4] Added "important" usage notice --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f0a98aa..6814dbc 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ First, publish the package's files: 'providers' => ServiceProvider::defaultProviders()->merge([ // ... App\Providers\RouteServiceProvider::class, - // Add the line below AFTER "App\Providers\RouteServiceProvider::class," + // IMPORTANT: add the following line AFTER "App\Providers\RouteServiceProvider::class," App\Providers\CookiesServiceProvider::class, ])->toArray(), ``` From b360e20eebad4b984b65cd122fefc28a64696f18 Mon Sep 17 00:00:00 2001 From: Toon Van den Bos Date: Thu, 21 Dec 2023 15:29:03 +0100 Subject: [PATCH 4/4] Updated wording --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6814dbc..c709f5f 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ If you want to customize the consent modal's views: More on [customization](#customization) below. -Now, we'll have to register and configure the used cookies in the freshly published `App\Providers\CookiesServiceProvider`: +Now, we'll have to register and configure the used cookies in the freshly published `App\Providers\CookiesServiceProvider::registerCookies()` method: ```php namespace App\Providers;