You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.2 KiB

<?php
namespace Luthfi\CrudGenerator\Generators;
/**
* Lang File Generator Class
*/
class LangFileGenerator extends BaseGenerator
{
/**
* {@inheritDoc}
*/
public function generate()
{
$locale = config('app.locale');
$langPath = $this->makeDirectory(resource_path('lang/'.$locale));
$this->createAppLangFile($langPath);
$this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getContent());
$this->command->info($this->modelNames['lang_name'].' lang files generated.');
}
/**
* {@inheritDoc}
*/
protected function getContent()
{
$locale = config('app.locale');
$langStubPath = __DIR__.'/../stubs/lang-'.$locale.'.stub';
if ($this->files->exists($langStubPath)) {
$stub = $this->files->get($langStubPath);
} else {
$stub = $this->files->get(__DIR__.'/../stubs/lang.stub');
}
$displayModelName = ucwords(str_replace('_', ' ', snake_case($this->modelNames['model_name'])));
$properLangFileContent = str_replace(
$this->modelNames['model_name'],
$displayModelName,
$this->replaceStubString($stub)
);
return $properLangFileContent;
}
/**
* Generate lang/app.php file if it doesn't exists
*
* @param string $langPath Directory path of lang files
* @return void
*/
private function createAppLangFile($langPath)
{
if (!$this->files->exists($langPath.'/app.php')) {
$this->generateFile($langPath.'/app.php', $this->getAppLangFileContent());
$this->command->info('lang/app.php generated.');
}
}
/**
* Get lang/app.php file content
*
* @return string
*/
private function getAppLangFileContent()
{
$locale = config('app.locale');
$langStubPath = __DIR__.'/../stubs/lang-app-'.$locale.'.stub';
if ($this->files->exists($langStubPath)) {
$stub = $this->files->get($langStubPath);
} else {
$stub = $this->files->get(__DIR__.'/../stubs/lang-app.stub');
}
return $stub;
}
}