|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace InfyOm\Generator\Generators\Scaffold; |
| 4 | + |
| 5 | +use InfyOm\Generator\Common\CommandData; |
| 6 | +use InfyOm\Generator\Generators\BaseGenerator; |
| 7 | +use InfyOm\Generator\Utils\FileUtil; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class JQueryDatatableAssetsGenerator. |
| 11 | + */ |
| 12 | +class JQueryDatatableAssetsGenerator extends BaseGenerator |
| 13 | +{ |
| 14 | + /** @var CommandData */ |
| 15 | + private $commandData; |
| 16 | + |
| 17 | + /** @var string */ |
| 18 | + private $path; |
| 19 | + |
| 20 | + /** @var string */ |
| 21 | + private $fileName; |
| 22 | + |
| 23 | + private $config; |
| 24 | + |
| 25 | + public function __construct(CommandData $commandData) |
| 26 | + { |
| 27 | + $this->commandData = $commandData; |
| 28 | + $this->path = $commandData->config->pathAssets.'js/'; |
| 29 | + $this->config = $this->commandData->config; |
| 30 | + $this->fileName = $this->config->tableName.'.js'; |
| 31 | + } |
| 32 | + |
| 33 | + public function generate() |
| 34 | + { |
| 35 | + $this->generateJquery(); |
| 36 | + } |
| 37 | + |
| 38 | + public function generateJquery() |
| 39 | + { |
| 40 | + $templateName = 'jquery'; |
| 41 | + |
| 42 | + if ($this->commandData->isLocalizedTemplates()) { |
| 43 | + $templateName .= '_locale'; |
| 44 | + } |
| 45 | + |
| 46 | + $columnsCount = 0; |
| 47 | + |
| 48 | + $fields = ''; |
| 49 | + foreach ($this->commandData->fields as $field) { |
| 50 | + if (in_array($field->name, ['id', 'created_at', 'updated_at', 'deleted_at'])) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + $fields .= "{ |
| 55 | + data: '$field->name', |
| 56 | + name: '$field->name' |
| 57 | + },"; |
| 58 | + |
| 59 | + $columnsCount++; |
| 60 | + } |
| 61 | + |
| 62 | + // Publish Datatable JS file |
| 63 | + $templateData = get_template('scaffold.'.$templateName, 'laravel-generator'); |
| 64 | + $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
| 65 | + $templateData = str_replace('$ACTION_COLUMN_COUNT$', $columnsCount, $templateData); |
| 66 | + $templateData = str_replace('$JQUERY_FIELDS$', $fields, $templateData); |
| 67 | + |
| 68 | + $path = $this->path.$this->config->tableName.'/'; |
| 69 | + if (! file_exists($path)) { |
| 70 | + FileUtil::createDirectoryIfNotExist($path); |
| 71 | + } |
| 72 | + file_put_contents($path.$this->fileName, $templateData); |
| 73 | + $this->commandData->commandComment("\n".$this->config->tableName.' assets added.'); |
| 74 | + |
| 75 | + // Publish JS Rendere Template |
| 76 | + $templateName = 'js_renderer_template'; |
| 77 | + $templateData = get_template('scaffold.'.$templateName, 'laravel-generator'); |
| 78 | + $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
| 79 | + |
| 80 | + $path = $this->config->pathViews.'templates/'; |
| 81 | + if (! file_exists($path)) { |
| 82 | + FileUtil::createDirectoryIfNotExist($path); |
| 83 | + } |
| 84 | + |
| 85 | + file_put_contents($path.'templates.php', $templateData); |
| 86 | + $this->commandData->commandComment("\n".'JS Render Templates added.'); |
| 87 | + |
| 88 | + // Publish Webpack mix lines |
| 89 | + $webpackMixContents = file_get_contents(base_path('webpack.mix.js')); |
| 90 | + $templateName = 'webpack_mix_js'; |
| 91 | + $templateData = get_template('scaffold.'.$templateName, 'laravel-generator'); |
| 92 | + $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
| 93 | + $webpackMixContents .= "\n\n".$templateData; |
| 94 | + |
| 95 | + file_put_contents(base_path('webpack.mix.js'), $webpackMixContents); |
| 96 | + $this->commandData->commandComment("\n".$this->commandData->config->mCamelPlural.' webpack.mix.js updated.'); |
| 97 | + } |
| 98 | +} |
0 commit comments