Skip to content

Commit

Permalink
Support for Laravel 10 (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored Feb 26, 2023
1 parent a33d942 commit 884e717
Show file tree
Hide file tree
Showing 229 changed files with 467 additions and 2,225 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [8.0, 8.1]

php-version: ['8.1', '8.2']
os: [ubuntu-latest, windows-latest, macos-latest]

dependencies: [locked]

experimental: [false]

name: PHP ${{ matrix.php-version }} - ${{ matrix.os }}

runs-on: ${{ matrix.os }}

continue-on-error: ${{ matrix.experimental }}

steps:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

services:
mysql:
image: mysql:5.7
image: mysql:8.0
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: laravel
Expand All @@ -25,8 +25,8 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [8.0, 8.1]
laravel-version: [9]
php-version: ['8.1', '8.2']
laravel-version: [10]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
tools: phplint, laravel/pint
- name: Check syntax
run: phplint .
Expand Down
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Upgrade Guide

## Upgrading from 2.x to 2.6
Version 2.6 was a minor release to add support for Laravel 10. This adopts the latest conventions from Laravel, most notably the addition of type-hints in code.

The only other change is the removal of `use_return_types` configuration, as this is now Blueprint's default behavior.

Inline with Blueprint's [Support Policy](https://github.com/laravel-shift/blueprint#support-policy), this release also drops support for Laravel 9. If you are still running Laravel 9, you may constrain your Blueprint version to `2.5.0`.

## Upgrading from 1.x to 2.x
Version 2.x was a superficial major release to reflect Blueprint's new [Support Policy](https://github.com/laravel-shift/blueprint#support-policy). There were no changes to the underlying grammar. A few configuration options and methods were changed. Additional notes are below. You may view the full set of changes in [#496](https://github.com/laravel-shift/blueprint/pull/496).

Expand Down
15 changes: 6 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
"name": "laravel-shift/blueprint",
"type": "library",
"description": "An expressive, human readable code generation tool.",
"keywords": [
"framework",
"laravel"
],
"keywords": ["framework", "laravel", "code generation"],
"license": "MIT",
"require": {
"doctrine/dbal": "^3.3",
"illuminate/console": "^9.0|^10.0",
"illuminate/filesystem": "^9.0|^10.0",
"illuminate/support": "^9.0|^10.0",
"illuminate/console": "^10.0",
"illuminate/filesystem": "^10.0",
"illuminate/support": "^10.0",
"laravel-shift/faker-registry": "^0.2.0",
"symfony/yaml": "^6.0"
"symfony/yaml": "^6.3"
},
"require-dev": {
"laravel/pint": "^1.2",
"mockery/mockery": "^1.4.4",
"orchestra/testbench": "^7.0|^8.0",
"orchestra/testbench": "^8.0",
"phpunit/phpunit": "^9.5.10"
},
"suggest": {
Expand Down
13 changes: 0 additions & 13 deletions config/blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,6 @@
*/
'fake_nullables' => true,

/*
|--------------------------------------------------------------------------
| Method Return Type Declarations
|--------------------------------------------------------------------------
|
| Enable or disable method return typehinting for blueprint generated
| methods. Enabling this will enforce code strictness which increases
| readability of code and will lower maintenance cost. This will only
| Work for projects running PHP v7.0 or higher.
|
*/
'use_return_types' => false,

/*
|--------------------------------------------------------------------------
| Use Guarded
Expand Down
5 changes: 0 additions & 5 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public static function appPath()
return str_replace('\\', '/', config('blueprint.app_path'));
}

public static function useReturnTypeHints()
{
return boolval(config('blueprint.use_return_types'));
}

public function parse($content, $strip_dashes = true)
{
$content = str_replace(["\r\n", "\r"], "\n", $content);
Expand Down
4 changes: 0 additions & 4 deletions src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class BuildCommand extends Command
/** @var Builder */
private $builder;

/**
* @param Filesystem $filesystem
* @param Builder $builder
*/
public function __construct(Filesystem $filesystem, Builder $builder)
{
parent::__construct();
Expand Down
3 changes: 0 additions & 3 deletions src/Commands/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ class NewCommand extends Command
/** @var Filesystem */
protected $filesystem;

/**
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
parent::__construct();
Expand Down
4 changes: 0 additions & 4 deletions src/Commands/TraceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class TraceCommand extends Command
/** @var Tracer */
private $tracer;

/**
* @param Filesystem $filesystem
* @param Tracer $tracer
*/
public function __construct(Filesystem $filesystem, Tracer $tracer)
{
parent::__construct();
Expand Down
34 changes: 12 additions & 22 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Concerns\HandlesImports;
use Blueprint\Concerns\HandlesTraits;
use Blueprint\Contracts\Generator;
use Blueprint\Models\Controller;
use Blueprint\Models\Model;
use Blueprint\Models\Statements\DispatchStatement;
use Blueprint\Models\Statements\EloquentStatement;
use Blueprint\Models\Statements\FireStatement;
Expand Down Expand Up @@ -72,10 +70,6 @@ protected function buildMethods(Controller $controller)
$reference = $this->fullyQualifyModelReference($controller->namespace(), Str::camel($context));
$variable = '$' . Str::camel($context);

// TODO: verify controller prefix references a model
$search = ' * @return \\Illuminate\\Http\\Response';
$method = str_replace($search, ' * @param \\' . $reference . ' ' . $variable . PHP_EOL . $search, $method);

$search = '(Request $request';
$method = str_replace($search, $search . ', ' . $context . ' ' . $variable, $method);
$this->addImport($controller, $reference);
Expand Down Expand Up @@ -116,7 +110,6 @@ protected function buildMethods(Controller $controller)
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof ResourceStatement) {
$fqcn = config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name();
$method = str_replace('* @return \\Illuminate\\Http\\Response', '* @return \\' . $fqcn, $method);
$this->addImport($controller, $fqcn);
$body .= self::INDENT . $statement->output() . PHP_EOL;

Expand Down Expand Up @@ -154,21 +147,18 @@ protected function buildMethods(Controller $controller)
$method = str_replace('{{ body }}', trim($body), $method);
}

if (Blueprint::useReturnTypeHints()) {
if (isset($fqcn) && $name !== 'destroy' && $controller->isApiResource()) {
$method = str_replace(')' . PHP_EOL, '): \\' . $fqcn . PHP_EOL, $method);
} else {
$returnType = match (true) {
$statement instanceof RenderStatement => 'Illuminate\View\View',
$statement instanceof RedirectStatement => 'Illuminate\Routing\Redirector',
default => 'Illuminate\Http\Response'
};

$method = Str::of($method)
->replace('* @return \\Illuminate\\Http\\Response', '* @return \\' . $returnType)
->replace(')' . PHP_EOL, '): \\' . $returnType . PHP_EOL)
->toString();
}
if ($statement instanceof RespondStatement && $statement->content()) {
$method = str_replace('): Response' . PHP_EOL, ')' . PHP_EOL, $method);
} else {
$returnType = match (true) {
$statement instanceof RenderStatement => 'Illuminate\View\View',
$statement instanceof RedirectStatement => 'Illuminate\Http\RedirectResponse',
$statement instanceof ResourceStatement => config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name(),
default => 'Illuminate\Http\Response'
};

$method = str_replace('): Response' . PHP_EOL, '): ' . Str::afterLast($returnType, '\\') . PHP_EOL, $method);
$this->addImport($controller, $returnType);
}

$methods .= PHP_EOL . $method;
Expand Down
6 changes: 0 additions & 6 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Concerns\HandlesImports;
use Blueprint\Concerns\HandlesTraits;
use Blueprint\Contracts\Generator;
Expand Down Expand Up @@ -54,11 +53,6 @@ protected function populateStub(string $stub, Model $model)
{
$stub = str_replace('{{ model }}', $model->name(), $stub);
$stub = str_replace('//', $this->buildDefinition($model), $stub);

if (Blueprint::useReturnTypeHints()) {
$stub = str_replace('definition()', 'definition(): array', $stub);
}

$stub = str_replace('{{ namespace }}', 'Database\Factories' . ($model->namespace() ? '\\' . $model->namespace() : ''), $stub);
$stub = str_replace('use {{ namespacedModel }};', $this->buildImports($model), $stub);

Expand Down
9 changes: 0 additions & 9 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Contracts\Generator;
use Blueprint\Models\Model;
use Blueprint\Tree;
Expand Down Expand Up @@ -109,10 +108,6 @@ protected function populateStub(string $stub, Model $model)
$stub = str_replace('{{ table }}', $model->tableName(), $stub);
$stub = str_replace('{{ definition }}', $this->buildDefinition($model), $stub);

if (Blueprint::useReturnTypeHints()) {
$stub = str_replace(['up()', 'down()'], ['up(): void', 'down(): void'], $stub);
}

if ($this->hasForeignKeyConstraints) {
$stub = $this->disableForeignKeyConstraints($stub);
}
Expand All @@ -137,10 +132,6 @@ protected function populatePolyStub(string $stub, string $parentTable)
$stub = str_replace('{{ table }}', $this->getPolyTableName($parentTable), $stub);
$stub = str_replace('{{ definition }}', $this->buildPolyTableDefinition($parentTable), $stub);

if (Blueprint::useReturnTypeHints()) {
$stub = str_replace(['up()', 'down()'], ['up(): void', 'down(): void'], $stub);
}

if ($this->hasForeignKeyConstraints) {
$stub = $this->disableForeignKeyConstraints($stub);
}
Expand Down
36 changes: 18 additions & 18 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Concerns\HandlesImports;
use Blueprint\Contracts\Generator;
use Blueprint\Models\Column;
use Blueprint\Models\Model;
Expand All @@ -11,6 +11,8 @@

class ModelGenerator extends AbstractClassGenerator implements Generator
{
use HandlesImports;

protected $types = ['models'];

public function output(Tree $tree): array
Expand Down Expand Up @@ -43,7 +45,9 @@ protected function populateStub(string $stub, Model $model)
{
if ($model->isPivot()) {
$stub = str_replace('class {{ class }} extends Model', 'class {{ class }} extends Pivot', $stub);
$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Relations\\Pivot;', $stub);
$this->addImport($model, 'Illuminate\\Database\\Eloquent\\Relations\\Pivot');
} else {
$this->addImport($model, 'Illuminate\\Database\\Eloquent\\Model');
}

$stub = str_replace('{{ namespace }}', $model->fullyQualifiedNamespace(), $stub);
Expand All @@ -54,9 +58,11 @@ protected function populateStub(string $stub, Model $model)
$body .= PHP_EOL . PHP_EOL;
$body .= $this->buildRelationships($model);

$this->addImport($model, 'Illuminate\\Database\\Eloquent\\Factories\\HasFactory');
$stub = str_replace('use HasFactory;', 'use HasFactory;' . PHP_EOL . PHP_EOL . ' ' . trim($body), $stub);

$stub = $this->addTraits($model, $stub);
$stub = str_replace('{{ imports }}', $this->buildImports($model), $stub);

return $stub;
}
Expand Down Expand Up @@ -161,11 +167,6 @@ protected function buildRelationships(Model $model)
{
$methods = '';
$template = $this->filesystem->stub('model.method.stub');
$commentTemplate = '';

if (config('blueprint.generate_phpdocs')) {
$commentTemplate = $this->filesystem->stub('model.method.comment.stub');
}

foreach ($model->relationships() as $type => $references) {
foreach ($references as $reference) {
Expand Down Expand Up @@ -250,19 +251,18 @@ protected function buildRelationships(Model $model)
$method_name = Str::plural($is_model_fqn ? Str::afterLast($column_name, '\\') : $column_name);
}

if (Blueprint::useReturnTypeHints()) {
$custom_template = str_replace(
'{{ method }}()',
'{{ method }}(): ' . Str::of('\Illuminate\Database\Eloquent\Relations\\')->append(Str::studly($type)),
$custom_template
);
}
$relationship_type = 'Illuminate\\Database\\Eloquent\\Relations\\' . Str::studly($type === 'morphedByMany' ? 'morphToMany' : $type);
$this->addImport($model, $relationship_type);
$custom_template = str_replace(
'{{ method }}()',
'{{ method }}(): ' . Str::afterLast($relationship_type, '\\'),
$custom_template
);

$method = str_replace('{{ method }}', Str::camel($method_name), $custom_template);
$method = str_replace('null', $relationship, $method);

$phpDoc = str_replace('{{ namespacedReturnClass }}', '\Illuminate\Database\Eloquent\Relations\\' . Str::ucfirst($type), $commentTemplate);

$methods .= $phpDoc . $method . PHP_EOL;
$methods .= $method . PHP_EOL;
}
}

Expand All @@ -275,7 +275,7 @@ protected function addTraits(Model $model, $stub)
return $stub;
}

$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;' . PHP_EOL . 'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub);
$this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes');
$stub = Str::replaceFirst('use HasFactory', 'use HasFactory, SoftDeletes', $stub);

return $stub;
Expand Down
5 changes: 0 additions & 5 deletions src/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Concerns\HandlesImports;
use Blueprint\Concerns\HandlesTraits;
use Blueprint\Contracts\Generator;
Expand Down Expand Up @@ -48,10 +47,6 @@ protected function populateStub(string $stub, BlueprintModel $model)
$stub = str_replace('//', $this->build($model), $stub);
$stub = str_replace('use Illuminate\Database\Seeder;', $this->buildImports($model), $stub);

if (Blueprint::useReturnTypeHints()) {
$stub = str_replace('public function run()', 'public function run(): void', $stub);
}

return $stub;
}

Expand Down
4 changes: 0 additions & 4 deletions src/Generators/Statements/FormRequestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ protected function populateStub(string $stub, string $name, $context, ValidateSt
$stub = str_replace('{{ class }}', $name, $stub);
$stub = str_replace('{{ rules }}', $this->buildRules($context, $validateStatement), $stub);

if (Blueprint::useReturnTypeHints()) {
$stub = str_replace(['authorize()', 'rules()'], ['authorize(): bool', 'rules(): array'], $stub);
}

return $stub;
}

Expand Down
Loading

0 comments on commit 884e717

Please sign in to comment.