Skip to content

Commit

Permalink
Merge pull request #12 from morcen/get-service-via-passage-facade
Browse files Browse the repository at this point in the history
Get service via Passage Facade
  • Loading branch information
morcen committed Aug 27, 2023
2 parents cb66475 + 75a5f56 commit 1934fa3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ return [
]
```

#### Using the `Passage` facade
If you wish not to use automatic routing of Passage and instead use the Passage services manuall in your controllers, you can use the `Passage` facade.
```php
// config/passage.php
return [
'services' => [
'github' => 'https://api.github.com/',
]
]
```

and in your controller:
```php
// app/Http/Controllers/UserController.php

use Morcen\Passage\Facades\Passage

class UserController extends Controller
{
public function index()
{
$response = Passage::getService('github')->get('users/morcen');
return $response->json();
}
}
```

### Disabling `Passage`
To disable `Passage` on a server/application level, set `PASSAGE_ENABLED` to `false` in your `.env` file:
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/MissingPassageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class MissingPassageService extends Exception
{
//
}
2 changes: 2 additions & 0 deletions src/Facades/Passage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

/**
* @see \Morcen\Passage\Passage
*
* @method static mixed getService(string $service)
*/
class Passage extends Facade
{
Expand Down
15 changes: 15 additions & 0 deletions src/Passage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

namespace Morcen\Passage;

use App\Exceptions\MissingPassageService;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class Passage
{
/**
* @throws MissingPassageService
*/
public function getService(string $service): PendingRequest
{
if (Http::hasMacro($service)) {
return Http::$service();
}

throw new MissingPassageService("The service \"{$service}\" is not available in your passage services.");
}
}

0 comments on commit 1934fa3

Please sign in to comment.