Completed
Push — feature/database-migrations ( b550b6 )
by Avtandil
02:27
created

Application::setBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $basePath of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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