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

orchestral / workbench / 12271151167

11 Dec 2024 06:48AM UTC coverage: 92.955% (+0.3%) from 92.612%
12271151167

push

github

crynobone
:white_check_mark: wip

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

541 of 582 relevant lines covered (92.96%)

14.24 hits per line

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

86.81
/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\Filesystem\Filesystem;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
use Orchestra\Testbench\Foundation\Console\Actions\EnsureDirectoryExists;
11
use Orchestra\Testbench\Foundation\Console\Actions\GeneratesFile;
12
use Orchestra\Workbench\Actions\DumpComposerAutoloads;
13
use Orchestra\Workbench\Actions\ModifyComposer;
14
use Orchestra\Workbench\Events\InstallEnded;
15
use Orchestra\Workbench\Events\InstallStarted;
16
use Orchestra\Workbench\Workbench;
17
use Symfony\Component\Console\Attribute\AsCommand;
18
use Symfony\Component\Console\Input\InputOption;
19

20
use function Orchestra\Testbench\join_paths;
21
use function Orchestra\Testbench\package_path;
22

23
#[AsCommand(name: 'workbench:devtool', description: 'Configure Workbench for package development')]
24
class DevToolCommand extends Command
25
{
26
    use Concerns\InteractsWithFiles;
27

28
    /**
29
     * Execute the console command.
30
     *
31
     * @return int
32
     */
33
    public function handle(Filesystem $filesystem)
34
    {
35
        $workingPath = package_path();
14✔
36

37
        event(new InstallStarted($this->input, $this->output, $this->components));
14✔
38

39
        $this->prepareWorkbenchDirectories($filesystem, $workingPath);
14✔
40
        $this->prepareWorkbenchNamespaces($filesystem, $workingPath);
14✔
41

42
        if ($this->option('install') === true && $this->option('skip-install') === false) {
14✔
43
            $this->call('workbench:install', [
8✔
44
                '--force' => $this->option('force'),
8✔
45
                '--no-devtool' => true,
8✔
46
                '--basic' => $this->option('basic'),
8✔
47
            ]);
8✔
48
        }
49

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

53
            (new DumpComposerAutoloads($workingPath))->handle();
14✔
54
        });
14✔
55
    }
56

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

64
        (new EnsureDirectoryExists(
14✔
65
            filesystem: $filesystem,
14✔
66
            components: $this->components,
14✔
67
        ))->handle(
14✔
68
            Collection::make([
14✔
69
                join_paths('app', 'Models'),
14✔
70
                join_paths('database', 'factories'),
14✔
71
                join_paths('database', 'migrations'),
14✔
72
                join_paths('database', 'seeders'),
14✔
73
            ])->when(
14✔
74
                $this->option('basic') === false,
14✔
75
                fn ($directories) => $directories->push(...['routes', join_paths('resources', 'views')])
14✔
76
            )->map(static fn ($directory) => join_paths($workbenchWorkingPath, $directory))
14✔
77
        );
14✔
78

79
        $this->callSilently('make:provider', [
14✔
80
            'name' => 'WorkbenchServiceProvider',
14✔
81
            '--preset' => 'workbench',
14✔
82
            '--force' => (bool) $this->option('force'),
14✔
83
        ]);
14✔
84

85
        $this->prepareWorkbenchDatabaseSchema($filesystem, $workbenchWorkingPath);
14✔
86

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

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

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

122
        $this->callSilently('make:user-factory', [
14✔
123
            '--preset' => 'workbench',
14✔
124
            '--force' => (bool) $this->option('force'),
14✔
125
        ]);
14✔
126

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

136
        if ($filesystem->isFile(join_paths($workingPath, 'database', 'factories', 'UserFactory.php'))) {
14✔
137
            $this->replaceInFile($filesystem, [
14✔
138
                'use Orchestra\Testbench\Factories\UserFactory;',
14✔
139
            ], [
14✔
140
                'use Workbench\Database\Factories\UserFactory;',
14✔
141
            ], join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php'));
14✔
142
        }
143
    }
144

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

153
        if (! $hasScriptsSection) {
14✔
154
            $content['scripts'] = [];
14✔
155
        }
156

157
        $postAutoloadDumpScripts = array_filter([
14✔
158
            '@clear',
14✔
159
            '@prepare',
14✔
160
            $hasTestbenchDusk ? '@dusk:install-chromedriver' : null,
14✔
161
        ]);
14✔
162

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

172
        $content['scripts']['clear'] = '@php vendor/bin/testbench package:purge-skeleton --ansi';
14✔
173
        $content['scripts']['prepare'] = '@php vendor/bin/testbench package:discover --ansi';
14✔
174

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

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

188
        if (! \array_key_exists('lint', $content['scripts'])) {
14✔
189
            $lintScripts = [];
14✔
190

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

197
            if (InstalledVersions::isInstalled('phpstan/phpstan')) {
14✔
198
                $lintScripts[] = '@php vendor/bin/phpstan analyse --verbose --ansi';
14✔
199
            }
200

201
            if (\count($lintScripts) > 0) {
14✔
202
                $content['scripts']['lint'] = $lintScripts;
14✔
203
            }
204
        }
205

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

220
        return $content;
14✔
221
    }
222

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

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

238
        $namespaces = [
14✔
239
            'Workbench\\App\\' => 'workbench/app/',
14✔
240
            'Workbench\\Database\\Factories\\' => 'workbench/database/factories/',
14✔
241
            'Workbench\\Database\\Seeders\\' => 'workbench/database/seeders/',
14✔
242
        ];
14✔
243

244
        foreach ($namespaces as $namespace => $path) {
14✔
245
            if (! \array_key_exists($namespace, $content['autoload-dev']['psr-4'])) {
14✔
246
                $content['autoload-dev']['psr-4'][$namespace] = $path;
14✔
247

248
                $this->components->task(\sprintf(
14✔
249
                    'Added [%s] for [%s] to Composer', $namespace, $path
14✔
250
                ));
14✔
251
            } else {
252
                $this->components->twoColumnDetail(
×
253
                    \sprintf('Composer already contain [%s] namespace', $namespace),
×
254
                    '<fg=yellow;options=bold>SKIPPED</>'
×
255
                );
×
256
            }
257
        }
258

259
        return $content;
14✔
260
    }
261

262
    /**
263
     * Get the console command options.
264
     *
265
     * @return array
266
     */
267
    protected function getOptions()
268
    {
269
        return [
14✔
270
            ['force', 'f', InputOption::VALUE_NONE, 'Overwrite any existing files'],
14✔
271
            ['install', null, InputOption::VALUE_NEGATABLE, 'Run Workbench installation'],
14✔
272
            ['basic', null, InputOption::VALUE_NONE, 'Workbench installation without discovers and routes'],
14✔
273

274
            /** @deprecated */
275
            ['skip-install', null, InputOption::VALUE_NONE, 'Skipped Workbench installation (deprecated)'],
14✔
276
        ];
14✔
277
    }
278
}
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