PHP code example of sergkulich / laravel-key-rotate

1. Go to this page and download the library: Download sergkulich/laravel-key-rotate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sergkulich / laravel-key-rotate example snippets


namespace App\Providers;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        if (app()->runningInConsole()) {
            // Subscribe The Listener to The Event
            KeyRotate::withListener()
                // Discover Models that are not in App\ namespace
                ->withDir(base_path('src/Domain'), 'Domain\\')
                // Exclude Models
                ->withoutModel(User::class)
                // Exclude Model Fields
                ->withoutModelFileds(Secret::class, ['secret'])
                // Without Cast
                ->withoutCast('encrypted:array')
                // With Custom Cast
                ->withCast(CustomCast::class);
        }
    }
}

$keyRotateEventClass = KeyRotate::getEventClass();

Event::listen($keyRotateEventClass, MyKeyRotateListener::class);

KeyRotate::getListenerClass();

namespace App\Providers;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        if (app()->runningInConsole()) {
            KeyRotate::withListener();
        }
    }
}

// Get Instance
$listener = KeyRotate::getListener();

// Call it either
$listener();
// or
$listener->handle();

$listener->withDir(base_path('src/Domain'), 'NameSpace\\Domain\\');

$listener->withModel(CustomModel::class);

$listener->withoutModel(User::class);

$listener->withoutModelFields(User::class, ['encrypted_text', 'encrypted_array']);

[
    'encrypted',
    'encrypted:array',
    'encrypted:collection',
    'encrypted:object',
    AsEncryptedArrayObject::class,
    AsEncryptedCollection::class,
];

// Exclude cast
$listener->withoutCast('encrypted:array');

// Include cast
$listener->withCast(CustomCast::class);
shell
php artisan key:rotate
shell
php artisan key:rotate --force