feat: add cart:make:shipping command

This commit is contained in:
Joel Male
2026-05-27 21:43:34 +10:00
parent b8140166c9
commit 76fba35643
2 changed files with 92 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Wearepixel\Cart\Commands;
use Illuminate\Console\Command;
class MakeShippingCommand extends Command
{
protected $signature = 'cart:make:shipping {name : The class name of the shipping rate}';
protected $description = 'Create a new ShippingRate class in app/Cart/Shipping.';
public function handle(): int
{
$name = $this->argument('name');
$directory = app_path('Cart/Shipping');
$path = "{$directory}/{$name}.php";
if (file_exists($path)) {
$this->error("Shipping rate already exists: app/Cart/Shipping/{$name}.php");
return self::FAILURE;
}
if (! is_dir($directory)) {
mkdir($directory, 0755, true);
}
file_put_contents($path, static::stub($name));
$this->info("Shipping rate created: app/Cart/Shipping/{$name}.php");
return self::SUCCESS;
}
public static function stub(string $className): string
{
return <<<PHP
<?php
declare(strict_types=1);
namespace App\Cart\Shipping;
use Wearepixel\Cart\Shipping\ShippingRate;
class {$className} extends ShippingRate
{
protected string \$name = 'Standard Shipping';
protected string \$value = '+10';
protected string \$target = 'total';
public function isApplicable(): bool
{
return true;
}
}
PHP;
}
}