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

orchestral / workbench / 20924530602

12 Jan 2026 03:14PM UTC coverage: 93.038%. Remained the same
20924530602

push

github

crynobone
Fix deprecated usage

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

1 of 1 new or added line in 1 file covered. (100.0%)

6 existing lines in 1 file now uncovered.

588 of 632 relevant lines covered (93.04%)

17.61 hits per line

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

91.33
/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\StubRegistrar;
18
use Orchestra\Workbench\Workbench;
19
use Symfony\Component\Console\Attribute\AsCommand;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23

24
use function Laravel\Prompts\confirm;
25
use function Orchestra\Sidekick\Filesystem\join_paths;
26
use function Orchestra\Sidekick\is_testbench_cli;
27
use function Orchestra\Testbench\package_path;
28

29
#[AsCommand(name: 'workbench:devtool', description: 'Configure Workbench for package development')]
30
class DevToolCommand extends Command implements PromptsForMissingInput
31
{
32
    /**
33
     * Namespace prefix for Workbench environment.
34
     */
35
    protected string $workbenchNamespacePrefix = 'Workbench\\';
36

37
    /**
38
     * Execute the console command.
39
     *
40
     * @return int
41
     */
42
    public function handle(Filesystem $filesystem)
43
    {
44
        $workingPath = package_path();
19✔
45

46
        event(new InstallStarted($this->input, $this->output, $this->components));
19✔
47

48
        $this->prepareWorkbenchNamespaces($filesystem, $workingPath);
19✔
49
        $this->prepareWorkbenchDirectories($filesystem, $workingPath);
19✔
50

51
        if ($this->option('install') === true) {
19✔
52
            $this->call('workbench:install', [
8✔
53
                '--force' => $this->option('force'),
8✔
54
                '--no-devtool' => true,
8✔
55
                '--basic' => $this->option('basic'),
8✔
56
            ]);
8✔
57
        }
58

59
        return tap(Command::SUCCESS, function ($exitCode) use ($workingPath) {
19✔
60
            event(new InstallEnded($this->input, $this->output, $this->components, $exitCode));
19✔
61

62
            (new DumpComposerAutoloads($workingPath))->handle();
19✔
63
        });
19✔
64
    }
65

66
    /**
67
     * Prepare workbench directories.
68
     */
69
    protected function prepareWorkbenchDirectories(Filesystem $filesystem, string $workingPath): void
70
    {
71
        $workbenchWorkingPath = join_paths($workingPath, 'workbench');
19✔
72

73
        (new EnsureDirectoryExists(
19✔
74
            filesystem: $filesystem,
19✔
75
            components: $this->components,
19✔
76
        ))->handle(
19✔
77
            (new Collection([
19✔
78
                join_paths('app', 'Models'),
19✔
79

80
                join_paths('database', 'factories'),
19✔
81
                join_paths('database', 'migrations'),
19✔
82
                join_paths('database', 'seeders'),
19✔
83
            ]))->when(
19✔
84
                $this->option('basic') === false,
19✔
85
                fn ($directories) => $directories->push(...['bootstrap', 'routes', join_paths('resources', 'views')])
19✔
86
            )->map(static fn ($directory) => join_paths($workbenchWorkingPath, $directory))
19✔
87
        );
19✔
88

89
        $this->callSilently('make:provider', [
19✔
90
            'name' => 'WorkbenchServiceProvider',
19✔
91
            '--preset' => 'workbench',
19✔
92
            '--force' => (bool) $this->option('force'),
19✔
93
        ]);
19✔
94

95
        StubRegistrar::replaceInFile($filesystem, join_paths($workbenchWorkingPath, 'Providers', 'WorkbenchServiceProvider.php'));
19✔
96

97
        $this->prepareWorkbenchDatabaseSchema($filesystem, $workbenchWorkingPath);
19✔
98

99
        if ($this->option('basic') === false) {
19✔
100
            foreach (['console', 'web'] as $route) {
15✔
101
                (new GeneratesFile(
15✔
102
                    filesystem: $filesystem,
15✔
103
                    components: $this->components,
15✔
104
                    force: (bool) $this->option('force'),
15✔
105
                ))->handle(
15✔
106
                    (string) Workbench::stubFile("routes.{$route}"),
15✔
107
                    join_paths($workbenchWorkingPath, 'routes', "{$route}.php")
15✔
108
                );
15✔
109
            }
110
        }
111
    }
112

113
    /**
114
     * Prepare workbench namespace to `composer.json`.
115
     */
116
    protected function prepareWorkbenchNamespaces(Filesystem $filesystem, string $workingPath): void
117
    {
118
        (new ModifyComposer($workingPath))
19✔
119
            ->handle(fn (array $content) => $this->appendScriptsToComposer(
19✔
120
                $this->appendAutoloadDevToComposer($content, $filesystem), $filesystem
19✔
121
            ));
19✔
122

123
        Workbench::flushCachedClassAndNamespaces();
19✔
124
    }
125

126
    /**
127
     * Prepare workbench database schema including user model, factory and seeder.
128
     */
129
    protected function prepareWorkbenchDatabaseSchema(Filesystem $filesystem, string $workingPath): void
130
    {
131
        $this->callSilently('make:user-model', [
19✔
132
            '--preset' => 'workbench',
19✔
133
            '--force' => (bool) $this->option('force'),
19✔
134
        ]);
19✔
135

136
        StubRegistrar::replaceInFile($filesystem, join_paths($workingPath, 'app', 'Models', 'User.php'));
19✔
137

138
        $this->callSilently('make:user-factory', [
19✔
139
            '--preset' => 'workbench',
19✔
140
            '--force' => (bool) $this->option('force'),
19✔
141
        ]);
19✔
142

143
        StubRegistrar::replaceInFile($filesystem, join_paths($workingPath, 'database', 'factories', 'UserFactory.php'));
19✔
144

145
        (new GeneratesFile(
19✔
146
            filesystem: $filesystem,
19✔
147
            components: $this->components,
19✔
148
            force: (bool) $this->option('force'),
19✔
149
        ))->handle(
19✔
150
            (string) Workbench::stubFile('seeders.database'),
19✔
151
            join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php')
19✔
152
        );
19✔
153

154
        StubRegistrar::replaceInFile($filesystem, join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php'));
19✔
155
    }
156

157
    /**
158
     * Append `scripts` to `composer.json`.
159
     */
160
    protected function appendScriptsToComposer(array $content, Filesystem $filesystem): array
161
    {
162
        $hasScriptsSection = \array_key_exists('scripts', $content);
19✔
163
        $hasTestbenchDusk = InstalledVersions::isInstalled('orchestra/testbench-dusk');
19✔
164

165
        if (! $hasScriptsSection) {
19✔
166
            $content['scripts'] = [];
19✔
167
        }
168

169
        $postAutoloadDumpScripts = array_filter([
19✔
170
            '@clear',
19✔
171
            '@prepare',
19✔
172
            $hasTestbenchDusk ? '@dusk:install-chromedriver' : null,
19✔
173
        ]);
19✔
174

175
        if (! \array_key_exists('post-autoload-dump', $content['scripts'])) {
19✔
176
            $content['scripts']['post-autoload-dump'] = $postAutoloadDumpScripts;
19✔
177
        } else {
178
            $content['scripts']['post-autoload-dump'] = array_values(array_unique([
×
179
                ...$postAutoloadDumpScripts,
×
180
                ...Arr::wrap($content['scripts']['post-autoload-dump']),
×
UNCOV
181
            ]));
×
182
        }
183

184
        $content['scripts']['clear'] = '@php vendor/bin/testbench package:purge-skeleton --ansi';
19✔
185
        $content['scripts']['prepare'] = '@php vendor/bin/testbench package:discover --ansi';
19✔
186

187
        if ($hasTestbenchDusk) {
19✔
UNCOV
188
            $content['scripts']['dusk:install-chromedriver'] = '@php vendor/bin/dusk-updater detect --auto-update --ansi';
×
189
        }
190

191
        $content['scripts']['build'] = '@php vendor/bin/testbench workbench:build --ansi';
19✔
192
        $content['scripts']['serve'] = [
19✔
193
            'Composer\\Config::disableProcessTimeout',
19✔
194
            '@build',
19✔
195
            $hasTestbenchDusk && is_testbench_cli(dusk: true)
19✔
UNCOV
196
                ? '@php vendor/bin/testbench-dusk serve --ansi'
×
197
                : '@php vendor/bin/testbench serve --ansi',
19✔
198
        ];
19✔
199

200
        if (! \array_key_exists('lint', $content['scripts'])) {
19✔
201
            $lintScripts = [];
19✔
202

203
            if (InstalledVersions::isInstalled('laravel/pint')) {
19✔
204
                $lintScripts[] = '@php vendor/bin/pint --ansi';
19✔
205
            } elseif ($filesystem->isFile(Workbench::packagePath('pint.json'))) {
×
UNCOV
206
                $lintScripts[] = 'pint';
×
207
            }
208

209
            if (InstalledVersions::isInstalled('phpstan/phpstan')) {
19✔
210
                $lintScripts[] = '@php vendor/bin/phpstan analyse --verbose --ansi';
19✔
211
            }
212

213
            if (\count($lintScripts) > 0) {
19✔
214
                $content['scripts']['lint'] = $lintScripts;
19✔
215
            }
216
        }
217

218
        if (
219
            $filesystem->isFile(Workbench::packagePath('phpunit.xml'))
19✔
220
            || $filesystem->isFile(Workbench::packagePath('phpunit.xml.dist'))
19✔
221
        ) {
222
            if (! \array_key_exists('test', $content['scripts'])) {
19✔
223
                $content['scripts']['test'] = [
19✔
224
                    '@clear',
19✔
225
                    InstalledVersions::isInstalled('pestphp/pest')
19✔
UNCOV
226
                        ? '@php vendor/bin/pest'
×
227
                        : '@php vendor/bin/phpunit',
19✔
228
                ];
19✔
229
            }
230
        }
231

232
        return $content;
19✔
233
    }
234

235
    /**
236
     * Append `autoload-dev` to `composer.json`.
237
     */
238
    protected function appendAutoloadDevToComposer(array $content, Filesystem $filesystem): array
239
    {
240
        /** @var array{autoload-dev?: array{psr-4?: array<string, string>}} $content */
241
        if (! \array_key_exists('autoload-dev', $content)) {
19✔
242
            $content['autoload-dev'] = [];
19✔
243
        }
244

245
        /** @var array{autoload-dev: array{psr-4?: array<string, string>}} $content */
246
        if (! \array_key_exists('psr-4', $content['autoload-dev'])) {
19✔
247
            $content['autoload-dev']['psr-4'] = [];
19✔
248
        }
249

250
        if (confirm('Prefix with `Workbench` namespace?', default: true) === false) {
19✔
251
            $this->workbenchNamespacePrefix = '';
4✔
252
        }
253

254
        $namespaces = [
19✔
255
            'workbench/app/' => $this->workbenchNamespacePrefix.'App\\',
19✔
256
            'workbench/database/factories/' => $this->workbenchNamespacePrefix.'Database\\Factories\\',
19✔
257
            'workbench/database/seeders/' => $this->workbenchNamespacePrefix.'Database\\Seeders\\',
19✔
258
        ];
19✔
259

260
        $autoloads = array_flip($content['autoload-dev']['psr-4']);
19✔
261

262
        foreach ($namespaces as $path => $namespace) {
19✔
263
            if (! \array_key_exists($path, $autoloads)) {
19✔
264
                $content['autoload-dev']['psr-4'][$namespace] = $path;
19✔
265

266
                $this->components->task(\sprintf(
19✔
267
                    'Added [%s] for [%s] to Composer', $namespace, './'.rtrim($path, '/')
19✔
268
                ));
19✔
269
            } else {
270
                $this->components->twoColumnDetail(
×
271
                    \sprintf('Composer already contains [%s] path assigned to [%s] namespace', './'.rtrim($path, '/'), $autoloads[$path]),
×
272
                    '<fg=yellow;options=bold>SKIPPED</>'
×
UNCOV
273
                );
×
274
            }
275
        }
276

277
        return $content;
19✔
278
    }
279

280
    /**
281
     * Prompt the user for any missing arguments.
282
     *
283
     * @return void
284
     */
285
    protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
286
    {
287
        $install = null;
18✔
288

289
        if (\is_null($input->getOption('install'))) {
18✔
290
            $install = confirm('Run Workbench installation?', true);
1✔
291
        }
292

293
        if (! \is_null($install)) {
18✔
294
            $input->setOption('install', $install);
1✔
295
        }
296
    }
297

298
    /**
299
     * Get the console command options.
300
     *
301
     * @return array
302
     */
303
    protected function getOptions()
304
    {
305
        return [
19✔
306
            ['force', 'f', InputOption::VALUE_NONE, 'Overwrite any existing files'],
19✔
307
            ['install', null, InputOption::VALUE_NEGATABLE, 'Run Workbench installation'],
19✔
308
            ['basic', null, InputOption::VALUE_NONE, 'Workbench installation without discovers and routes'],
19✔
309
        ];
19✔
310
    }
311
}
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

© 2026 Coveralls, Inc