|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digitonic\IexCloudSdk; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
|
|
|
|
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use Digitonic\IexCloudSdk\Contracts\IEXCloud; |
|
8
|
|
|
use Digitonic\IexCloudSdk\Exceptions\InvalidConfig; |
|
9
|
|
|
|
|
10
|
|
|
class IexCloudSdkServiceProvider extends ServiceProvider |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Bootstrap the application services. |
|
14
|
|
|
*/ |
|
15
|
170 |
|
public function boot() |
|
16
|
|
|
{ |
|
17
|
170 |
|
if ($this->app->runningInConsole()) { |
|
18
|
170 |
|
$this->publishes([ |
|
19
|
170 |
|
__DIR__.'/../config/config.php' => config_path('iex-cloud-sdk.php'), |
|
20
|
170 |
|
], 'config'); |
|
21
|
|
|
} |
|
22
|
170 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Register the application services. |
|
26
|
|
|
*/ |
|
27
|
170 |
|
public function register() |
|
28
|
|
|
{ |
|
29
|
170 |
|
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'iex-cloud-sdk'); |
|
30
|
|
|
|
|
31
|
|
|
$this->app->bind(IEXCloud::class, function () { |
|
32
|
59 |
|
$config = config('iex-cloud-sdk'); |
|
33
|
|
|
|
|
34
|
59 |
|
$this->guardAgainstInvalidConfig($config); |
|
35
|
|
|
|
|
36
|
56 |
|
$guzzle = new Client([ |
|
37
|
56 |
|
'base_uri' => $config['sandbox'] ? $config['sandbox_base_url'] : $config['base_url'], |
|
38
|
|
|
'headers' => [ |
|
39
|
|
|
'Accept' => 'application/json', |
|
40
|
|
|
'Content-Type' => 'application/json' |
|
41
|
|
|
], |
|
42
|
56 |
|
'query' => ['token' => $config['secret_key']] |
|
43
|
|
|
]); |
|
44
|
|
|
|
|
45
|
56 |
|
return new \Digitonic\IexCloudSdk\Client($guzzle); |
|
46
|
170 |
|
}); |
|
47
|
170 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array|null $config |
|
51
|
|
|
*/ |
|
52
|
59 |
|
protected function guardAgainstInvalidConfig(array $config = null) |
|
53
|
|
|
{ |
|
54
|
59 |
|
if (empty($config['base_url'])) { |
|
55
|
1 |
|
throw InvalidConfig::baseUrlNotSpecified(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
58 |
|
if (empty($config['secret_key'])) { |
|
59
|
1 |
|
throw InvalidConfig::apiKeyNotSpecified(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
57 |
|
if (empty($config['public_key'])) { |
|
63
|
1 |
|
throw InvalidConfig::apiKeyNotSpecified(); |
|
64
|
|
|
} |
|
65
|
56 |
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: