5
5
6
6
namespace MagentoHackathon \Composer \Magento \Deploystrategy ;
7
7
8
+ use Composer \Util \Filesystem ;
9
+
8
10
/**
9
11
* Abstract deploy strategy
10
12
*/
@@ -61,6 +63,19 @@ abstract class DeploystrategyAbstract
61
63
*/
62
64
protected $ deployedFiles = array ();
63
65
66
+ /**
67
+ * List of files/folders which were
68
+ * remove via this remove operation
69
+ *
70
+ * @var array
71
+ */
72
+ protected $ removedFiles = array ();
73
+
74
+ /**
75
+ * @var Filesystem
76
+ */
77
+ protected $ filesystem ;
78
+
64
79
/**
65
80
* Constructor
66
81
*
@@ -69,8 +84,9 @@ abstract class DeploystrategyAbstract
69
84
*/
70
85
public function __construct ($ sourceDir , $ destDir )
71
86
{
72
- $ this ->destDir = $ destDir ;
73
- $ this ->sourceDir = $ sourceDir ;
87
+ $ this ->destDir = $ destDir ;
88
+ $ this ->sourceDir = $ sourceDir ;
89
+ $ this ->filesystem = new Filesystem ;
74
90
}
75
91
76
92
/**
@@ -357,8 +373,8 @@ public function create($source, $dest)
357
373
*/
358
374
public function remove ($ source , $ dest )
359
375
{
360
- $ sourcePath = $ this ->getSourceDir () . '/ ' . $ this ->removeTrailingSlash ($ source );
361
- $ destPath = $ this ->getDestDir () . '/ ' . $ dest ;
376
+ $ sourcePath = $ this ->getSourceDir () . '/ ' . ltrim ( $ this ->removeTrailingSlash ($ source), '\\ / ' );
377
+ $ destPath = $ this ->getDestDir () . '/ ' . ltrim ( $ dest, '\\ / ' ) ;
362
378
363
379
// If source doesn't exist, check if it's a glob expression, otherwise we have nothing we can do
364
380
if (!file_exists ($ sourcePath )) {
@@ -382,7 +398,8 @@ public function remove($source, $dest)
382
398
if (basename ($ sourcePath ) !== basename ($ destPath )) {
383
399
$ destPath .= '/ ' . basename ($ source );
384
400
}
385
- self ::rmdirRecursive ($ destPath );
401
+ $ this ->filesystem ->remove ($ destPath );
402
+ $ this ->addRemovedFile ($ destPath );
386
403
}
387
404
388
405
/**
@@ -395,20 +412,17 @@ public function rmEmptyDirsRecursive($dir, $stopDir = null)
395
412
{
396
413
$ absoluteDir = $ this ->getDestDir () . '/ ' . $ dir ;
397
414
if (is_dir ($ absoluteDir )) {
415
+
398
416
$ iterator = new \RecursiveIteratorIterator (
399
- new \RecursiveDirectoryIterator ($ absoluteDir ),
417
+ new \RecursiveDirectoryIterator ($ absoluteDir, \RecursiveDirectoryIterator:: SKIP_DOTS ),
400
418
\RecursiveIteratorIterator::CHILD_FIRST
401
419
);
402
420
403
- foreach ($ iterator as $ item ) {
404
- /** @var SplFileInfo $item */
405
- $ path = (string )$ item ;
406
- if (!strcmp ($ item ->getFilename (), '. ' ) || !strcmp ($ item ->getFilename (), '.. ' )) {
407
- continue ;
408
- }
421
+ if (iterator_count ($ iterator ) > 0 ) {
409
422
// The directory contains something, do not remove
410
423
return ;
411
424
}
425
+
412
426
// RecursiveIteratorIterator have opened handle on $absoluteDir
413
427
// that cause Windows to block the directory and not remove it until
414
428
// the iterator will be destroyed.
@@ -427,24 +441,6 @@ public function rmEmptyDirsRecursive($dir, $stopDir = null)
427
441
}
428
442
}
429
443
430
- /**
431
- * Recursively removes the specified directory or file
432
- *
433
- * @param $dir
434
- */
435
- public static function rmdirRecursive ($ dir )
436
- {
437
- $ fs = new \Composer \Util \Filesystem ();
438
- if (is_dir ($ dir )){
439
- $ result = $ fs ->removeDirectory ($ dir );
440
- }else {
441
- @unlink ($ dir );
442
- }
443
-
444
- return ;
445
- }
446
-
447
-
448
444
/**
449
445
* Create the module's files in the given destination.
450
446
*
@@ -468,6 +464,17 @@ public function addDeployedFile($file)
468
464
$ this ->deployedFiles [] = $ file ;
469
465
}
470
466
467
+ /**
468
+ * Add a file/folder to the list of removed files
469
+ * @param string $file
470
+ */
471
+ public function addRemovedFile ($ file )
472
+ {
473
+ //strip of destination deploy location
474
+ $ file = preg_replace (sprintf ('/^%s/ ' , preg_quote ($ this ->getDestDir (), '/ ' )), '' , $ file );
475
+ $ this ->removedFiles [] = $ file ;
476
+ }
477
+
471
478
/**
472
479
* Get all the deployed files
473
480
*
@@ -477,4 +484,14 @@ public function getDeployedFiles()
477
484
{
478
485
return $ this ->deployedFiles ;
479
486
}
487
+
488
+ /**
489
+ * Get all the removed files
490
+ *
491
+ * @return array
492
+ */
493
+ public function getRemovedFiles ()
494
+ {
495
+ return $ this ->removedFiles ;
496
+ }
480
497
}
0 commit comments