Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/ObjectDeleteCommand.php
- This file was copied from src/app/Console/ObjectUpdateCommand.php.
<?php | <?php | ||||
namespace App\Console; | namespace App\Console; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
use Illuminate\Support\Facades\Schema; | use Illuminate\Support\Facades\Schema; | ||||
/** | /** | ||||
* This abstract class provides a means to treat objects in our model using CRUD. | * This abstract class provides a means to treat objects in our model using CRUD. | ||||
*/ | */ | ||||
abstract class ObjectUpdateCommand extends ObjectCommand | abstract class ObjectDeleteCommand extends ObjectCommand | ||||
{ | { | ||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
$this->description = "Update a {$this->objectName}"; | $this->description = "Delete a {$this->objectName}"; | ||||
$this->signature = sprintf( | $this->signature = sprintf( | ||||
"%s%s:update {%s}", | "%s%s:delete {%s}", | ||||
$this->commandPrefix ? $this->commandPrefix . ":" : "", | $this->commandPrefix ? $this->commandPrefix . ":" : "", | ||||
$this->objectName, | $this->objectName, | ||||
$this->objectName | $this->objectName | ||||
); | ); | ||||
$class = new $this->objectClass(); | $class = new $this->objectClass(); | ||||
try { | try { | ||||
foreach (Schema::getColumnListing($class->getTable()) as $column) { | foreach (Schema::getColumnListing($class->getTable()) as $column) { | ||||
if ($column == "id") { | if ($column == "id") { | ||||
continue; | continue; | ||||
} | } | ||||
$this->signature .= " {--{$column}=}"; | $this->signature .= " {--{$column}=}"; | ||||
} | } | ||||
} catch (\Exception $e) { | } catch (\Exception $e) { | ||||
\Log::error("Could not extract options: {$e->getMessage()}"); | \Log::error("Could not extract options: {$e->getMessage()}"); | ||||
} | } | ||||
$classes = class_uses_recursive($this->objectClass); | $classes = class_uses_recursive($this->objectClass); | ||||
if (in_array(SoftDeletes::class, $classes)) { | |||||
$this->signature .= " {--with-deleted : Include deleted {$this->objectName}s}"; | |||||
} | |||||
parent::__construct(); | parent::__construct(); | ||||
} | } | ||||
public function getProperties() | public function getProperties() | ||||
{ | { | ||||
if (!empty($this->properties)) { | if (!empty($this->properties)) { | ||||
return $this->properties; | return $this->properties; | ||||
} | } | ||||
Show All 17 Lines | abstract class ObjectDeleteCommand extends ObjectCommand | ||||
/** | /** | ||||
* Execute the console command. | * Execute the console command. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function handle() | public function handle() | ||||
{ | { | ||||
$result = parent::handle(); | |||||
if (!$result) { | |||||
return 1; | |||||
} | |||||
$argument = $this->argument($this->objectName); | $argument = $this->argument($this->objectName); | ||||
$object = $this->getObject($this->objectClass, $argument, $this->objectTitle); | $object = $this->getObject($this->objectClass, $argument, $this->objectTitle); | ||||
if (!$object) { | if (!$object) { | ||||
$this->error("No such {$this->objectName} {$argument}"); | $this->error("No such {$this->objectName} {$argument}"); | ||||
return 1; | return 1; | ||||
} | } | ||||
foreach ($this->getProperties() as $property => $value) { | if ($this->commandPrefix == 'scalpel') { | ||||
if ($property == "deleted_at" && $value == "null") { | $this->objectClass::withoutEvents( | ||||
$value = null; | function () use ($object) { | ||||
$object->delete(); | |||||
} | } | ||||
); | |||||
$object->{$property} = $value; | |||||
} | } | ||||
$object->timestamps = false; | |||||
$object->save(['timestamps' => false]); | |||||
$this->cacheRefresh($object); | |||||
} | } | ||||
} | } |