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