|
1
|
|
|
<?php namespace Packedge\Workbench\Generators; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
4
|
|
|
use Packedge\Workbench\Package; |
|
5
|
|
|
use Packedge\Workbench\Exceptions\DirectoryExistsException; |
|
6
|
|
|
use Packedge\Workbench\Parsers\PackageParser; |
|
7
|
|
|
|
|
8
|
|
|
class PackageGenerator |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $packagePath; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var PackageParser |
|
16
|
|
|
*/ |
|
17
|
|
|
private $packageParser; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Filesystem |
|
20
|
|
|
*/ |
|
21
|
|
|
private $filesystem; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var GeneratorInterface[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $generators = []; |
|
26
|
|
|
|
|
27
|
|
|
protected $directories = [ |
|
28
|
|
|
'src' |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
6 |
|
public function __construct(PackageParser $packageParser = null, Filesystem $filesystem = null) |
|
32
|
|
|
{ |
|
33
|
6 |
|
$this->packageParser = $packageParser ?: new PackageParser; |
|
34
|
6 |
|
$this->filesystem = $filesystem ?: new Filesystem; |
|
35
|
6 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function create(Package $package) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->packagePath = getcwd() . '/' . $this->packageParser->parse($package->getPackageName())->toDirectoryName(); |
|
40
|
|
|
if($this->filesystem->exists($this->packagePath)) throw new DirectoryExistsException($this->packagePath); |
|
41
|
|
|
|
|
42
|
|
|
$this->setup(); |
|
43
|
|
|
foreach($this->generators as $generator) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->buildDirectories($generator->getDirectories()); |
|
46
|
|
|
$generator->create($this->packagePath); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function setup() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->buildBaseDirectories(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function buildBaseDirectories() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->filesystem->makeDirectory($this->packagePath); |
|
58
|
|
|
$this->buildDirectories($this->directories); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function buildDirectories(array $dirs) |
|
62
|
|
|
{ |
|
63
|
|
|
foreach($dirs as $dir) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->filesystem->makeDirectory($this->packagePath . '/' . $dir); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param GeneratorInterface $generators |
|
71
|
|
|
*/ |
|
72
|
|
|
public function addGenerator(GeneratorInterface $generators) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->generators[] = $generators; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function getDirectories() |
|
81
|
|
|
{ |
|
82
|
3 |
|
return $this->directories; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|