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

orchestral / workbench / 12343061968

15 Dec 2024 11:09PM UTC coverage: 92.611% (+0.4%) from 92.243%
12343061968

push

github

crynobone
wip

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

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

19 existing lines in 2 files now uncovered.

564 of 609 relevant lines covered (92.61%)

14.81 hits per line

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

87.01
/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
     * Namespace prefix for Workbench environment.
32
     */
33
    protected string $workbenchNamespacePrefix = 'Workbench\\';
34

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

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

46
        $this->prepareWorkbenchDirectories($filesystem, $workingPath);
15✔
47
        $this->prepareWorkbenchNamespaces($filesystem, $workingPath);
15✔
48

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

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

60
            (new DumpComposerAutoloads($workingPath))->handle();
15✔
61
        });
15✔
62
    }
63

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

71
        (new EnsureDirectoryExists(
15✔
72
            filesystem: $filesystem,
15✔
73
            components: $this->components,
15✔
74
        ))->handle(
15✔
75
            Collection::make([
15✔
76
                join_paths('app', 'Models'),
15✔
77
                join_paths('database', 'factories'),
15✔
78
                join_paths('database', 'migrations'),
15✔
79
                join_paths('database', 'seeders'),
15✔
80
            ])->when(
15✔
81
                $this->option('basic') === false,
15✔
82
                fn ($directories) => $directories->push(...['routes', join_paths('resources', 'views')])
15✔
83
            )->map(static fn ($directory) => join_paths($workbenchWorkingPath, $directory))
15✔
84
        );
15✔
85

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

92
        $this->prepareWorkbenchDatabaseSchema($filesystem, $workbenchWorkingPath);
15✔
93

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

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

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

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

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

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

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

160
        if (! $hasScriptsSection) {
15✔
161
            $content['scripts'] = [];
15✔
162
        }
163

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

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

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

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

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

195
        if (! \array_key_exists('lint', $content['scripts'])) {
15✔
196
            $lintScripts = [];
15✔
197

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

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

208
            if (\count($lintScripts) > 0) {
15✔
209
                $content['scripts']['lint'] = $lintScripts;
15✔
210
            }
211
        }
212

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

227
        return $content;
15✔
228
    }
229

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

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

245
        if (confirm('Prefix with `Workbench` namespace?', default: false) === false) {
15✔
246
            $this->workbenchNamespacePrefix = '';
1✔
247
        }
248

249
        $namespaces = [
15✔
250
            'workbench/app/' => $this->workbenchNamespacePrefix.'App\\',
15✔
251
            'workbench/database/factories/' => $this->workbenchNamespacePrefix.'Database\\Factories\\',
15✔
252
            'workbench/database/seeders/' => $this->workbenchNamespacePrefix.'Database\\Seeders\\',
15✔
253
        ];
15✔
254

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

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

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

272
        return $content;
15✔
273
    }
274

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

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

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

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

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

© 2025 Coveralls, Inc