From e0d8ff4f7b04f07c1ce83c2e1b2f22bd2d069eba Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 31 Oct 2022 00:36:47 +0330 Subject: [PATCH 1/2] feat: add `shield:publish` CLI Command --- src/Commands/Publish.php | 202 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 src/Commands/Publish.php diff --git a/src/Commands/Publish.php b/src/Commands/Publish.php new file mode 100644 index 000000000..dd0429c18 --- /dev/null +++ b/src/Commands/Publish.php @@ -0,0 +1,202 @@ +]'; + + /** + * the Command's Arguments + * + * @var array + */ + protected $arguments = [ + 'view' => 'If you plan to customize the Shield views.', + 'model' => 'If you plan to customize the UserModel.', + ]; + + protected $defaultViewsPath = VENDORPATH . 'codeigniter4/shield/src/Views'; + private ContentReplacer $replacer; + + /** + * Displays the help for the spark cli script itself. + */ + public function run(array $params): void + { + $this->replacer = new ContentReplacer(); + + if (current($params) === 'view') { + $this->publishViews(); + + exit; + } + + if (current($params) === 'model') { + $this->publishUserModel(); + + exit; + } + + if (! array_key_exists(current($params), $this->arguments)) { + $postedArgument = CLI::promptByKey( + ['Here is the list of your choice:', 'What do you want to do?'], + $this->arguments, + 'required' + ); + + if ($postedArgument === 'view') { + $this->publishViews(); + + exit; + } + + if ($postedArgument === 'model') { + $this->publishUserModel(); + + exit; + } + } + } + + private function publishViews(): void + { + $this->checkShieldIsSetup(); + $this->copyAndPasteViews(); + $this->setCustomViewsInAuth(); + } + + private function checkShieldIsSetup(): void + { + $path = APPPATH . 'Config/Auth.php'; + $cleanPath = clean_path($path); + if (! is_file($path)) { + CLI::error(" Error: Not found file '{$cleanPath}'."); + + exit; + } + } + + private function copyAndPasteViews(): void + { + $copyFrom = $this->defaultViewsPath; + + $pasteTo = APPPATH . 'Views/Shield/'; + + try { + directory_mirror($copyFrom, $pasteTo); + CLI::write(CLI::color(' Copy Views: ', 'green') . 'All files copy to "App/Views/Shield".'); + } catch (Throwable $e) { + CLI::error(' Error.') . 'There was a problem with the copy original views.'; + } + } + + private function setCustomViewsInAuth(): void + { + $path = APPPATH . 'Config/Auth.php'; + + $replaces = [ + '\CodeIgniter\Shield\Views\login' => '\App\Views\Shield\login', + '\CodeIgniter\Shield\Views\register' => '\App\Views\Shield\register', + '\CodeIgniter\Shield\Views\layout' => '\App\Views\Shield\layout', + '\CodeIgniter\Shield\Views\email_2fa_show' => '\App\Views\Shield\email_2fa_show', + '\CodeIgniter\Shield\Views\email_2fa_verify' => '\App\Views\Shield\email_2fa_verify', + '\CodeIgniter\Shield\Views\Email\email_2fa_email' => '\App\Views\Shield\Email\email_2fa_email', + '\CodeIgniter\Shield\Views\Email\email_activate_email' => '\App\Views\Shield\Email\email_activate_email', + '\CodeIgniter\Shield\Views\email_activate_show' => '\App\Views\Shield\email_activate_show', + '\CodeIgniter\Shield\Views\magic_link_form' => '\App\Views\Shield\magic_link_form', + '\CodeIgniter\Shield\Views\magic_link_message' => '\App\Views\Shield\magic_link_message', + '\CodeIgniter\Shield\Views\Email\magic_link_email' => '\App\Views\Shield\Email\magic_link_email', + ]; + + $cleanPath = clean_path($path); + $content = file_get_contents($path); + + $output = $this->replacer->replace($content, $replaces); + + if ($output === $content) { + CLI::write(CLI::color(' Set Views : ', 'green') . 'All views is set in file "App/Config/Auth".'); + + return; + } + + if (write_file($path, $output)) { + CLI::write(CLI::color(' Set New Views: ', 'green') . "We have updated file '{$cleanPath}' for custoum view."); + } else { + CLI::error(" Error updating file '{$cleanPath}'."); + } + } + + private function publishUserModel(): void + { + $content = <<<'TPLMODEL' + allowedFields = array_merge($this->allowedFields, [ + // Add here your custom fields + // 'first_name', + + ]); + } + } + TPLMODEL; + + $modelName = CLI::prompt('Please enter your model name?', null, 'required'); + + $output = $this->replacer->replace($content, ['CustomModel' => $modelName]); + + $path = APPPATH . "Models/{$modelName}.php"; + + $cleanPath = clean_path($path); + + if (write_file($path, $output, 'x')) { + CLI::write(CLI::color(' Done: ', 'green') . "The model was created in path '{$cleanPath}'."); + } else { + CLI::error(" Error created file '{$cleanPath}'."); + } + } +} From f84a4182476783ad4957860e5a7b7f2816e23bed Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 31 Oct 2022 01:00:12 +0330 Subject: [PATCH 2/2] fix: rector analysis --- src/Commands/Publish.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Commands/Publish.php b/src/Commands/Publish.php index dd0429c18..0a2a6afe9 100644 --- a/src/Commands/Publish.php +++ b/src/Commands/Publish.php @@ -7,6 +7,7 @@ use CodeIgniter\CLI\BaseCommand; use CodeIgniter\CLI\CLI; use CodeIgniter\Shield\Commands\Setup\ContentReplacer; +use Throwable; class Publish extends BaseCommand { @@ -120,7 +121,7 @@ private function copyAndPasteViews(): void directory_mirror($copyFrom, $pasteTo); CLI::write(CLI::color(' Copy Views: ', 'green') . 'All files copy to "App/Views/Shield".'); } catch (Throwable $e) { - CLI::error(' Error.') . 'There was a problem with the copy original views.'; + CLI::error(' Error. There was a problem with the copy original views.'); } }