|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Longman\TelegramBot; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Container\Container; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
use const DIRECTORY_SEPARATOR; |
|
10
|
|
|
|
|
11
|
|
|
class Application extends Telegram |
|
12
|
|
|
{ |
|
13
|
|
|
protected $basePath; |
|
14
|
|
|
protected $appPath; |
|
15
|
|
|
protected $environmentPath; |
|
16
|
|
|
protected $environmentFile = '.env'; |
|
17
|
|
|
protected $hasBeenBootstrapped = false; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(string $basePath = null) |
|
20
|
|
|
{ |
|
21
|
|
|
if ($basePath) { |
|
|
|
|
|
|
22
|
|
|
$this->setBasePath($basePath); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$this->registerBaseBindings(); |
|
26
|
|
|
|
|
27
|
|
|
$bootstrappers = [ |
|
28
|
|
|
\Longman\TelegramBot\Bootstrap\LoadEnvironmentVariables::class, |
|
29
|
|
|
\Longman\TelegramBot\Bootstrap\SetupDatabase::class, |
|
30
|
|
|
\Longman\TelegramBot\Bootstrap\SetupMigrations::class, |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
$this->bootstrapWith($bootstrappers); |
|
34
|
|
|
|
|
35
|
|
|
parent::__construct(env('TG_API_KEY'), env('TG_BOT_NAME')); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function registerBaseBindings(): void |
|
39
|
|
|
{ |
|
40
|
|
|
static::setInstance($this); |
|
41
|
|
|
|
|
42
|
|
|
$this->instance('app', $this); |
|
43
|
|
|
|
|
44
|
|
|
$this->instance(Container::class, $this); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function bootstrapWith(array $bootstrappers) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->hasBeenBootstrapped = true; |
|
50
|
|
|
|
|
51
|
|
|
foreach ($bootstrappers as $bootstrapper) { |
|
52
|
|
|
$this->make($bootstrapper)->bootstrap($this); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function hasBeenBootstrapped(): bool |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->hasBeenBootstrapped; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function setBasePath($basePath): Application |
|
62
|
|
|
{ |
|
63
|
|
|
$this->basePath = rtrim($basePath, '\/'); |
|
64
|
|
|
|
|
65
|
|
|
$this->bindPathsInContainer(); |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function bindPathsInContainer() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->instance('path', $this->path()); |
|
73
|
|
|
$this->instance('path.base', $this->basePath()); |
|
74
|
|
|
//$this->instance('path.config', $this->configPath()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function path(string $path = ''): string |
|
78
|
|
|
{ |
|
79
|
|
|
$appPath = $this->appPath ?: $this->basePath . DIRECTORY_SEPARATOR . 'app'; |
|
80
|
|
|
|
|
81
|
|
|
return $appPath . ($path ? DIRECTORY_SEPARATOR . $path : $path); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function basePath(string $path = ''): string |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->basePath . ($path ? DIRECTORY_SEPARATOR . $path : $path); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function environmentPath(): string |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->environmentPath ?: $this->basePath; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function configPath(string $path = ''): string |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->basePath . DIRECTORY_SEPARATOR . 'config' . ($path ? DIRECTORY_SEPARATOR . $path : $path); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function bootstrapPath(string $path = ''): string |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->basePath . DIRECTORY_SEPARATOR . 'bootstrap' . ($path ? DIRECTORY_SEPARATOR . $path : $path); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getCachedConfigPath(): string |
|
105
|
|
|
{ |
|
106
|
|
|
return $_ENV['APP_CONFIG_CACHE'] ?? $this->bootstrapPath() . '/cache/config.php'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function loadEnvironmentFrom($file): Application |
|
110
|
|
|
{ |
|
111
|
|
|
$this->environmentFile = $file; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function environmentFile(): string |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->environmentFile ?: '.env'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function environmentFilePath(): string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->environmentPath() . DIRECTORY_SEPARATOR . $this->environmentFile(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function environment(...$environments) |
|
127
|
|
|
{ |
|
128
|
|
|
if (count($environments) > 0) { |
|
129
|
|
|
$patterns = is_array($environments[0]) ? $environments[0] : $environments; |
|
130
|
|
|
|
|
131
|
|
|
return Str::is($patterns, $this['env']); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this['env']; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function isLocal(): bool |
|
138
|
|
|
{ |
|
139
|
|
|
return $this['env'] === 'local'; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: