|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GarbuzIvan\LaravelGeneratorPackage\Builder; |
|
6
|
|
|
|
|
7
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Builder\Components\Migration; |
|
8
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Builder\Components\Model; |
|
9
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Configuration; |
|
10
|
|
|
use Illuminate\Support\Facades\File; |
|
11
|
|
|
|
|
12
|
|
|
class FileGenerator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Configuration |
|
16
|
|
|
*/ |
|
17
|
|
|
private Configuration $config; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Package |
|
21
|
|
|
*/ |
|
22
|
|
|
private Package $package; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* FileGenerator constructor. |
|
26
|
|
|
* @param Configuration $config |
|
27
|
|
|
*/ |
|
28
|
2 |
|
public function __construct(Configuration $config) |
|
29
|
|
|
{ |
|
30
|
2 |
|
$this->config = $config; |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Package $package |
|
35
|
|
|
* @return bool |
|
36
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function make(Package $package): bool |
|
39
|
|
|
{ |
|
40
|
2 |
|
$this->package = $package; |
|
41
|
2 |
|
$this->templates(); |
|
42
|
2 |
|
app(Migration::class)->make($package); |
|
43
|
2 |
|
app(Model::class)->make($package); |
|
44
|
2 |
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Copy all templates file |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function templates(): bool |
|
53
|
|
|
{ |
|
54
|
2 |
|
$templatesDir = realpath(__DIR__ . '/../../templates'); |
|
55
|
2 |
|
$files = File::allFiles($templatesDir, true); |
|
56
|
2 |
|
foreach ($files as $file) { |
|
57
|
2 |
|
$pathDir = str_replace($templatesDir, '', $file->getPath()); |
|
58
|
2 |
|
$pathDir = $this->package->getPath($pathDir); |
|
59
|
2 |
|
if (!file_exists($pathDir)) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
2 |
|
$content = $this->replacement(file_get_contents($file->getRealPath())); |
|
63
|
2 |
|
$newPathFile = str_replace([$templatesDir, '.stub'], '', $file->getRealPath()); |
|
64
|
2 |
|
$newPathFile = $this->package->getPath($newPathFile); |
|
65
|
2 |
|
file_put_contents($newPathFile, $content); |
|
66
|
|
|
} |
|
67
|
2 |
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string|null $content |
|
72
|
|
|
* @return string|null |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function replacement(?string $content = null): ?string |
|
75
|
|
|
{ |
|
76
|
2 |
|
$content = str_replace([ |
|
77
|
2 |
|
'%PACKAGE_GIT%', |
|
78
|
|
|
'%PACKAGE_PVENDOR%', |
|
79
|
|
|
'%PACKAGE_PNAME%', |
|
80
|
|
|
'%PACKAGE_NAMESPACE%', |
|
81
|
|
|
'%PACKAGE_NAMESPACE_SLASHES%', |
|
82
|
|
|
'%PACKAGE_CONFIG_NAME%', |
|
83
|
|
|
'%PACKAGE_NAME%', |
|
84
|
|
|
'%PACKAGE_DESCRIPTIONS%', |
|
85
|
|
|
], [ |
|
86
|
2 |
|
$this->package->getPackageVendor() . '/' . $this->package->getPackageName(), |
|
87
|
2 |
|
$this->package->getPackageVendor(), |
|
88
|
2 |
|
$this->package->getPackageName(), |
|
89
|
2 |
|
$this->package->getNamespace(), |
|
90
|
2 |
|
$this->package->getNamespaceSlashes(), |
|
91
|
2 |
|
$this->package->getPackageVendor() . '-' . $this->package->getPackageName(), |
|
92
|
2 |
|
$this->package->getName(), |
|
93
|
2 |
|
$this->package->getDescripton(), |
|
94
|
|
|
], $content); |
|
95
|
2 |
|
return $content; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|