diff --git a/README.md b/README.md index f694eb6..15dcbb4 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,10 @@ Use the [github issue tool](https://github.com/potsky/laravel-localization-helpe ## 6. Change Log +### v2.x.1 + +- fix a bug when using backup files and when a dot is in your laravel installation path ([#20](https://github.com/potsky/laravel-localization-helpers/issues/20)) + ### v2.x.0 - new command `localization:clear` to remove backups diff --git a/src/Potsky/LaravelLocalizationHelpers/Command/LocalizationMissing.php b/src/Potsky/LaravelLocalizationHelpers/Command/LocalizationMissing.php index 1437d80..9955fdd 100644 --- a/src/Potsky/LaravelLocalizationHelpers/Command/LocalizationMissing.php +++ b/src/Potsky/LaravelLocalizationHelpers/Command/LocalizationMissing.php @@ -110,6 +110,7 @@ public function fire() { $folders = $this->manager->getPath( $this->folders ); $this->display = ! $this->option( 'silent' ); + $extension = $this->option( 'php-file-extension' ); ////////////////////////////////////////////////// // Display where translations are searched in // @@ -129,7 +130,7 @@ public function fire() //////////////////////////////// // Parse all lemmas from code // //////////////////////////////// - $lemmas = $this->manager->extractTranslationsFromFolders( $folders , $this->trans_methods , $this->option( 'php-file-extension' ) ); + $lemmas = $this->manager->extractTranslationsFromFolders( $folders , $this->trans_methods , $extension ); if ( count( $lemmas ) === 0 ) { @@ -493,7 +494,7 @@ public function fire() foreach ( $job as $file_lang_path => $file_content ) { - $backup_path = preg_replace( '/\..+$/' , '.' . $now . '.php' , $file_lang_path ); + $backup_path = $this->manager->getBackupPath( $file_lang_path , $now , $extension ); if ( ! $this->option( 'dry-run' ) ) { diff --git a/src/Potsky/LaravelLocalizationHelpers/Factory/Localization.php b/src/Potsky/LaravelLocalizationHelpers/Factory/Localization.php index 5fc9ad9..a564801 100644 --- a/src/Potsky/LaravelLocalizationHelpers/Factory/Localization.php +++ b/src/Potsky/LaravelLocalizationHelpers/Factory/Localization.php @@ -413,18 +413,29 @@ public function getBackupDate( $offsetDay = 0 ) * Return all lang backup files * * @param string $lang_directory the lang directory + * @param string $ext * * @return array */ - public function getBackupFiles( $lang_directory ) + public function getBackupFiles( $lang_directory , $ext = 'php' ) { - $files = $lang_directory . '/*/*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9].php'; + $files = $lang_directory . '/*/*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9].' . $ext; return glob( $files ); } - public function deleteBackupFiles( $lang_folder_path , $days = 0 , $dryRun = false ) + /** + * Delete backup files + * + * @param string $lang_folder_path + * @param int $days + * @param bool|false $dryRun + * @param string $ext + * + * @return bool + */ + public function deleteBackupFiles( $lang_folder_path , $days = 0 , $dryRun = false , $ext = 'php' ) { if ( $days < 0 ) { @@ -459,7 +470,7 @@ public function deleteBackupFiles( $lang_folder_path , $days = 0 , $dryRun = fal foreach ( $this->getBackupFiles( $dir_lang ) as $file ) { - $fileDate = $this->getBackupFileDate( $file ); + $fileDate = $this->getBackupFileDate( $file , $ext ); // @codeCoverageIgnoreStart // Cannot happen because of glob but safer @@ -526,17 +537,18 @@ public function isDateOlderThanDays( \DateTime $date , $days ) * @param array $folders An array of folder to search for lemma in * @param array $trans_methods An array of PHP lang functions * @param bool|false $regex Is lemma a regex ? - * @param bool|false $shortOutput Output style for fiel paths + * @param bool|false $shortOutput Output style for file paths + * @param string $ext * * @return array|false */ - public function findLemma( $lemma , $folders , $trans_methods , $regex = false , $shortOutput = false ) + public function findLemma( $lemma , $folders , $trans_methods , $regex = false , $shortOutput = false , $ext = 'php' ) { $files = array(); foreach ( $folders as $path ) { - foreach ( $this->getFilesWithExtension( $path ) as $php_file_path => $dumb ) + foreach ( $this->getFilesWithExtension( $path , $ext ) as $php_file_path => $dumb ) { foreach ( $this->extractTranslationFromPhpFile( $php_file_path , $trans_methods ) as $k => $v ) { @@ -704,18 +716,33 @@ public function isALevel( $level ) return in_array( $level , self::$PHP_CS_FIXER_LEVELS ); } + /** + * Get the backup file path according to the current file path + * + * @param string $file_lang_path + * @param string $date + * @param string $ext + * + * @return mixed + */ + public function getBackupPath( $file_lang_path , $date , $ext = 'php' ) + { + return preg_replace( '/\.' . $ext . '$/' , '.' . $date . '.' . $ext , $file_lang_path ); + } + /** * Return the date of a backup file * * @param string $file a backup file path + * @param string $ext * * @return \DateTime|null */ - private function getBackupFileDate( $file ) + private function getBackupFileDate( $file , $ext = 'php' ) { $matches = array(); - if ( preg_match( '@^(.*)([0-9]{8}_[0-9]{6})\\.php$@' , $file , $matches ) === 1 ) + if ( preg_match( '@^(.*)([0-9]{8}_[0-9]{6})\\.' . $ext . '$@' , $file , $matches ) === 1 ) { return \DateTime::createFromFormat( self::BACKUP_DATE_FORMAT , $matches[ 2 ] ); } diff --git a/tests/fixes/Gh20Tests.php b/tests/fixes/Gh20Tests.php new file mode 100644 index 0000000..af8402e --- /dev/null +++ b/tests/fixes/Gh20Tests.php @@ -0,0 +1,21 @@ +assertSame( '/nia/nio/message.' . $now . '.php' , $manager->getBackupPath( '/nia/nio/message.php' , $now ) ); + $this->assertSame( '/var/www/kd/domain.com/message.' . $now . '.php' , $manager->getBackupPath( '/var/www/kd/domain.com/message.php' , $now ) ); + $this->assertSame( '/var/www/kd/domain.com/message.' . $now . '.txt' , $manager->getBackupPath( '/var/www/kd/domain.com/message.txt' , $now , 'txt' ) ); + } +}