|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ptondereau\PackMe\Crafters; |
|
4
|
|
|
|
|
5
|
|
|
use ConstantNull\Backstubber\FileGenerator; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Ptondereau\PackMe\Package; |
|
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class PHPCrafter. |
|
12
|
|
|
*/ |
|
13
|
|
|
class PHPCrafter implements CrafterInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Stub file generator. |
|
17
|
|
|
* |
|
18
|
|
|
* @var FileGenerator |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $stubber; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Filesystem |
|
24
|
|
|
*/ |
|
25
|
|
|
private $filesystem; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* PHPCrafter constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param FileGenerator $stubber |
|
31
|
|
|
* @param Filesystem $filesystem |
|
32
|
|
|
*/ |
|
33
|
6 |
|
public function __construct(FileGenerator $stubber, Filesystem $filesystem) |
|
34
|
|
|
{ |
|
35
|
6 |
|
$this->stubber = $stubber; |
|
36
|
6 |
|
$this->filesystem = $filesystem; |
|
37
|
6 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Craft the application with parameters. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Package $package |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function craft(Package $package) |
|
47
|
|
|
{ |
|
48
|
3 |
|
$stubPath = realpath(__DIR__.'/../stubs'); |
|
49
|
|
|
|
|
50
|
|
|
// set delimiters |
|
51
|
3 |
|
$this->stubber->withDelimiters('{{', '}}'); |
|
52
|
|
|
|
|
53
|
|
|
// set keywords |
|
54
|
3 |
|
$this->stubber->setRaw($package->toArray()); |
|
55
|
|
|
|
|
56
|
3 |
|
$this->filesystem->mirror($stubPath, $package->getDestination()); |
|
57
|
|
|
|
|
58
|
|
|
// array of all stub files |
|
59
|
3 |
|
$stubFiles = new \RecursiveIteratorIterator( |
|
60
|
3 |
|
new \RecursiveDirectoryIterator( |
|
61
|
3 |
|
$package->getDestination(), |
|
62
|
2 |
|
\RecursiveDirectoryIterator::SKIP_DOTS |
|
63
|
1 |
|
) |
|
64
|
1 |
|
); |
|
65
|
|
|
|
|
66
|
|
|
// find and replace |
|
67
|
3 |
|
foreach ($stubFiles as $stub) { |
|
68
|
3 |
|
$new = pathinfo($stub); |
|
69
|
3 |
|
$this->stubber->useStub($stub); |
|
70
|
3 |
|
if ($this->isConfigFile($new['basename'])) { |
|
71
|
3 |
|
$this->stubber->generate($new['dirname'].'/'.Str::slug($package->getPackage()).'.php'); |
|
72
|
3 |
|
} elseif ($this->isServiceProviderFile($new['basename'])) { |
|
73
|
3 |
|
$this->stubber->generate($new['dirname'].'/'.$package->getPackage().'ServiceProvider.php'); |
|
74
|
1 |
|
} else { |
|
75
|
3 |
|
$this->stubber->generate($new['dirname'].'/'.$new['filename']); |
|
76
|
|
|
} |
|
77
|
3 |
|
$this->filesystem->remove($stub); |
|
78
|
1 |
|
} |
|
79
|
3 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Detect if a file is the config file. |
|
83
|
|
|
* |
|
84
|
|
|
* @param $file |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
3 |
|
private function isConfigFile($file) |
|
89
|
|
|
{ |
|
90
|
3 |
|
return $file === 'package.php.stub'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Detect if a file is the service provider file. |
|
95
|
|
|
* |
|
96
|
|
|
* @param $file |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
3 |
|
private function isServiceProviderFile($file) |
|
101
|
|
|
{ |
|
102
|
3 |
|
return $file === 'ServiceProvider.php.stub'; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|