1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\InitPhpRepository\Factory; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
5
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class TemplateVarFactory |
9
|
|
|
*/ |
10
|
|
|
class TemplateVarFactory |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @return array |
14
|
|
|
* |
15
|
|
|
* @throws \Exception |
16
|
|
|
*/ |
17
|
|
|
public function create() |
18
|
|
|
{ |
19
|
|
|
$bag = new ParameterBag(); |
20
|
|
|
|
21
|
|
|
// - Git |
22
|
|
|
$this->setGitVariables($bag); |
23
|
|
|
// - Global |
24
|
|
|
$this->setGlobalVariables($bag); |
25
|
|
|
// - Composer |
26
|
|
|
$this->setComposerVariables($bag); |
27
|
|
|
// - Autoload |
28
|
|
|
$this->setAutoloadVariables($bag); |
29
|
|
|
|
30
|
|
|
$bag->resolve(); |
31
|
|
|
|
32
|
|
|
return $bag->all(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ParameterBag $bag |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
* |
40
|
|
|
* @throws \Exception |
41
|
|
|
*/ |
42
|
|
|
protected function setGitVariables(ParameterBag $bag) |
43
|
|
|
{ |
44
|
|
|
$gitUsername = trim(shell_exec('git config --global user.name')); |
45
|
|
|
if ('' === $gitUsername) { |
46
|
|
|
throw new \Exception("Git username cannot be empty ! Use git config user.name 'NAME' to define it"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
preg_match( |
50
|
|
|
'#github\.com(?:(?:(.*)\/)|(?::(.*)\.git))$#m', |
51
|
|
|
shell_exec('git remote -v show -n origin'), |
52
|
|
|
$matches |
53
|
|
|
); |
54
|
|
|
$gitId = trim($matches[1]); |
55
|
|
|
$gitId = '' === $gitId |
56
|
|
|
? trim($matches[2]) |
57
|
|
|
: $gitId |
58
|
|
|
; |
59
|
|
|
if ('' === $gitId) { |
60
|
|
|
throw new \Exception("Unabled to define github id !"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$bag->set('git.username', $gitUsername); |
64
|
|
|
$bag->set('git.id', $gitId); |
65
|
|
|
$bag->set('git.url', 'github.com/%git.id%'); |
66
|
|
|
|
67
|
|
|
list($vendor, $id) = explode('/', $gitId); |
68
|
|
|
$bag->set('github.vendor', $vendor); |
69
|
|
|
$bag->set('github.id', $id); |
70
|
|
|
$bag->set('github.url', 'https://%git.url%'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ParameterBag $bag |
75
|
|
|
*/ |
76
|
|
|
protected function setGlobalVariables(ParameterBag $bag) |
77
|
|
|
{ |
78
|
|
|
$bag->set('id', str_replace('_', '-', ContainerBuilder::underscore($bag->get('github.id')))); |
79
|
|
|
$bag->set('name', ucwords(str_replace('_', ' ', ContainerBuilder::underscore($bag->get('github.id'))))); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param ParameterBag $bag |
84
|
|
|
*/ |
85
|
|
|
protected function setComposerVariables(ParameterBag $bag) |
86
|
|
|
{ |
87
|
|
|
$bag->set('composer.package.name', str_replace('_', '-', ContainerBuilder::underscore($bag->get('git.id')))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ParameterBag $bag |
92
|
|
|
*/ |
93
|
|
|
protected function setAutoloadVariables(ParameterBag $bag) |
94
|
|
|
{ |
95
|
|
|
$bag->set( |
96
|
|
|
'autoload.namespace', |
97
|
|
|
sprintf( |
98
|
|
|
'%s\\%s', |
99
|
|
|
ContainerBuilder::camelize($bag->get('github.vendor')), |
100
|
|
|
ContainerBuilder::camelize($bag->get('github.id')) |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|