Skip to content

Commit 35bb370

Browse files
committed
add support for vendor files for the trans command
1 parent dd9ef50 commit 35bb370

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/Commands/TransCommand.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Themsaid\Langman\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
67
use Themsaid\Langman\Manager;
78

89
class TransCommand extends Command
@@ -28,6 +29,13 @@ class TransCommand extends Command
2829
*/
2930
protected $fileName;
3031

32+
/**
33+
* The name of the package.
34+
*
35+
* @var string
36+
*/
37+
protected $packageName;
38+
3139
/**
3240
* The name of the only language we're going to alter its file.
3341
*
@@ -108,6 +116,20 @@ private function parseKey()
108116
}
109117
}
110118

119+
if (Str::contains($this->fileName, '::')) {
120+
try {
121+
$parts = explode('::', $this->fileName);
122+
123+
$this->manager->setPathToVendorPackage($parts[0]);
124+
125+
$this->packageName = $parts[0];
126+
} catch (\ErrorException $e) {
127+
$this->error('Could not recognize the package.');
128+
129+
return false;
130+
}
131+
}
132+
111133
return true;
112134
}
113135

@@ -152,7 +174,7 @@ private function fillKey()
152174
$values = $this->collectValues($languages);
153175

154176
$this->manager->fillKeys(
155-
$this->fileName,
177+
str_replace($this->packageName.'::', '', $this->fileName),
156178
[$this->key => $values]
157179
);
158180

tests/TransCommandTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ public function testCommandAsksForConfirmationToCreateFileIfNotFound()
3434
$this->artisan('langman:trans', ['key' => 'users.name']);
3535
}
3636

37+
public function testCommandAsksForConfirmationToCreatePackageFileIfNotFound()
38+
{
39+
$this->createTempFiles([
40+
'vendor' => ['package' => ['en' => [], 'sp' => []]],
41+
]);
42+
43+
$manager = $this->app[Manager::class];
44+
$command = m::mock('\Themsaid\Langman\Commands\TransCommand[confirm]', [$manager]);
45+
$command->shouldReceive('confirm')->once()->andReturn(true);
46+
47+
$this->app['artisan']->add($command);
48+
$this->artisan('langman:trans', ['key' => 'package::file.name']);
49+
50+
$this->assertFileNotExists($this->app['config']['langman.path'].'/vendor/package/en/file.php');
51+
}
52+
3753
public function testCommandExitsWhenFileNotFoundAndConfirmationFalse()
3854
{
3955
$this->createTempFiles(['en' => []]);
@@ -84,6 +100,26 @@ public function testCommandAsksForValuePerLanguageAndWriteToFile()
84100
$this->assertEquals('naam', $nlFile['name']);
85101
}
86102

103+
public function testCommandAsksForValuePerLanguageForPackageAndWriteToFile()
104+
{
105+
$this->createTempFiles([
106+
'vendor' => ['package' => ['en' => ['users' => "<?php\n return [];"], 'sp' => ['users' => "<?php\n return [];"]]],
107+
]);
108+
109+
$manager = $this->app[Manager::class];
110+
$command = m::mock('\Themsaid\Langman\Commands\TransCommand[ask]', [$manager]);
111+
$command->shouldReceive('ask')->once()->with('/users\.name:en/', null)->andReturn('name');
112+
$command->shouldReceive('ask')->once()->with('/users\.name:sp/', null)->andReturn('naam');
113+
114+
$this->app['artisan']->add($command);
115+
$this->artisan('langman:trans', ['key' => 'package::users.name']);
116+
117+
$enFile = (array) include $this->app['config']['langman.path'].'/vendor/package/en/users.php';
118+
$nlFile = (array) include $this->app['config']['langman.path'].'/vendor/package/sp/users.php';
119+
$this->assertEquals('name', $enFile['name']);
120+
$this->assertEquals('naam', $nlFile['name']);
121+
}
122+
87123
public function testCommandAsksForValuePerLanguageAndUpdatingExistingInFile()
88124
{
89125
$this->createTempFiles([

0 commit comments

Comments
 (0)