22
33use Azi \Generators \Theme ;
44use GuzzleHttp \Client ;
5+ use RecursiveDirectoryIterator ;
6+ use RecursiveIteratorIterator ;
57use RuntimeException ;
68use Symfony \Component \Console \Command \Command ;
79use Symfony \Component \Console \Input \InputArgument ;
810use Symfony \Component \Console \Input \InputInterface ;
911use Symfony \Component \Console \Input \InputOption ;
1012use Symfony \Component \Console \Output \OutputInterface ;
13+ use Symfony \Component \Filesystem \Filesystem ;
1114
1215/**
1316 * Class NewCommand
@@ -28,6 +31,10 @@ class NewCommand extends Command
2831
2932 protected $ app ;
3033 protected $ version ;
34+ /**
35+ * @var Filesystem
36+ */
37+ protected $ fileSystem ;
3138
3239 /**
3340 * NewCommand constructor.
@@ -111,10 +118,13 @@ public function execute( InputInterface $input, OutputInterface $output )
111118
112119 $ output ->writeln ('<info>Downloading wordpress..</info> this might take a while ' );
113120 $ zipFile = $ this ->download ();
121+
114122 $ output ->writeln ('<info>Extracting package</info> ' );
115123 $ this ->extract ($ zipFile );
124+
116125 $ output ->writeln ('<info>Generating wordpress theme & installing timber</info> ' );
117126 (new Theme ($ this ->getApp (), $ input , $ output ))->generate ();
127+
118128 $ output ->writeln ('<comment>All done! Build something amazing.</comment> ' );
119129 }
120130
@@ -200,14 +210,71 @@ private function createCacheDirectory()
200210 public function extract ( $ file )
201211 {
202212 $ projectPath = getcwd () . '/ ' . $ this ->getApp ();
203-
204- $ zip = new \ZipArchive ();
213+ $ zip = new \ZipArchive ();
205214 $ zip ->open ($ file );
206215 $ zip ->extractTo ($ projectPath );
207216
208- # Kind of a hack but works.
209- exec ("mv " . $ projectPath . "/wordpress/* $ projectPath " );
210- rmdir ($ projectPath . '/wordpress ' );
217+ $ this ->move ($ projectPath . "/wordpress/ " , $ projectPath );
218+ $ this ->delete ($ projectPath . "/wordpress/ " );
219+ }
220+
221+ /**
222+ * Delete a directory recursively
223+ *
224+ * @param $directory
225+ * @return bool
226+ * @link http://stackoverflow.com/a/1653776/2641971
227+ */
228+ public function delete ( $ directory )
229+ {
230+ if (!file_exists ($ directory )) {
231+ return true ;
232+ }
233+
234+ if (!is_dir ($ directory )) {
235+ return unlink ($ directory );
236+ }
237+
238+ foreach (scandir ($ directory ) as $ item ) {
239+ if ($ item == '. ' || $ item == '.. ' ) {
240+ continue ;
241+ }
242+
243+ if (!$ this ->delete ($ directory . DIRECTORY_SEPARATOR . $ item )) {
244+ return false ;
245+ }
246+
247+ }
248+
249+ return rmdir ($ directory );
250+ }
251+
252+ /**
253+ * Move files from one directory to another
254+ *
255+ * @param $source
256+ * @param $destination
257+ * @link http://stackoverflow.com/a/27290570/2641971
258+ */
259+ public function move ( $ source , $ destination )
260+ {
261+ $ this ->fileSystem = new Filesystem ();
262+
263+ if (!is_dir ($ destination )) {
264+ $ this ->fileSystem ->mkdir ($ destination );
265+ }
266+
267+ $ directoryIterator = new \RecursiveDirectoryIterator ($ source , \RecursiveDirectoryIterator::SKIP_DOTS );
268+ $ iterator = new \RecursiveIteratorIterator ($ directoryIterator , \RecursiveIteratorIterator::SELF_FIRST );
269+ foreach ($ iterator as $ item ) {
270+
271+ if ($ item ->isDir ()) {
272+ $ this ->fileSystem ->mkdir ($ destination . DIRECTORY_SEPARATOR . $ iterator ->getSubPathName ());
273+ } else {
274+ $ this ->fileSystem ->rename ($ item , $ destination . DIRECTORY_SEPARATOR . $ iterator ->getSubPathName ());
275+ }
276+ }
277+
211278 }
212279
213280 /**
0 commit comments