Skip to content

Commit

Permalink
Merge pull request #8 from kg-bot/analysis-XkVDGa
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
kg-bot committed Jun 23, 2018
2 parents e2c4cf9 + 390ea31 commit ed95caf
Showing 1 changed file with 49 additions and 52 deletions.
101 changes: 49 additions & 52 deletions src/Classes/ExportLocalizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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 @@ -41,29 +41,29 @@ 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['lang'], [$this, 'parseLangFiles']);
array_walk($files['vendor'], [$this, 'parseVendorFiles']);

// 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 @@ -77,40 +77,40 @@ 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)
)
)
);

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

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

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

// Fetch vendor files from filtered php files
$vendorFiles = array_diff( $phpFiles, $nonVendorFiles );
$vendorFiles = array_diff($phpFiles, $nonVendorFiles);

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

Expand Down Expand Up @@ -143,13 +143,13 @@ public function toArray()
*
* @return array
*/
public function toFlat( $prefix = '.' )
public function toFlat($prefix = '.')
{
$results = [];
foreach ( $this->strings as $lang => $strings ) {
foreach ( $strings as $lang_array => $lang_messages ) {
$key = $lang . $prefix . $lang_array;
$results[ $key ] = $lang_messages;
foreach ($this->strings as $lang => $strings) {
foreach ($strings as $lang_array => $lang_messages) {
$key = $lang.$prefix.$lang_array;
$results[$key] = $lang_messages;
}
}

Expand All @@ -163,29 +163,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 @@ -196,30 +196,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 ] ) ) {

array_push( $this->strings[ $language ][ $package ], [ $packageName => $fileContents ] );
if (array_key_exists($package, $this->strings[$language])) {
array_push($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 Down

0 comments on commit ed95caf

Please sign in to comment.