Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make 'show' command support for langman:show foo/bar #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Commands/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ private function tableRows()
private function filesFromKey()
{
try {
if (Str::contains($this->file, '/')) {
return $this->manager->files()[explode('/', $this->file)[1]];// e.g. 'foo/bar' will return 'bar'
}
return $this->manager->files()[$this->file];
} catch (\ErrorException $e) {
$this->error(sprintf('Language file %s.php not found!', $this->file));
Expand Down
3 changes: 2 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function files()
}
})->map(function ($files) {
return $files->keyBy(function ($file) {
return basename($file->getPath());
$tmp = str_replace($this->path, '', $file->getRealPath());
return substr($tmp, 1, 2);// e.g. '/en/foo/bar.php' will return 'en'
})->map(function ($file) {
return $file->getRealPath();
});
Expand Down
33 changes: 33 additions & 0 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ public function testFilesMethod()
$this->assertEquals($expected, $manager->files());
}

public function testFilesMethodWithNestedDirectory()
{
$manager = $this->app[\Themsaid\Langman\Manager::class];

$this->createTempFilesRecursive([
'en' => [
'foo' => [
'user' => '',
'category' => '']],
'nl' => [
'foo' => [
'user' => '',
'category' => '']],
'vendor' => [
'package' => [
'en' => ['user' => '', 'product' => ''],
'sp' => ['user' => '', 'product' => '']]],
]);

$expected = [
'user' => [
'en' => __DIR__.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'en'.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'user.php',
'nl' => __DIR__.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'nl'.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'user.php',
],
'category' => [
'en' => __DIR__.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'en'.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'category.php',
'nl' => __DIR__.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'nl'.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'category.php',
],
];

$this->assertEquals($expected, $manager->files());
}

public function testLanguagesMethod()
{
$manager = $this->app[\Themsaid\Langman\Manager::class];
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public function createTempFiles($files = [])
}
}

public function createTempFilesRecursive($files = [], $prefix = __DIR__.'/temp/')
{
foreach ($files as $file => $content) {
if (is_array($content)) {
mkdir($prefix.'/'.$file);
$this->createTempFilesRecursive($content, $prefix.'/'.$file.'/');
} else {
file_put_contents($prefix.'/'.$file.'.php', $content);
}
}
}

public function resolveApplicationConsoleKernel($app)
{
$app->singleton('artisan', function ($app) {
Expand Down