diff --git a/bin/doctum b/bin/doctum index 569d076b..1aab12a7 100755 --- a/bin/doctum +++ b/bin/doctum @@ -1,15 +1,15 @@ #!/bin/bash cwd=$(dirname $0) pushd ${cwd}/../src/ -rm -rf cache/store/ +rm -rf ../docs/build/main/ cache/store/ php -dmemory_limit=-1 \ vendor/bin/doctum.php \ update \ doctum.config.php \ -v popd diff --git a/src/app/Console/Command.php b/src/app/Console/Command.php index 2036fb70..63808a8e 100644 --- a/src/app/Console/Command.php +++ b/src/app/Console/Command.php @@ -1,104 +1,112 @@ 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) { - $object = $objectClass::find($objectIdOrTitle); + if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { + $object = $objectClass::withTrashed()->find($objectIdOrTitle); + } else { + $object = $objectClass::find($objectIdOrTitle); + } if (!$object && !empty($objectTitle)) { - $object = $objectClass::where($objectTitle, $objectIdOrTitle)->first(); + 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); } /** * 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/Commands/DiscountsCommand.php b/src/app/Console/Commands/DiscountsCommand.php new file mode 100644 index 00000000..ad469186 --- /dev/null +++ b/src/app/Console/Commands/DiscountsCommand.php @@ -0,0 +1,12 @@ +properties = $this->getProperties(); + + $entitleable = call_user_func_array( + [$this->properties['entitleable_type'], 'find'], + [$this->properties['entitleable_id']] + ); + + if (!$entitleable) { + $this->error("No such {$this->properties['entitleable_type']}"); + return 1; + } + + if (!array_key_exists('entitleable_id', $this->properties)) { + $this->error("Specify --entitleable_id"); + } + + if (array_key_exists('sku_id', $this->properties)) { + $sku = \App\Sku::find($this->properties['sku_id']); + + if (!$sku) { + $this->error("No such SKU {$this->properties['sku_id']}"); + return 1; + } + + if ($this->properties['cost'] == null) { + $this->properties['cost'] = $sku->cost; + } + } + + if (array_key_exists('wallet_id', $this->properties)) { + $wallet = \App\Wallet::find($this->properties['wallet_id']); + + if (!$wallet) { + $this->error("No such wallet {$this->properties['wallet_id']}"); + return 1; + } + } + + parent::handle(); + } +} diff --git a/src/app/Console/Commands/Scalpel/Entitlement/ReadCommand.php b/src/app/Console/Commands/Scalpel/Entitlement/ReadCommand.php new file mode 100644 index 00000000..fd6437a7 --- /dev/null +++ b/src/app/Console/Commands/Scalpel/Entitlement/ReadCommand.php @@ -0,0 +1,13 @@ +description = "Create a {$this->objectName}"; + $this->signature = sprintf( + "%s%s:create", + $this->commandPrefix ? $this->commandPrefix . ":" : "", + $this->objectName + ); + + $class = new $this->objectClass(); + + foreach ($class->getFillable() as $fillable) { + $this->signature .= " {--{$fillable}=}"; + } + + parent::__construct(); + } + + public function getProperties() + { + if (!empty($this->properties)) { + return $this->properties; + } + + $class = new $this->objectClass(); + + $this->properties = []; + + foreach ($class->getFillable() as $fillable) { + $this->properties[$fillable] = $this->option($fillable); + } + + return $this->properties; + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $this->getProperties(); + + $class = new $this->objectClass(); + + $object = $this->objectClass::create($this->properties); + + if ($object) { + $this->info($object->{$class->getKeyName()}); + } else { + $this->error("Object could not be created."); + } + + return $object; + } +} diff --git a/src/app/Console/ObjectListCommand.php b/src/app/Console/ObjectListCommand.php new file mode 100644 index 00000000..21d13d8a --- /dev/null +++ b/src/app/Console/ObjectListCommand.php @@ -0,0 +1,56 @@ +description = "List {$this->objectName}s"; + + $this->signature = $this->commandPrefix ? $this->commandPrefix . ":" : ""; + $this->signature .= "{$this->objectName}s"; + + $classes = class_uses_recursive($this->objectClass); + + if (in_array(SoftDeletes::class, $classes)) { + $this->signature .= " {--with-deleted : Include deleted {$this->objectName}s}"; + } + + $this->signature .= " {--attr=* : Attributes other than the primary unique key to include}"; + + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $classes = class_uses_recursive($this->objectClass); + + if (in_array(SoftDeletes::class, $classes) && $this->option('with-deleted')) { + $objects = $this->objectClass::withTrashed(); + } else { + $objects = new $this->objectClass(); + } + + $objects->each( + function ($object) { + if ($object->deleted_at) { + $this->info("{$this->toString($object)} (deleted at {$object->deleted_at}"); + } else { + $this->info("{$this->toString($object)}"); + } + } + ); + } +} diff --git a/src/app/Console/ObjectReadCommand.php b/src/app/Console/ObjectReadCommand.php new file mode 100644 index 00000000..bbccae63 --- /dev/null +++ b/src/app/Console/ObjectReadCommand.php @@ -0,0 +1,44 @@ +description = "Read a {$this->objectName}"; + + $this->signature = sprintf( + "%s%s:read {%s}", + $this->commandPrefix ? $this->commandPrefix . ":" : "", + $this->objectName, + $this->objectName + ); + + $this->signature .= " {--attr=* : Attributes other than the primary unique key to include}"; + + parent::__construct(); + } + + /** + * 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; + } + + $this->info($this->toString($object)); + } +} diff --git a/src/app/Console/ObjectRelationListCommand.php b/src/app/Console/ObjectRelationListCommand.php new file mode 100644 index 00000000..786dede6 --- /dev/null +++ b/src/app/Console/ObjectRelationListCommand.php @@ -0,0 +1,90 @@ +description = "List {$this->objectRelation} for a {$this->objectName}"; + + $this->signature = sprintf( + "%s%s:%s {%s}", + $this->commandPrefix ? $this->commandPrefix . ":" : "", + $this->objectName, + $this->objectRelation, + $this->objectName + ); + + $this->signature .= " {--attr=* : Attributes other than the primary unique key to include}"; + + parent::__construct(); + } + + /** + * 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; + } + + if (method_exists($object, $this->objectRelation)) { + $result = call_user_func([$object, $this->objectRelation]); + } elseif (property_exists($object, $this->objectRelation)) { + $result = $object->{"{$this->objectRelation}"}; + } else { + $this->error("No such relation {$this->objectRelation}"); + return 1; + } + + if ($result instanceof \Illuminate\Database\Eloquent\Collection) { + $result->each( + function ($entry) { + $this->info($this->toString($entry)); + } + ); + } elseif ($result instanceof \Illuminate\Database\Eloquent\Relations\Relation) { + $result->each( + function ($entry) { + $this->info($this->toString($entry)); + } + ); + } elseif (is_array($result)) { + foreach ($result as $entry) { + $this->info($this->toString($entry)); + } + } else { + $this->info($this->toString($result)); + } + } +} diff --git a/src/app/Console/ObjectUpdateCommand.php b/src/app/Console/ObjectUpdateCommand.php new file mode 100644 index 00000000..f8245bd8 --- /dev/null +++ b/src/app/Console/ObjectUpdateCommand.php @@ -0,0 +1,96 @@ +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]); + } +}