Skip to content

Commit 5e14d42

Browse files
authored
Add configuration for custom storage (#326)
* 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
1 parent 0a41429 commit 5e14d42

File tree

6 files changed

+114
-11
lines changed

6 files changed

+114
-11
lines changed

.github/workflows/run-tests.yml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: Tests
22

3-
on: [push, pull_request]
3+
on:
4+
pull_request:
5+
branches:
6+
- master
47

58
jobs:
69
test:
@@ -9,12 +12,24 @@ jobs:
912
fail-fast: true
1013
matrix:
1114
os: [ubuntu-latest, windows-latest]
12-
php: ['7.2', '7.3', '7.4', '8.0']
13-
laravel: [6.*, 7.*, 8.*]
15+
php: ["7.3", "7.4", "8.0", "8.1", "8.2"]
16+
laravel: [8.*, 9.*, 10.*]
1417
dependency-version: [prefer-stable]
1518
exclude:
16-
- laravel: 8.*
17-
php: 7.2
19+
- laravel: 9.*
20+
php: 7.3
21+
- laravel: 10.*
22+
php: 7.3
23+
- laravel: 9.*
24+
php: 7.4
25+
- laravel: 10.*
26+
php: 7.4
27+
- laravel: 9.*
28+
php: 8.0
29+
- laravel: 10.*
30+
php: 8.0
31+
- laravel: 10.*
32+
php: 8.1
1833

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

README.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Salesforce REST API Client for Laravel <img align="right" src="https://raw.githubusercontent.com/omniphx/images/master/Forrest.png">
22

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

599599
For more information about Guzzle responses and event listeners, refer to their [documentation](http://guzzle.readthedocs.org).
600+
601+
### Creating a custom store
602+
603+
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`:
604+
605+
```php
606+
'storage' => [
607+
'type' => App\Storage\CustomStorage::class,
608+
],
609+
```
610+
611+
You class can be named anything but it must implement `Omniphx\Forrest\Interfaces\StorageInterface`:
612+
613+
```php
614+
<?php
615+
616+
namespace App\Storage;
617+
618+
use Session;
619+
use Omniphx\Forrest\Exceptions\MissingKeyException;
620+
use Omniphx\Forrest\Interfaces\StorageInterface;
621+
622+
class CustomStorage implements StorageInterface
623+
{
624+
public $path;
625+
626+
public function __construct()
627+
{
628+
$this->path = 'app.custom.path';
629+
}
630+
631+
/**
632+
* Store into session.
633+
*
634+
* @param $key
635+
* @param $value
636+
*
637+
* @return void
638+
*/
639+
public function put($key, $value)
640+
{
641+
return Session::put($this->path.$key, $value);
642+
}
643+
644+
/**
645+
* Get from session.
646+
*
647+
* @param $key
648+
*
649+
* @return mixed
650+
*/
651+
public function get($key)
652+
{
653+
if(!$this->has($key)) {
654+
throw new MissingKeyException(sprintf('No value for requested key: %s', $key));
655+
}
656+
657+
return Session::get($this->path.$key);
658+
}
659+
660+
/**
661+
* Check if storage has a key.
662+
*
663+
* @param $key
664+
*
665+
* @return bool
666+
*/
667+
public function has($key)
668+
{
669+
return Session::has($this->path.$key);
670+
}
671+
}
672+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"guzzlehttp/guzzle": "~6.0|~7.0"
3030
},
3131
"require-dev": {
32-
"phpspec/phpspec": "~6.0"
32+
"phpspec/phpspec": "~6.0|~7.0"
3333
},
3434
"autoload": {
3535
"psr-4": {

src/Omniphx/Forrest/Client.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,16 @@ public function getInstanceURL()
701701
return $this->instanceURLRepo->get();
702702
}
703703

704+
/**
705+
* Accessor to get the token object
706+
*
707+
* @return mixed
708+
*/
709+
public function getToken()
710+
{
711+
return $this->tokenRepo->get();
712+
}
713+
704714
/**
705715
* Returns any resource that is available to the authenticated
706716
* user. Reference Force.com's REST API guide to read about more

src/Omniphx/Forrest/Providers/Laravel/ForrestServiceProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Omniphx\Forrest\Providers\Laravel\LaravelCache;
88
use Omniphx\Forrest\Providers\Laravel\LaravelSession;
99
use Omniphx\Forrest\Providers\ObjectStorage;
10+
use Omniphx\Forrest\Interfaces\StorageInterface;
1011

1112
class ForrestServiceProvider extends BaseServiceProvider
1213
{
@@ -41,7 +42,11 @@ protected function getStorage($storageType)
4142
case 'object':
4243
return new ObjectStorage();
4344
default:
44-
return new LaravelSession(app('config'), app('request')->session());
45+
if(class_exists($storageType) && new $storageType() instanceof StorageInterface) {
46+
return new $storageType();
47+
} else {
48+
return new LaravelSession(app('config'), app('request')->session());
49+
}
4550
}
4651
}
47-
}
52+
}

src/config/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
* Salesforce token when user refreshes the page. If you choose 'object', the token is stored on the object
6565
* instance and will persist as long as the object remains in memory.
6666
*/
67-
'storage' => [
68-
'type' => 'session', // Options include: 'session', 'cache', 'object'
67+
'storage' => [
68+
'type' => 'session', // Options include: 'session', 'cache', 'object', or class instance of Omniphx\Forrest\Interfaces\StorageInterface
6969
'path' => 'forrest_', // unique storage path to avoid collisions
7070
'expire_in' => 3600, // number of seconds to expire cache/session
7171
'store_forever' => false, // never expire cache/session

0 commit comments

Comments
 (0)