Potsky\LaravelLocalizationHelpers\Factory\Localization::deleteBackupFiles PHP 메소드

deleteBackupFiles() 공개 메소드

Delete backup files
public deleteBackupFiles ( string $lang_folder_path, integer $days, boolean | false $dryRun = false, string $ext = 'php' ) : boolean
$lang_folder_path string
$days integer
$dryRun boolean | false
$ext string
리턴 boolean
    public function deleteBackupFiles($lang_folder_path, $days = 0, $dryRun = false, $ext = 'php')
    {
        if ($days < 0) {
            return false;
        }
        try {
            $dir_lang = $this->getLangPath($lang_folder_path);
        } catch (Exception $e) {
            switch ($e->getCode()) {
                //@codeCoverageIgnoreStart
                case self::NO_LANG_FOLDER_FOUND_IN_THESE_PATHS:
                    $this->messageBag->writeError("No lang folder found in these paths:");
                    foreach ($e->getParameter() as $path) {
                        $this->messageBag->writeError("- " . $path);
                    }
                    break;
                    //@codeCoverageIgnoreEnd
                //@codeCoverageIgnoreEnd
                case self::NO_LANG_FOLDER_FOUND_IN_YOUR_CUSTOM_PATH:
                    $this->messageBag->writeError('No lang folder found in your custom path: "' . $e->getParameter() . '"');
                    break;
            }
            return false;
        }
        $return = true;
        foreach ($this->getBackupFiles($dir_lang) as $file) {
            $fileDate = $this->getBackupFileDate($file, $ext);
            // @codeCoverageIgnoreStart
            // Cannot happen because of glob but safer
            if (is_null($fileDate)) {
                $this->messageBag->writeError('Unable to detect date in file ' . $file);
                $return = false;
                continue;
            }
            // @codeCoverageIgnoreEnd
            if ($this->isDateOlderThanDays($fileDate, $days)) {
                if ($dryRun === true) {
                    $deleted = true;
                } else {
                    $deleted = unlink($file);
                }
                if ($deleted === true) {
                    $this->messageBag->writeInfo('Deleting file ' . $file);
                } else {
                    $this->messageBag->writeError('Unable to delete file ' . $file);
                    $return = false;
                }
            } else {
                $this->messageBag->writeInfo('Skip file ' . $file . ' (not older than ' . $days . 'day' . Tools::getPlural($days) . ')');
            }
        }
        return $return;
    }

Usage Example

 /**
  * Error when days is negative
  */
 public function testErrorDaysNegative()
 {
     /** @noinspection PhpVoidFunctionResultUsedInspection */
     $return = Artisan::call('localization:clear', array('--days' => -3));
     $this->assertEquals(1, $return);
     $manager = new Localization(new MessageBag());
     $this->assertFalse($manager->deleteBackupFiles('', -3, true));
 }