Page MenuHomePhorge

ObjectDeleteCommand.php
No OneTemporary

Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None

ObjectDeleteCommand.php

<?php
namespace App\Console;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Schema;
/**
* This abstract class provides a means to treat objects in our model using CRUD.
*/
abstract class ObjectDeleteCommand extends ObjectCommand
{
public function __construct()
{
$this->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);
if (in_array(SoftDeletes::class, $classes)) {
$this->signature .= " {--with-deleted : Consider 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()
{
$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) {
if ($object->deleted_at) {
$object->forceDelete();
} else {
$object->delete();
}
}
);
} else {
if ($object->deleted_at) {
$object->forceDelete();
} else {
$object->delete();
}
}
}
}

File Metadata

Mime Type
text/x-php
Expires
Fri, Apr 24, 9:48 AM (1 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18804393
Default Alt Text
ObjectDeleteCommand.php (2 KB)

Event Timeline