1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Plugins; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Contains; |
6
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Logger; |
7
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Plugin as Contract; |
8
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config; |
9
|
|
|
use Helldar\LaravelLangPublisher\Facades\Path; |
10
|
|
|
use Helldar\Support\Concerns\Makeable; |
11
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\Directory; |
12
|
|
|
|
13
|
|
|
abstract class Plugin implements Contract |
14
|
|
|
{ |
15
|
|
|
use Contains; |
16
|
|
|
use Logger; |
|
|
|
|
17
|
|
|
use Makeable; |
18
|
|
|
|
19
|
|
|
public function target(): ?string |
20
|
|
|
{ |
21
|
|
|
return null; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function has(): bool |
25
|
|
|
{ |
26
|
|
|
$this->log('Check if the package is installed:', $this->vendor()); |
27
|
|
|
|
28
|
|
|
$source = $this->basePath() . '/' . $this->vendor(); |
29
|
|
|
|
30
|
|
|
return Directory::exists($source); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function targetPath(string $locale, string $filename): string |
34
|
|
|
{ |
35
|
|
|
$this->log('Getting a link to the target file for:', $filename, '(', $locale, ')'); |
36
|
|
|
|
37
|
|
|
$path = $this->resolveTargetPath($locale, $filename); |
38
|
|
|
|
39
|
|
|
$target = $this->targetFilename($locale, $filename); |
40
|
|
|
|
41
|
|
|
return $this->cleanPath($path . '/' . $target); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function targetFilename(string $locale, string $filename): string |
45
|
|
|
{ |
46
|
|
|
$this->log('Retrieving the changed filename:', $filename, '(', $locale, ')'); |
47
|
|
|
|
48
|
|
|
if ($this->isJson($filename)) { |
49
|
|
|
return $locale . '.json'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->fileBasename($filename); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function basePath(): string |
56
|
|
|
{ |
57
|
|
|
return Config::basePath(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function fileBasename(string $filename): string |
61
|
|
|
{ |
62
|
|
|
return Path::basename($filename); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function cleanPath(?string $path): ?string |
66
|
|
|
{ |
67
|
|
|
return Path::clean($path, true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function resolveTargetPath(string $locale, string $filename): ?string |
71
|
|
|
{ |
72
|
|
|
$this->log('Resolving the path to the target file:', $filename, '(', $locale, ')'); |
73
|
|
|
|
74
|
|
|
$path = $this->cleanPath($this->target()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if ($this->isPhp($filename)) { |
77
|
|
|
$path .= '/' . $locale; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->cleanPath($path); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|