Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Command.php
<?php | <?php | ||||
namespace App\Console; | namespace App\Console; | ||||
class Command extends \Illuminate\Console\Command | abstract 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. | * Find the domain. | ||||
* | * | ||||
* @param string $domain Domain ID or namespace | * @param string $domain Domain ID or namespace | ||||
* | * | ||||
* @return \App\Domain|null | * @return \App\Domain|null | ||||
*/ | */ | ||||
public function getDomain($domain) | public function getDomain($domain) | ||||
{ | { | ||||
return $this->getObject(\App\Domain::class, $domain, 'namespace'); | return $this->getObject(\App\Domain::class, $domain, 'namespace'); | ||||
} | } | ||||
/** | /** | ||||
* Find an object. | * Find an object. | ||||
* | * | ||||
* @param string $objectClass The name of the class | * @param string $objectClass The name of the class | ||||
* @param string $objectIdOrTitle The name of a database field to match. | * @param string $objectIdOrTitle The name of a database field to match. | ||||
* @param string|null $objectTitle An additional database field to match. | * @param string|null $objectTitle An additional database field to match. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function getObject($objectClass, $objectIdOrTitle, $objectTitle) | public function getObject($objectClass, $objectIdOrTitle, $objectTitle = null) | ||||
{ | { | ||||
if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { | if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { | ||||
$object = $objectClass::withTrashed()->find($objectIdOrTitle); | $object = $objectClass::withTrashed()->find($objectIdOrTitle); | ||||
} else { | } else { | ||||
$object = $objectClass::find($objectIdOrTitle); | $object = $objectClass::find($objectIdOrTitle); | ||||
} | } | ||||
if (!$object && !empty($objectTitle)) { | if (!$object && !empty($objectTitle)) { | ||||
Show All 26 Lines | abstract class Command extends \Illuminate\Console\Command | ||||
* | * | ||||
* @return \App\Wallet|null | * @return \App\Wallet|null | ||||
*/ | */ | ||||
public function getWallet($wallet) | public function getWallet($wallet) | ||||
{ | { | ||||
return $this->getObject(\App\Wallet::class, $wallet, null); | 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. | * Return a string for output, with any additional attributes specified as well. | ||||
* | * | ||||
* @param mixed $entry An object | * @param mixed $entry An object | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
protected function toString($entry) | protected function toString($entry) | ||||
Show All 34 Lines |