This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Astrotomic/dev-v2
v2.0.0
- Loading branch information
Showing
13 changed files
with
365 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/vendor/ | ||
/composer.lock | ||
/phpunit.xml | ||
/build/ | ||
/coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Astrotomic\LaravelGuzzle; | ||
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use Illuminate\Support\Manager; | ||
use InvalidArgumentException; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
class Factory extends Manager | ||
{ | ||
/** | ||
* The registered custom client configs. | ||
* | ||
* @var array[] | ||
*/ | ||
protected $registeredConfigs = []; | ||
|
||
/** | ||
* @param string|UriInterface|null $baseUri | ||
* @param array $config | ||
* | ||
* @return GuzzleClient | ||
*/ | ||
public function make($baseUri = null, array $config = []): GuzzleClient | ||
{ | ||
if ($baseUri !== null) { | ||
$config['base_uri'] = $baseUri; | ||
} | ||
|
||
return $this->createClient($config); | ||
} | ||
|
||
public function client(?string $identifier = null): GuzzleClient | ||
{ | ||
return $this->driver($identifier); | ||
} | ||
|
||
public function driver($driver = null): GuzzleClient | ||
{ | ||
return parent::driver($driver); | ||
} | ||
|
||
public function getDefaultDriver(): string | ||
{ | ||
return $this->config->get('guzzle.default_client'); | ||
} | ||
|
||
public function register(string $identifier, array $config): self | ||
{ | ||
$this->registeredConfigs[$identifier] = $config; | ||
|
||
return $this; | ||
} | ||
|
||
protected function createDriver($driver): GuzzleClient | ||
{ | ||
if (isset($this->customCreators[$driver])) { | ||
return $this->callCustomCreator($driver); | ||
} elseif (isset($this->registeredConfigs[$driver])) { | ||
return $this->createClient(array_merge( | ||
$this->registeredConfigs[$driver], | ||
$this->config->get('guzzle.clients.'.$driver, []) | ||
)); | ||
} elseif ($this->config->has('guzzle.clients.'.$driver)) { | ||
return $this->createClient( | ||
$this->config->get('guzzle.clients.'.$driver) | ||
); | ||
} | ||
|
||
throw new InvalidArgumentException("Client [$driver] not supported."); | ||
} | ||
|
||
protected function callCustomCreator($driver): GuzzleClient | ||
{ | ||
return $this->customCreators[$driver]( | ||
$this->container, | ||
$this->prepareConfig($this->config->get('guzzle.clients.'.$driver, [])) | ||
); | ||
} | ||
|
||
protected function prepareConfig(array $config): array | ||
{ | ||
return array_merge( | ||
$this->config->get('guzzle.default_config', []), | ||
$config | ||
); | ||
} | ||
|
||
protected function createClient(array $config): GuzzleClient | ||
{ | ||
return new GuzzleClient($this->prepareConfig($config)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.