Skip to content

Commit

Permalink
cp fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
potsky committed Jan 29, 2016
1 parent ff330ea commit 4b56ca7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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' ) )
{
Expand Down
45 changes: 36 additions & 9 deletions src/Potsky/LaravelLocalizationHelpers/Factory/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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 ] );
}
Expand Down
21 changes: 21 additions & 0 deletions tests/fixes/Gh20Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Potsky\LaravelLocalizationHelpers\Factory\Localization;
use Potsky\LaravelLocalizationHelpers\Factory\MessageBag;

class Gh20Tests extends TestCase
{
/**
* https://github.com/potsky/laravel-localization-helpers/issues/20
*/
public function testDotInPath()
{
$messageBag = new MessageBag();
$manager = new Localization( $messageBag );
$now = '20160129_202938';

$this->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' ) );
}
}

0 comments on commit 4b56ca7

Please sign in to comment.