diff --git a/src/Manager.php b/src/Manager.php index 4ff5824..7b68424 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -57,21 +57,30 @@ public function files() return $this->disk->extension($file) == 'php'; }); - $filesByFile = $files->groupBy(function ($file) { - $fileName = $file->getBasename('.'.$file->getExtension()); + $languageDirectoryIndex = count(explode(DIRECTORY_SEPARATOR, $this->path)); + $filesByFile = $files->groupBy(function ($file) use ($languageDirectoryIndex) { + $fileName = $file->getBasename('.'.$file->getExtension()); if (Str::contains($file->getPath(), 'vendor')) { $fileName = str_replace('.php', '', $file->getFileName()); - $packageName = basename(dirname($file->getPath())); - return "{$packageName}::{$fileName}"; } else { - return $fileName; + $directories = explode(DIRECTORY_SEPARATOR, $file->getPathname()); + // remove lang path + $directories = array_slice($directories, $languageDirectoryIndex + 1); + // remove file name (inc. extension) + $directories = array_slice($directories, 0, count($directories) - 1); + // add file name without extension + $directories[] = $fileName; + // use the laravel SEPARATOR for lang, not the platform specific one + return implode('/', $directories); } - })->map(function ($files) { - return $files->keyBy(function ($file) { - return basename($file->getPath()); + })->map(function ($files) use ($languageDirectoryIndex) { + return $files->keyBy(function ($file) use ($languageDirectoryIndex) { + $directories = explode(DIRECTORY_SEPARATOR, $file->getPath()); + // ignoring the path directories, the very next one is the language code + return $directories[$languageDirectoryIndex]; })->map(function ($file) { return $file->getRealPath(); }); diff --git a/tests/ShowCommandTest.php b/tests/ShowCommandTest.php index 904c7c7..eb62945 100644 --- a/tests/ShowCommandTest.php +++ b/tests/ShowCommandTest.php @@ -69,6 +69,23 @@ public function testCommandOutputForFileWithNestedKeys() $this->assertRegExp('/name.last(?:.*)last/', $this->consoleOutput()); } + public function testCommandOutputForFileWithNestedFiles() + { + $separator = '/'; + $nestedFile = "nested{$separator}user"; // nested/user + + $this->createTempFiles([ + 'en' => [$nestedFile => " ['first' => 'first', 'last' => 'last']];"], + 'sp' => [$nestedFile => " ['first' => 'firstsp']];"], + ]); + + $this->artisan('langman:show', ['key' => 'nested/user']); + + $this->assertRegExp('/key(?:.*)en(?:.*)sp/', $this->consoleOutput()); + $this->assertRegExp('/name.first(?:.*)first(?:.*)firstsp/', $this->consoleOutput()); + $this->assertRegExp('/name.last(?:.*)last/', $this->consoleOutput()); + } + public function testCommandOutputForKey() { $this->createTempFiles([ diff --git a/tests/TestCase.php b/tests/TestCase.php index 9e94e0b..a5a742c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,14 +19,14 @@ public function setUp() { parent::setUp(); - exec('rm -rf '.__DIR__.'/temp/*'); + $this->removeTempFiles(); } public function tearDown() { parent::tearDown(); - exec('rm -rf '.__DIR__.'/temp/*'); + $this->removeTempFiles(); $this->consoleOutput = ''; } @@ -34,19 +34,24 @@ public function tearDown() public function createTempFiles($files = []) { foreach ($files as $dir => $dirFiles) { - mkdir(__DIR__.'/temp/'.$dir); + mkdir(__DIR__.'/temp/'.$dir, 0777, true); foreach ($dirFiles as $file => $content) { if (is_array($content)) { - mkdir(__DIR__.'/temp/'.$dir.'/'.$file); + mkdir(__DIR__.'/temp/'.$dir.'/'.$file, 0777, true); foreach ($content as $subDir => $subContent) { - mkdir(__DIR__.'/temp/vendor/'.$file.'/'.$subDir); + mkdir(__DIR__.'/temp/vendor/'.$file.'/'.$subDir, 0777, true); foreach ($subContent as $subFile => $subsubContent) { file_put_contents(__DIR__.'/temp/'.$dir.'/'.$file.'/'.$subDir.'/'.$subFile.'.php', $subsubContent); } } } else { + $fileParts = explode('/', $file); + if (count($fileParts) > 1) { + $fileParts = array_slice($fileParts, 0, count($fileParts) - 1); + } + mkdir(__DIR__.'/temp/'.$dir.'/'.implode('/', $fileParts), 0777, true); file_put_contents(__DIR__.'/temp/'.$dir.'/'.$file.'.php', $content); } } @@ -71,4 +76,28 @@ public function consoleOutput() { return $this->consoleOutput ?: $this->consoleOutput = $this->app[Kernel::class]->output(); } + + private function removeTempFiles() + { + $this->rrmdir(__DIR__.'/temp', '/\.gitignore$/i', true); + } + + private function rrmdir($dir, $ignoreRegex, $skipTopLevel = false) + { + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (is_dir($dir.DIRECTORY_SEPARATOR.$object)) { + $this->rrmdir($dir.DIRECTORY_SEPARATOR.$object, $ignoreRegex); + } elseif (empty($ignoreRegex) || empty(preg_match($ignoreRegex, $object))) { + unlink($dir.DIRECTORY_SEPARATOR.$object); + } + } + } + if (empty($skipTopLevel)) { + rmdir($dir); + } + } + } }