From 8fc6e25ea58cff87902c42ec8695fb70d680a7c3 Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 24 Jun 2026 20:21:18 +0500 Subject: [PATCH 1/6] feat: ability print array in multiline format refs: https://github.com/RonasIT/larabuilder/issues/74 --- src/Enums/ExpressionAttributeEnum.php | 8 +++++++ src/Printer.php | 20 ++++------------ src/Support/NodeValueFactory.php | 24 +++++++++---------- .../AddMiddlewarePrependToGroup.php | 6 ++++- .../PropertyVisitors/AddArrayPropertyItem.php | 5 +++- src/Visitors/PropertyVisitors/SetProperty.php | 7 +++++- .../bootstrap_with_prepend_group.php | 10 ++++++-- ...bootstrap_with_prepend_group_as_string.php | 5 +++- ...with_prepend_group_as_string_set_class.php | 5 +++- ...ootstrap_without_changed_prepend_group.php | 5 +++- 10 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 src/Enums/ExpressionAttributeEnum.php diff --git a/src/Enums/ExpressionAttributeEnum.php b/src/Enums/ExpressionAttributeEnum.php new file mode 100644 index 0000000..d13625a --- /dev/null +++ b/src/Enums/ExpressionAttributeEnum.php @@ -0,0 +1,8 @@ +hasParentOfType($node, PropertyItem::class)) { + $isMultiline = $node->getAttribute(ExpressionAttributeEnum::SetArrayMultiline->value, false); + + if ($isMultiline) { return '[' . $this->pCommaSeparatedMultiline($node->items, true) . $this->nl . ']'; } return parent::pExpr_Array($node); } - protected function hasParentOfType(Node $node, string $type): bool - { - $parent = $node->getAttribute(StatementAttributeEnum::Parent->value); - - while ($parent !== null) { - if ($parent instanceof $type) { - return true; - } - - $parent = $parent->getAttribute(StatementAttributeEnum::Parent->value); - } - - return false; - } - protected function pStmt_Property(Property $node): string { $newLine = ($this->shouldAddNewlineBeforeIfTypeDiffers($node, Property::class)) ? $this->nl : ''; diff --git a/src/Support/NodeValueFactory.php b/src/Support/NodeValueFactory.php index 405ebff..dd90c87 100644 --- a/src/Support/NodeValueFactory.php +++ b/src/Support/NodeValueFactory.php @@ -14,37 +14,37 @@ class NodeValueFactory { - public static function make(mixed $value): NodeValueDTO + public static function make(mixed $value, array $attributes = []): NodeValueDTO { $type = get_debug_type($value); $node = match ($type) { - 'int' => new Int_($value), - 'array' => static::makeArrayValue($value), - 'string' => new String_($value), - 'float' => new Float_($value), - 'bool' => static::makeBoolValue($value), - 'null' => new ConstFetch(new Name('null')), + 'int' => new Int_($value, $attributes), + 'array' => static::makeArrayValue($value, $attributes), + 'string' => new String_($value, $attributes), + 'float' => new Float_($value, $attributes), + 'bool' => static::makeBoolValue($value, $attributes), + 'null' => new ConstFetch(new Name('null'), $attributes), }; return new NodeValueDTO($node, new Identifier($type)); } - protected static function makeBoolValue(bool $value): ConstFetch + protected static function makeBoolValue(bool $value, array $attributes): ConstFetch { $name = new Name($value ? 'true' : 'false'); - return new ConstFetch($name); + return new ConstFetch($name, $attributes); } - protected static function makeArrayValue(array $values): Array_ + protected static function makeArrayValue(array $values, array $attributes): Array_ { $items = []; foreach ($values as $key => $val) { - $items[] = new ArrayItem(static::make($val)->node, static::make($key)->node); + $items[] = new ArrayItem(static::make($val, $attributes)->node, static::make($key)->node); } - return new Array_($items); + return new Array_($items, $attributes); } } diff --git a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php index ea3ca9f..40f9a00 100644 --- a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php +++ b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php @@ -14,7 +14,9 @@ use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Nop; +use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum; use RonasIT\Larabuilder\Enums\InsertPositionEnum; +use RonasIT\Larabuilder\Enums\StatementAttributeEnum; class AddMiddlewarePrependToGroup extends AbstractAppBootstrapVisitor { @@ -125,7 +127,9 @@ protected function buildPrependToGroupCall(): Expression protected function buildMiddlewareArg(array $middlewares): Arg { - return new Arg(new Array_($middlewares)); + return new Arg(new Array_($middlewares, [ + ExpressionAttributeEnum::SetArrayMultiline->value => true, + ])); } protected function getMiddlewareList(): array diff --git a/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php b/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php index aab7085..55a636b 100644 --- a/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php +++ b/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php @@ -8,6 +8,7 @@ use PhpParser\Node\Identifier; use PhpParser\Node\PropertyItem; use PhpParser\Node\Stmt\Property; +use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum; use RonasIT\Larabuilder\Exceptions\UnexpectedPropertyTypeException; class AddArrayPropertyItem extends SetProperty @@ -21,7 +22,9 @@ public function __construct( parent::__construct($name, $value); $this->arrayItem = new ArrayItem($this->property->node); - $arrayNode = new Array_([$this->arrayItem]); + $arrayNode = new Array_([$this->arrayItem], [ + ExpressionAttributeEnum::SetArrayMultiline->value => true, + ]); $this->propertyItem = new PropertyItem($this->name, $arrayNode); $this->typeIdentifier = new Identifier('array'); diff --git a/src/Visitors/PropertyVisitors/SetProperty.php b/src/Visitors/PropertyVisitors/SetProperty.php index d8d1343..4b288b3 100644 --- a/src/Visitors/PropertyVisitors/SetProperty.php +++ b/src/Visitors/PropertyVisitors/SetProperty.php @@ -9,6 +9,7 @@ use RonasIT\Larabuilder\Contracts\InsertNodeContract; use RonasIT\Larabuilder\DTO\NodeValueDTO; use RonasIT\Larabuilder\Enums\AccessModifierEnum; +use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum; use RonasIT\Larabuilder\Support\NodeValueFactory; class SetProperty extends AbstractPropertyVisitor implements InsertNodeContract @@ -24,7 +25,11 @@ public function __construct( ) { parent::__construct($name); - $this->property = NodeValueFactory::make($value); + $attributes = is_array($value) + ? [ExpressionAttributeEnum::SetArrayMultiline->value => true] + : []; + + $this->property = NodeValueFactory::make($value, $attributes); $this->propertyItem = new PropertyItem($this->name, $this->property->node); $this->typeIdentifier = $this->property->typeNode; diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php index 9dd5260..7f2c60d 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group.php @@ -12,8 +12,14 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', ['throttle:60,10', FakeClass::class]); - $middleware->prependToGroup('web', ['throttle:10,10', FakeClass::class]); + $middleware->prependToGroup('api', [ + 'throttle:60,10', + FakeClass::class, + ]); + $middleware->prependToGroup('web', [ + 'throttle:10,10', + FakeClass::class, + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string.php index bd7306b..820332e 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string.php @@ -11,7 +11,10 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', ['throttle:60,10', 'some_middleware']); + $middleware->prependToGroup('api', [ + 'throttle:60,10', + 'some_middleware', + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string_set_class.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string_set_class.php index bed591c..39e6ac4 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string_set_class.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_with_prepend_group_as_string_set_class.php @@ -11,7 +11,10 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', [\Illuminate\Auth\Middleware\Authenticate::class, 'some_middleware']); + $middleware->prependToGroup('api', [ + \Illuminate\Auth\Middleware\Authenticate::class, + 'some_middleware', + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php index 0052a0e..691f34f 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php @@ -12,7 +12,10 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', ['throttle:60,10', Authenticate::class]); + $middleware->prependToGroup('api', [ + 'throttle:60,10', + Authenticate::class, + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // From 5a260ef36db44cb14c1ca1643aba0eb2753707c9 Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 24 Jun 2026 20:28:34 +0500 Subject: [PATCH 2/6] feat: ability print array in multiline format refs: https://github.com/RonasIT/larabuilder/issues/74 --- src/Enums/ExpressionAttributeEnum.php | 2 +- src/Printer.php | 2 +- .../AppBootstrapVisitors/AddMiddlewarePrependToGroup.php | 2 +- src/Visitors/PropertyVisitors/AddArrayPropertyItem.php | 2 +- src/Visitors/PropertyVisitors/SetProperty.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Enums/ExpressionAttributeEnum.php b/src/Enums/ExpressionAttributeEnum.php index d13625a..32ca234 100644 --- a/src/Enums/ExpressionAttributeEnum.php +++ b/src/Enums/ExpressionAttributeEnum.php @@ -4,5 +4,5 @@ enum ExpressionAttributeEnum: string { - case SetArrayMultiline = 'set-array-multiline'; + case IsArrayMultiline = 'is_array_multiline'; } diff --git a/src/Printer.php b/src/Printer.php index 163683d..2bcb230 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -42,7 +42,7 @@ protected function removeDuplicateEmptyLines(string $code): string protected function pExpr_Array(Array_ $node): string { - $isMultiline = $node->getAttribute(ExpressionAttributeEnum::SetArrayMultiline->value, false); + $isMultiline = $node->getAttribute(ExpressionAttributeEnum::IsArrayMultiline->value, false); if ($isMultiline) { return '[' . $this->pCommaSeparatedMultiline($node->items, true) . $this->nl . ']'; diff --git a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php index 40f9a00..d9aaaea 100644 --- a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php +++ b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php @@ -128,7 +128,7 @@ protected function buildPrependToGroupCall(): Expression protected function buildMiddlewareArg(array $middlewares): Arg { return new Arg(new Array_($middlewares, [ - ExpressionAttributeEnum::SetArrayMultiline->value => true, + ExpressionAttributeEnum::IsArrayMultiline->value => true, ])); } diff --git a/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php b/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php index 55a636b..fc1d0bf 100644 --- a/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php +++ b/src/Visitors/PropertyVisitors/AddArrayPropertyItem.php @@ -23,7 +23,7 @@ public function __construct( $this->arrayItem = new ArrayItem($this->property->node); $arrayNode = new Array_([$this->arrayItem], [ - ExpressionAttributeEnum::SetArrayMultiline->value => true, + ExpressionAttributeEnum::IsArrayMultiline->value => true, ]); $this->propertyItem = new PropertyItem($this->name, $arrayNode); diff --git a/src/Visitors/PropertyVisitors/SetProperty.php b/src/Visitors/PropertyVisitors/SetProperty.php index 4b288b3..040be68 100644 --- a/src/Visitors/PropertyVisitors/SetProperty.php +++ b/src/Visitors/PropertyVisitors/SetProperty.php @@ -26,7 +26,7 @@ public function __construct( parent::__construct($name); $attributes = is_array($value) - ? [ExpressionAttributeEnum::SetArrayMultiline->value => true] + ? [ExpressionAttributeEnum::IsArrayMultiline->value => true] : []; $this->property = NodeValueFactory::make($value, $attributes); From 9071f83eb194369508c0c078154ada16222ebf20 Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 24 Jun 2026 20:48:59 +0500 Subject: [PATCH 3/6] feat: ability print array in multiline format refs: https://github.com/RonasIT/larabuilder/issues/74 --- src/Printer.php | 1 - .../AppBootstrapVisitors/AddMiddlewarePrependToGroup.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Printer.php b/src/Printer.php index 2bcb230..fd630de 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -4,7 +4,6 @@ use PhpParser\Node; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\PropertyItem; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Property; diff --git a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php index d9aaaea..a1b4b62 100644 --- a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php +++ b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php @@ -16,7 +16,6 @@ use PhpParser\Node\Stmt\Nop; use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum; use RonasIT\Larabuilder\Enums\InsertPositionEnum; -use RonasIT\Larabuilder\Enums\StatementAttributeEnum; class AddMiddlewarePrependToGroup extends AbstractAppBootstrapVisitor { From a052ba834c6319be8333754cbede4e20ba246cad Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Mon, 29 Jun 2026 16:29:38 +0500 Subject: [PATCH 4/6] fix: compare middlewares refs: https://github.com/RonasIT/larabuilder/issues/74 --- .gitignore | 1 + .../AddMiddlewarePrependToGroup.php | 66 +++++++++++++------ tests/AppBootstrapBuilderTest.php | 1 + .../bootstrap_with_prepend_group.php | 7 +- ...ootstrap_without_changed_prepend_group.php | 2 + 5 files changed, 55 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 5dae3d3..4d804f2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /.idea .phpunit.result.cache .phpunit.cache +composer.lock diff --git a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php index a1b4b62..adf3f99 100644 --- a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php +++ b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php @@ -2,6 +2,7 @@ namespace RonasIT\Larabuilder\Visitors\AppBootstrapVisitors; +use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Array_; @@ -11,14 +12,18 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Name; +use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Nop; +use PhpParser\Node\UseItem; use RonasIT\Larabuilder\Enums\ExpressionAttributeEnum; use RonasIT\Larabuilder\Enums\InsertPositionEnum; class AddMiddlewarePrependToGroup extends AbstractAppBootstrapVisitor { + protected array $originalNamespaces = []; + public function __construct( protected string $group, protected array $middlewares, @@ -30,6 +35,18 @@ public function __construct( ); } + public function leaveNode(Node $node): Node + { + if ($node instanceof UseItem) { + $this->originalNamespaces[] = [ + 'namespace' => $node->name->toString(), + 'alias' => $node->alias?->toString() ?? null, + ]; + } + + return parent::leaveNode($node); + } + protected function insertNode(MethodCall $node): MethodCall { /** @var Closure $closure */ @@ -66,50 +83,57 @@ protected function findMiddlewareGroupIndex(array $stmts): ?int protected function updateMiddlewareGroup(Closure $closure, int $groupIndex): void { - $originalMiddlewares = $closure->stmts[$groupIndex]->expr->args[1]->value->value - ?? $closure->stmts[$groupIndex]->expr->args[1]->value->class->name - ?? $closure->stmts[$groupIndex]->expr->args[1]->value->items; + $middlewares = $closure->stmts[$groupIndex]->expr->args[1]; - $originalMiddlewares = is_array($originalMiddlewares) - ? $originalMiddlewares - : [new ArrayItem($closure->stmts[$groupIndex]->expr->args[1]->value)]; + $originalMiddlewares = ($middlewares->value instanceof Array_) + ? $middlewares->value->items + : [new ArrayItem($middlewares->value)]; $mergedMiddlewares = $this->mergeMiddlewares($originalMiddlewares); $closure->stmts[$groupIndex]->expr->args[1] = $this->buildMiddlewareArg($mergedMiddlewares); } - protected function mergeMiddlewares(array $originalMiddlewareList): array + protected function mergeMiddlewares(array $originMiddlewares): array { + $originalResolved = array_map(fn ($middleware) => $this->resolveMiddlewareName($middleware), $originMiddlewares); + $filteredNewList = []; foreach ($this->middlewares as $middleware) { - $sameMiddlewareKey = array_find_key( - $originalMiddlewareList, - fn ($originalMiddleware) => $this->isSameMiddleware($middleware, $originalMiddleware), - ); - - if (is_null($sameMiddlewareKey)) { + if (!in_array($middleware, $originalResolved)) { $filteredNewList[] = $this->makeArrayItem($middleware); } } return match ($this->position) { - InsertPositionEnum::Start => [...$filteredNewList, ...$originalMiddlewareList], - InsertPositionEnum::End => [...$originalMiddlewareList, ...$filteredNewList], + InsertPositionEnum::Start => [...$filteredNewList, ...$originMiddlewares], + InsertPositionEnum::End => [...$originMiddlewares, ...$filteredNewList], }; } - protected function isSameMiddleware(string $newMiddleware, ArrayItem $originalMiddleware): bool + protected function resolveMiddlewareName($middleware): ?string { - if ($originalMiddleware->value instanceof ClassConstFetch) { - $originalName = $originalMiddleware->value->class->name; + if ($middleware->value instanceof String_) { + return $middleware->value->value; + } + + $isClass = $middleware->value instanceof ClassConstFetch; + + if ($isClass && $middleware->value->class instanceof FullyQualified) { + return $middleware->value->class->name; + } + + if ($isClass) { + $found = array_find($this->originalNamespaces, function (array $namespace) use ($middleware) { + return $middleware->value->class->name === class_basename($namespace['namespace']) + || $middleware->value->class->name === $namespace['alias']; + }); - return $originalName === $newMiddleware - || $originalName === class_basename($newMiddleware); + return $found['namespace'] ?? null; } - return $originalMiddleware->value->value === $newMiddleware; + return null; } protected function buildPrependToGroupCall(): Expression diff --git a/tests/AppBootstrapBuilderTest.php b/tests/AppBootstrapBuilderTest.php index 8c02c15..59ccb74 100644 --- a/tests/AppBootstrapBuilderTest.php +++ b/tests/AppBootstrapBuilderTest.php @@ -183,6 +183,7 @@ public function testAddMiddlewarePrependToGroupExistsMiddlewares() ->addMiddlewarePrependToGroup('api', [ 'throttle:60,10', Authenticate::class, + \Illuminate\Routing\Controllers\Middleware::class, ]) ->save(); } diff --git a/tests/Support/OriginStructures/bootstrap_with_prepend_group.php b/tests/Support/OriginStructures/bootstrap_with_prepend_group.php index 0052a0e..6a18bd7 100644 --- a/tests/Support/OriginStructures/bootstrap_with_prepend_group.php +++ b/tests/Support/OriginStructures/bootstrap_with_prepend_group.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; use Illuminate\Auth\Middleware\Authenticate; +use Illuminate\Routing\Controllers\Middleware as SomeMiddleware; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( @@ -12,7 +13,11 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - $middleware->prependToGroup('api', ['throttle:60,10', Authenticate::class]); + $middleware->prependToGroup('api', [ + 'throttle:60,10', + Authenticate::class, + SomeMiddleware::class, + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php index 691f34f..6a18bd7 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; use Illuminate\Auth\Middleware\Authenticate; +use Illuminate\Routing\Controllers\Middleware as SomeMiddleware; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( @@ -15,6 +16,7 @@ $middleware->prependToGroup('api', [ 'throttle:60,10', Authenticate::class, + SomeMiddleware::class, ]); }) ->withExceptions(function (Exceptions $exceptions): void { From d811e0ec5c1b3c8293306af08dd9079f0aa51adf Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 1 Jul 2026 18:42:24 +0500 Subject: [PATCH 5/6] fix: compare middlewares refs: https://github.com/RonasIT/larabuilder/issues/74 --- README.md | 3 ++- .../AddMiddlewarePrependToGroup.php | 22 +++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b9fa114..96ba2ac 100644 --- a/README.md +++ b/README.md @@ -115,8 +115,9 @@ new AppBootstrapBuilder(bootstrap_path('app.php')) MyMiddleware::class, 'throttle:60,1', ]) - ->addMiddlewarePrependToGroup('web', WebMiddleware::class,, InsertPositionEnum::Start) + ->addMiddlewarePrependToGroup('web', WebMiddleware::class, InsertPositionEnum::Start) ->save(); +``` **Note:** Provide the full class name (FQCN) for class-based middleware — the method imports it automatically. diff --git a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php index adf3f99..cad90be 100644 --- a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php +++ b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php @@ -22,7 +22,7 @@ class AddMiddlewarePrependToGroup extends AbstractAppBootstrapVisitor { - protected array $originalNamespaces = []; + protected array $importedNamespaces = []; public function __construct( protected string $group, @@ -38,7 +38,7 @@ public function __construct( public function leaveNode(Node $node): Node { if ($node instanceof UseItem) { - $this->originalNamespaces[] = [ + $this->importedNamespaces[] = [ 'namespace' => $node->name->toString(), 'alias' => $node->alias?->toString() ?? null, ]; @@ -85,11 +85,11 @@ protected function updateMiddlewareGroup(Closure $closure, int $groupIndex): voi { $middlewares = $closure->stmts[$groupIndex]->expr->args[1]; - $originalMiddlewares = ($middlewares->value instanceof Array_) + $originMiddlewares = ($middlewares->value instanceof Array_) ? $middlewares->value->items : [new ArrayItem($middlewares->value)]; - $mergedMiddlewares = $this->mergeMiddlewares($originalMiddlewares); + $mergedMiddlewares = $this->mergeMiddlewares($originMiddlewares); $closure->stmts[$groupIndex]->expr->args[1] = $this->buildMiddlewareArg($mergedMiddlewares); } @@ -101,7 +101,7 @@ protected function mergeMiddlewares(array $originMiddlewares): array $filteredNewList = []; foreach ($this->middlewares as $middleware) { - if (!in_array($middleware, $originalResolved)) { + if (!in_array($middleware, $originalResolved, true)) { $filteredNewList[] = $this->makeArrayItem($middleware); } } @@ -125,7 +125,7 @@ protected function resolveMiddlewareName($middleware): ?string } if ($isClass) { - $found = array_find($this->originalNamespaces, function (array $namespace) use ($middleware) { + $found = array_find($this->importedNamespaces, function (array $namespace) use ($middleware) { return $middleware->value->class->name === class_basename($namespace['namespace']) || $middleware->value->class->name === $namespace['alias']; }); @@ -148,6 +148,11 @@ protected function buildPrependToGroupCall(): Expression return new Expression($methodCall); } + protected function getMiddlewareList(): array + { + return array_map(fn ($middleware) => $this->makeArrayItem($middleware), $this->middlewares); + } + protected function buildMiddlewareArg(array $middlewares): Arg { return new Arg(new Array_($middlewares, [ @@ -155,11 +160,6 @@ protected function buildMiddlewareArg(array $middlewares): Arg ])); } - protected function getMiddlewareList(): array - { - return array_map(fn ($middleware) => $this->makeArrayItem($middleware), $this->middlewares); - } - protected function makeArrayItem(string $middleware): ArrayItem { if (class_exists($middleware)) { From bed87d2f6d14bbcf433dba052177e4fda1ba857d Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Fri, 10 Jul 2026 10:26:25 +0500 Subject: [PATCH 6/6] fix: compare middlewares refs: https://github.com/RonasIT/larabuilder/issues/74 --- .../AppBootstrapVisitors/AddMiddlewarePrependToGroup.php | 5 +++-- .../OriginStructures/bootstrap_with_prepend_group.php | 4 ++++ .../bootstrap_without_changed_prepend_group.php | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php index cad90be..7bfc66a 100644 --- a/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php +++ b/src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php @@ -74,8 +74,9 @@ protected function removeNopPlaceholder(Closure $closure): void protected function findMiddlewareGroupIndex(array $stmts): ?int { - return array_find_key($stmts, function (Expression $stmt) { - return !empty($stmt->expr->name) + return array_find_key($stmts, function (Node $stmt) { + return $stmt instanceof Expression + && !empty($stmt->expr->name) && $stmt->expr->name->toString() === $this->targetMethod && $stmt->expr->args[0]->value->value === $this->group; }); diff --git a/tests/Support/OriginStructures/bootstrap_with_prepend_group.php b/tests/Support/OriginStructures/bootstrap_with_prepend_group.php index 6a18bd7..09bad76 100644 --- a/tests/Support/OriginStructures/bootstrap_with_prepend_group.php +++ b/tests/Support/OriginStructures/bootstrap_with_prepend_group.php @@ -13,6 +13,10 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { + if (app()->environment('production')) { + $middleware->append('auth'); + } + $middleware->prependToGroup('api', [ 'throttle:60,10', Authenticate::class, diff --git a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php index 6a18bd7..09bad76 100644 --- a/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php +++ b/tests/fixtures/AppBootstrapBuilderTest/bootstrap_without_changed_prepend_group.php @@ -13,6 +13,10 @@ health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { + if (app()->environment('production')) { + $middleware->append('auth'); + } + $middleware->prependToGroup('api', [ 'throttle:60,10', Authenticate::class,