-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
339 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace Lukasss93\Larex\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\File; | ||
use Keboola\Csv\Exception; | ||
use Keboola\Csv\InvalidArgumentException; | ||
use Lukasss93\Larex\Utils; | ||
use RecursiveArrayIterator; | ||
use RecursiveIteratorIterator; | ||
|
||
class LarexImportCommand extends Command | ||
{ | ||
protected $file = 'resources/lang/localization.csv'; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'larex:import {--f|force : Overwrite csv file if already exists}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Import entries from resources/lang files'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
* @throws Exception | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function handle(): void | ||
{ | ||
$languages = collect([]); | ||
$rawValues = collect([]); | ||
|
||
$this->warn('Importing entries...'); | ||
|
||
//get all php files | ||
$files = File::glob(resource_path('lang/**/*.php')); | ||
|
||
foreach($files as $file) { | ||
$array = include $file; | ||
$group = pathinfo($file, PATHINFO_FILENAME); | ||
$lang = basename(dirname($file)); | ||
|
||
if(!$languages->contains($lang)) { | ||
$languages->push($lang); | ||
} | ||
|
||
//loop through array recursive | ||
$iterator = new RecursiveIteratorIterator( | ||
new RecursiveArrayIterator($array), | ||
RecursiveIteratorIterator::SELF_FIRST | ||
); | ||
$path = []; | ||
foreach($iterator as $key => $value) { | ||
$path[$iterator->getDepth()] = $key; | ||
if(!is_array($value)) { | ||
$rawValues->push([ | ||
'group' => $group, | ||
'key' => implode('.', array_slice($path, 0, $iterator->getDepth() + 1)), | ||
'lang' => $lang, | ||
'value' => $value | ||
]); | ||
} | ||
} | ||
} | ||
|
||
//creating the csv file | ||
$header = collect(['group', 'key'])->merge($languages); | ||
$data = collect([]); | ||
|
||
foreach($rawValues as $rawValue) { | ||
$index = $data->search(function($item, $key) use ($rawValue) { | ||
return $item[0] === $rawValue['group'] && $item[1] === $rawValue['key']; | ||
}); | ||
|
||
if($index === false) { | ||
$output = [ | ||
$rawValue['group'], | ||
$rawValue['key'] | ||
]; | ||
|
||
for($i = 2; $i < $header->count(); $i++) { | ||
$real = $rawValue['lang'] === $header->get($i) ? $rawValue['value'] : ''; | ||
$output[$i] = $real; | ||
} | ||
|
||
$data->push($output); | ||
} else { | ||
for($i = 2; $i < $header->count(); $i++) { | ||
$code = $rawValue['lang'] === $header->get($i) ? $rawValue['value'] : null; | ||
|
||
if($code !== null) { | ||
$new = $data->get($index); | ||
$new[$i] = $rawValue['value']; | ||
$data->put($index, $new); | ||
} | ||
} | ||
} | ||
} | ||
|
||
//add header | ||
$data->prepend($header->toArray()); | ||
$data = $data->values(); | ||
|
||
|
||
$force = $this->option('force'); | ||
|
||
|
||
//check file exists | ||
if(File::exists(base_path($this->file)) && !$force) { | ||
$this->error("The '{$this->file}' already exists."); | ||
return; | ||
} | ||
|
||
|
||
Utils::collectionToCsv($data, base_path($this->file)); | ||
$this->info('Files imported successfully.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Lukasss93\Larex\Tests; | ||
|
||
use Illuminate\Support\Facades\File; | ||
|
||
class LarexImportTest extends TestCase | ||
{ | ||
/** | ||
* @param string $firstFile | ||
* @param string $secondFile | ||
* @param string $outputFile | ||
* @dataProvider providerImportCommand | ||
*/ | ||
public function test_larex_import_command(string $firstFile, string $secondFile, string $outputFile): void | ||
{ | ||
File::makeDirectory(resource_path('lang/en'), 0755, true, true); | ||
File::makeDirectory(resource_path('lang/it'), 0755, true, true); | ||
File::put(resource_path('lang/en/app.php'), $this->getTestStub($firstFile)); | ||
File::put(resource_path('lang/it/app.php'), $this->getTestStub($secondFile)); | ||
|
||
$this->artisan('larex:import') | ||
->expectsOutput('Importing entries...') | ||
->expectsOutput('Files imported successfully.') | ||
->run(); | ||
|
||
self::assertFileExists(resource_path('lang/localization.csv')); | ||
self::assertEquals($this->getTestStub($outputFile), | ||
File::get(resource_path('lang/localization.csv'))); | ||
} | ||
|
||
public function test_larex_import_command_with_no_force(): void | ||
{ | ||
$this->artisan('larex:init')->run(); | ||
|
||
File::makeDirectory(resource_path('lang/en'), 0755, true, true); | ||
File::makeDirectory(resource_path('lang/it'), 0755, true, true); | ||
File::put(resource_path('lang/en/app.php'), $this->getTestStub('import-input-en-simple')); | ||
File::put(resource_path('lang/it/app.php'), $this->getTestStub('import-input-it-simple')); | ||
|
||
$this->artisan('larex:import') | ||
->expectsOutput('Importing entries...') | ||
->expectsOutput("The '{$this->file}' already exists.") | ||
->run(); | ||
|
||
self::assertFileExists(resource_path('lang/localization.csv')); | ||
self::assertNotEquals( | ||
$this->getTestStub('import-output-simple'), | ||
File::get(resource_path('lang/localization.csv')) | ||
); | ||
} | ||
|
||
public function test_larex_import_command_with_force(): void | ||
{ | ||
$this->artisan('larex:init')->run(); | ||
|
||
File::makeDirectory(resource_path('lang/en'), 0755, true, true); | ||
File::makeDirectory(resource_path('lang/it'), 0755, true, true); | ||
File::put(resource_path('lang/en/app.php'), $this->getTestStub('import-input-en-simple')); | ||
File::put(resource_path('lang/it/app.php'), $this->getTestStub('import-input-it-simple')); | ||
|
||
$this->artisan('larex:import -f') | ||
->expectsOutput('Importing entries...') | ||
->expectsOutput('Files imported successfully.') | ||
->run(); | ||
|
||
self::assertFileExists(resource_path('lang/localization.csv')); | ||
self::assertEquals( | ||
$this->getTestStub('import-output-simple'), | ||
File::get(resource_path('lang/localization.csv')) | ||
); | ||
} | ||
|
||
public function providerImportCommand(): array | ||
{ | ||
return [ | ||
'simple' => ['import-input-en-simple', 'import-input-it-simple', 'import-output-simple'], | ||
'complex' => ['import-input-en-complex', 'import-input-it-complex', 'import-output-complex'] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.