From 6d4d4d11662cffc26c0da4cce64f4dd78c89f008 Mon Sep 17 00:00:00 2001 From: Pakpoom Yimsukanan Date: Wed, 8 Nov 2017 10:49:25 +0700 Subject: [PATCH 1/3] make 'show' command support for `langman:show foo/bar` --- src/Commands/ShowCommand.php | 3 +++ src/Manager.php | 3 ++- tests/ManagerTest.php | 33 +++++++++++++++++++++++++++++++++ tests/TestCase.php | 12 ++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Commands/ShowCommand.php b/src/Commands/ShowCommand.php index d2b79bc..3f0fd1e 100644 --- a/src/Commands/ShowCommand.php +++ b/src/Commands/ShowCommand.php @@ -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)); diff --git a/src/Manager.php b/src/Manager.php index 4ff5824..a82caeb 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -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(); }); diff --git a/tests/ManagerTest.php b/tests/ManagerTest.php index deac505..51f970c 100644 --- a/tests/ManagerTest.php +++ b/tests/ManagerTest.php @@ -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]; diff --git a/tests/TestCase.php b/tests/TestCase.php index 9e94e0b..c7df43e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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) { From 307ddeab92c85d94c6b168ffff317c4d3d4b92a2 Mon Sep 17 00:00:00 2001 From: Pakpoom Yimsukanan Date: Wed, 8 Nov 2017 11:09:19 +0700 Subject: [PATCH 2/3] Apply StyleCI fix --- src/Commands/ShowCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ShowCommand.php b/src/Commands/ShowCommand.php index 3f0fd1e..4eed455 100644 --- a/src/Commands/ShowCommand.php +++ b/src/Commands/ShowCommand.php @@ -147,7 +147,7 @@ private function tableRows() private function filesFromKey() { try { - if(Str::contains( $this->file, '/')) { + 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]; From 6086f967dff4d3198d1e2e6e03bfff0f56bcc0c2 Mon Sep 17 00:00:00 2001 From: Pakpoom Yimsukanan Date: Wed, 8 Nov 2017 15:20:36 +0700 Subject: [PATCH 3/3] fixup! Apply StyleCI fix --- src/Commands/ShowCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ShowCommand.php b/src/Commands/ShowCommand.php index 4eed455..26a0f0a 100644 --- a/src/Commands/ShowCommand.php +++ b/src/Commands/ShowCommand.php @@ -147,7 +147,7 @@ private function tableRows() private function filesFromKey() { try { - if (Str::contains( $this->file, '/')) { + 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];