diff --git a/src/app/Console/Command.php b/src/app/Console/Command.php index ee3624b5..00f2c204 100644 --- a/src/app/Console/Command.php +++ b/src/app/Console/Command.php @@ -1,112 +1,148 @@ getObject(\App\Domain::class, $domain, 'namespace'); } /** * Find an object. * * @param string $objectClass The name of the class * @param string $objectIdOrTitle The name of a database field to match. * @param string|null $objectTitle An additional database field to match. * * @return mixed */ public function getObject($objectClass, $objectIdOrTitle, $objectTitle = null) { if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { $object = $objectClass::withTrashed()->find($objectIdOrTitle); } else { $object = $objectClass::find($objectIdOrTitle); } if (!$object && !empty($objectTitle)) { if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { $object = $objectClass::withTrashed()->where($objectTitle, $objectIdOrTitle)->first(); } else { $object = $objectClass::where($objectTitle, $objectIdOrTitle)->first(); } } return $object; } /** * Find the user. * * @param string $user User ID or email * * @return \App\User|null */ public function getUser($user) { return $this->getObject(\App\User::class, $user, 'email'); } /** * Find the wallet. * * @param string $wallet Wallet ID * * @return \App\Wallet|null */ public function getWallet($wallet) { return $this->getObject(\App\Wallet::class, $wallet, null); } + public function handle() + { + if ($this->dangerous) { + $this->warn( + "This command is a dangerous scalpel command with potentially significant unintended consequences" + ); + + $confirmation = $this->confirm("Are you sure you understand what's about to happen?"); + + if (!$confirmation) { + $this->info("Better safe than sorry."); + return false; + } + + $this->info("VĂ¡monos!"); + } + + return true; + } + /** * Return a string for output, with any additional attributes specified as well. * * @param mixed $entry An object * * @return string */ protected function toString($entry) { /** * Haven't figured out yet, how to test if this command implements an option for additional * attributes. if (!in_array('attr', $this->options())) { return $entry->{$entry->getKeyName()}; } */ $str = [ $entry->{$entry->getKeyName()} ]; foreach ($this->option('attr') as $attr) { if ($attr == $entry->getKeyName()) { $this->warn("Specifying {$attr} is not useful."); continue; } if (!array_key_exists($attr, $entry->toArray())) { $this->error("Attribute {$attr} isn't available"); continue; } if (is_numeric($entry->{$attr})) { $str[] = $entry->{$attr}; } else { $str[] = !empty($entry->{$attr}) ? $entry->{$attr} : "null"; } } return implode(" ", $str); } } diff --git a/src/app/Console/ObjectDeleteCommand.php b/src/app/Console/ObjectDeleteCommand.php index 7a7e9026..3e228480 100644 --- a/src/app/Console/ObjectDeleteCommand.php +++ b/src/app/Console/ObjectDeleteCommand.php @@ -1,95 +1,95 @@ description = "Delete a {$this->objectName}"; $this->signature = sprintf( "%s%s:delete {%s}", $this->commandPrefix ? $this->commandPrefix . ":" : "", $this->objectName, $this->objectName ); $class = new $this->objectClass(); try { foreach (Schema::getColumnListing($class->getTable()) as $column) { if ($column == "id") { continue; } $this->signature .= " {--{$column}=}"; } } catch (\Exception $e) { \Log::error("Could not extract options: {$e->getMessage()}"); } $classes = class_uses_recursive($this->objectClass); parent::__construct(); } public function getProperties() { if (!empty($this->properties)) { return $this->properties; } $class = new $this->objectClass(); $this->properties = []; foreach (Schema::getColumnListing($class->getTable()) as $column) { if ($column == "id") { continue; } if (($value = $this->option($column)) !== null) { $this->properties[$column] = $value; } } return $this->properties; } /** * Execute the console command. * * @return mixed */ public function handle() { $result = parent::handle(); if (!$result) { return 1; } $argument = $this->argument($this->objectName); $object = $this->getObject($this->objectClass, $argument, $this->objectTitle); if (!$object) { $this->error("No such {$this->objectName} {$argument}"); return 1; } if ($this->commandPrefix == 'scalpel') { $this->objectClass::withoutEvents( function () use ($object) { $object->delete(); } ); } } } diff --git a/src/app/Console/ObjectUpdateCommand.php b/src/app/Console/ObjectUpdateCommand.php index 5633b753..b046a097 100644 --- a/src/app/Console/ObjectUpdateCommand.php +++ b/src/app/Console/ObjectUpdateCommand.php @@ -1,99 +1,107 @@ description = "Update a {$this->objectName}"; $this->signature = sprintf( "%s%s:update {%s}", $this->commandPrefix ? $this->commandPrefix . ":" : "", $this->objectName, $this->objectName ); $class = new $this->objectClass(); try { foreach (Schema::getColumnListing($class->getTable()) as $column) { if ($column == "id") { continue; } $this->signature .= " {--{$column}=}"; } } catch (\Exception $e) { \Log::error("Could not extract options: {$e->getMessage()}"); } $classes = class_uses_recursive($this->objectClass); if (in_array(SoftDeletes::class, $classes)) { $this->signature .= " {--with-deleted : Include deleted {$this->objectName}s}"; } parent::__construct(); } public function getProperties() { if (!empty($this->properties)) { return $this->properties; } $class = new $this->objectClass(); $this->properties = []; foreach (Schema::getColumnListing($class->getTable()) as $column) { if ($column == "id") { continue; } if (($value = $this->option($column)) !== null) { $this->properties[$column] = $value; } } return $this->properties; } /** * Execute the console command. * * @return mixed */ public function handle() { $argument = $this->argument($this->objectName); $object = $this->getObject($this->objectClass, $argument, $this->objectTitle); if (!$object) { $this->error("No such {$this->objectName} {$argument}"); return 1; } foreach ($this->getProperties() as $property => $value) { if ($property == "deleted_at" && $value == "null") { $value = null; } $object->{$property} = $value; } $object->timestamps = false; - $object->save(['timestamps' => false]); - $this->cacheRefresh($object); + + if ($this->commandPrefix == 'scalpel') { + $this->objectClass::withoutEvents( + function () use ($object) { + $object->save(); + } + ); + } else { + $object->save(); + } } } diff --git a/src/app/Console/ScalpelCommand.php b/src/app/Console/ScalpelCommand.php deleted file mode 100644 index 96cdfce3..00000000 --- a/src/app/Console/ScalpelCommand.php +++ /dev/null @@ -1,26 +0,0 @@ -dangerous) { - $this->warn( - "This command is a dangerous scalpel command with potentially significant unintended consequences" - ); - - $confirmation = $this->confirm("Are you sure you understand what's about to happen?"); - - if (!$confirmation) { - $this->info("Better safe than sorry."); - return false; - } - - $this->info("VĂ¡monos!"); - } - - return true; - } -}