|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\ClassVariance\Commands; |
| 6 | + |
| 7 | +use Tempest\Console\ConsoleCommand; |
| 8 | +use Tempest\Console\HasConsole; |
| 9 | +use Tempest\Support\Filesystem; |
| 10 | + |
| 11 | +use function Tempest\root_path; |
| 12 | +use function Tempest\src_path; |
| 13 | + |
| 14 | +final class PublishConfigCommand |
| 15 | +{ |
| 16 | + use HasConsole; |
| 17 | + |
| 18 | + #[ConsoleCommand( |
| 19 | + name: 'class-variance:publish:config', |
| 20 | + description: 'Publishes a class variance config file to your application', |
| 21 | + )] |
| 22 | + public function __invoke(): void |
| 23 | + { |
| 24 | + $suggested = src_path('class-variance.config.php'); |
| 25 | + $relative = ltrim(str_replace(root_path(), '', $suggested), '/\\'); |
| 26 | + |
| 27 | + $destination = $this->console->ask( |
| 28 | + question: 'Where should the config file be created?', |
| 29 | + default: $relative, |
| 30 | + ); |
| 31 | + |
| 32 | + $destination = str_starts_with($destination, '/') |
| 33 | + ? $destination |
| 34 | + : root_path($destination); |
| 35 | + |
| 36 | + if (Filesystem\is_file($destination)) { |
| 37 | + $overwrite = $this->console->confirm( |
| 38 | + question: "The file <em>{$relative}</em> already exists. Overwrite it?", |
| 39 | + default: false, |
| 40 | + ); |
| 41 | + |
| 42 | + if (! $overwrite) { |
| 43 | + $this->console->info('Aborted.'); |
| 44 | + |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + Filesystem\ensure_directory_exists(dirname($destination)); |
| 50 | + Filesystem\write_file($destination, $this->stub()); |
| 51 | + |
| 52 | + $this->console->success("Config published to <em>{$relative}</em>."); |
| 53 | + } |
| 54 | + |
| 55 | + private function stub(): string |
| 56 | + { |
| 57 | + $stub = <<<'PHP' |
| 58 | + <?php |
| 59 | +
|
| 60 | + declare(strict_types=1); |
| 61 | +
|
| 62 | + use Tempest\ClassVariance\Classmaps\Classmap; |
| 63 | + use Tempest\ClassVariance\Config\TailwindClassVarianceConfig; |
| 64 | +
|
| 65 | + /** |
| 66 | + * Class Variance configuration. |
| 67 | + * |
| 68 | + * This file is auto-discovered by Tempest. The object you return here is |
| 69 | + * bound in the container and picked up by tv() and the TvMergerInitializer. |
| 70 | + * |
| 71 | + * Available options: |
| 72 | + * |
| 73 | + * $prefix — Tailwind class prefix, e.g. 'tw-' (default: '') |
| 74 | + * $separator — Variant separator, e.g. '_' (default: ':') |
| 75 | + * $extend — Additive class-group definitions merged on top of the defaults |
| 76 | + * $override — Class-group definitions that fully replace the matching defaults |
| 77 | + * |
| 78 | + * To add custom utilities you can use $extend: |
| 79 | + * |
| 80 | + * $extend: new Classmap( |
| 81 | + * classGroups: [ |
| 82 | + * 'my-group' => ['my-class', ['my-prefix']], |
| 83 | + * ], |
| 84 | + * conflictingClassGroups: [ |
| 85 | + * 'my-group' => ['another-group'], |
| 86 | + * ], |
| 87 | + * ), |
| 88 | + * |
| 89 | + * For cv() / GenericClassVarianceConfig, swap TailwindClassVarianceConfig |
| 90 | + * for GenericClassVarianceConfig and use its $separator and $classGroups options. |
| 91 | + */ |
| 92 | + return new TailwindClassVarianceConfig( |
| 93 | + prefix: '', |
| 94 | + separator: ':', |
| 95 | + extend: null, |
| 96 | + override: null, |
| 97 | + ); |
| 98 | + PHP; |
| 99 | + |
| 100 | + return str_replace(' ', '', $stub); |
| 101 | + } |
| 102 | +} |
0 commit comments