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\Mode; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class VariableBagFactory |
10
|
|
|
*/ |
11
|
|
|
class VariableBagFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return ParameterBag |
15
|
|
|
* |
16
|
|
|
* @throws \Exception |
17
|
|
|
*/ |
18
|
|
|
public function load($mode) |
19
|
|
|
{ |
20
|
|
|
$bag = new ParameterBag(); |
21
|
|
|
|
22
|
|
|
$this->setGlobalVar($bag, $mode); |
23
|
|
|
$this->setExtraVar($bag, $mode); |
24
|
|
|
|
25
|
|
|
$bag->resolve(); |
26
|
|
|
|
27
|
|
|
return $bag; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ParameterBag $bag |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
protected function setGlobalVar(ParameterBag $bag, $mode) |
35
|
|
|
{ |
36
|
|
|
// - Git variables |
37
|
|
|
$gitUsername = trim(shell_exec('git config --global user.name')); |
38
|
|
|
if ('' === $gitUsername) { |
39
|
|
|
throw new \Exception("Git username cannot be empty ! Use git config user.name 'NAME' to define it"); |
40
|
|
|
} |
41
|
|
|
// Ensure CamelCase style for git username |
42
|
|
|
$gitUsername = ContainerBuilder::camelize($gitUsername); |
43
|
|
|
|
44
|
|
|
$remoteListOutput = shell_exec('git remote -v show -n origin'); |
45
|
|
|
|
46
|
|
|
if (0 === preg_match('#github\.com:(.*)(?:\.git)$#m', $remoteListOutput, $matches)) { |
47
|
|
|
preg_match('#github\.com\/([^\/]+\/[^\/]+)#m', $remoteListOutput, $matches); |
48
|
|
|
} |
49
|
|
|
$githubRepositoryUrlId = trim($matches[1]); |
50
|
|
|
if ('' === $githubRepositoryUrlId) { |
51
|
|
|
throw new \Exception("Unabled to define github repository url id !"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$bag->set('git.repository.url_id', $githubRepositoryUrlId); |
55
|
|
|
$tmp = explode('/', $githubRepositoryUrlId); |
56
|
|
|
$bag->set('git.repository.url_id_without_vendor', array_pop($tmp)); |
57
|
|
|
$bag->set('git.repository.url', sprintf('github.com/%s', $githubRepositoryUrlId)); |
58
|
|
|
|
59
|
|
|
// - Composer variables |
60
|
|
|
$composerPackageName = str_replace('_', '-', ContainerBuilder::underscore($githubRepositoryUrlId)); |
61
|
|
|
|
62
|
|
|
$bag->set('composer.package.name', $composerPackageName); |
63
|
|
|
$bag->set('composer.config.type', Mode::PROJECT === $mode ? 'project' : 'library'); |
64
|
|
|
|
65
|
|
|
// - Autoloading variables |
66
|
|
|
$autoloadNamespace = implode( |
67
|
|
|
'\\', |
68
|
|
|
array_map( |
69
|
|
|
function ($part) { |
70
|
|
|
return ContainerBuilder::camelize($part); |
71
|
|
|
}, |
72
|
|
|
explode('/', $githubRepositoryUrlId) |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
$autoloadPsr0Namespace = str_replace('\\', '\\\\', $autoloadNamespace); |
76
|
|
|
|
77
|
|
|
$bag->set('git.username', $gitUsername); |
78
|
|
|
$bag->set('autoload.namespace', $autoloadNamespace); |
79
|
|
|
$bag->set('autoload.namespace.psr_0', $autoloadPsr0Namespace); |
80
|
|
|
$bag->set('autoload.namespace.psr_4', sprintf('%s\\\\', $autoloadPsr0Namespace)); |
81
|
|
|
|
82
|
|
|
$id = preg_replace('#[^/]+/(.*)#', '\1', $composerPackageName); |
83
|
|
|
|
84
|
|
|
$bag->set('id', $id); |
85
|
|
|
$bag->set('name', ucwords(str_replace('-', ' ', $id))); |
86
|
|
|
|
87
|
|
|
$bag->set('current.year', date('Y')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ParameterBag $bag |
92
|
|
|
* @param string $mode |
93
|
|
|
*/ |
94
|
|
|
protected function setExtraVar(ParameterBag $bag, $mode) |
95
|
|
|
{ |
96
|
|
|
$extraList = [ |
97
|
|
|
'gitignore.extra' => '', |
98
|
|
|
'composer.config.extra.description' => '', |
99
|
|
|
'composer.config.extra.keyword' => '', |
100
|
|
|
'composer.config.extra.version' => '', |
101
|
|
|
'composer.config.extra.provide' => '', |
102
|
|
|
'composer.config.extra.suggest' => '', |
103
|
|
|
'travis.config.extra.env' => '', |
104
|
|
|
'travis.config.extra.install' => '', |
105
|
|
|
'readme.extra.badges' => '', |
106
|
|
|
'readme.extra.badges.travis' => '', |
107
|
|
|
'readme.extra.install_steps' => 'composer require %composer.package.name%', |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
if (Mode::PROJECT !== $mode) { |
111
|
|
|
// @codingStandardsIgnoreStart |
112
|
|
|
$extraList['readme.extra.badges'] = <<<EOS |
113
|
|
|
|
114
|
|
|
[](https://travis-ci.org/%git.repository.url_id%) [](https://php.net/)%readme.extra.badges.travis% |
115
|
|
|
|
116
|
|
|
EOS; |
117
|
|
|
// @codingStandardsIgnoreEnd |
118
|
|
|
// Git ignore - only project need a composer.lock |
119
|
|
|
$extraList['gitignore.extra'] = <<<EOS |
120
|
|
|
|
121
|
|
|
composer.lock |
122
|
|
|
EOS; |
123
|
|
|
// Composer |
124
|
|
|
$extraList['composer.config.extra.description'] = <<<EOS |
125
|
|
|
|
126
|
|
|
"description": "XXX", |
127
|
|
|
EOS; |
128
|
|
|
$extraList['composer.config.extra.keyword'] = <<<EOS |
129
|
|
|
|
130
|
|
|
"keywords": ["XXX"], |
131
|
|
|
EOS; |
132
|
|
|
$extraList['composer.config.extra.version'] = <<<EOS |
133
|
|
|
|
134
|
|
|
"version": "0.1.0", |
135
|
|
|
EOS; |
136
|
|
|
$extraList['composer.config.extra.provide'] = <<<EOS |
137
|
|
|
|
138
|
|
|
"provide": { |
139
|
|
|
"yoanm/XXX": "~0.1" |
140
|
|
|
}, |
141
|
|
|
EOS; |
142
|
|
|
$extraList['composer.config.extra.suggest'] = <<<EOS |
143
|
|
|
|
144
|
|
|
"suggest": { |
145
|
|
|
"YYY/ZZZ": "Description" |
146
|
|
|
}, |
147
|
|
|
EOS; |
148
|
|
|
} else { |
149
|
|
|
// Readme - install steps |
150
|
|
|
$extraList['readme.extra.install_steps'] = <<<EOS |
151
|
|
|
git clone [email protected]:%git.repository.url_id%.git |
152
|
|
|
cd %git.repository.url_id_without_vendor% |
153
|
|
|
composer build |
154
|
|
|
EOS; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (Mode::SYMFONY_LIBRARY === $mode) { |
158
|
|
|
// Travis |
159
|
|
|
$extraList['travis.config.extra.env'] = <<<EOS |
160
|
|
|
|
161
|
|
|
env: |
162
|
|
|
- SYMFONY_VERSION=2.7.* |
163
|
|
|
- SYMFONY_VERSION=2.8.* |
164
|
|
|
- SYMFONY_VERSION=3.* |
165
|
|
|
|
166
|
|
|
EOS; |
167
|
|
|
$extraList['travis.config.extra.install'] = <<<EOS |
168
|
|
|
- composer require "symfony/symfony:\${SYMFONY_VERSION}" |
169
|
|
|
|
170
|
|
|
EOS; |
171
|
|
|
// Readme - extra travis badges |
172
|
|
|
// @codingStandardsIgnoreStart |
173
|
|
|
$extraList['readme.extra.badges.travis'] = <<<EOS |
174
|
|
|
[](https://symfony.com/) |
175
|
|
|
EOS; |
176
|
|
|
// @codingStandardsIgnoreEnd |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
foreach ($extraList as $extraKey => $extraValue) { |
180
|
|
|
$bag->set($extraKey, $extraValue); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|