1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\DefaultPhpRepository\Factory; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Finder\Finder; |
5
|
|
|
use Yoanm\DefaultPhpRepository\Command\RepositoryType; |
6
|
|
|
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper; |
7
|
|
|
use Yoanm\DefaultPhpRepository\Model\Template; |
8
|
|
|
use Yoanm\DefaultPhpRepository\Model\FolderTemplate; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class TemplateListFactory |
12
|
|
|
*/ |
13
|
|
|
class TemplateListFactory |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param string $repositoryType |
17
|
|
|
* |
18
|
|
|
* @return Template[] |
19
|
|
|
*/ |
20
|
|
|
public function create($repositoryType) |
21
|
|
|
{ |
22
|
|
|
$fileTemplateList = [ |
23
|
|
|
'git.readme' => 'README.md.twig', |
24
|
|
|
'git.license' => 'LICENSE.twig', |
25
|
|
|
'git.contributing' => 'CONTRIBUTING.md.twig', |
26
|
|
|
'git.gitignore' => '.gitignore.twig', |
27
|
|
|
'composer.config' => 'composer.json.twig', |
28
|
|
|
'phpcs.config' => 'phpcs.xml.dist.twig', |
29
|
|
|
'phpunit.config' => 'phpunit.xml.dist.twig', |
30
|
|
|
'behat.config' => 'behat.yml.twig', |
31
|
|
|
'ci.scrutinizer' => '.scrutinizer.yml.twig', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
if (RepositoryType::LIBRARY === $repositoryType) { |
35
|
|
|
$fileTemplateList['ci.travis'] = '.travis.yml.twig'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$list = []; |
39
|
|
|
foreach ($fileTemplateList as $templateId => $templateName) { |
40
|
|
|
$list[$templateId] = $this->createTemplate($templateId, $templateName); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$folderTemplateList = [ |
44
|
|
|
'phpunit.folders' => 'tests', |
45
|
|
|
'behat.folders' => 'features', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$basePath = TemplateHelper::getTemplateBasePath(); |
49
|
|
|
foreach ($folderTemplateList as $templateId => $templateFolder) { |
50
|
|
|
$folderTemplate = $list[$templateId] = new FolderTemplate($templateId, $templateFolder); |
51
|
|
|
// Iterate over files |
52
|
|
|
$count = 0; |
53
|
|
|
$path = realpath(sprintf('%s/base/%s', $basePath, $templateFolder)); |
54
|
|
|
$toRemove = sprintf('%s/base/', $basePath); |
55
|
|
|
foreach ((new Finder())->files()->in($path) as $file) { |
56
|
|
|
$folderTemplate->addFile( |
57
|
|
|
$this->createTemplate( |
58
|
|
|
sprintf('%s.%s', $templateId, $count), |
59
|
|
|
str_replace($toRemove, '', $file->getPathname()) |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
$count++; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Reorder final list |
67
|
|
|
$orderedList = [ |
68
|
|
|
'git.readme' => $list['git.readme'], |
69
|
|
|
'git.license' => $list['git.license'], |
70
|
|
|
'git.contributing' => $list['git.contributing'], |
71
|
|
|
'git.gitignore' => $list['git.gitignore'], |
72
|
|
|
'composer.config' => $list['composer.config'], |
73
|
|
|
'phpcs.config' => $list['phpcs.config'], |
74
|
|
|
'phpunit.config' => $list['phpunit.config'], |
75
|
|
|
'phpunit.folders' => $list['phpunit.folders'], |
76
|
|
|
'behat.config' => $list['behat.config'], |
77
|
|
|
'behat.folders' => $list['behat.folders'], |
78
|
|
|
'ci.scrutinizer' => $list['ci.scrutinizer'], |
79
|
|
|
]; |
80
|
|
|
if (RepositoryType::LIBRARY === $repositoryType) { |
81
|
|
|
$orderedList['ci.travis'] = $list['ci.travis']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $orderedList; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $id |
89
|
|
|
* @param string $templateName |
90
|
|
|
* |
91
|
|
|
* @return Template |
92
|
|
|
*/ |
93
|
|
|
protected function createTemplate($id, $templateName) |
94
|
|
|
{ |
95
|
|
|
return new Template($id, $templateName, $this->getOutputFilePath($templateName)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $templateName |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
protected function getOutputFilePath($templateName) |
105
|
|
|
{ |
106
|
|
|
return str_replace('.twig', '', $templateName); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|