Skip to content
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
25 changes: 17 additions & 8 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
17 changes: 17 additions & 0 deletions tests/ShowCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => "<?php\n return ['name' => ['first' => 'first', 'last' => 'last']];"],
'sp' => [$nestedFile => "<?php\n return ['name' => ['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([
Expand Down
39 changes: 34 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,39 @@ 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 = '';
}

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);
}
}
Expand All @@ -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);
}
}
}
}