Skip to content

Commit

Permalink
Merge pull request #21 from kg-bot/analysis-8nwZLy
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
kg-bot authored Dec 10, 2018
2 parents 635d30c + 2178e3f commit 4e019fa
Showing 1 changed file with 72 additions and 72 deletions.
144 changes: 72 additions & 72 deletions src/Classes/ExportLocalizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ExportLocalizations implements \JsonSerializable
/**
* @var string
*/
protected $excludePath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR;
protected $excludePath = DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR;

/**
* @var string
Expand All @@ -46,30 +46,30 @@ class ExportLocalizations implements \JsonSerializable
public function export()
{
// Check if value is cached and set array to cached version
if ( Cache::has( config( 'laravel-localization.caches.key' ) ) ) {
$this->strings = Cache::get( config( 'laravel-localization.caches.key' ) );
if (Cache::has(config('laravel-localization.caches.key'))) {
$this->strings = Cache::get(config('laravel-localization.caches.key'));

return $this;
}

// Collect language files and build array with translations
$files = $this->findLanguageFiles( resource_path( 'lang' ) );
$files = $this->findLanguageFiles(resource_path('lang'));

// Parse translations and create final array
array_walk( $files[ 'lang' ], [ $this, 'parseLangFiles' ] );
array_walk( $files[ 'vendor' ], [ $this, 'parseVendorFiles' ] );
array_walk( $files[ 'json' ], [ $this, 'parseJsonFiles' ] );
array_walk($files['lang'], [$this, 'parseLangFiles']);
array_walk($files['vendor'], [$this, 'parseVendorFiles']);
array_walk($files['json'], [$this, 'parseJsonFiles']);

// Trigger event for final translated array
event( new LaravelLocalizationExported( $this->strings ) );
event(new LaravelLocalizationExported($this->strings));

// If timeout > 0 save array to cache
if ( config( 'laravel-localization.caches.timeout', 0 ) > 0 ) {
Cache::store( config( 'laravel-localization.caches.driver', 'file' ) )
if (config('laravel-localization.caches.timeout', 0) > 0) {
Cache::store(config('laravel-localization.caches.driver', 'file'))
->put(
config( 'laravel-localization.caches.key', 'localization.array' ),
config('laravel-localization.caches.key', 'localization.array'),
$this->strings,
config( 'laravel-localization.caches.timeout', 60 )
config('laravel-localization.caches.timeout', 60)
);
}

Expand All @@ -83,58 +83,58 @@ public function export()
*
* @return array
*/
protected function findLanguageFiles( $path )
protected function findLanguageFiles($path)
{
// Loop through directories
$dirIterator = new \RecursiveDirectoryIterator( $path, \RecursiveDirectoryIterator::SKIP_DOTS );
$recIterator = new \RecursiveIteratorIterator( $dirIterator );
$dirIterator = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
$recIterator = new \RecursiveIteratorIterator($dirIterator);

// Fetch only php files - skip others
$phpFiles = array_values(
array_map( 'current',
array_map('current',
iterator_to_array(
new \RegexIterator( $recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH )
new \RegexIterator($recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH)
)
)
);

$jsonFiles = array_values(
array_map( 'current',
array_map('current',
iterator_to_array(
new \RegexIterator( $recIterator, $this->jsonRegex, \RecursiveRegexIterator::GET_MATCH )
new \RegexIterator($recIterator, $this->jsonRegex, \RecursiveRegexIterator::GET_MATCH)
)
)
);

$files = array_merge( $phpFiles, $jsonFiles );
$files = array_merge($phpFiles, $jsonFiles);

// Sort array by filepath
sort( $files );
sort($files);

// Remove full path from items
array_walk( $files, function ( &$item ) {
$item = str_replace( resource_path( 'lang' ), '', $item );
} );
array_walk($files, function (&$item) {
$item = str_replace(resource_path('lang'), '', $item);
});

// Fetch non-vendor files from filtered php files
$nonVendorFiles = array_filter( $files, function ( $file ) {
return strpos( $file, $this->excludePath ) === false && strpos( $file, '.json' ) === false;
} );
$nonVendorFiles = array_filter($files, function ($file) {
return strpos($file, $this->excludePath) === false && strpos($file, '.json') === false;
});

// Fetch vendor files from filtered php files
$vendorFiles = array_filter( array_diff( $files, $nonVendorFiles ), function ( $file ) {
return strpos( $file, 'json' ) === false;
} );
$vendorFiles = array_filter(array_diff($files, $nonVendorFiles), function ($file) {
return strpos($file, 'json') === false;
});

// Fetch .json files from filtered files
$jsonFiles = array_filter( $files, function ( $file ) {
return strpos( $file, '.json' ) !== false;
} );
$jsonFiles = array_filter($files, function ($file) {
return strpos($file, '.json') !== false;
});

return [
'lang' => array_values( $nonVendorFiles ),
'vendor' => array_values( $vendorFiles ),
'json' => array_values( $jsonFiles ),
'lang' => array_values($nonVendorFiles),
'vendor' => array_values($vendorFiles),
'json' => array_values($jsonFiles),
];
}

Expand Down Expand Up @@ -167,22 +167,22 @@ public function toArray()
*
* @return array
*/
public function toFlat( $prefix = '.' )
public function toFlat($prefix = '.')
{
$results = [];
foreach ( $this->strings as $lang => $strings ) {
if ( $lang !== 'json' ) {
foreach ( $strings as $lang_array => $lang_messages ) {
$key = $lang . $prefix . $lang_array;
$results[ $key ] = $lang_messages;
foreach ($this->strings as $lang => $strings) {
if ($lang !== 'json') {
foreach ($strings as $lang_array => $lang_messages) {
$key = $lang.$prefix.$lang_array;
$results[$key] = $lang_messages;
}
} else {
foreach ( $strings as $json_lang => $json_strings ) {
$key = $json_lang . $prefix . '__JSON__';
if ( array_key_exists( $key, $results ) ) {
$results[ $key ] = $json_strings;
foreach ($strings as $json_lang => $json_strings) {
$key = $json_lang.$prefix.'__JSON__';
if (array_key_exists($key, $results)) {
$results[$key] = $json_strings;
} else {
$results[ $key ] = $json_strings;
$results[$key] = $json_strings;
}
}
}
Expand All @@ -198,29 +198,29 @@ public function toFlat( $prefix = '.' )
*/
public function toCollection()
{
return collect( $this->strings );
return collect($this->strings);
}

/**
* Method to parse language files.
*
* @param string $file
*/
protected function parseLangFiles( $file )
protected function parseLangFiles($file)
{
// Base package name without file ending
$packageName = basename( $file, '.php' );
$packageName = basename($file, '.php');

// Get package, language and file contents from language file
// /<language_code>/(<package/)<filename>.php
$language = explode( DIRECTORY_SEPARATOR, $file )[ 1 ];
$fileContents = require resource_path( 'lang' ) . DIRECTORY_SEPARATOR . $file;
$language = explode(DIRECTORY_SEPARATOR, $file)[1];
$fileContents = require resource_path('lang').DIRECTORY_SEPARATOR.$file;

// Check if language already exists in array
if ( array_key_exists( $language, $this->strings ) ) {
$this->strings[ $language ][ $packageName ] = $fileContents;
if (array_key_exists($language, $this->strings)) {
$this->strings[$language][$packageName] = $fileContents;
} else {
$this->strings[ $language ] = [
$this->strings[$language] = [
$packageName => $fileContents,
];
}
Expand All @@ -231,27 +231,27 @@ protected function parseLangFiles( $file )
*
* @param string $file
*/
protected function parseVendorFiles( $file )
protected function parseVendorFiles($file)
{
// Base package name without file ending
$packageName = basename( $file, '.php' );
$packageName = basename($file, '.php');

// Get package, language and file contents from language file
// /vendor/<package>/<language_code>/<filename>.php
$package = explode( DIRECTORY_SEPARATOR, $file )[ 2 ];
$language = explode( DIRECTORY_SEPARATOR, $file )[ 3 ];
$fileContents = require resource_path( 'lang' ) . DIRECTORY_SEPARATOR . $file;
$package = explode(DIRECTORY_SEPARATOR, $file)[2];
$language = explode(DIRECTORY_SEPARATOR, $file)[3];
$fileContents = require resource_path('lang').DIRECTORY_SEPARATOR.$file;

// Check if language already exists in array
if ( array_key_exists( $language, $this->strings ) ) {
if (array_key_exists($language, $this->strings)) {
// Check if package already exists in language
if ( array_key_exists( $package, $this->strings[ $language ] ) ) {
$this->strings[ $language ][ $package ][ $packageName ] = $fileContents;
if (array_key_exists($package, $this->strings[$language])) {
$this->strings[$language][$package][$packageName] = $fileContents;
} else {
$this->strings[ $language ][ $package ] = [ $packageName => $fileContents ];
$this->strings[$language][$package] = [$packageName => $fileContents];
}
} else {
$this->strings[ $language ] = [
$this->strings[$language] = [

$package => [

Expand All @@ -261,22 +261,22 @@ protected function parseVendorFiles( $file )
}
}

protected function parseJsonFiles( $file )
protected function parseJsonFiles($file)
{
// Base package name without file ending
$language = basename( $file, '.json' );
$language = basename($file, '.json');

// Get package, language and file contents from language file
// /<language_code>/(<package/)<filename>.php
$fileContents = json_decode( file_get_contents( resource_path( 'lang' ) . $file ) );
$fileContents = json_decode(file_get_contents(resource_path('lang').$file));

// Check if language already exists in array
if ( array_key_exists( 'json', $this->strings ) ) {
if ( array_key_exists( $language, $this->strings[ 'json' ] ) ) {
$this->strings[ 'json' ][ $language ] = $fileContents;
if (array_key_exists('json', $this->strings)) {
if (array_key_exists($language, $this->strings['json'])) {
$this->strings['json'][$language] = $fileContents;
}
} else {
$this->strings[ 'json' ] = [
$this->strings['json'] = [

$language => $fileContents,
];
Expand Down

0 comments on commit 4e019fa

Please sign in to comment.