Skip to content

Instantly share code, notes, and snippets.

@bsadnu
Created October 14, 2016 07:44
Show Gist options
  • Save bsadnu/65a09e2e94a0282260e7e3aac4b6e2cd to your computer and use it in GitHub Desktop.
Save bsadnu/65a09e2e94a0282260e7e3aac4b6e2cd to your computer and use it in GitHub Desktop.
Extends Appzcoder\CrudGenerator generate lang command. Particulary disable capital first letter in lang filenames.
<?php
namespace App\Console\Commands;
use Appzcoder\CrudGenerator\Commands\CrudLangCommand;
use File;
/**
* Extends Appzcoder\CrudGenerator generate lang command. Particulary disable capital first letter in lang filenames.
*/
class CrudLangCommandCustom extends CrudLangCommand
{
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->crudName = lcfirst($this->argument('name'));
$this->locales = explode(',', $this->option('locales'));
$fields = $this->option('fields');
$fieldsArray = explode(';', $fields);
$this->formFields = array();
if ($fields) {
$x = 0;
foreach ($fieldsArray as $item) {
$itemArray = explode('#', $item);
$this->formFields[$x]['name'] = trim($itemArray[0]);
$this->formFields[$x]['type'] = trim($itemArray[1]);
$this->formFields[$x]['required'] = (isset($itemArray[2]) && (trim($itemArray[2]) == 'req' || trim($itemArray[2]) == 'required')) ? true : false;
$x++;
}
}
foreach ($this->locales as $locale) {
$locale = trim($locale);
$path = config('view.paths')[0] . '/../lang/' . $locale . '/';
//create directory for locale
if (!File::isDirectory($path)) {
File::makeDirectory($path, 0755, true);
}
$langFile = $this->viewDirectoryPath . 'lang.stub';
$newLangFile = $path . $this->crudName . '.php';
if (!File::copy($langFile, $newLangFile)) {
echo "failed to copy $langFile...\n";
} else {
$this->templateVars($newLangFile);
}
$this->info('Lang [' . $locale . '] created successfully.');
}
}
/**
* Translate form's fields.
*
* @param string $newLangFile
*
* @return void
*/
private function templateVars($newLangFile)
{
$messages = [];
foreach ($this->formFields as $field) {
$index = $field['name'];
$text = ucwords(strtolower(str_replace('_', ' ', $index)));
$messages[] = "'$index' => '$text'";
}
File::put($newLangFile, str_replace('%%messages%%', implode(",\n", $messages), File::get($newLangFile)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment