Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
11 KB
Referenced Files
None
Subscribers
None
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 @@
<?php
namespace App\Console;
class Command extends \Illuminate\Console\Command
{
+ /**
+ * Annotate this command as being dangerous for any potential unintended consequences.
+ *
+ * Commands are considered dangerous if;
+ *
+ * * observers are deliberately not triggered, meaning that the deletion of an object model that requires the
+ * associated observer to clean some things up, or charge a wallet or something, are deliberately not triggered,
+ *
+ * * deletion of objects and their relations rely on database foreign keys with obscure cascading,
+ *
+ * * a command will result in the permanent, irrecoverable loss of data.
+ *
+ * @var boolean
+ */
+ protected $dangerous = false;
+
/**
* Find the domain.
*
* @param string $domain Domain ID or namespace
*
* @return \App\Domain|null
*/
public function getDomain($domain)
{
return $this->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 @@
<?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 ScalpelCommand
+abstract class ObjectDeleteCommand extends Command
{
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);
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..c33cb6ba 100644
--- a/src/app/Console/ObjectUpdateCommand.php
+++ b/src/app/Console/ObjectUpdateCommand.php
@@ -1,99 +1,107 @@
<?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 ObjectUpdateCommand extends ObjectCommand
{
public function __construct()
{
$this->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]);
+ if ($this->commandPrefix == 'scalpel') {
+ $this->objectClass::withoutEvents(
+ function () use ($object) {
+ $object->save();
+ }
+ );
+ } else {
+ $object->save();
+ }
$this->cacheRefresh($object);
}
}
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 @@
-<?php
-
-namespace App\Console;
-
-class ScalpelCommand extends ObjectCommand
-{
- 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;
- }
-}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Apr 4, 7:27 AM (1 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18823016
Default Alt Text
(11 KB)

Event Timeline