Skip to content

Commit

Permalink
Merge pull request #26 from florianv/lumen
Browse files Browse the repository at this point in the history
Add support for Lumen
  • Loading branch information
florianv committed Dec 22, 2016
2 parents 3b0d7ea + f94ba1f commit e1e6a2a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 22 deletions.
35 changes: 29 additions & 6 deletions README.md
@@ -1,5 +1,5 @@
<img src="https://github.com/florianv/swap/blob/master/doc/logo.png" width="200px" align="left"/>
> Currency exchange rates library for Laravel
> Currency exchange rates library for Laravel and Lumen
[![Build status](http://img.shields.io/travis/florianv/laravel-swap.svg?style=flat-square)](https://travis-ci.org/florianv/laravel-swap)
[![Total Downloads](https://img.shields.io/packagist/dt/florianv/laravel-swap.svg?style=flat-square)](https://packagist.org/packages/florianv/laravel-swap)
Expand All @@ -12,16 +12,18 @@

## QuickStart

1) Install via Composer:
### Installation

```bash
$ composer require florianv/laravel-swap php-http/message php-http/guzzle6-adapter
```

2) Configure the Service Provider and alias:
### Laravel

Configure the Service Provider and alias:

```php
// config/app.php
// /config/app.php
'providers' => [
Swap\Laravel\SwapServiceProvider::class
],
Expand All @@ -31,13 +33,34 @@ $ composer require florianv/laravel-swap php-http/message php-http/guzzle6-adapt
]
```

3) Publish the Package configuration
Publish the Package configuration

```bash
$ php artisan vendor:publish --provider="Swap\Laravel\SwapServiceProvider"
```

4) Start using it!
### Lumen

Configure the Service Provider and alias:

```php
// /boostrap/app.php

// Register the facade
$app->withFacades(true, [
Swap\Laravel\Facades\Swap::class => 'Swap'
]);

// Load the configuration
$app->configure('swap');

// Register the service provider
$app->register(Swap\Laravel\SwapServiceProvider::class);
```

Copy the [configuration](config/swap.php) to `/config/swap.php` if you wish to override it.

## Usage

```php
// Get the latest EUR/USD rate
Expand Down
3 changes: 2 additions & 1 deletion composer.json
@@ -1,9 +1,10 @@
{
"name": "florianv/laravel-swap",
"type": "library",
"description": "Currency exchange rates library for Laravel",
"description": "Currency exchange rates library for Laravel and Lumen",
"keywords": [
"laravel",
"lumen",
"currency",
"money",
"rate",
Expand Down
27 changes: 26 additions & 1 deletion doc/readme.md
Expand Up @@ -3,6 +3,8 @@
## Index
* [Installation](#installation)
* [Setup](#setup)
* [Laravel](#laravel)
* [Lumen](#lumen)
* [Configuration](#configuration)
* [Usage](#usage)
* [Cache](#cache)
Expand All @@ -25,10 +27,12 @@ composer require florianv/laravel-swap php-http/message php-http/guzzle6-adapter

## Setup

### Laravel

Configure the Service Provider and alias:

```php
// config/app.php
// /config/app.php
'providers' => [
Swap\Laravel\SwapServiceProvider::class
],
Expand All @@ -44,6 +48,27 @@ Publish the Package configuration
$ php artisan vendor:publish --provider="Swap\Laravel\SwapServiceProvider"
```

### Lumen

Configure the Service Provider and alias:

```php
// /boostrap/app.php

// Register the facade
$app->withFacades(true, [
Swap\Laravel\Facades\Swap::class => 'Swap'
]);

// Load the configuration
$app->configure('swap');

// Register the service provider
$app->register(Swap\Laravel\SwapServiceProvider::class);
```

Copy the [configuration](config/swap.php) to `/config/swap.php` if you wish to override it.

## Configuration

By default Swap uses the [Fixer.io](http://fixer.io) service to fetch rates.
Expand Down
40 changes: 26 additions & 14 deletions src/SwapServiceProvider.php
Expand Up @@ -14,7 +14,7 @@
use Exchanger\Exchanger;
use Exchanger\Service\Chain;
use Exchanger\Service\PhpArray;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use Http\Discovery\HttpClientDiscovery;
Expand All @@ -34,7 +34,7 @@ final class SwapServiceProvider extends ServiceProvider
public function boot()
{
$source = realpath(__DIR__.'/../config/swap.php');
$this->publishes([$source => config_path('swap.php')]);
$this->publishes([$source => $this->getConfigPath('swap.php')]);
$this->mergeConfigFrom($source, 'swap');
}

Expand All @@ -53,9 +53,9 @@ public function register()
/**
* Register the http related stuff.
*
* @param Application $app
* @param Container $app
*/
private function registerHttp(Application $app)
private function registerHttp(Container $app)
{
$app->singleton('swap.http_client', function ($app) {
if ($httpClient = $app->config->get('swap.http_client')) {
Expand All @@ -77,9 +77,9 @@ private function registerHttp(Application $app)
/**
* Register the core services.
*
* @param Application $app
* @param Container $app
*/
private function registerServices(Application $app)
private function registerServices(Container $app)
{
foreach ($app->config->get('swap.services', []) as $name => $config) {
if (false === $config) {
Expand Down Expand Up @@ -116,9 +116,9 @@ private function registerServices(Application $app)
/**
* Register the chain service.
*
* @param Application $app
* @param Container $app
*/
private function registerChain(Application $app)
private function registerChain(Container $app)
{
$app->singleton('swap.chain', function ($app) {
$this->registerServices($app);
Expand All @@ -130,9 +130,9 @@ private function registerChain(Application $app)
/**
* Registers the cache.
*
* @param Application $app
* @param Container $app
*/
private function registerCacheItemPool(Application $app)
private function registerCacheItemPool(Container $app)
{
$app->singleton('swap.cache_item_pool', function ($app) {
if ($cacheItemPool = $app->config->get('swap.cache_item_pool')) {
Expand All @@ -152,9 +152,9 @@ private function registerCacheItemPool(Application $app)
/**
* Register the exchange rate provider.
*
* @param Application $app
* @param Container $app
*/
private function registerExchangeRateProvider(Application $app)
private function registerExchangeRateProvider(Container $app)
{
$app->singleton('swap.exchange_rate_provider', function ($app) {
return new Exchanger($app['swap.chain'], $app['swap.cache_item_pool'], $app->config->get('swap.options', []));
Expand All @@ -164,9 +164,9 @@ private function registerExchangeRateProvider(Application $app)
/**
* Registers Swap.
*
* @param Application $app
* @param Container $app
*/
private function registerSwap(Application $app)
private function registerSwap(Container $app)
{
$app->singleton('swap', function ($app) {
return new Swap($app['swap.exchange_rate_provider']);
Expand Down Expand Up @@ -200,4 +200,16 @@ private function getServiceClass($name)

return 'Exchanger\\Service\\'.$camelized;
}

/**
* Gets the full path to the config.
*
* @param string $path
*
* @return string
*/
private function getConfigPath($path = '')
{
return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
}
}

0 comments on commit e1e6a2a

Please sign in to comment.