1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Application; |
6
|
|
|
|
7
|
|
|
use Dotenv\Dotenv; |
8
|
|
|
use Dotenv\Exception\InvalidPathException; |
9
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
10
|
|
|
use League\Container\ServiceProvider\BootableServiceProviderInterface; |
11
|
|
|
|
12
|
|
|
abstract class AppServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface |
13
|
|
|
{ |
14
|
|
|
protected $provides = [ |
15
|
|
|
Config::class, |
16
|
|
|
'environment', |
17
|
|
|
'base_path', |
18
|
|
|
'resources_path', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Determine environment where application is currently is running on. |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
abstract public function environment(): string; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Base path of the application. |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
abstract public function basePath(): string; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Path to the "resources folder". |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
abstract public function resourcesPath(): string; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Method will be invoked on registration of a service provider implementing |
44
|
|
|
* this interface. Provides ability for eager loading of Service Providers. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
2 |
|
public function boot(): void |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
2 |
|
(new Dotenv($this->basePath()))->load(); |
52
|
1 |
|
} catch (InvalidPathException $exception) { |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$this->container->share(Config::class, function () { |
56
|
2 |
|
return new Config($_ENV); |
57
|
2 |
|
}); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Use the register method to register items with the container via the |
62
|
|
|
* protected $this->container property or the `getContainer` method |
63
|
|
|
* from the ContainerAwareTrait. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
2 |
|
public function register(): void |
68
|
|
|
{ |
69
|
2 |
|
$this->container->share('environment', $this->environment()); |
70
|
2 |
|
$this->container->share('base_path', $this->basePath()); |
71
|
2 |
|
$this->container->share('resources_path', $this->resourcesPath()); |
72
|
2 |
|
} |
73
|
|
|
} |
74
|
|
|
|