55
66namespace MagentoHackathon \Composer \Magento \Deploystrategy ;
77
8+ use Composer \Util \Filesystem ;
9+
810/**
911 * Abstract deploy strategy
1012 */
@@ -61,6 +63,19 @@ abstract class DeploystrategyAbstract
6163 */
6264 protected $ deployedFiles = array ();
6365
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+
6479 /**
6580 * Constructor
6681 *
@@ -69,8 +84,9 @@ abstract class DeploystrategyAbstract
6984 */
7085 public function __construct ($ sourceDir , $ destDir )
7186 {
72- $ this ->destDir = $ destDir ;
73- $ this ->sourceDir = $ sourceDir ;
87+ $ this ->destDir = $ destDir ;
88+ $ this ->sourceDir = $ sourceDir ;
89+ $ this ->filesystem = new Filesystem ;
7490 }
7591
7692 /**
@@ -357,8 +373,8 @@ public function create($source, $dest)
357373 */
358374 public function remove ($ source , $ dest )
359375 {
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, '\\ / ' ) ;
362378
363379 // If source doesn't exist, check if it's a glob expression, otherwise we have nothing we can do
364380 if (!file_exists ($ sourcePath )) {
@@ -382,7 +398,8 @@ public function remove($source, $dest)
382398 if (basename ($ sourcePath ) !== basename ($ destPath )) {
383399 $ destPath .= '/ ' . basename ($ source );
384400 }
385- self ::rmdirRecursive ($ destPath );
401+ $ this ->filesystem ->remove ($ destPath );
402+ $ this ->addRemovedFile ($ destPath );
386403 }
387404
388405 /**
@@ -395,20 +412,17 @@ public function rmEmptyDirsRecursive($dir, $stopDir = null)
395412 {
396413 $ absoluteDir = $ this ->getDestDir () . '/ ' . $ dir ;
397414 if (is_dir ($ absoluteDir )) {
415+
398416 $ iterator = new \RecursiveIteratorIterator (
399- new \RecursiveDirectoryIterator ($ absoluteDir ),
417+ new \RecursiveDirectoryIterator ($ absoluteDir, \RecursiveDirectoryIterator:: SKIP_DOTS ),
400418 \RecursiveIteratorIterator::CHILD_FIRST
401419 );
402420
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 ) {
409422 // The directory contains something, do not remove
410423 return ;
411424 }
425+
412426 // RecursiveIteratorIterator have opened handle on $absoluteDir
413427 // that cause Windows to block the directory and not remove it until
414428 // the iterator will be destroyed.
@@ -427,24 +441,6 @@ public function rmEmptyDirsRecursive($dir, $stopDir = null)
427441 }
428442 }
429443
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-
448444 /**
449445 * Create the module's files in the given destination.
450446 *
@@ -468,6 +464,17 @@ public function addDeployedFile($file)
468464 $ this ->deployedFiles [] = $ file ;
469465 }
470466
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+
471478 /**
472479 * Get all the deployed files
473480 *
@@ -477,4 +484,14 @@ public function getDeployedFiles()
477484 {
478485 return $ this ->deployedFiles ;
479486 }
487+
488+ /**
489+ * Get all the removed files
490+ *
491+ * @return array
492+ */
493+ public function getRemovedFiles ()
494+ {
495+ return $ this ->removedFiles ;
496+ }
480497}
0 commit comments