Skip to content

Commit 35fc6ae

Browse files
committed
Add NewCommand::copyDir method
1 parent e3451bc commit 35fc6ae

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/Commands/NewCommand.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use Framework\CLI\CLI;
1313
use Framework\CLI\Command;
14+
use RecursiveDirectoryIterator;
15+
use RecursiveIteratorIterator;
1416

1517
/**
1618
* Class NewCommand.
@@ -32,15 +34,33 @@ protected function create(string $package, string $name) : void
3234
CLI::error('Package ' . $package . ' not found');
3335
return;
3436
}
35-
$source = \strtr($source, [' ' => '\ ']);
36-
$dir = \strtr($directory, [' ' => '\ ']);
37-
\shell_exec("cp -r {$source}/* {$dir}");
37+
$this->copyDir($source, $directory);
3838
CLI::write(
3939
$name . ' structure created at "' . $directory . '"',
4040
CLI::FG_GREEN
4141
);
4242
}
4343

44+
protected function copyDir(string $source, string $directory) : void
45+
{
46+
$iterator = new RecursiveIteratorIterator(
47+
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
48+
RecursiveIteratorIterator::SELF_FIRST
49+
);
50+
foreach ($iterator as $item) {
51+
if ($item->isDir()) {
52+
$dir = $directory . \DIRECTORY_SEPARATOR . $iterator->getSubPathname();
53+
if ( ! \mkdir($dir, 0755, true) && ! \is_dir($dir)) {
54+
CLI::error(
55+
\sprintf('Directory "%s" could not be created', $dir)
56+
);
57+
}
58+
continue;
59+
}
60+
\copy((string) $item, $directory . \DIRECTORY_SEPARATOR . $iterator->getSubPathname());
61+
}
62+
}
63+
4464
protected function getDirectory() : string
4565
{
4666
$directory = $this->console->getArgument(0);

0 commit comments

Comments
 (0)