Skip to content

Commit

Permalink
Merge pull request #73 from kslimani/laravel-6
Browse files Browse the repository at this point in the history
[Review Required] Drop cache adapter support with Laravel 5.8+
  • Loading branch information
florianv committed Oct 10, 2019
2 parents 8bd68f8 + 83bc8d7 commit 9f6f769
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ phpunit.xml
vendor
bin/
.php_cs.cache
.phpunit.*
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -15,6 +15,16 @@ Swap allows you to retrieve currency exchange rates from various services such a
$ composer require php-http/curl-client nyholm/psr7 php-http/message florianv/laravel-swap
```

### Laravel 5.7 or lesser

If you use cache, add also PSR-6 adapter and PSR-16 bridge cache dependencies :

```bash
$ composer require cache/illuminate-adapter cache/simple-cache-bridge
```

These dependencies are not required with Laravel 5.8 or greater which [implements PSR-16](https://github.com/laravel/framework/pull/27217).

### Laravel 5.5+

If you don't use auto-discovery, add the `ServiceProvider` to the providers array in `config/app.php`:
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Expand Up @@ -32,12 +32,10 @@
},
"require": {
"php": "^7.1.3",
"florianv/swap": "^4.0",
"cache/illuminate-adapter": "^0.2.0",
"cache/simple-cache-bridge": "^1.0"
"florianv/swap": "^4.0"
},
"require-dev": {
"graham-campbell/testbench": "~5.2",
"graham-campbell/testbench": "^5.3",
"php-http/guzzle6-adapter": "^1.0",
"nyholm/psr7": "^1.0"
},
Expand Down
10 changes: 10 additions & 0 deletions doc/readme.md
Expand Up @@ -56,6 +56,16 @@ Copy the package config to your local config with the publish command:
$ php artisan vendor:publish --provider="Swap\Laravel\SwapServiceProvider"
```

__Laravel 5.7 or lesser :__

If you use cache, add also PSR-6 adapter and PSR-16 bridge cache dependencies :

```bash
$ composer require cache/illuminate-adapter cache/simple-cache-bridge
```

These dependencies are not required with Laravel 5.8 or greater which [implements PSR-16](https://github.com/laravel/framework/pull/27217).

### Lumen

Configure the Service Provider and alias:
Expand Down
21 changes: 19 additions & 2 deletions src/SwapServiceProvider.php
Expand Up @@ -99,9 +99,26 @@ private function getConfigPath($path = '')
private function getSimpleCache()
{
if ($cache = $this->app->config->get('swap.cache')) {
$store = $this->app['cache']->store($cache)->getStore();
$store = $this->app['cache']->store($cache);

return new SimpleCacheBridge(new IlluminateCachePool($store));
// Check simple cache PSR-16 compatibility
if ($store instanceof \Psr\SimpleCache\CacheInterface) {
return $store;
}

// Ensure PSR-6 adapter class exists
if (! class_exists(IlluminateCachePool::class)) {
throw new \Exception("cache/illuminate-adapter dependency is missing");
}

// Ensure PSR-16 bridge class exists
if (! class_exists(SimpleCacheBridge::class)) {
throw new \Exception("cache/simple-cache-bridge dependency is missing");
}

return new SimpleCacheBridge(
new IlluminateCachePool($store->getStore())
);
}

return null;
Expand Down
16 changes: 7 additions & 9 deletions tests/SwapServiceProviderTest.php
Expand Up @@ -21,25 +21,23 @@ class SwapServiceProviderTest extends AbstractTestCase
{
use ServiceProviderTrait;

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "access_key" option must be provided.
*/
public function testMissingServiceConfig()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "access_key" option must be provided.');

$this->app->config->set('swap.services', [
'currency_layer' => true,
]);

$this->app['swap'];
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage The service "unknown" is not registered.
*/
public function testUnknownService()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The service "unknown" is not registered.');

$this->app->config->set('swap.services', [
'unknown' => true,
]);
Expand All @@ -49,7 +47,7 @@ public function testUnknownService()

public function testEmptyServices()
{
$this->app['swap'];
Assert::assertInstanceOf(Swap::class, $this->app['swap']);
}

public function testSwapIsInjectable()
Expand Down

0 comments on commit 9f6f769

Please sign in to comment.