Skip to content

Commit 4899dc9

Browse files
authoredDec 21, 2023
Merge pull request #29 from whitecube/feature-service-provider
Feature service provider
2 parents 784a4d8 + b360e20 commit 4899dc9

File tree

4 files changed

+109
-26
lines changed

4 files changed

+109
-26
lines changed
 

‎README.md

+42-26
Original file line numberDiff line numberDiff line change
@@ -59,41 +59,57 @@ This package will auto-register its service provider.
5959

6060
First, publish the package's files:
6161

62-
1. Publish the configuration file: `php artisan vendor:publish --tag=laravel-cookie-consent-config`
63-
2. Publish the customizable views: `php artisan vendor:publish --tag=laravel-cookie-consent-views`
64-
3. Publish the translation files: `php artisan vendor:publish --tag=laravel-cookie-consent-lang`
62+
1. Publish the `CookiesServiceProvider` file: `php artisan vendor:publish --tag=laravel-cookie-consent-service-provider`
63+
2. Add the Service Provider to the `providers` array in `config/app.php`:
64+
```php
65+
'providers' => ServiceProvider::defaultProviders()->merge([
66+
// ...
67+
App\Providers\RouteServiceProvider::class,
68+
// IMPORTANT: add the following line AFTER "App\Providers\RouteServiceProvider::class,"
69+
App\Providers\CookiesServiceProvider::class,
70+
])->toArray(),
71+
```
72+
3. Publish the configuration file: `php artisan vendor:publish --tag=laravel-cookie-consent-config`
73+
74+
If you want to customize the consent modal's views:
75+
76+
1. Publish the customizable views: `php artisan vendor:publish --tag=laravel-cookie-consent-views`
77+
2. Publish the translation files: `php artisan vendor:publish --tag=laravel-cookie-consent-lang`
6578

6679
More on [customization](#customization) below.
6780

68-
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`.
81+
Now, we'll have to register and configure the used cookies in the freshly published `App\Providers\CookiesServiceProvider::registerCookies()` method:
6982

7083
```php
84+
namespace App\Providers;
85+
7186
use Whitecube\LaravelCookieConsent\Consent;
7287
use Whitecube\LaravelCookieConsent\Facades\Cookies;
88+
use Whitecube\LaravelCookieConsent\CookiesServiceProvider as ServiceProvider;
7389

74-
public function boot()
90+
class CookiesServiceProvider extends ServiceProvider
7591
{
76-
// Register Laravel's base cookies under the "required" cookies section:
77-
Cookies::essentials()
78-
->session()
79-
->csrf();
80-
81-
// Register all Analytics cookies at once using one single shorthand method:
82-
Cookies::analytics()
83-
->google(env('GOOGLE_ANALYTICS_ID'));
84-
85-
// Register custom cookies under the pre-existing "optional" category:
86-
Cookies::optional()
87-
->name('darkmode_enabled')
88-
->description('This cookie helps us remember your preferences regarding the interface\'s brightness.')
89-
->duration(120)
90-
->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue()));
91-
92-
// Register custom cookies under a custom "accessibility" category:
93-
Cookies::accessibility()
94-
->name('high_contrast_enabled')
95-
->description('This cookie helps us remember your preferences regarding color contrast.')
96-
->duration(60 * 24 * 365);
92+
/**
93+
* Define the cookies users should be aware of.
94+
*/
95+
protected function registerCookies(): void
96+
{
97+
// Register Laravel's base cookies under the "required" cookies section:
98+
Cookies::essentials()
99+
->session()
100+
->csrf();
101+
102+
// Register all Analytics cookies at once using one single shorthand method:
103+
Cookies::analytics()
104+
->google(env('GOOGLE_ANALYTICS_ID'));
105+
106+
// Register custom cookies under the pre-existing "optional" category:
107+
Cookies::optional()
108+
->name('darkmode_enabled')
109+
->description('This cookie helps us remember your preferences regarding the interface\'s brightness.')
110+
->duration(120);
111+
->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue()));
112+
}
97113
}
98114
```
99115

‎src/CookiesServiceProvider.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Whitecube\LaravelCookieConsent;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
abstract class CookiesServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register any application services.
11+
*/
12+
public function register()
13+
{
14+
$this->booted(function () {
15+
$this->registerCookies();
16+
});
17+
}
18+
19+
/**
20+
* Define the cookies users should be aware of.
21+
*/
22+
abstract protected function registerCookies(): void;
23+
24+
/**
25+
* Bootstrap any application services.
26+
*/
27+
public function boot()
28+
{
29+
//
30+
}
31+
}

‎src/ServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function register()
3131
*/
3232
public function boot()
3333
{
34+
$this->publishes([
35+
LCC_ROOT.'/stubs/CookiesServiceProvider.php' => app_path('Providers/CookiesServiceProvider.php'),
36+
], 'laravel-cookie-consent-service-provider');
37+
3438
$this->publishes([
3539
LCC_ROOT.'/config/cookieconsent.php' => config_path('cookieconsent.php'),
3640
], 'laravel-cookie-consent-config');

‎stubs/CookiesServiceProvider.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Whitecube\LaravelCookieConsent\Consent;
6+
use Whitecube\LaravelCookieConsent\Facades\Cookies;
7+
use Whitecube\LaravelCookieConsent\CookiesServiceProvider as ServiceProvider;
8+
9+
class CookiesServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Define the cookies users should be aware of.
13+
*/
14+
protected function registerCookies(): void
15+
{
16+
// Register Laravel's base cookies under the "required" cookies section:
17+
Cookies::essentials()
18+
->session()
19+
->csrf();
20+
21+
// Register all Analytics cookies at once using one single shorthand method:
22+
// Cookies::analytics()
23+
// ->google(env('GOOGLE_ANALYTICS_ID'));
24+
25+
// Register custom cookies under the pre-existing "optional" category:
26+
// Cookies::optional()
27+
// ->name('darkmode_enabled')
28+
// ->description('This cookie helps us remember your preferences regarding the interface\'s brightness.')
29+
// ->duration(120);
30+
// ->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue()));
31+
}
32+
}

0 commit comments

Comments
 (0)
Please sign in to comment.