Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workbench code generators #22

Merged
merged 11 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions canvas
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
<?php

$workingPath = getcwd();
$config = ['preset' => is_file("{$workingPath}/artisan") ? 'laravel' : 'package'];

define('CANVAS_WORKING_PATH', $workingPath);

$config = ['preset' => Orchestra\Canvas\Canvas::presetFromEnvironment($workingPath)];

require $_composer_autoload_path ?? __DIR__.'/vendor/autoload.php';

if (file_exists("{$workingPath}/canvas.yaml")) {
$config = Symfony\Component\Yaml\Yaml::parseFile("{$workingPath}/canvas.yaml");
if (file_exists($workingPath.DIRECTORY_SEPARATOR.'canvas.yaml')) {
$config = Symfony\Component\Yaml\Yaml::parseFile($workingPath.DIRECTORY_SEPARATOR.'canvas.yaml');
}

$files = new Illuminate\Filesystem\Filesystem();
Expand Down
32 changes: 24 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
},
"autoload-dev": {
"psr-4": {
"Orchestra\\Canvas\\Tests\\": "tests/"
"Orchestra\\Canvas\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"require": {
Expand All @@ -32,12 +35,12 @@
"composer/semver": "^3.0",
"illuminate/database": "^10.17",
"illuminate/support": "^10.17",
"orchestra/canvas-core": "^8.3",
"orchestra/canvas-core": "^8.4",
"symfony/yaml": "^6.2"
},
"require-dev": {
"laravel/pint": "^1.6",
"orchestra/testbench": "^8.8.1",
"orchestra/testbench": "^8.9",
"phpstan/phpstan": "^1.10.5",
"phpunit/phpunit": "^10.1"
},
Expand All @@ -55,15 +58,28 @@
}
},
"scripts": {
"post-autoload-dump": "@composer run prepare",
"prepare": "@php testbench package:discover --ansi",
"post-autoload-dump": [
"@clear",
"@prepare"
],
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"ci": [
"@composer audit",
"@composer run prepare",
"@audit",
"@prepare",
"@lint",
"@test"
],
"lint": [
"@php vendor/bin/phpstan analyse",
"@php vendor/bin/pint"
],
"test": "@php vendor/bin/phpunit -c ./ --color"
"test": "@php vendor/bin/phpunit -c ./ --color",
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"build": "@php vendor/bin/testbench workbench:build",
"serve": [
"@build",
"@php vendor/bin/testbench serve"
]
},
"minimum-stability": "dev"
}
26 changes: 26 additions & 0 deletions src/Canvas.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,35 @@

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class Canvas
{
/**
* Assume the preset from environment.
*/
public static function presetFromEnvironment(string $basePath): string
{
/** detect `testbench.yaml` */
$testbenchYaml = Collection::make([
'testbench.yaml',
'testbench.yaml.example',
'testbench.yaml.dist',
])->filter(fn ($filename) => file_exists($basePath.DIRECTORY_SEPARATOR.$filename))
->first();

if (! \is_null($testbenchYaml)) {
return 'package';
}

return Collection::make([
file_exists($basePath.DIRECTORY_SEPARATOR.'artisan'),
file_exists($basePath.DIRECTORY_SEPARATOR.'bootstrap'.DIRECTORY_SEPARATOR.'app.php'),
is_dir($basePath.DIRECTORY_SEPARATOR.'bootstrap'.DIRECTORY_SEPARATOR.'cache'),
])->reject(fn ($condition) => $condition === true)
->isEmpty() ? 'laravel' : 'package';
}

/**
* Make Preset from configuration.
*
Expand Down
34 changes: 30 additions & 4 deletions src/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use Illuminate\Console\Application as Artisan;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Orchestra\Canvas\Core\Presets\Preset;
use Orchestra\Workbench\Workbench;
use Symfony\Component\Yaml\Yaml;

class LaravelServiceProvider extends ServiceProvider implements DeferrableProvider
Expand All @@ -21,11 +24,15 @@ class LaravelServiceProvider extends ServiceProvider implements DeferrableProvid
public function register()
{
$this->app->singleton('orchestra.canvas', function (Application $app) {
$files = $app->make('files');
$filesystem = $app->make('files');

if (\defined('TESTBENCH_WORKING_PATH') && class_exists(Workbench::class)) {
return $this->registerCanvasForWorkbench($filesystem);
}

$config = ['preset' => 'laravel'];

if ($files->exists($app->basePath('canvas.yaml'))) {
if ($filesystem->exists($app->basePath('canvas.yaml'))) {
$config = Yaml::parseFile($app->basePath('canvas.yaml'));
} else {
Arr::set($config, 'testing.extends', [
Expand All @@ -38,7 +45,7 @@ public function register()

$config['user-auth-provider'] = $this->userProviderModel();

return Canvas::preset($config, $app->basePath(), $files);
return Canvas::preset($config, $app->basePath(), $filesystem);
});
}

Expand All @@ -57,7 +64,7 @@ public function boot()
*/
$preset = $app->make('orchestra.canvas');

if ($app->runningUnitTests()) {
if (\defined('TESTBENCH_WORKING_PATH') || $app->runningUnitTests()) {
$artisan->add(new Commands\Channel($preset));
$artisan->add(new Commands\Component($preset));
$artisan->add(new Commands\Console($preset));
Expand Down Expand Up @@ -89,6 +96,25 @@ public function boot()
});
}

/**
* Regiseter canvas for workbench.
*/
protected function registerCanvasForWorkbench(Filesystem $filesystem): Preset
{
return Canvas::preset(
[
'preset' => Presets\PackageWorkbench::class,
'testing' => [
'extends' => ['unit' => 'PHPUnit\Framework\TestCase',
'feature' => 'Orchestra\Testbench\TestCase',
],
],
],
rtrim(Workbench::packagePath(), DIRECTORY_SEPARATOR),
$filesystem
);
}

/**
* Get the services provided by the provider.
*
Expand Down
113 changes: 113 additions & 0 deletions src/Presets/PackageWorkbench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Orchestra\Canvas\Presets;

use Orchestra\Canvas\Core\Presets\Preset;
use Orchestra\Workbench\Workbench;

class PackageWorkbench extends Preset
{
/**
* Preset name.
*/
public function name(): string
{
return 'workbench';
}

/**
* Get the path to the base working directory.
*/
public function laravelPath(): string
{
return Workbench::laravelPath();
}

/**
* Get the path to the source directory.
*/
public function sourcePath(): string
{
return Workbench::path('app');
}

/**
* Preset namespace.
*/
public function rootNamespace(): string
{
return 'Workbench\App';
}

/**
* Model namespace.
*/
public function modelNamespace(): string
{
return 'Workbench\App\Models';
}

/**
* Provider namespace.
*/
public function providerNamespace(): string
{
return 'Workbench\App\Providers';
}

/**
* Databases namespace.
*/
public function factoryNamespace(): string
{
return 'Workbench\Database\Factories';
}

/**
* Databases namespace.
*/
public function seederNamespace(): string
{
return 'Workbench\Database\Seeders';
}

/**
* Get the path to the resource directory.
*/
public function resourcePath(): string
{
return Workbench::path('resources');
}

/**
* Get the path to the factory directory.
*/
public function factoryPath(): string
{
return Workbench::path('database/factories');
}

/**
* Get the path to the migration directory.
*/
public function migrationPath(): string
{
return Workbench::path('database/migrations');
}

/**
* Get the path to the seeder directory.
*/
public function seederPath(): string
{
return Workbench::path('database/seeders');
}

/**
* Get custom stub path.
*/
public function getCustomStubPath(): ?string
{
return null;
}
}
2 changes: 1 addition & 1 deletion src/Processors/GeneratesFactoryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function buildClass(string $name): string

$model = class_basename($namespaceModel);

$factoryNamespace = $this->preset->config('factory.namespace', 'Database\Factories');
$factoryNamespace = $this->preset->factoryNamespace();

if (Str::startsWith($namespaceModel, 'App\\Models')) {
$namespace = Str::beforeLast($factoryNamespace.'\\'.Str::after($namespaceModel, 'App\\Models\\'), '\\');
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/GeneratesSeederCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ protected function getNamespace(string $name): string
*/
protected function rootNamespace(): string
{
return 'Database\Seeders\\';
return $this->preset->seederNamespace();
}
}
2 changes: 2 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
providers:
- Orchestra\Canvas\LaravelServiceProvider
2 changes: 1 addition & 1 deletion tests/Feature/CommandsProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function it_can_setup_laravel_preset()
$this->assertSame("{$directory}/resources", $preset->resourcePath());
$this->assertSame("{$directory}/database/factories", $preset->factoryPath());
$this->assertSame("{$directory}/database/migrations", $preset->migrationPath());
$this->assertSame("{$directory}/database/seeds", $preset->seederPath());
$this->assertSame("{$directory}/database/seeders", $preset->seederPath());
}
}
Loading