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