diff --git a/README.md b/README.md index 75c4cae..649dd3b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -> 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) @@ -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 ], @@ -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 diff --git a/composer.json b/composer.json index a08682c..4a48919 100644 --- a/composer.json +++ b/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", diff --git a/doc/readme.md b/doc/readme.md index c668b19..b24690a 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -3,6 +3,8 @@ ## Index * [Installation](#installation) * [Setup](#setup) + * [Laravel](#laravel) + * [Lumen](#lumen) * [Configuration](#configuration) * [Usage](#usage) * [Cache](#cache) @@ -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 ], @@ -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. diff --git a/src/SwapServiceProvider.php b/src/SwapServiceProvider.php index 45d52ca..0ea292f 100644 --- a/src/SwapServiceProvider.php +++ b/src/SwapServiceProvider.php @@ -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; @@ -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'); } @@ -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')) { @@ -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) { @@ -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); @@ -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')) { @@ -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', [])); @@ -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']); @@ -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); + } }