Skip to content

Commit e2c4cf9

Browse files
committed
Fixed parseVendorFiles
1 parent 8272be1 commit e2c4cf9

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed

src/Classes/ExportLocalizations.php

+63-51
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ExportLocalizations implements \JsonSerializable
2626
/**
2727
* @var string
2828
*/
29-
protected $excludePath = DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR;
29+
protected $excludePath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR;
3030

3131
/**
3232
* @var string
@@ -41,30 +41,30 @@ class ExportLocalizations implements \JsonSerializable
4141
public function export()
4242
{
4343
// Check if value is cached and set array to cached version
44-
if (Cache::has(config('laravel-localization.caches.key'))) {
45-
$this->strings = Cache::get(config('laravel-localization.caches.key'));
44+
if ( Cache::has( config( 'laravel-localization.caches.key' ) ) ) {
45+
$this->strings = Cache::get( config( 'laravel-localization.caches.key' ) );
4646

4747
return $this;
4848
}
4949

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

5353
// Parse translations and create final array
54-
array_walk($files['lang'], [$this, 'parseLangFiles']);
55-
array_walk($files['vendor'], [$this, 'parseVendorFiles']);
54+
array_walk( $files[ 'lang' ], [ $this, 'parseLangFiles' ] );
55+
array_walk( $files[ 'vendor' ], [ $this, 'parseVendorFiles' ] );
5656

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

6060
// If timeout > 0 save array to cache
61-
if (config('laravel-localization.caches.timeout', 0) > 0) {
62-
Cache::store(config('laravel-localization.caches.driver', 'file'))
63-
->put(
64-
config('laravel-localization.caches.key', 'localization.array'),
65-
$this->strings,
66-
config('laravel-localization.caches.timeout', 60)
67-
);
61+
if ( config( 'laravel-localization.caches.timeout', 0 ) > 0 ) {
62+
Cache::store( config( 'laravel-localization.caches.driver', 'file' ) )
63+
->put(
64+
config( 'laravel-localization.caches.key', 'localization.array' ),
65+
$this->strings,
66+
config( 'laravel-localization.caches.timeout', 60 )
67+
);
6868
}
6969

7070
return $this;
@@ -77,40 +77,40 @@ public function export()
7777
*
7878
* @return array
7979
*/
80-
protected function findLanguageFiles($path)
80+
protected function findLanguageFiles( $path )
8181
{
8282
// Loop through directories
83-
$dirIterator = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
84-
$recIterator = new \RecursiveIteratorIterator($dirIterator);
83+
$dirIterator = new \RecursiveDirectoryIterator( $path, \RecursiveDirectoryIterator::SKIP_DOTS );
84+
$recIterator = new \RecursiveIteratorIterator( $dirIterator );
8585

8686
// Fetch only php files - skip others
8787
$phpFiles = array_values(
88-
array_map('current',
88+
array_map( 'current',
8989
iterator_to_array(
90-
new \RegexIterator($recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH)
90+
new \RegexIterator( $recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH )
9191
)
9292
)
9393
);
9494

9595
// Sort array by filepath
96-
sort($phpFiles);
96+
sort( $phpFiles );
9797

9898
// Remove full path from items
99-
array_walk($phpFiles, function (&$item) {
100-
$item = str_replace(resource_path('lang'), '', $item);
101-
});
99+
array_walk( $phpFiles, function ( &$item ) {
100+
$item = str_replace( resource_path( 'lang' ), '', $item );
101+
} );
102102

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

108108
// Fetch vendor files from filtered php files
109-
$vendorFiles = array_diff($phpFiles, $nonVendorFiles);
109+
$vendorFiles = array_diff( $phpFiles, $nonVendorFiles );
110110

111111
return [
112-
'lang' => array_values($nonVendorFiles),
113-
'vendor' => array_values($vendorFiles),
112+
'lang' => array_values( $nonVendorFiles ),
113+
'vendor' => array_values( $vendorFiles ),
114114
];
115115
}
116116

@@ -143,13 +143,13 @@ public function toArray()
143143
*
144144
* @return array
145145
*/
146-
public function toFlat($prefix = '.')
146+
public function toFlat( $prefix = '.' )
147147
{
148148
$results = [];
149-
foreach ($this->strings as $lang => $strings) {
150-
foreach ($strings as $lang_array => $lang_messages) {
151-
$key = $lang.$prefix.$lang_array;
152-
$results[$key] = $lang_messages;
149+
foreach ( $this->strings as $lang => $strings ) {
150+
foreach ( $strings as $lang_array => $lang_messages ) {
151+
$key = $lang . $prefix . $lang_array;
152+
$results[ $key ] = $lang_messages;
153153
}
154154
}
155155

@@ -163,29 +163,29 @@ public function toFlat($prefix = '.')
163163
*/
164164
public function toCollection()
165165
{
166-
return collect($this->strings);
166+
return collect( $this->strings );
167167
}
168168

169169
/**
170170
* Method to parse language files.
171171
*
172172
* @param string $file
173173
*/
174-
protected function parseLangFiles($file)
174+
protected function parseLangFiles( $file )
175175
{
176176
// Base package name without file ending
177-
$packageName = basename($file, '.php');
177+
$packageName = basename( $file, '.php' );
178178

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

184184
// Check if language already exists in array
185-
if (array_key_exists($language, $this->strings)) {
186-
$this->strings[$language][$packageName] = $fileContents;
185+
if ( array_key_exists( $language, $this->strings ) ) {
186+
$this->strings[ $language ][ $packageName ] = $fileContents;
187187
} else {
188-
$this->strings[$language] = [
188+
$this->strings[ $language ] = [
189189
$packageName => $fileContents,
190190
];
191191
}
@@ -196,23 +196,35 @@ protected function parseLangFiles($file)
196196
*
197197
* @param string $file
198198
*/
199-
protected function parseVendorFiles($file)
199+
protected function parseVendorFiles( $file )
200200
{
201201
// Base package name without file ending
202-
$packageName = basename($file, '.php');
202+
$packageName = basename( $file, '.php' );
203203

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

210210
// Check if language already exists in array
211-
if (array_key_exists($language, $this->strings)) {
212-
$this->strings[$language][$package.$this->packageSeparator.$packageName] = $fileContents;
211+
if ( array_key_exists( $language, $this->strings ) ) {
212+
// Check if package already exists in language
213+
if ( array_key_exists( $package, $this->strings[ $language ] ) ) {
214+
215+
array_push( $this->strings[ $language ][ $package ], [ $packageName => $fileContents ] );
216+
} else {
217+
218+
$this->strings[ $language ][ $package ] = [ $packageName => $fileContents ];
219+
}
220+
213221
} else {
214-
$this->strings[$language] = [
215-
$package.$this->packageSeparator.$packageName => $fileContents,
222+
$this->strings[ $language ] = [
223+
224+
$package => [
225+
226+
$packageName => $fileContents,
227+
],
216228
];
217229
}
218230
}

0 commit comments

Comments
 (0)