Skip to content

Commit 1223787

Browse files
committed
Added larex:import command
1 parent f18dce4 commit 1223787

18 files changed

+339
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased]
7+
## v1.2 - 2020-07-18
88
### Added
9+
- Added `larex:import` command
910
- Added PHPUnit tests
1011

11-
## [1.1] - 2020-07-07
12+
## v1.1 - 2020-07-07
1213
### Added
1314
- Added line validation
1415

1516
### Fixed
1617
- Failed to parse some rows
1718

18-
## [1.0] - 2020-07-04
19+
## v1.0 - 2020-07-04
1920
### Added
2021
- First release

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This step *can be skipped* if package auto-discovery is enabled.
3030

3131
## Usage
3232
1. First, you must create the initial CSV file with `php artisan larex:init`.<br>
33-
Or you can use `php artisan larex:init --base` to init the CSV file with default Laravel entries.<br>
33+
Or you can use `php artisan larex:import` to import entries from resources/lang files.<br>
3434
The csv file has the following columns:
3535
* group (basically the file name)
3636
* key (the array key)
@@ -42,6 +42,8 @@ This step *can be skipped* if package auto-discovery is enabled.
4242
3. Finally, you can use `php artisan larex` to translate your entries from the csv file to the laravel php files.
4343

4444
### Tips
45+
* You can import existing laravel php files with `php artisan larex:import`.
46+
* You can use `php artisan larex:init --base` to init the CSV file with default Laravel entries.
4547
* The **key** column inside the CSV file supports the **dot notation** for nested arrays.
4648
* You can watch your CSV file with `php artisan larex --watch`
4749
* You can use `php artisan larex:sort` to sort the CSV file by group and key.

src/Console/LarexCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class LarexCommand extends Command
1212
{
13-
protected $file = 'resources' . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . 'localization.csv';
13+
protected $file = 'resources/lang/localization.csv';
1414

1515
/**
1616
* The name and signature of the console command.

src/Console/LarexImportCommand.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Lukasss93\Larex\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
use Keboola\Csv\Exception;
8+
use Keboola\Csv\InvalidArgumentException;
9+
use Lukasss93\Larex\Utils;
10+
use RecursiveArrayIterator;
11+
use RecursiveIteratorIterator;
12+
13+
class LarexImportCommand extends Command
14+
{
15+
protected $file = 'resources/lang/localization.csv';
16+
17+
/**
18+
* The name and signature of the console command.
19+
*
20+
* @var string
21+
*/
22+
protected $signature = 'larex:import {--f|force : Overwrite csv file if already exists}';
23+
24+
/**
25+
* The console command description.
26+
*
27+
* @var string
28+
*/
29+
protected $description = 'Import entries from resources/lang files';
30+
31+
/**
32+
* Execute the console command.
33+
*
34+
* @return void
35+
* @throws Exception
36+
* @throws InvalidArgumentException
37+
*/
38+
public function handle(): void
39+
{
40+
$languages = collect([]);
41+
$rawValues = collect([]);
42+
43+
$this->warn('Importing entries...');
44+
45+
//get all php files
46+
$files = File::glob(resource_path('lang/**/*.php'));
47+
48+
foreach($files as $file) {
49+
$array = include $file;
50+
$group = pathinfo($file, PATHINFO_FILENAME);
51+
$lang = basename(dirname($file));
52+
53+
if(!$languages->contains($lang)) {
54+
$languages->push($lang);
55+
}
56+
57+
//loop through array recursive
58+
$iterator = new RecursiveIteratorIterator(
59+
new RecursiveArrayIterator($array),
60+
RecursiveIteratorIterator::SELF_FIRST
61+
);
62+
$path = [];
63+
foreach($iterator as $key => $value) {
64+
$path[$iterator->getDepth()] = $key;
65+
if(!is_array($value)) {
66+
$rawValues->push([
67+
'group' => $group,
68+
'key' => implode('.', array_slice($path, 0, $iterator->getDepth() + 1)),
69+
'lang' => $lang,
70+
'value' => $value
71+
]);
72+
}
73+
}
74+
}
75+
76+
//creating the csv file
77+
$header = collect(['group', 'key'])->merge($languages);
78+
$data = collect([]);
79+
80+
foreach($rawValues as $rawValue) {
81+
$index = $data->search(function($item, $key) use ($rawValue) {
82+
return $item[0] === $rawValue['group'] && $item[1] === $rawValue['key'];
83+
});
84+
85+
if($index === false) {
86+
$output = [
87+
$rawValue['group'],
88+
$rawValue['key']
89+
];
90+
91+
for($i = 2; $i < $header->count(); $i++) {
92+
$real = $rawValue['lang'] === $header->get($i) ? $rawValue['value'] : '';
93+
$output[$i] = $real;
94+
}
95+
96+
$data->push($output);
97+
} else {
98+
for($i = 2; $i < $header->count(); $i++) {
99+
$code = $rawValue['lang'] === $header->get($i) ? $rawValue['value'] : null;
100+
101+
if($code !== null) {
102+
$new = $data->get($index);
103+
$new[$i] = $rawValue['value'];
104+
$data->put($index, $new);
105+
}
106+
}
107+
}
108+
}
109+
110+
//add header
111+
$data->prepend($header->toArray());
112+
$data = $data->values();
113+
114+
115+
$force = $this->option('force');
116+
117+
118+
//check file exists
119+
if(File::exists(base_path($this->file)) && !$force) {
120+
$this->error("The '{$this->file}' already exists.");
121+
return;
122+
}
123+
124+
125+
Utils::collectionToCsv($data, base_path($this->file));
126+
$this->info('Files imported successfully.');
127+
}
128+
}

src/Console/LarexInitCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class LarexInitCommand extends Command
1010
{
11-
protected $file = 'resources' . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . 'localization.csv';
11+
protected $file = 'resources/lang/localization.csv';
1212

1313
/**
1414
* The name and signature of the console command.

src/Console/LarexSortCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace Lukasss93\Larex\Console;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
67
use Keboola\Csv\Exception;
78
use Keboola\Csv\InvalidArgumentException;
89
use Lukasss93\Larex\Utils;
910

1011
class LarexSortCommand extends Command
1112
{
12-
protected $file = 'resources' . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . 'localization.csv';
13+
protected $file = 'resources/lang/localization.csv';
1314

1415
/**
1516
* The name and signature of the console command.
@@ -36,6 +37,12 @@ public function handle(): void
3637
{
3738
$this->warn('Sorting che CSV rows...');
3839

40+
if(!File::exists(base_path($this->file))) {
41+
$this->error("The '$this->file' does not exists.");
42+
$this->line('Please create it with: php artisan larex:init');
43+
return;
44+
}
45+
3946
[$header, $rows] = Utils::csvToCollection(base_path($this->file))->partition(function($item, $key) {
4047
return $key === 0;
4148
});

src/LarexServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\ServiceProvider;
66
use Lukasss93\Larex\Console\LarexCommand;
7+
use Lukasss93\Larex\Console\LarexImportCommand;
78
use Lukasss93\Larex\Console\LarexInitCommand;
89
use Lukasss93\Larex\Console\LarexSortCommand;
910

@@ -19,7 +20,8 @@ public function register(): void
1920
$this->commands([
2021
LarexCommand::class,
2122
LarexInitCommand::class,
22-
LarexSortCommand::class
23+
LarexSortCommand::class,
24+
LarexImportCommand::class
2325
]);
2426
}
2527

src/Utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static function collectionToCsv(Collection $array, string $filename): voi
4545

4646
public static function getStub(string $name): string
4747
{
48-
$content = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Stubs' . DIRECTORY_SEPARATOR . $name . '.stub');
48+
$content = file_get_contents(__DIR__ . '/Stubs/' . $name . '.stub');
4949
return self::normalizeEOLs($content);
5050
}
5151

tests/LarexImportTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Lukasss93\Larex\Tests;
4+
5+
use Illuminate\Support\Facades\File;
6+
7+
class LarexImportTest extends TestCase
8+
{
9+
/**
10+
* @param string $firstFile
11+
* @param string $secondFile
12+
* @param string $outputFile
13+
* @dataProvider providerImportCommand
14+
*/
15+
public function test_larex_import_command(string $firstFile, string $secondFile, string $outputFile): void
16+
{
17+
File::makeDirectory(resource_path('lang/en'), 0755, true, true);
18+
File::makeDirectory(resource_path('lang/it'), 0755, true, true);
19+
File::put(resource_path('lang/en/app.php'), $this->getTestStub($firstFile));
20+
File::put(resource_path('lang/it/app.php'), $this->getTestStub($secondFile));
21+
22+
$this->artisan('larex:import')
23+
->expectsOutput('Importing entries...')
24+
->expectsOutput('Files imported successfully.')
25+
->run();
26+
27+
self::assertFileExists(resource_path('lang/localization.csv'));
28+
self::assertEquals($this->getTestStub($outputFile),
29+
File::get(resource_path('lang/localization.csv')));
30+
}
31+
32+
public function test_larex_import_command_with_no_force(): void
33+
{
34+
$this->artisan('larex:init')->run();
35+
36+
File::makeDirectory(resource_path('lang/en'), 0755, true, true);
37+
File::makeDirectory(resource_path('lang/it'), 0755, true, true);
38+
File::put(resource_path('lang/en/app.php'), $this->getTestStub('import-input-en-simple'));
39+
File::put(resource_path('lang/it/app.php'), $this->getTestStub('import-input-it-simple'));
40+
41+
$this->artisan('larex:import')
42+
->expectsOutput('Importing entries...')
43+
->expectsOutput("The '{$this->file}' already exists.")
44+
->run();
45+
46+
self::assertFileExists(resource_path('lang/localization.csv'));
47+
self::assertNotEquals(
48+
$this->getTestStub('import-output-simple'),
49+
File::get(resource_path('lang/localization.csv'))
50+
);
51+
}
52+
53+
public function test_larex_import_command_with_force(): void
54+
{
55+
$this->artisan('larex:init')->run();
56+
57+
File::makeDirectory(resource_path('lang/en'), 0755, true, true);
58+
File::makeDirectory(resource_path('lang/it'), 0755, true, true);
59+
File::put(resource_path('lang/en/app.php'), $this->getTestStub('import-input-en-simple'));
60+
File::put(resource_path('lang/it/app.php'), $this->getTestStub('import-input-it-simple'));
61+
62+
$this->artisan('larex:import -f')
63+
->expectsOutput('Importing entries...')
64+
->expectsOutput('Files imported successfully.')
65+
->run();
66+
67+
self::assertFileExists(resource_path('lang/localization.csv'));
68+
self::assertEquals(
69+
$this->getTestStub('import-output-simple'),
70+
File::get(resource_path('lang/localization.csv'))
71+
);
72+
}
73+
74+
public function providerImportCommand(): array
75+
{
76+
return [
77+
'simple' => ['import-input-en-simple', 'import-input-it-simple', 'import-output-simple'],
78+
'complex' => ['import-input-en-complex', 'import-input-it-complex', 'import-output-complex']
79+
];
80+
}
81+
}

tests/LarexSortTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@ public function test_sort_command(): void
1919

2020
self::assertEquals($this->getTestStub('sort-output'), File::get(base_path($this->file)));
2121
}
22+
23+
public function test_sort_command_if_file_does_not_exists(): void
24+
{
25+
$this->artisan('larex:sort')
26+
->expectsOutput('Sorting che CSV rows...')
27+
->expectsOutput("The '$this->file' does not exists.")
28+
->expectsOutput('Please create it with: php artisan larex:init')
29+
->run();
30+
}
2231
}

0 commit comments

Comments
 (0)