forked from shaggyz/laravel-gettext
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathFileSystem.php
More file actions
517 lines (428 loc) · 14.3 KB
/
FileSystem.php
File metadata and controls
517 lines (428 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php namespace Xinax\LaravelGettext;
use Xinax\LaravelGettext\Config\Models\Config;
use Xinax\LaravelGettext\Exceptions\LocaleFileNotFoundException;
use Xinax\LaravelGettext\Exceptions\DirectoryNotFoundException;
use Xinax\LaravelGettext\Exceptions\FileCreationException;
class FileSystem {
/**
* Package configuration model
*
* @var Config
*/
protected $configuration;
/**
* File system base path
* All paths will be relative to this
*
* @var String
*/
protected $basePath;
/**
* Storage path for file generation
*
* @var String
*/
protected $storagePath;
/**
* Storage directory name for view compilation
*
* @var String
*/
protected $storageContainer;
/**
* Sets configuration
*
* @param Config $config
* @param String $basePath
* @param String $storagePath
*/
public function __construct(Config $config, $basePath, $storagePath)
{
$this->configuration = $config;
$this->basePath = $basePath;
$this->storagePath = $storagePath;
$this->storageContainer = "views";
}
/**
* Build views in order to parse php files
*
* @param Array $viewPaths
* @param String $domain
*
* @return Boolean status
*/
public function compileViews(Array $viewPaths, $domain)
{
// Check the output directory
$targetDir = $this->storagePath . DIRECTORY_SEPARATOR . $this->storageContainer;
if (!file_exists($targetDir)) {
$this->createDirectory($targetDir);
}
// Domain separation
$domainDir = $targetDir . DIRECTORY_SEPARATOR . $domain;
$this->clearDirectory($domainDir);
$this->createDirectory($domainDir);
foreach ( $viewPaths as $path ) {
$path = $this->basePath . DIRECTORY_SEPARATOR . $path;
$fs = new \Illuminate\Filesystem\Filesystem($path);
$glob = $fs->allFiles(realpath($path));
$compiler = new \Illuminate\View\Compilers\BladeCompiler($fs, $domainDir);
foreach ($glob as $file) {
$compiler->setPath($file);
$contents = $compiler->compileString($fs->get($file));
$compiledPath = $compiler->getCompiledPath($compiler->getPath());
$fs->put($compiledPath . '.php', $contents);
}
}
return true;
}
/**
* Constructs and returns the full path to
* translation files
*
* @param String $append
* @return String
*/
public function getDomainPath($append = null)
{
$path = array(
$this->basePath,
$this->configuration->getTranslationsPath(),
"i18n"
);
if (!is_null($append)) {
array_push($path, $append);
}
return implode(DIRECTORY_SEPARATOR, $path);
}
/**
* Creates a configured .po file on $path. If write is true the file will
* be created, otherwise the file contents are returned.
*
* @param String $path
* @param String $locale
* @param String $domain
* @param Boolean $write
* @return Integer | String
*/
public function createPOFile($path, $locale, $domain, $write = true)
{
$project = $this->configuration->getProject();
$timestamp = date("Y-m-d H:iO");
$translator = $this->configuration->getTranslator();
$encoding = $this->configuration->getEncoding();
$relativePath = $this->getRelativePath($path, $this->basePath);
$template = 'msgid ""' . "\n";
$template .= 'msgstr ""' . "\n";
$template .= '"Project-Id-Version: ' . $project . '\n' . "\"\n";
$template .= '"POT-Creation-Date: ' . $timestamp . '\n' . "\"\n";
$template .= '"PO-Revision-Date: ' . $timestamp . '\n' . "\"\n";
$template .= '"Last-Translator: ' . $translator . '\n' . "\"\n";
$template .= '"Language-Team: ' . $translator . '\n' . "\"\n";
$template .= '"Language: ' . $locale . '\n' . "\"\n";
$template .= '"MIME-Version: 1.0' . '\n' . "\"\n";
$template .= '"Content-Type: text/plain; charset=' . $encoding . '\n' . "\"\n";
$template .= '"Content-Transfer-Encoding: 8bit' . '\n' . "\"\n";
$template .= '"X-Generator: Poedit 1.5.4' . '\n' . "\"\n";
$template .= '"X-Poedit-KeywordsList: _' . '\n' . "\"\n";
$template .= '"X-Poedit-Basepath: ' . $relativePath . '\n' . "\"\n";
$template .= '"X-Poedit-SourceCharset: ' . $encoding . '\n' . "\"\n";
// Source paths
$sourcePaths = $this->configuration->getSourcesFromDomain($domain);
// Compiled views on paths
if (count($sourcePaths)) {
// View compilation
$this->compileViews($sourcePaths, $domain);
array_push($sourcePaths, $this->getStorageForDomain($domain));
$i = 0;
foreach ($sourcePaths as $sourcePath) {
$template .= '"X-Poedit-SearchPath-' . $i . ': ' . $sourcePath . '\n' . "\"\n";
$i++;
}
}
if ($write) {
// File creation
$file = fopen($path, "w");
$result = fwrite($file, $template);
fclose($file);
return $result;
} else {
// Contents for update
return $template . "\n";
}
}
/**
* Tries to create a directory in $path
*
* @param $path
* @throws Exceptions\FileCreationException
*/
protected function createDirectory($path)
{
if (!mkdir($path)) {
throw new FileCreationException(
"I can't create the directory: $path");
}
}
/**
* Adds a new locale directory + .po file
*
* @param String $localePath
* @param String $locale
* @throws FileCreationException
*/
public function addLocale($localePath, $locale)
{
$this->createDirectory($localePath);
$gettextPath = $localePath .
DIRECTORY_SEPARATOR .
"LC_MESSAGES";
$this->createDirectory($gettextPath);
// File generation for each domain
foreach ($this->configuration->getAllDomains() as $domain) {
$localePOPath = implode(array(
$localePath,
"LC_MESSAGES",
$domain . ".po",
), DIRECTORY_SEPARATOR);
if (!$this->createPOFile($localePOPath, $locale, $domain)) {
throw new FileCreationException(
"I can't create the file: $localePOPath");
}
}
}
/**
* Update the .po file headers (mainly source-file paths) by domain
*
* @param String $localePath
* @param String $locale
* @param String $domain
* @throws LocaleFileNotFoundException
* @return Boolean
*/
public function updateLocale($localePath, $locale, $domain)
{
$localePOPath = implode(array(
$localePath,
"LC_MESSAGES",
$domain . ".po",
), DIRECTORY_SEPARATOR);
if (!file_exists($localePOPath) ||
!$localeContents = file_get_contents($localePOPath)
) {
throw new LocaleFileNotFoundException(
"I can't read $localePOPath verify your locale structure");
}
$newHeader = $this->createPOFile($localePOPath, $locale, $domain, false);
// Header replacement
$localeContents = preg_replace('/^([^#])+:?/', $newHeader, $localeContents);
if (!file_put_contents($localePOPath, $localeContents)) {
throw new LocaleFileNotFoundException("I can't write on $localePOPath");
}
return true;
}
/**
* Return the relative path from a file or directory to another
*
* @param String $from
* @param String $to
* @return String $path
* @author Laurent Goussard
**/
public function getRelativePath($from, $to)
{
// some compatibility fixes for Windows paths
$from = is_dir($from) ? rtrim($from, '\/') . '/' : $from;
$to = is_dir($to) ? rtrim($to, '\/') . '/' : $to;
$from = str_replace('\\', '/', $from);
$to = str_replace('\\', '/', $to);
$from = explode('/', $from);
$to = explode('/', $to);
$relPath = $to;
foreach($from as $depth => $dir) {
// find first non-matching dir
if($dir === $to[$depth]) {
// ignore this directory
array_shift($relPath);
} else {
// get number of remaining dirs to $from
$remaining = count($from) - $depth;
if($remaining > 1) {
// add traversals up to first matching dir
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
} else {
$relPath[0] = './' . $relPath[0];
}
}
}
return implode('/', $relPath);
}
/**
* Checks the needed directories. Optionally checks
* each locale directory, if $checkLocales is true.
*
* @param bool $checkLocales
* @return bool
* @throws Exceptions\DirectoryNotFoundException
*/
public function checkDirectoryStructure($checkLocales = false)
{
// Application base path
if (!file_exists($this->basePath)) {
throw new Exceptions\DirectoryNotFoundException(
"Missing root path directory: " . $this->basePath .
", check the 'base-path' key in your configuration."
);
}
// Domain path
$domainPath = $this->getDomainPath();
// Translation files domain path
if (!file_exists($domainPath)) {
throw new Exceptions\DirectoryNotFoundException(
"Missing base required directory: $domainPath" .
"<br>Remember run <b>artisan gettext:create</b> for first time."
);
}
if ($checkLocales) {
foreach ($this->configuration->getSupportedLocales() as $locale) {
// Default locale is not needed
if ($locale == $this->configuration->getLocale()) {
continue;
}
$localePath = $this->getDomainPath($locale);
if (!file_exists($localePath)) {
$hint = "<br>May be you forget run <b>artisan gettext:update</b>?";
throw new Exceptions\DirectoryNotFoundException(
"Missing locale required directory: $localePath" . $hint);
}
}
}
return true;
}
/**
* Creates the localization directories and files, by domain
* Returns an array with all created paths
*
* @return Array paths
*/
public function generateLocales()
{
// Application base path
$this->createDirectory($this->getDomainPath());
$localePaths = array();
// Locale directories
foreach ($this->configuration->getSupportedLocales() as $locale) {
// We don't want a locale folder for the default language
if ($locale == $this->configuration->getLocale()) {
continue;
}
$localePath = $this->getDomainPath($locale);
if (!file_exists($localePath)) {
// Locale directory is created
$this->addLocale($localePath, $locale);
array_push($localePaths, $localePath);
}
}
return $localePaths;
}
/**
* Gets the package configuration model.
*
* @return Config
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* Sets the Package configuration model.
*
* @param Config $configuration the configuration
* @return self
*/
public function setConfiguration(Config $configuration)
{
$this->configuration = $configuration;
return $this;
}
/**
* Gets the File system base path
* All paths will be relative to this.
*
* @return String
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Sets the File system base path
* All paths will be relative to this.
*
* @param String $basePath the base path
* @return self
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
return $this;
}
/**
* Gets the Storage path for file generation.
*
* @return String
*/
public function getStoragePath()
{
return $this->storagePath;
}
/**
* Sets the Storage path for file generation.
*
* @param String $storagePath the storage path
* @return self
*/
public function setStoragePath($storagePath)
{
$this->storagePath = $storagePath;
return $this;
}
/**
* Returns the full path for a domain storage directory
*
* @param String $domain
* @return String
*/
public function getStorageForDomain($domain)
{
$domainPath = $this->storagePath .
DIRECTORY_SEPARATOR .
$this->storageContainer .
DIRECTORY_SEPARATOR .
$domain;
return $this->getRelativePath($this->basePath, $domainPath);
}
/**
* Removes the directory contents recursively
*
* @param String $path
* @return void
*/
public static function clearDirectory($path)
{
if (!file_exists($path)) {
return;
}
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}
rmdir($path);
}
}