• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orchestral / workbench / 12245858654

09 Dec 2024 10:59PM UTC coverage: 90.216% (-3.9%) from 94.118%
12245858654

push

github

crynobone
wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

544 of 603 relevant lines covered (90.22%)

13.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.11
/src/Console/DevToolCommand.php
1
<?php
2

3
namespace Orchestra\Workbench\Console;
4

5
use Composer\InstalledVersions;
6
use Illuminate\Console\Command;
7
use Illuminate\Contracts\Console\PromptsForMissingInput;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Collection;
11
use Orchestra\Testbench\Foundation\Console\Actions\EnsureDirectoryExists;
12
use Orchestra\Testbench\Foundation\Console\Actions\GeneratesFile;
13
use Orchestra\Workbench\Actions\DumpComposerAutoloads;
14
use Orchestra\Workbench\Actions\ModifyComposer;
15
use Orchestra\Workbench\Events\InstallEnded;
16
use Orchestra\Workbench\Events\InstallStarted;
17
use Orchestra\Workbench\Workbench;
18
use Symfony\Component\Console\Attribute\AsCommand;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22

23
use function Laravel\Prompts\confirm;
24
use function Orchestra\Testbench\join_paths;
25
use function Orchestra\Testbench\package_path;
26

27
#[AsCommand(name: 'workbench:devtool', description: 'Configure Workbench for package development')]
28
class DevToolCommand extends Command implements PromptsForMissingInput
29
{
30
    use Concerns\InteractsWithFiles;
31

32
    /**
33
     * Execute the console command.
34
     *
35
     * @return int
36
     */
37
    public function handle(Filesystem $filesystem)
38
    {
39
        $workingPath = package_path();
13✔
40

41
        event(new InstallStarted($this->input, $this->output, $this->components));
13✔
42

43
        $this->prepareWorkbenchDirectories($filesystem, $workingPath);
13✔
44
        $this->prepareWorkbenchNamespaces($filesystem, $workingPath);
13✔
45

46
        if ($this->option('install') === true) {
13✔
47
            $this->call('workbench:install', [
8✔
48
                '--force' => $this->option('force'),
8✔
49
                '--no-devtool' => true,
8✔
50
                '--basic' => $this->option('basic'),
8✔
51
            ]);
8✔
52
        }
53

54
        return tap(Command::SUCCESS, function ($exitCode) use ($workingPath) {
13✔
55
            event(new InstallEnded($this->input, $this->output, $this->components, $exitCode));
13✔
56

57
            (new DumpComposerAutoloads($workingPath))->handle();
13✔
58
        });
13✔
59
    }
60

61
    /**
62
     * Prepare workbench directories.
63
     */
64
    protected function prepareWorkbenchDirectories(Filesystem $filesystem, string $workingPath): void
65
    {
66
        $workbenchWorkingPath = join_paths($workingPath, 'workbench');
13✔
67

68
        (new EnsureDirectoryExists(
13✔
69
            filesystem: $filesystem,
13✔
70
            components: $this->components,
13✔
71
        ))->handle(
13✔
72
            Collection::make([
13✔
73
                join_paths('app', 'Models'),
13✔
74

75
                join_paths('database', 'factories'),
13✔
76
                join_paths('database', 'migrations'),
13✔
77
                join_paths('database', 'seeders'),
13✔
78
            ])->when(
13✔
79
                $this->option('basic') === false,
13✔
80
                fn ($directories) => $directories->push(...['bootstrap', 'routes', join_paths('resources', 'views')])
13✔
81
            )->map(static fn ($directory) => join_paths($workbenchWorkingPath, $directory))
13✔
82
        );
13✔
83

84
        $this->callSilently('make:provider', [
13✔
85
            'name' => 'WorkbenchServiceProvider',
13✔
86
            '--preset' => 'workbench',
13✔
87
            '--force' => (bool) $this->option('force'),
13✔
88
        ]);
13✔
89

90
        $this->prepareWorkbenchDatabaseSchema($filesystem, $workbenchWorkingPath);
13✔
91

92
        if ($this->option('basic') === false) {
13✔
93
            foreach (['console', 'web'] as $route) {
9✔
94
                (new GeneratesFile(
9✔
95
                    filesystem: $filesystem,
9✔
96
                    components: $this->components,
9✔
97
                    force: (bool) $this->option('force'),
9✔
98
                ))->handle(
9✔
99
                    (string) Workbench::stubFile("routes.{$route}"),
9✔
100
                    join_paths($workbenchWorkingPath, 'routes', "{$route}.php")
9✔
101
                );
9✔
102
            }
103
        }
104
    }
105

106
    /**
107
     * Prepare workbench namespace to `composer.json`.
108
     */
109
    protected function prepareWorkbenchNamespaces(Filesystem $filesystem, string $workingPath): void
110
    {
111
        (new ModifyComposer($workingPath))
13✔
112
            ->handle(fn (array $content) => $this->appendScriptsToComposer(
13✔
113
                $this->appendAutoloadDevToComposer($content, $filesystem), $filesystem
13✔
114
            ));
13✔
115
    }
116

117
    /**
118
     * Prepare workbench database schema including user model, factory and seeder.
119
     */
120
    protected function prepareWorkbenchDatabaseSchema(Filesystem $filesystem, string $workingPath): void
121
    {
122
        $this->callSilently('make:user-model', [
13✔
123
            '--preset' => 'workbench',
13✔
124
            '--force' => (bool) $this->option('force'),
13✔
125
        ]);
13✔
126

127
        $this->callSilently('make:user-factory', [
13✔
128
            '--preset' => 'workbench',
13✔
129
            '--force' => (bool) $this->option('force'),
13✔
130
        ]);
13✔
131

132
        (new GeneratesFile(
13✔
133
            filesystem: $filesystem,
13✔
134
            components: $this->components,
13✔
135
            force: (bool) $this->option('force'),
13✔
136
        ))->handle(
13✔
137
            (string) Workbench::stubFile('seeders.database'),
13✔
138
            join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php')
13✔
139
        );
13✔
140

141
        if ($filesystem->exists(join_paths($workingPath, 'database', 'factories', 'UserFactory.php'))) {
13✔
142
            $filesystem->replaceInFile([
13✔
143
                'use Orchestra\Testbench\Factories\UserFactory;',
13✔
144
            ], [
13✔
145
                'use Workbench\Database\Factories\UserFactory;',
13✔
146
            ], join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php'));
13✔
147
        }
148
    }
149

150
    /**
151
     * Append `scripts` to `composer.json`.
152
     */
153
    protected function appendScriptsToComposer(array $content, Filesystem $filesystem): array
154
    {
155
        $hasScriptsSection = \array_key_exists('scripts', $content);
13✔
156
        $hasTestbenchDusk = InstalledVersions::isInstalled('orchestra/testbench-dusk');
13✔
157

158
        if (! $hasScriptsSection) {
13✔
159
            $content['scripts'] = [];
13✔
160
        }
161

162
        $postAutoloadDumpScripts = array_filter([
13✔
163
            '@clear',
13✔
164
            '@prepare',
13✔
165
            $hasTestbenchDusk ? '@dusk:install-chromedriver' : null,
13✔
166
        ]);
13✔
167

168
        if (! \array_key_exists('post-autoload-dump', $content['scripts'])) {
13✔
169
            $content['scripts']['post-autoload-dump'] = $postAutoloadDumpScripts;
13✔
170
        } else {
171
            $content['scripts']['post-autoload-dump'] = array_values(array_unique([
×
172
                ...$postAutoloadDumpScripts,
×
173
                ...Arr::wrap($content['scripts']['post-autoload-dump']),
×
174
            ]));
×
175
        }
176

177
        $content['scripts']['clear'] = '@php vendor/bin/testbench package:purge-skeleton --ansi';
13✔
178
        $content['scripts']['prepare'] = '@php vendor/bin/testbench package:discover --ansi';
13✔
179

180
        if ($hasTestbenchDusk) {
13✔
181
            $content['scripts']['dusk:install-chromedriver'] = '@php vendor/bin/dusk-updater detect --auto-update --ansi';
×
182
        }
183

184
        $content['scripts']['build'] = '@php vendor/bin/testbench workbench:build --ansi';
13✔
185
        $content['scripts']['serve'] = [
13✔
186
            'Composer\\Config::disableProcessTimeout',
13✔
187
            '@build',
13✔
188
            $hasTestbenchDusk && \defined('TESTBENCH_DUSK')
13✔
189
                ? '@php vendor/bin/testbench-dusk serve --ansi'
×
190
                : '@php vendor/bin/testbench serve --ansi',
13✔
191
        ];
13✔
192

193
        if (! \array_key_exists('lint', $content['scripts'])) {
13✔
194
            $lintScripts = [];
13✔
195

196
            if (InstalledVersions::isInstalled('laravel/pint')) {
13✔
197
                $lintScripts[] = '@php vendor/bin/pint --ansi';
13✔
198
            } elseif ($filesystem->exists(Workbench::packagePath('pint.json'))) {
×
199
                $lintScripts[] = 'pint';
×
200
            }
201

202
            if (InstalledVersions::isInstalled('phpstan/phpstan')) {
13✔
203
                $lintScripts[] = '@php vendor/bin/phpstan analyse --verbose --ansi';
13✔
204
            }
205

206
            if (\count($lintScripts) > 0) {
13✔
207
                $content['scripts']['lint'] = $lintScripts;
13✔
208
            }
209
        }
210

211
        if (
212
            $filesystem->exists(Workbench::packagePath('phpunit.xml'))
13✔
213
            || $filesystem->exists(Workbench::packagePath('phpunit.xml.dist'))
13✔
214
        ) {
215
            if (! \array_key_exists('test', $content['scripts'])) {
×
216
                $content['scripts']['test'] = [
×
217
                    '@clear',
×
218
                    InstalledVersions::isInstalled('pestphp/pest')
×
219
                        ? '@php vendor/bin/pest'
×
220
                        : '@php vendor/bin/phpunit',
×
221
                ];
×
222
            }
223
        }
224

225
        return $content;
13✔
226
    }
227

228
    /**
229
     * Append `autoload-dev` to `composer.json`.
230
     */
231
    protected function appendAutoloadDevToComposer(array $content, Filesystem $filesystem): array
232
    {
233
        /** @var array{autoload-dev?: array{psr-4?: array<string, string>}} $content */
234
        if (! \array_key_exists('autoload-dev', $content)) {
13✔
235
            $content['autoload-dev'] = [];
×
236
        }
237

238
        /** @var array{autoload-dev: array{psr-4?: array<string, string>}} $content */
239
        if (! \array_key_exists('psr-4', $content['autoload-dev'])) {
13✔
240
            $content['autoload-dev']['psr-4'] = [];
×
241
        }
242

243
        $namespaces = [
13✔
244
            'Workbench\\App\\' => 'workbench/app/',
13✔
245
            'Workbench\\Database\\Factories\\' => 'workbench/database/factories/',
13✔
246
            'Workbench\\Database\\Seeders\\' => 'workbench/database/seeders/',
13✔
247
        ];
13✔
248

249
        foreach ($namespaces as $namespace => $path) {
13✔
250
            if (! \array_key_exists($namespace, $content['autoload-dev']['psr-4'])) {
13✔
251
                $content['autoload-dev']['psr-4'][$namespace] = $path;
×
252

253
                $this->components->task(\sprintf(
×
254
                    'Added [%s] for [%s] to Composer', $namespace, $path
×
255
                ));
×
256
            } else {
257
                $this->components->twoColumnDetail(
13✔
258
                    \sprintf('Composer already contain [%s] namespace', $namespace),
13✔
259
                    '<fg=yellow;options=bold>SKIPPED</>'
13✔
260
                );
13✔
261
            }
262
        }
263

264
        return $content;
13✔
265
    }
266

267
    /**
268
     * Prompt the user for any missing arguments.
269
     *
270
     * @return void
271
     */
272
    protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
273
    {
274
        $install = null;
13✔
275

276
        if ($input->getOption('skip-install') === true) {
13✔
277
            $install = false;
×
278
        } elseif (\is_null($input->getOption('install'))) {
13✔
279
            $install = confirm('Run Workbench installation?', true);
×
280
        }
281

282
        if (! \is_null($install)) {
13✔
283
            $input->setOption('install', $install);
×
284
        }
285
    }
286

287
    /**
288
     * Get the console command options.
289
     *
290
     * @return array
291
     */
292
    protected function getOptions()
293
    {
294
        return [
13✔
295
            ['force', 'f', InputOption::VALUE_NONE, 'Overwrite any existing files'],
13✔
296
            ['install', null, InputOption::VALUE_NEGATABLE, 'Run Workbench installation'],
13✔
297
            ['basic', null, InputOption::VALUE_NONE, 'Workbench installation without discovers and routes'],
13✔
298

299
            /** @deprecated */
300
            ['skip-install', null, InputOption::VALUE_NONE, 'Skipped Workbench installation (deprecated)'],
13✔
301
        ];
13✔
302
    }
303
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc