Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
PublishViewsCommand
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
11
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 publishOption
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 promptForCategory
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 formatPublishableChoices
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 parseChoiceIntoKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Console\Commands;
6
7use Hyde\Console\Concerns\Command;
8use Illuminate\Support\Facades\Artisan;
9
10use function str_replace;
11use function sprintf;
12use function strstr;
13
14/**
15 * Publish the Hyde Blade views.
16 */
17class PublishViewsCommand extends Command
18{
19    /** @var string */
20    protected $signature = 'publish:views {category? : The category to publish}';
21
22    /** @var string */
23    protected $description = 'Publish the hyde components for customization. Note that existing files will be overwritten';
24
25    /** @var array<string, array<string, string>> */
26    protected array $options = [
27        'layouts' => [
28            'name' => 'Blade Layouts',
29            'description' => 'Shared layout views, such as the app layout, navigation menu, and Markdown page templates',
30            'group' => 'hyde-layouts',
31        ],
32        'components' => [
33            'name' => 'Blade Components',
34            'description' => 'More or less self contained components, extracted for customizability and DRY code',
35            'group' => 'hyde-components',
36        ],
37        'page-404' => [
38            'name' => '404 Page',
39            'description' => 'A beautiful 404 error page by the Laravel Collective',
40            'group' => 'hyde-page-404',
41        ],
42    ];
43
44    public function handle(): int
45    {
46        $selected = (string) ($this->argument('category') ?? $this->promptForCategory());
47
48        if ($selected === 'all' || $selected === '') {
49            foreach ($this->options as $key => $_ignored) {
50                $this->publishOption($key);
51            }
52        } else {
53            $this->publishOption($selected);
54        }
55
56        return Command::SUCCESS;
57    }
58
59    protected function publishOption(string $selected): void
60    {
61        Artisan::call('vendor:publish', [
62            '--tag' => $this->options[$selected]['group'] ?? $selected,
63            '--force' => true,
64        ], $this->output);
65    }
66
67    protected function promptForCategory(): string
68    {
69        /** @var string $choice */
70        $choice = $this->choice(
71            'Which category do you want to publish?',
72            $this->formatPublishableChoices(),
73            0
74        );
75
76        $selection = $this->parseChoiceIntoKey($choice);
77
78        $this->infoComment(sprintf("Selected category [%s]\n", $selection ?: 'all'));
79
80        return $selection;
81    }
82
83    protected function formatPublishableChoices(): array
84    {
85        $keys = ['Publish all categories listed below'];
86        foreach ($this->options as $key => $option) {
87            $keys[] = "<comment>$key</comment>: {$option['description']}";
88        }
89
90        return $keys;
91    }
92
93    protected function parseChoiceIntoKey(string $choice): string
94    {
95        return strstr(str_replace(['<comment>', '</comment>'], '', $choice), ':', true) ?: '';
96    }
97}