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

setExtension, setExcludeExtensions & Support .json #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
182 changes: 165 additions & 17 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ class Manager
* @var array
*/
private $syncPaths;

/**
* The extension to the language files.
*
* @var string
*/
private $extension;
/**
* The extension to the language files.
*
* @var array
*/
private $exclude_extensions;
/**
* Manager constructor.
*
Expand All @@ -42,8 +53,45 @@ public function __construct(Filesystem $disk, $path, array $syncPaths)
$this->disk = $disk;
$this->path = $path;
$this->syncPaths = $syncPaths;
$this->extension = 'php';
$this->exclude_extensions = ['json',];
}
/**
* Manager setExtension.
*
* @param string $extension
*/
public function setExtension($extension){
$this->extension = $extension;
}
/**
* Manager setExcludeExtensions.
*
* @param array $exclude_extensions
*/
public function setExcludeExtensions($exclude_extensions){
$this->exclude_extensions = $exclude_extensions;
}
/**
* get all the json file content
*
*
* @return array
*/
public function getJsonFilesContent($filesByFile,$lang)
{

$data = [];
foreach ($filesByFile as $fileName => $value) {
if (array_key_exists($lang,$filesByFile[$fileName])) // check is language avl
{
$contents = $this->disk->get($filesByFile[$fileName][$lang]); // in get method provide full path of json file
$data[$fileName][$lang] = json_decode($contents, true); // data['example_file']['en'] = array of json file content
}
}
return $data;

}

/**
* Array of language files grouped by file name.
*
Expand All @@ -54,14 +102,14 @@ public function __construct(Filesystem $disk, $path, array $syncPaths)
public function files()
{
$files = Collection::make($this->disk->allFiles($this->path))->filter(function ($file) {
return $this->disk->extension($file) == 'php';
return $this->disk->extension($file) == $this->extension;
});

$filesByFile = $files->groupBy(function ($file) {
$fileName = $file->getBasename('.'.$file->getExtension());

if (Str::contains($file->getPath(), 'vendor')) {
$fileName = str_replace('.php', '', $file->getFileName());
$fileName = str_replace('.'.$this->extension, '', $file->getFileName());

$packageName = basename(dirname($file->getPath()));

Expand Down Expand Up @@ -105,7 +153,64 @@ private function neglectVendorFiles($filesByFile)

return $return;
}
/**
* Include only specified names of files.
*
* @param array $filesByFile
* @param array $includedFileNames
* @return array
*/
public function filterByIncludedFileNames($filesByFile,$includedFileNames)
{
$return = [];

foreach ($filesByFile as $key => $value) {
if (in_array($key,$includedFileNames)) {
$return[$key] = $value;
}
}

return $return;
}
/**
* Exclude only specified names of files.
*
* @param array $filesByFile
* @param array $includedFileNames
* @return array
*/
public function filterByExcludedFileNames($filesByFile,$excludedFileNames)
{
$return = [];

foreach ($filesByFile as $key => $value) {
if (!in_array($key,$excludedFileNames)) {
$return[$key] = $value;
}
}

return $return;
}
/**
* Get all supported languages after exclude or include.
*
* @param array $filesByFile
*
* @return array
*/
public function getSupportedLanguages($filesByFile)
{
$return = [];

foreach ($filesByFile as $key => $nesValue) {
foreach($nesValue as $key => $nesValue)
{
$return[$key] = 1;
}
}

return array_keys($return);
}
/**
* Array of supported languages.
*
Expand All @@ -120,12 +225,12 @@ public function languages()
}, $this->disk->directories($this->path));

$languages = array_filter($languages, function ($directory) {
return $directory != 'vendor' && $directory != 'json';
return $directory != 'vendor' && !in_array($directory, $this->exclude_extensions);
});

sort($languages);

return Arr::except($languages, ['vendor', 'json']);
return Arr::except($languages, array_merge(['vendor',],$this->exclude_extensions));
}

/**
Expand All @@ -137,9 +242,12 @@ public function languages()
public function createFile($fileName)
{
foreach ($this->languages() as $languageKey) {
$file = $this->path."/{$languageKey}/{$fileName}.php";
$file = $this->path."/{$languageKey}/{$fileName}.".$this->extension;
if (! $this->disk->exists($file)) {
file_put_contents($file, "<?php \n\n return[];");
if($this->extension == "json")
file_put_contents($file, "{ \n\n }");
if($this->extension == "php")
file_put_contents($file, "<?php \n\n return[];");
}
}
}
Expand All @@ -159,7 +267,7 @@ public function fillKeys($fileName, array $keys)

foreach ($keys as $key => $values) {
foreach ($values as $languageKey => $value) {
$filePath = $this->path."/{$languageKey}/{$fileName}.php";
$filePath = $this->path."/{$languageKey}/{$fileName}.".$this->extension;

Arr::set($appends[$filePath], $key, $value);
}
Expand All @@ -184,7 +292,7 @@ public function fillKeys($fileName, array $keys)
public function removeKey($fileName, $key)
{
foreach ($this->languages() as $language) {
$filePath = $this->path."/{$language}/{$fileName}.php";
$filePath = $this->path."/{$language}/{$fileName}.".$this->extension;

$fileContent = $this->getFileContent($filePath);

Expand All @@ -203,17 +311,58 @@ public function removeKey($fileName, $key)
*/
public function writeFile($filePath, array $translations)
{
$content = "<?php \n\nreturn [";

$content .= $this->stringLineMaker($translations);

$content .= "\n];";
switch ($this->extension) {
case "json":
$content = "{\n";

$content .= $this->stringJsonLineMaker($translations);

$content .= "\n}";
break;
case "php":
$content = "<?php \n\nreturn [";

$content .= $this->stringLineMaker($translations);

$content .= "\n];";
break;
default:
$content = "<?php \n\nreturn [";

$content .= $this->stringLineMaker($translations);

$content .= "\n];";
}

file_put_contents($filePath, $content);
}

/**
* Write the lines of the inner array of the JSON language file.
*
* @param $array
* @return string
*/
private function stringJsonLineMaker($array, $prepend = '')
{
$output = '';

foreach ($array as $key => $value) {
if (is_array($value)) {
$value = $this->stringJsonLineMaker($value, $prepend.' ');
$output .= "\n".$prepend.' "'.$key.'" : {"'.$value.'" "'.$prepend.'" },';
} else {
$value = str_replace("\'", "'", addslashes($value));
$key = str_replace("\'", "'", addslashes($key));
$output .= "\n".$prepend.' "'.$key.'" : "'.$value.'",';
}
}
$output = substr($output,0,-1);
// dd($output);
return $output;
}
/**
* Write the lines of the inner array of the language file.
* Write the lines of the inner array of the PHP language file.
*
* @param $array
* @return string
Expand All @@ -236,7 +385,6 @@ private function stringLineMaker($array, $prepend = '')

return $output;
}

/**
* Get the content in the given file path.
*
Expand Down