richan-fongdasen/firestore-laravel

A Google Cloud Firestore driver for Laravel Cache and Session.

1.0.0 2024-05-26 04:34 UTC

This package is auto-updated.

Last update: 2024-12-07 06:23:05 UTC


README

Latest Version on Packagist License: MIT PHPStan Test Coding Style codecov Total Downloads

This package allows you to use Google Cloud Firestore as a driver for Cache and Session store in Laravel application. This package is built on top of the official Google Cloud Firestore PHP client library.

Warning

This package is not yet compatible with Laravel Octane.

Requirements

  • PHP 8.2 or higher
  • Laravel 10.0 or higher
  • PHP Extension: grpc
  • PHP Extension: protobuf
  • Google Cloud Firestore Credentials

Installation

You can install the package via composer:

composer require richan-fongdasen/firestore-laravel

You can publish the config file with:

php artisan vendor:publish --tag="firestore-laravel-config"

This is the contents of the published config file:

return [
    'project_id'  => env('GOOGLE_CLOUD_PROJECT'),
    'credentials' => env('GOOGLE_APPLICATION_CREDENTIALS'),
    'database'    => env('FIRESTORE_DATABASE', '(default)'),

    'cache' => [
        'collection'           => env('FIRESTORE_CACHE_COLLECTION', 'cache'),
        'key_attribute'        => env('FIRESTORE_CACHE_KEY_ATTR', 'key'),
        'value_attribute'      => env('FIRESTORE_CACHE_VALUE_ATTR', 'value'),
        'expiration_attribute' => env('FIRESTORE_CACHE_EXPIRATION_ATTR', 'expired_at'),
    ],

    'session' => [
        'collection' => env('FIRESTORE_SESSION_COLLECTION', 'sessions'),
    ],
];

Configuration

Setting Up Firestore Authentication

Please see the Authentication guide for more information on authenticating your Google Cloud Firestore client.

Package Configuration

You can configure the package by setting the following environment variables in your .env file.

GOOGLE_CLOUD_PROJECT=your-google-cloud-project-id
GOOGLE_APPLICATION_CREDENTIALS="/path-to/your-service-account.json"
FIRESTORE_DATABASE="(default)"
FIRESTORE_CACHE_COLLECTION=cache
FIRESTORE_CACHE_KEY_ATTR=key
FIRESTORE_CACHE_VALUE_ATTR=value
FIRESTORE_CACHE_EXPIRATION_ATTR=expired_at
FIRESTORE_SESSION_COLLECTION=sessions

Cache Store Configuration

In order to use Firestore as a cache store, you need to append the following configuration into the config/cache.php file.

'stores' => [
    'firestore' => [
        'driver' => 'firestore',
    ],
],

Session Driver Configuration

In order to use Firestore as a session store, you need to modify the SESSION_DRIVER environment variable in your .env file.

SESSION_DRIVER=firestore

Usage

There is no special usage for this package. You can use the Cache and Session store as you normally do in Laravel.

// Cache store
Cache::put('key', 'value', 60); // Store a value in the cache for 60 seconds

$value = Cache::get('key'); // Retrieve a value from the cache

// Session
session(['key' => 'value']); // Store a value in the session

$value = session('key'); // Retrieve a value from the session

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.