Skip to content

Commit

Permalink
Add configuration for custom storage (#326)
Browse files Browse the repository at this point in the history
* Add configuration for custom storage

* Clean up configuration

* Add latest laravel and php versions to matrix

* Matrix now at L8 and PHP v7.3

* Fix typo

* Update matrix

* Upgrade phpspec
  • Loading branch information
omniphx authored Feb 26, 2023
1 parent 0a41429 commit 5e14d42
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 11 deletions.
25 changes: 20 additions & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Tests

on: [push, pull_request]
on:
pull_request:
branches:
- master

jobs:
test:
Expand All @@ -9,12 +12,24 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: ['7.2', '7.3', '7.4', '8.0']
laravel: [6.*, 7.*, 8.*]
php: ["7.3", "7.4", "8.0", "8.1", "8.2"]
laravel: [8.*, 9.*, 10.*]
dependency-version: [prefer-stable]
exclude:
- laravel: 8.*
php: 7.2
- laravel: 9.*
php: 7.3
- laravel: 10.*
php: 7.3
- laravel: 9.*
php: 7.4
- laravel: 10.*
php: 7.4
- laravel: 9.*
php: 8.0
- laravel: 10.*
php: 8.0
- laravel: 10.*
php: 8.1

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

Expand Down
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Salesforce REST API Client for Laravel <img align="right" src="https://raw.githubusercontent.com/omniphx/images/master/Forrest.png">

[![Laravel](https://img.shields.io/badge/Laravel-9.0-orange.svg?style=flat-square)](http://laravel.com)
[![Laravel](https://img.shields.io/badge/Laravel-10.x-orange.svg?style=flat-square)](http://laravel.com)
[![Latest Stable Version](https://img.shields.io/packagist/v/omniphx/forrest.svg?style=flat-square)](https://packagist.org/packages/omniphx/forrest)
[![Total Downloads](https://img.shields.io/packagist/dt/omniphx/forrest.svg?style=flat-square)](https://packagist.org/packages/omniphx/forrest)
[![License](https://img.shields.io/packagist/l/omniphx/forrest.svg?style=flat-square)](https://packagist.org/packages/omniphx/forrest)
Expand Down Expand Up @@ -597,3 +597,76 @@ $forrest2->authenticate();
```

For more information about Guzzle responses and event listeners, refer to their [documentation](http://guzzle.readthedocs.org).

### Creating a custom store

If you'd prefer to use storage other than `session`, `cache` or `object`, you can implement a custom implementation by configuring a custom class instance in `storage.type`:

```php
'storage' => [
'type' => App\Storage\CustomStorage::class,
],
```

You class can be named anything but it must implement `Omniphx\Forrest\Interfaces\StorageInterface`:

```php
<?php

namespace App\Storage;

use Session;
use Omniphx\Forrest\Exceptions\MissingKeyException;
use Omniphx\Forrest\Interfaces\StorageInterface;

class CustomStorage implements StorageInterface
{
public $path;

public function __construct()
{
$this->path = 'app.custom.path';
}

/**
* Store into session.
*
* @param $key
* @param $value
*
* @return void
*/
public function put($key, $value)
{
return Session::put($this->path.$key, $value);
}

/**
* Get from session.
*
* @param $key
*
* @return mixed
*/
public function get($key)
{
if(!$this->has($key)) {
throw new MissingKeyException(sprintf('No value for requested key: %s', $key));
}

return Session::get($this->path.$key);
}

/**
* Check if storage has a key.
*
* @param $key
*
* @return bool
*/
public function has($key)
{
return Session::has($this->path.$key);
}
}
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"guzzlehttp/guzzle": "~6.0|~7.0"
},
"require-dev": {
"phpspec/phpspec": "~6.0"
"phpspec/phpspec": "~6.0|~7.0"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 10 additions & 0 deletions src/Omniphx/Forrest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,16 @@ public function getInstanceURL()
return $this->instanceURLRepo->get();
}

/**
* Accessor to get the token object
*
* @return mixed
*/
public function getToken()
{
return $this->tokenRepo->get();
}

/**
* Returns any resource that is available to the authenticated
* user. Reference Force.com's REST API guide to read about more
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Omniphx\Forrest\Providers\Laravel\LaravelCache;
use Omniphx\Forrest\Providers\Laravel\LaravelSession;
use Omniphx\Forrest\Providers\ObjectStorage;
use Omniphx\Forrest\Interfaces\StorageInterface;

class ForrestServiceProvider extends BaseServiceProvider
{
Expand Down Expand Up @@ -41,7 +42,11 @@ protected function getStorage($storageType)
case 'object':
return new ObjectStorage();
default:
return new LaravelSession(app('config'), app('request')->session());
if(class_exists($storageType) && new $storageType() instanceof StorageInterface) {
return new $storageType();
} else {
return new LaravelSession(app('config'), app('request')->session());
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
* Salesforce token when user refreshes the page. If you choose 'object', the token is stored on the object
* instance and will persist as long as the object remains in memory.
*/
'storage' => [
'type' => 'session', // Options include: 'session', 'cache', 'object'
'storage' => [
'type' => 'session', // Options include: 'session', 'cache', 'object', or class instance of Omniphx\Forrest\Interfaces\StorageInterface
'path' => 'forrest_', // unique storage path to avoid collisions
'expire_in' => 3600, // number of seconds to expire cache/session
'store_forever' => false, // never expire cache/session
Expand Down

0 comments on commit 5e14d42

Please sign in to comment.