recursiveRemoveDirectory() public method

See also: http://lixlpixel.org/recursive_function/php/recursive_directory_delete/
public recursiveRemoveDirectory ( string $directory, boolean $empty = false ) : boolean
$directory string
$empty boolean
return boolean
Beispiel #1
0
 /**
  * @param string $path
  *
  * @return bool
  */
 private function emptyDirectory($path)
 {
     $errors = array();
     $dir = new FilesystemIterator($path);
     foreach ($dir as $file => $info) {
         if ($info->isDir()) {
             $this->verbose('<debug>Filesystem::recursiveRemoveDirectory() <comment>' . $file . '</comment></debug>');
             if (!isset($fs)) {
                 $fs = new Filesystem();
             }
             if (!$fs->recursiveRemoveDirectory($file)) {
                 $errors[] = $file;
             }
         } else {
             $this->verbose('<debug>unlink() <comment>' . $file . '</comment></debug>');
             if (!unlink($file)) {
                 $errors[] = $file;
             }
         }
     }
     if (!$errors) {
         return true;
     }
     $message = sprintf("Failed to empty directory %s, unable to remove:\n", var_export($path, true));
     foreach ($errors as $error) {
         $message .= sprintf(" - %s\n", var_export($error, true));
     }
     throw new RuntimeException($message);
 }
 public function testExecute()
 {
     $application = $this->getApplication();
     $application->add(new ListCommand());
     $command = $this->getApplication()->find('dev:module:create');
     $root = getcwd();
     // delete old module
     if (is_dir($root . '/N98Magerun_UnitTest')) {
         $filesystem = new Filesystem();
         $filesystem->recursiveRemoveDirectory($root . '/N98Magerun_UnitTest');
         clearstatcache();
     }
     $commandTester = new CommandTester($command);
     $commandTester->execute(array('command' => $command->getName(), '--add-all' => true, '--add-setup' => true, '--add-readme' => true, '--add-composer' => true, '--modman' => true, '--description' => 'Unit Test Description', '--author-name' => 'Unit Test', '--author-email' => '*****@*****.**', 'vendorNamespace' => 'N98Magerun', 'moduleName' => 'UnitTest'));
     $this->assertFileExists($root . '/N98Magerun_UnitTest/composer.json');
     $this->assertFileExists($root . '/N98Magerun_UnitTest/readme.md');
     $moduleBaseFolder = $root . '/N98Magerun_UnitTest/src/app/code/local/N98Magerun/UnitTest/';
     $this->assertFileExists($moduleBaseFolder . 'etc/config.xml');
     $this->assertFileExists($moduleBaseFolder . 'Block');
     $this->assertFileExists($moduleBaseFolder . 'Model');
     $this->assertFileExists($moduleBaseFolder . 'Helper');
     $this->assertFileExists($moduleBaseFolder . 'data/n98magerun_unittest_setup');
     $this->assertFileExists($moduleBaseFolder . 'sql/n98magerun_unittest_setup');
     // delete old module
     if (is_dir($root . '/N98Magerun_UnitTest')) {
         $filesystem = new Filesystem();
         $filesystem->recursiveRemoveDirectory($root . '/N98Magerun_UnitTest');
         clearstatcache();
     }
 }
 /**
  * @param $root
  * @return bool|Filesystem
  */
 protected function _deleteOldModule($root)
 {
     // delete old module
     $filesystem = false;
     if (is_dir($root . '/N98Magerun_UnitTest')) {
         $filesystem = new Filesystem();
         $filesystem->recursiveRemoveDirectory($root . '/N98Magerun_UnitTest');
         clearstatcache();
     }
     return $filesystem;
 }
 /**
  * @return void
  */
 public function execute()
 {
     if (is_dir(getcwd() . '/vendor')) {
         $finder = new Finder();
         $finder->files()->depth(3)->in(getcwd() . '/vendor');
         if ($finder->count() == 0) {
             $filesystem = new Filesystem();
             $filesystem->recursiveRemoveDirectory(getcwd() . '/vendor');
         }
     }
 }
Beispiel #5
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->detectMagento($output);
     $finder = Finder::create()->directories()->depth(0)->in($this->getApplication()->getMagentoRootFolder() . '/var/generation');
     $vendorNameToFilter = $input->getArgument('vendorName');
     $filesystem = new Filesystem();
     foreach ($finder as $directory) {
         if (!empty($vendorNameToFilter) && $directory->getBasename() != $vendorNameToFilter) {
             continue;
         }
         /* @var $directory \Symfony\Component\Finder\SplFileInfo */
         $filesystem->recursiveRemoveDirectory($directory->getPathname());
         $output->writeln('<info>Removed <comment>' . $directory->getBasename() . '</comment> folder</info>');
     }
 }
Beispiel #6
0
 public function testParentIsNotRemovedIfEmptyIsTrue()
 {
     $tmp = sys_get_temp_dir();
     $basePath = $tmp . "/n98_testdir";
     $folder1 = $basePath . "/folder1";
     $folder2 = $basePath . "/folder2";
     $file1 = $folder1 . "/file1.txt";
     $file2 = $folder2 . "/file2.txt";
     @mkdir($folder1, 0777, true);
     @mkdir($folder2, 0777, true);
     touch($file1);
     touch($file2);
     $this->fileSystem->recursiveRemoveDirectory($basePath, true);
     $this->assertFileExists($basePath);
     $this->assertFileNotExists($folder1);
     $this->assertFileNotExists($folder2);
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->chooseInstallationFolder($input, $output);
     $this->detectMagento($output);
     $this->getApplication()->setAutoExit(false);
     /* @var $dialog DialogHelper */
     $dialog = $this->getHelper('dialog');
     $shouldUninstall = $input->getOption('force');
     if (!$shouldUninstall) {
         $shouldUninstall = $dialog->askConfirmation($output, '<question>Really uninstall ?</question> <comment>[n]</comment>: ', false);
     }
     if ($shouldUninstall) {
         $input = new StringInput('db:drop --force');
         $this->getApplication()->run($input, $output);
         $fileSystem = new Filesystem();
         $output->writeln('<info>Remove directory </info><comment>' . $this->_magentoRootFolder . '</comment>');
         try {
             $fileSystem->recursiveRemoveDirectory($this->_magentoRootFolder);
         } catch (Exception $e) {
             $output->writeln('<error>' . $e->getMessage() . '</error>');
         }
         $output->writeln('<info>Done</info>');
     }
 }
Beispiel #8
0
 /**
  * Remove empty composer extraction folder
  */
 protected function removeEmptyFolders()
 {
     if (is_dir(getcwd() . '/vendor')) {
         $finder = new Finder();
         $finder->files()->depth(3)->in(getcwd() . '/vendor');
         if ($finder->count() == 0) {
             $filesystem = new Filesystem();
             $filesystem->recursiveRemoveDirectory(getcwd() . '/vendor');
         }
     }
 }