1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\Generators\Services; |
4
|
|
|
|
5
|
|
|
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract; |
6
|
|
|
use Webfactor\Laravel\Generators\Contracts\ServiceInterface; |
7
|
|
|
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray; |
8
|
|
|
|
9
|
|
|
class LanguageService extends ServiceAbstract implements ServiceInterface |
10
|
|
|
{ |
11
|
|
|
private $languageFile; |
12
|
|
|
|
13
|
|
|
private $translation = []; |
14
|
|
|
|
15
|
|
|
private $currentLanguage; |
16
|
|
|
|
17
|
|
|
public function call() |
18
|
|
|
{ |
19
|
|
|
$this->currentLanguage = \Lang::locale(); |
20
|
|
|
$this->relativeToBasePath = 'resources/lang/' . $this->currentLanguage; |
21
|
|
|
$this->languageFile = $this->getFilePath(); |
22
|
|
|
|
23
|
|
|
$this->writeFile($this->getName($this->command->entity)); |
24
|
|
|
|
25
|
|
|
$this->addLatestFileToIdeStack(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getName(string $entity): string |
29
|
|
|
{ |
30
|
|
|
return snake_case($entity); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Build the class with the given name. |
35
|
|
|
* |
36
|
|
|
* @param string $name |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
private function writeFile($name) |
41
|
|
|
{ |
42
|
|
|
if ($this->filesystem->exists($this->languageFile)) { |
43
|
|
|
$this->translation = include $this->languageFile; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->translation = array_add($this->translation, $name, [ |
47
|
|
|
'singular' => ucfirst($name), |
48
|
|
|
'plural' => ucfirst(str_plural($name)), |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
return $this->filesystem->put($this->languageFile, $this->getTranslationFileContent()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function getFilePath() |
55
|
|
|
{ |
56
|
|
|
return resource_path('lang/' . $this->currentLanguage) . '/models.php'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function getTranslationFileContent() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$content = ShortSyntaxArray::parse($this->translation); |
62
|
|
|
|
63
|
|
|
return <<<FILE |
64
|
|
|
<?php |
65
|
|
|
|
66
|
|
|
return {$content}; |
67
|
|
|
FILE; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.