Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
47.06% |
32 / 68 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
NewCommand | |
47.06% |
32 / 68 |
|
0.00% |
0 / 6 |
117.74 | |
0.00% |
0 / 1 |
create | |
64.29% |
9 / 14 |
|
0.00% |
0 / 1 |
4.73 | |||
getComposerSource | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
getDistroSource | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
copyDir | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
5.31 | |||
getDirectory | |
42.11% |
8 / 19 |
|
0.00% |
0 / 1 |
16.51 | |||
promptDirectory | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Command Line Tool. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Aplus\Commands; |
11 | |
12 | use Framework\CLI\CLI; |
13 | use Framework\CLI\Command; |
14 | use RecursiveDirectoryIterator; |
15 | use RecursiveIteratorIterator; |
16 | |
17 | /** |
18 | * Class NewCommand. |
19 | * |
20 | * @package aplus |
21 | */ |
22 | abstract class NewCommand extends Command |
23 | { |
24 | protected function create(string $package, string $name) : void |
25 | { |
26 | $directory = $this->getDirectory(); |
27 | $source = $this->getComposerSource($package); |
28 | if ( ! $source) { |
29 | $source = $this->getComposerSource($package, true); |
30 | if ( ! $source) { |
31 | $source = $this->getDistroSource($package); |
32 | } |
33 | } |
34 | if ( ! $source) { |
35 | CLI::error('Package aplus/' . $package . ' not found'); |
36 | return; |
37 | } |
38 | $this->copyDir($source, $directory); |
39 | CLI::write( |
40 | $name . ' structure created at "' . $directory . '"', |
41 | CLI::FG_GREEN |
42 | ); |
43 | } |
44 | |
45 | protected function getComposerSource(string $package, bool $global = false) : false | string |
46 | { |
47 | $source = $global |
48 | ? __DIR__ . '/../../../../../' |
49 | : __DIR__ . '/../../'; |
50 | $source .= 'vendor/aplus/' . $package; |
51 | if (\is_dir($source)) { |
52 | return \realpath($source); |
53 | } |
54 | return false; |
55 | } |
56 | |
57 | protected function getDistroSource(string $package) : false | string |
58 | { |
59 | $source = __DIR__ . '/../../../../packages/' . $package; |
60 | if (\is_dir($source)) { |
61 | return \realpath($source); |
62 | } |
63 | return false; |
64 | } |
65 | |
66 | protected function copyDir(string $source, string $directory) : void |
67 | { |
68 | $iterator = new RecursiveIteratorIterator( |
69 | new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
70 | RecursiveIteratorIterator::SELF_FIRST |
71 | ); |
72 | foreach ($iterator as $item) { |
73 | if ($item->isDir()) { |
74 | $dir = $directory . \DIRECTORY_SEPARATOR . $iterator->getSubPathname(); |
75 | if ( ! \mkdir($dir, 0755, true) && ! \is_dir($dir)) { |
76 | CLI::error( |
77 | \sprintf('Directory "%s" could not be created', $dir) |
78 | ); |
79 | } |
80 | continue; |
81 | } |
82 | \copy((string) $item, $directory . \DIRECTORY_SEPARATOR . $iterator->getSubPathname()); |
83 | } |
84 | } |
85 | |
86 | protected function getDirectory() : string |
87 | { |
88 | $directory = $this->console->getArgument(0); |
89 | if ($directory === null) { |
90 | $directory = $this->promptDirectory(); |
91 | } |
92 | if ( ! \str_starts_with($directory, '/')) { |
93 | $directory = \getcwd() . '/' . $directory; |
94 | } |
95 | if (\file_exists($directory)) { |
96 | CLI::error( |
97 | \sprintf('The path "%s" already exists', $directory) |
98 | ); |
99 | } |
100 | if ( ! \mkdir($directory, 0755, true) && ! \is_dir($directory)) { |
101 | CLI::error( |
102 | \sprintf('Directory "%s" could not be created', $directory) |
103 | ); |
104 | } |
105 | $realpath = \realpath($directory); |
106 | if ($realpath === false) { |
107 | CLI::error( |
108 | \sprintf('Was not possible get the realpath of "%s"', $directory) |
109 | ); |
110 | } |
111 | return $realpath; // @phpstan-ignore-line |
112 | } |
113 | |
114 | protected function promptDirectory() : string |
115 | { |
116 | $directory = CLI::prompt('Directory'); |
117 | $directory = \trim($directory); |
118 | if ($directory === '') { |
119 | CLI::error('Directory path cannot be empty. Try again.', null); |
120 | return $this->promptDirectory(); |
121 | } |
122 | if ( ! \str_starts_with($directory, '/')) { |
123 | $directory = \getcwd() . '/' . $directory; |
124 | } |
125 | if (\file_exists($directory)) { |
126 | CLI::error('Directory already exists. Try Again.', null); |
127 | return $this->promptDirectory(); |
128 | } |
129 | return $directory; |
130 | } |
131 | } |