1 | <?php |
||
14 | class CreateCommand extends AbstractBaseCommand |
||
15 | { |
||
16 | /** |
||
17 | * Crafter. |
||
18 | * |
||
19 | * @var CrafterInterface |
||
20 | */ |
||
21 | protected $crafter; |
||
22 | |||
23 | /** |
||
24 | * CreateCommand constructor. |
||
25 | * |
||
26 | * @param CrafterInterface $crafter |
||
27 | * @param HelperSet $helperSet |
||
28 | */ |
||
29 | 9 | public function __construct(CrafterInterface $crafter, HelperSet $helperSet) |
|
30 | { |
||
31 | 9 | parent::__construct($helperSet); |
|
32 | 9 | $this->crafter = $crafter; |
|
33 | 9 | } |
|
34 | |||
35 | public function __invoke($dir, InputInterface $input, OutputInterface $output) |
||
36 | { |
||
37 | $this->input = $input; |
||
38 | $this->output = $output; |
||
39 | |||
40 | $output->writeln('<info>I need to know a little more about your future awesome laravel package...</info>'); |
||
41 | $output->writeln(''); |
||
42 | |||
43 | $package = new Package( |
||
44 | $this->askForPackageName(), |
||
45 | $this->askForAuthor(), |
||
46 | $dir |
||
47 | ); |
||
48 | $package->setDescription($this->askForDescription()); |
||
49 | |||
50 | $output->writeln(''); |
||
51 | $output->writeln('<info>Crafting your laravel package...</info>'); |
||
52 | |||
53 | $this->crafter->craft($package); |
||
54 | |||
55 | $output->writeln(''); |
||
56 | $output->writeln('<info>Successfully crafted!</info>'); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param string $package |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | protected function askForPackageName($package = 'vendor/package') |
||
84 | |||
85 | /** |
||
86 | * @param string $author |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | protected function askForAuthor($author = 'Author Name <[email protected]>') |
||
91 | { |
||
92 | $self = $this; |
||
93 | |||
94 | return $this->askAndValidate( |
||
95 | 'Author [<comment>'.$author.'</comment>]: ', |
||
96 | function ($value) use ($self, $author) { |
||
97 | if (null === $value) { |
||
98 | return $author; |
||
99 | } |
||
100 | |||
101 | $value = $value ?: $author; |
||
102 | $parsed = $self->parseAuthorString($value); |
||
103 | |||
104 | return sprintf('%s <%s>', $parsed['name'], $parsed['email']); |
||
105 | }, |
||
106 | null, |
||
107 | $author |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param bool|string $description |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | protected function askForDescription($description = false) |
||
125 | |||
126 | /** |
||
127 | * Parse the author string. |
||
128 | * |
||
129 | * @private |
||
130 | * |
||
131 | * @param string $author |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | 9 | private function parseAuthorString($author) |
|
136 | { |
||
137 | 9 | if (preg_match('/^(?P<name>[- \.,\p{L}\p{N}\'’]+) <(?P<email>.+?)>$/u', $author, $match)) { |
|
138 | 6 | if ($this->isValidEmail($match['email'])) { |
|
139 | return [ |
||
140 | 3 | 'name' => trim($match['name']), |
|
141 | 3 | 'email' => $match['email'], |
|
142 | 3 | ]; |
|
143 | } |
||
144 | 3 | } |
|
145 | 6 | throw new \InvalidArgumentException( |
|
146 | 'Invalid author string. Must be in the format: '. |
||
147 | 'John Smith <[email protected]>' |
||
148 | 6 | ); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Check if a given email is a good email. |
||
153 | * |
||
154 | * @param $email |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | 6 | private function isValidEmail($email) |
|
171 | } |
||
172 |