From 3f25b62ab661848306531e360edcb64abfbef7fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 10 Mar 2020 22:11:31 +0100 Subject: [PATCH] [V1] Update 'migrations:export' message (#3470) * Update command message * Export migrations command improvements --- app/Console/Commands/ExportMigrations.php | 78 +++++++++++++---------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/app/Console/Commands/ExportMigrations.php b/app/Console/Commands/ExportMigrations.php index ca9d7d4d560d..c05c4fb02522 100644 --- a/app/Console/Commands/ExportMigrations.php +++ b/app/Console/Commands/ExportMigrations.php @@ -17,14 +17,14 @@ class ExportMigrations extends Command * * @var string */ - protected $signature = 'migrations:export'; + protected $signature = 'migrations:export {--user=}'; /** * The console command description. * * @var string */ - protected $description = 'Export account migrations to folder'; + protected $description = 'Export account migrations to folder.'; /** * Create a new command instance. @@ -43,39 +43,53 @@ class ExportMigrations extends Command */ public function handle() { - foreach (User::all() as $user) { - $this->account = $user->account; + $this->info('Note: Migrations will be stored inside of (storage/migrations) folder.'); - $date = date('Y-m-d'); - $accountKey = $this->account->account_key; + if($this->option('user')) { + $record = User::findOrFail($this->option('user')); + return $this->export($record); + } - $output = fopen('php://output', 'w') or Utils::fatalError(); + $users = User::all(); - $fileName = "{$accountKey}-{$date}-invoiceninja"; - - $data = [ - 'company' => $this->getCompany(), - 'users' => $this->getUsers(), - 'tax_rates' => $this->getTaxRates(), - 'clients' => $this->getClients(), - 'products' => $this->getProducts(), - 'invoices' => $this->getInvoices(), - 'quotes' => $this->getQuotes(), - 'payments' => array_merge($this->getPayments(), $this->getCredits()), - 'credits' => $this->getCreditsNotes(), - 'documents' => $this->getDocuments(), - 'company_gateways' => $this->getCompanyGateways(), - 'client_gateway_tokens' => $this->getClientGatewayTokens(), - ]; - - $file = storage_path("migrations/{$fileName}.zip"); - - $zip = new \ZipArchive(); - $zip->open($file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); - $zip->addFromString('migration.json', json_encode($data, JSON_PRETTY_PRINT)); - $zip->close(); - - $this->info('User with id #'. $user->id . ' exported.'); + foreach($users as $user) { + $this->export($user); } } + + private function export($user) + { + $this->account = $user->account; + + $date = date('Y-m-d'); + $accountKey = $this->account->account_key; + + $output = fopen('php://output', 'w') or Utils::fatalError(); + + $fileName = "{$accountKey}-{$date}-invoiceninja"; + + $data = [ + 'company' => $this->getCompany(), + 'users' => $this->getUsers(), + 'tax_rates' => $this->getTaxRates(), + 'clients' => $this->getClients(), + 'products' => $this->getProducts(), + 'invoices' => $this->getInvoices(), + 'quotes' => $this->getQuotes(), + 'payments' => array_merge($this->getPayments(), $this->getCredits()), + 'credits' => $this->getCreditsNotes(), + 'documents' => $this->getDocuments(), + 'company_gateways' => $this->getCompanyGateways(), + 'client_gateway_tokens' => $this->getClientGatewayTokens(), + ]; + + $file = storage_path("migrations/{$fileName}.zip"); + + $zip = new \ZipArchive(); + $zip->open($file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); + $zip->addFromString('migration.json', json_encode($data, JSON_PRETTY_PRINT)); + $zip->close(); + + $this->info('User with id #' . $user->id . ' exported.'); + } }