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

orchestral / workbench / 12275174728

11 Dec 2024 11:12AM UTC coverage: 91.118% (+0.2%) from 90.894%
12275174728

push

github

crynobone
Merge branch '8.x' into 9.x

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

554 of 608 relevant lines covered (91.12%)

14.49 hits per line

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

86.45
/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
    /**
31
     * Execute the console command.
32
     *
33
     * @return int
34
     */
35
    public function handle(Filesystem $filesystem)
36
    {
37
        $workingPath = package_path();
15✔
38

39
        event(new InstallStarted($this->input, $this->output, $this->components));
15✔
40

41
        $this->prepareWorkbenchDirectories($filesystem, $workingPath);
15✔
42
        $this->prepareWorkbenchNamespaces($filesystem, $workingPath);
15✔
43

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

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

55
            (new DumpComposerAutoloads($workingPath))->handle();
15✔
56
        });
15✔
57
    }
58

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

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

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

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

88
        $this->prepareWorkbenchDatabaseSchema($filesystem, $workbenchWorkingPath);
15✔
89

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

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

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

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

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

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

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

156
        if (! $hasScriptsSection) {
15✔
157
            $content['scripts'] = [];
15✔
158
        }
159

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

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

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

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

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

191
        if (! \array_key_exists('lint', $content['scripts'])) {
15✔
192
            $lintScripts = [];
15✔
193

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

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

204
            if (\count($lintScripts) > 0) {
15✔
205
                $content['scripts']['lint'] = $lintScripts;
15✔
206
            }
207
        }
208

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

223
        return $content;
15✔
224
    }
225

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

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

241
        $namespacePrefix = '';
15✔
242

243
        if (confirm('Prefix with `Workbench` namespace?', default: true)) {
15✔
244
            $namespacePrefix = 'Workbench\\';
×
245
        }
246

247
        $namespaces = [
15✔
248
            'workbench/app/' => $namespacePrefix.'App\\',
15✔
249
            'workbench/database/factories/' => $namespacePrefix.'Database\\Factories\\',
15✔
250
            'workbench/database/seeders/' => $namespacePrefix.'Database\\Seeders\\',
15✔
251
        ];
15✔
252

253
        $autoloads = array_flip($content['autoload-dev']['psr-4']);
15✔
254

255
        foreach ($namespaces as $path => $namespace) {
15✔
256
            if (! \array_key_exists($path, $autoloads)) {
15✔
257
                $content['autoload-dev']['psr-4'][$namespace] = $path;
15✔
258

259
                $this->components->task(\sprintf(
15✔
260
                    'Added [%s] for [%s] to Composer', $namespace, '/'.rtrim($path, '/')
15✔
261
                ));
15✔
262
            } else {
263
                $this->components->twoColumnDetail(
×
264
                    \sprintf('Composer already contains [%s] path assigned to [%s] namespace', '/'.rtrim($path, '/'), $autoloads[$path]),
×
265
                    '<fg=yellow;options=bold>SKIPPED</>'
×
266
                );
×
267
            }
268
        }
269

270
        return $content;
15✔
271
    }
272

273
    /**
274
     * Prompt the user for any missing arguments.
275
     *
276
     * @return void
277
     */
278
    protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
279
    {
280
        $install = null;
14✔
281

282
        if ($input->getOption('skip-install') === true) {
14✔
283
            $install = false;
×
284
        } elseif (\is_null($input->getOption('install'))) {
14✔
285
            $install = confirm('Run Workbench installation?', true);
1✔
286
        }
287

288
        if (! \is_null($install)) {
14✔
289
            $input->setOption('install', $install);
1✔
290
        }
291
    }
292

293
    /**
294
     * Get the console command options.
295
     *
296
     * @return array
297
     */
298
    protected function getOptions()
299
    {
300
        return [
15✔
301
            ['force', 'f', InputOption::VALUE_NONE, 'Overwrite any existing files'],
15✔
302
            ['install', null, InputOption::VALUE_NEGATABLE, 'Run Workbench installation'],
15✔
303
            ['basic', null, InputOption::VALUE_NONE, 'Workbench installation without discovers and routes'],
15✔
304

305
            /** @deprecated */
306
            ['skip-install', null, InputOption::VALUE_NONE, 'Skipped Workbench installation (deprecated)'],
15✔
307
        ];
15✔
308
    }
309
}
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