1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliqArts\Scavenger; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Goutte\Client as GoutteClient; |
9
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
10
|
|
|
use Illuminate\Foundation\AliasLoader; |
11
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
12
|
|
|
use Monolog\Handler\StreamHandler; |
13
|
|
|
use Monolog\Logger; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use ReliqArts\Scavenger\Console\Command\Seek; |
16
|
|
|
use ReliqArts\Scavenger\Contract\ConfigProvider as ConfigProviderContract; |
17
|
|
|
use ReliqArts\Scavenger\Contract\Seeker as SeekerContract; |
18
|
|
|
use ReliqArts\Scavenger\Facade\Scavenger; |
19
|
|
|
use ReliqArts\Scavenger\Helper\NodeProximityAssistant; |
20
|
|
|
use ReliqArts\Scavenger\Service\ConfigProvider; |
21
|
|
|
use ReliqArts\Scavenger\Service\Seeker; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Service Provider. |
25
|
|
|
* |
26
|
|
|
* @codeCoverageIgnore |
27
|
|
|
*/ |
28
|
|
|
class ScavengerServiceProvider extends BaseServiceProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected const ASSET_DIRECTORY = __DIR__ . '/..'; |
34
|
|
|
private const LOG_FILE_PREFIX = 'scavenger-'; |
35
|
|
|
private const LOGGER_NAME = 'Scavenger.Seeker'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* List of commands. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected array $commands = [ |
43
|
|
|
Seek::class, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Perform post-registration booting of services. |
48
|
|
|
*/ |
49
|
|
|
public function boot(): void |
50
|
|
|
{ |
51
|
|
|
$this->handleConfig(); |
52
|
|
|
$this->handleMigrations(); |
53
|
|
|
$this->handleAssets(); |
54
|
|
|
$this->handleCommands(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register bindings in the container. |
59
|
|
|
*/ |
60
|
|
|
public function register(): void |
61
|
|
|
{ |
62
|
|
|
$loader = AliasLoader::getInstance(); |
63
|
|
|
|
64
|
|
|
$this->app->singleton(ConfigProviderContract::class, ConfigProvider::class); |
65
|
|
|
$this->app->bind( |
66
|
|
|
SeekerContract::class, |
67
|
|
|
function (): SeekerContract { |
68
|
|
|
$configProvider = resolve(ConfigProviderContract::class); |
69
|
|
|
|
70
|
|
|
return new Seeker( |
71
|
|
|
$this->getLogger($configProvider), |
72
|
|
|
$this->getGoutteClient($configProvider), |
73
|
|
|
$configProvider, |
74
|
|
|
new NodeProximityAssistant() |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$loader->alias('Scavenger', Scavenger::class); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the services provided by the provider. |
84
|
|
|
*/ |
85
|
|
|
public function provides(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
SeekerContract::class, |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Publish assets. |
94
|
|
|
*/ |
95
|
|
|
protected function handleAssets(): void |
96
|
|
|
{ |
97
|
|
|
// ... |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Register Configuration. |
102
|
|
|
*/ |
103
|
|
|
protected function handleConfig(): void |
104
|
|
|
{ |
105
|
|
|
// merge config |
106
|
|
|
$this->mergeConfigFrom(static::ASSET_DIRECTORY . '/config/config.php', 'scavenger'); |
107
|
|
|
|
108
|
|
|
// allow publishing config |
109
|
|
|
$this->publishes([ |
110
|
|
|
static::ASSET_DIRECTORY . '/config/config.php' => config_path('scavenger.php'), |
111
|
|
|
], 'scavenger-config'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Command files. |
116
|
|
|
*/ |
117
|
|
|
private function handleCommands(): void |
118
|
|
|
{ |
119
|
|
|
// Register the commands... |
120
|
|
|
if ($this->app->runningInConsole()) { |
121
|
|
|
$this->commands($this->commands); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Migration files. |
127
|
|
|
*/ |
128
|
|
|
private function handleMigrations(): void |
129
|
|
|
{ |
130
|
|
|
// Load the migrations... |
131
|
|
|
$this->loadMigrationsFrom(static::ASSET_DIRECTORY . '/database/migrations'); |
132
|
|
|
|
133
|
|
|
// allow publishing of migrations |
134
|
|
|
$this->publishes([ |
135
|
|
|
static::ASSET_DIRECTORY . '/database/migrations/' => database_path('migrations'), |
136
|
|
|
], 'scavenger-migrations'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @throws Exception |
141
|
|
|
*/ |
142
|
|
|
private function getLogger(ConfigProvider $configProvider): LoggerInterface |
143
|
|
|
{ |
144
|
|
|
$logFilename = self::LOG_FILE_PREFIX . microtime(true); |
145
|
|
|
$logger = new Logger(self::LOGGER_NAME); |
146
|
|
|
|
147
|
|
|
$logger->pushHandler(new StreamHandler( |
148
|
|
|
storage_path('logs/' . $configProvider->getLogDir() . "/{$logFilename}.log"), |
149
|
|
|
$configProvider->isLoggingEnabled() ? Logger::DEBUG : Logger::CRITICAL |
150
|
|
|
)); |
151
|
|
|
|
152
|
|
|
return $logger; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param GoutteClient $goutteClient |
157
|
|
|
*/ |
158
|
|
|
private function getGoutteClient(ConfigProvider $configProvider): GoutteClient |
159
|
|
|
{ |
160
|
|
|
$goutteClient = new GoutteClient(); |
161
|
|
|
|
162
|
|
|
return $goutteClient->setClient( |
163
|
|
|
new GuzzleClient($configProvider->getGuzzleSettings()) |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|