1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CL\ComposerInit\Prompt; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
use Symfony\Component\Console\Helper\DialogHelper; |
7
|
|
|
use CL\ComposerInit\GitConfig; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Ivan Kerin <[email protected]> |
11
|
|
|
* @copyright (c) 2014 Clippings Ltd. |
12
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
13
|
|
|
*/ |
14
|
|
|
class PackageNamePrompt implements PromptInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var GitConfig |
18
|
|
|
*/ |
19
|
|
|
private $gitConfig; |
20
|
|
|
|
21
|
1 |
|
public function __construct(GitConfig $gitConfig) |
22
|
|
|
{ |
23
|
1 |
|
$this->gitConfig = $gitConfig; |
24
|
1 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return GitConfig |
28
|
|
|
*/ |
29
|
1 |
|
public function getGitConfig() |
30
|
|
|
{ |
31
|
1 |
|
return $this->gitConfig; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string|null |
36
|
|
|
*/ |
37
|
1 |
|
public function getDefault() |
38
|
|
|
{ |
39
|
1 |
|
return $this->gitConfig->getOrigin(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param OutputInterface $output |
44
|
|
|
* @param DialogHelper $dialog |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
1 |
|
public function getValues(OutputInterface $output, DialogHelper $dialog) |
48
|
|
|
{ |
49
|
1 |
|
$default = $this->getDefault(); |
50
|
|
|
|
51
|
1 |
|
$value = $dialog->ask( |
52
|
1 |
|
$output, |
53
|
1 |
|
"<info>Package Name</info> ({$default}): ", |
54
|
|
|
$default |
55
|
1 |
|
); |
56
|
|
|
|
57
|
1 |
|
$parts = explode('/', $default); |
58
|
1 |
|
$owner = $parts[0]; |
59
|
1 |
|
$title = isset($parts[1]) ? $parts[1] : $default; |
60
|
|
|
|
61
|
|
|
return [ |
62
|
1 |
|
'package_name' => $value, |
63
|
1 |
|
'package_owner' => $owner, |
64
|
1 |
|
'package_title' => $title, |
65
|
1 |
|
'package_classname' => $this->toCamelCase($title), |
66
|
1 |
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Translates a string with underscores |
71
|
|
|
* into camel case (e.g. first-name -> FirstName) |
72
|
|
|
* |
73
|
|
|
* @param string $text String in underscore format |
74
|
|
|
* @return string $text translated into camel caps |
75
|
|
|
*/ |
76
|
5 |
|
public function toCamelCase($text) |
77
|
|
|
{ |
78
|
5 |
|
$text[0] = strtoupper($text[0]); |
79
|
|
|
|
80
|
5 |
|
$capitalise = function ($word) { |
81
|
4 |
|
return strtoupper($word[1]); |
82
|
5 |
|
}; |
83
|
|
|
|
84
|
5 |
|
return preg_replace_callback('/[_\ \-]([a-zA-Z0-9])/', $capitalise, $text); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|