Skip to content

Commit

Permalink
feature: add Laravel adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangjchandler committed Mar 6, 2024
1 parent 5f018be commit 4acd579
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ $mapIt = new MapIt(
);
```

### Laravel

If you're using Laravel, this package provides a service provider that automatically registers the `MapIt` class and uses `.env` variables to configure the key and URL.

```sh
MAPIT_KEY=...
MAPIT_URL=...
```

You can then request it from the container when you need to use it.

```php
use C6Digital\MapIt\MapIt;

class MyController
{
public function __invoke(MapIt $mapIt)
{
// ...
}
}
```

### Retrieving postcodes

```php
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
"phpstan/extension-installer": true
}
},
"extra": {
"laravel": {
"providers": [
"C6Digital\\MapIt\\Laravel\\MapItServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
8 changes: 8 additions & 0 deletions config/mapit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [

'key' => env('MAPIT_KEY'),
'url' => env('MAPIT_URL', 'https://mapit.mysociety.org'),

];
32 changes: 32 additions & 0 deletions src/Laravel/MapItServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace C6Digital\MapIt\Laravel;

use C6Digital\MapIt\MapIt;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class MapItServiceProvider extends ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../config/mapit.php',
'mapit'
);

$this->app->singleton(MapIt::class, static function (Application $app): MapIt {
return new MapIt(
key: $app['config']->get('mapit.key'),
url: $app['config']->get('mapit.url'),
);
});
}

public function boot()
{
$this->publishes([
__DIR__.'/../config/mapit.php' => config_path('mapit.php'),
]);
}
}

0 comments on commit 4acd579

Please sign in to comment.