mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
feat: add cart:make:driver command
This commit is contained in:
@@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Wearepixel\Cart\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class MakeDriverCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'cart:make:driver {name : The class name of the custom cart driver}';
|
||||||
|
|
||||||
|
protected $description = 'Create a new CartDriver implementation in app/Cart/Drivers.';
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$name = $this->argument('name');
|
||||||
|
$directory = app_path('Cart/Drivers');
|
||||||
|
$path = "{$directory}/{$name}.php";
|
||||||
|
|
||||||
|
if (file_exists($path)) {
|
||||||
|
$this->error("Driver already exists: app/Cart/Drivers/{$name}.php");
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_dir($directory)) {
|
||||||
|
mkdir($directory, 0755, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
file_put_contents($path, static::stub($name));
|
||||||
|
$this->info("Driver created: app/Cart/Drivers/{$name}.php");
|
||||||
|
$this->line(" Register it in config/cart.php under <comment>drivers</comment>.");
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function stub(string $className): string
|
||||||
|
{
|
||||||
|
return <<<PHP
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Cart\Drivers;
|
||||||
|
|
||||||
|
use Wearepixel\Cart\Drivers\Contracts\CartDriver;
|
||||||
|
|
||||||
|
class {$className} implements CartDriver
|
||||||
|
{
|
||||||
|
private string \$sessionKey;
|
||||||
|
|
||||||
|
public function __construct(string \$sessionKey)
|
||||||
|
{
|
||||||
|
\$this->sessionKey = \$sessionKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSessionKey(): string
|
||||||
|
{
|
||||||
|
return \$this->sessionKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSessionKey(string \$sessionKey): void
|
||||||
|
{
|
||||||
|
\$this->sessionKey = \$sessionKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDriverName(): string
|
||||||
|
{
|
||||||
|
return 'custom';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItems(): array
|
||||||
|
{
|
||||||
|
// TODO: retrieve items from your storage backend
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function putItems(array \$items): void
|
||||||
|
{
|
||||||
|
// TODO: persist items to your storage backend
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getConditions(): array
|
||||||
|
{
|
||||||
|
// TODO: retrieve conditions from your storage backend
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function putConditions(array \$conditions): void
|
||||||
|
{
|
||||||
|
// TODO: persist conditions to your storage backend
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clearItems(): void
|
||||||
|
{
|
||||||
|
// TODO: remove only items from your storage backend
|
||||||
|
}
|
||||||
|
|
||||||
|
public function flush(): void
|
||||||
|
{
|
||||||
|
// TODO: remove both items and conditions from your storage backend
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSessionModel(): mixed
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PHP;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Wearepixel\Cart\Commands\MakeDriverCommand;
|
||||||
|
|
||||||
|
describe('MakeDriverCommand', function () {
|
||||||
|
test('stub contains the given class name', function () {
|
||||||
|
$stub = MakeDriverCommand::stub('RedisDriver');
|
||||||
|
expect($stub)->toContain('class RedisDriver implements CartDriver');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stub uses the App\\Cart\\Drivers namespace', function () {
|
||||||
|
$stub = MakeDriverCommand::stub('RedisDriver');
|
||||||
|
expect($stub)->toContain('namespace App\\Cart\\Drivers');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stub imports the CartDriver interface', function () {
|
||||||
|
$stub = MakeDriverCommand::stub('RedisDriver');
|
||||||
|
expect($stub)->toContain('use Wearepixel\\Cart\\Drivers\\Contracts\\CartDriver');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stub declares all CartDriver interface methods', function () {
|
||||||
|
$stub = MakeDriverCommand::stub('RedisDriver');
|
||||||
|
expect($stub)->toContain('public function getSessionKey(): string');
|
||||||
|
expect($stub)->toContain('public function setSessionKey(string $sessionKey): void');
|
||||||
|
expect($stub)->toContain('public function getDriverName(): string');
|
||||||
|
expect($stub)->toContain('public function getItems(): array');
|
||||||
|
expect($stub)->toContain('public function putItems(array $items): void');
|
||||||
|
expect($stub)->toContain('public function getConditions(): array');
|
||||||
|
expect($stub)->toContain('public function putConditions(array $conditions): void');
|
||||||
|
expect($stub)->toContain('public function clearItems(): void');
|
||||||
|
expect($stub)->toContain('public function flush(): void');
|
||||||
|
expect($stub)->toContain('public function getSessionModel(): mixed');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user