Description
What needs to be done?
Implement a new method addMiddlewarePrependToGroup(string $group, array|string $middleware, InsertPositionEnum $position = InsertPositionEnum::End): self in AppBootstrapBuilder.
This method must update bootstrap/app.php and add the call $middleware->prependToGroup($group, [...]) inside the withMiddleware(...) closure in the Application::configure(...) chain.
Behavior rules:
If withMiddleware(...) does not exist in bootstrap/app.php:
- Create
->withMiddleware(function (Middleware $middleware): void { ... }) inside the Application::configure(...) method chain. - Add
$middleware->prependToGroup($group, [...]) inside that closure.
If withMiddleware(...) exists, but there is no prependToGroup call for the target group:
- Add a new call to
$middleware->prependToGroup($group, [...]) inside the existing closure.
If prependToGroup for the target group already exists:
- Append the passed middleware values to the existing array.
- Do not add duplicates.
- If a passed middleware already exists in the array, skip it.
If $middleware is passed as a single string:
- Treat it as an array with one item.
If all passed middleware values already exist in the target prependToGroup call:
- Do not change the file content.
If prependToGroup exists for other groups:
- Do not modify those calls.
- Add or update only the call for the requested group.
If the parsed bootstrap/app.php file contains a class, trait, interface, or enum declaration:
- Throw
InvalidBootstrapAppFileException.
Implementation requirements:
- Automatically add import for
Illuminate\Foundation\Configuration\Middleware if it is missing. - Preserve the original file formatting.
Expected Outcome
What is the expected result?
The project has a working addMiddlewarePrependToGroup method in AppBootstrapBuilder that safely updates bootstrap/app.php.
Result expectations:
- A new visitor
AddMiddlewarePrependToGroup extended from AbstractAppBootstrapVisitor is added in src/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php. AppBootstrapBuilder exposes the new public method.- The method creates
withMiddleware(...) when it is missing. - The method adds a new
prependToGroup call when the target group does not exist. - The method updates an existing
prependToGroup call for the target group without duplicates. - The method supports both
array and string input. - The method imports
Illuminate\Foundation\Configuration\Middleware automatically if needed. - The method preserves file formatting and does not modify unrelated code.
- Invalid bootstrap file structures cause
InvalidBootstrapAppFileException.
Verification Scenarios
How can this be tested?
withMiddleware(...) is missing:
- Run
addMiddlewarePrependToGroup('api', [AutoDocMiddleware::class, 'throttle:60,1']). - Verify that
withMiddleware(function (Middleware $middleware): void { ... }) is created in bootstrap/app.php. - Verify that
$middleware->prependToGroup('api', [...]) is added inside it.
withMiddleware(...) exists, but prependToGroup('api', ...) does not exist:
- Run the method.
- Verify that a new
prependToGroup('api', [...]) call is added to the end of existing closure. - Run the method with the start position arg.
- Verify that a new
prependToGroup('api', [...]) call is added to the start of existing closure.
prependToGroup('api', ...) exists and none of the passed middleware are present:
- Verify that all new middleware are appended to the existing array.
prependToGroup('api', ...) exists and some passed middleware are already present:
- Verify that only missing middleware are added.
- Verify that duplicates are not created.
prependToGroup('api', ...) exists and all passed middleware are already present:
- Verify that the file remains unchanged.
prependToGroup(...) exists for another group, for example web:
- Run the method for
api. - Verify that the
web group is not changed. - Verify that a new
api call is added or updated correctly.
Pass a single string instead of an array:
- Run
addMiddlewarePrependToGroup('api', 'throttle:60,1'). - Verify that the result matches behavior for a one-item array.
Pass duplicated values in the input array:
- Example:
['throttle:60,1', 'throttle:60,1'] - Verify that only one value is added.
File contains class, trait, interface, or enum:
- Verify that the method throws
InvalidBootstrapAppFileException.
Missing import for Illuminate\Foundation\Configuration\Middleware:
- Verify that the import is added automatically.
Existing import already present:
- Verify that the import is not duplicated.
Formatting:
- Verify that the original formatting is preserved after save.
- Add automated tests for all scenarios above.
- Ensure no debug information or errors in the console.
Description
What needs to be done?
Implement a new method
addMiddlewarePrependToGroup(string $group, array|string $middleware, InsertPositionEnum $position = InsertPositionEnum::End): selfin AppBootstrapBuilder.This method must update
bootstrap/app.phpand add the call$middleware->prependToGroup($group, [...])inside thewithMiddleware(...)closure in theApplication::configure(...)chain.Behavior rules:
If
withMiddleware(...)does not exist inbootstrap/app.php:->withMiddleware(function (Middleware $middleware): void { ... })inside theApplication::configure(...)method chain.$middleware->prependToGroup($group, [...])inside that closure.If
withMiddleware(...)exists, but there is noprependToGroupcall for the target group:$middleware->prependToGroup($group, [...])inside the existing closure.If
prependToGroupfor the target group already exists:If
$middlewareis passed as a single string:If all passed middleware values already exist in the target
prependToGroupcall:If
prependToGroupexists for other groups:If the parsed
bootstrap/app.phpfile contains a class, trait, interface, or enum declaration:InvalidBootstrapAppFileException.Implementation requirements:
Illuminate\Foundation\Configuration\Middlewareif it is missing.Expected Outcome
What is the expected result?
The project has a working
addMiddlewarePrependToGroupmethod inAppBootstrapBuilderthat safely updatesbootstrap/app.php.Result expectations:
AddMiddlewarePrependToGroupextended fromAbstractAppBootstrapVisitoris added insrc/Visitors/AppBootstrapVisitors/AddMiddlewarePrependToGroup.php.AppBootstrapBuilderexposes the new public method.withMiddleware(...)when it is missing.prependToGroupcall when the target group does not exist.prependToGroupcall for the target group without duplicates.arrayandstringinput.Illuminate\Foundation\Configuration\Middlewareautomatically if needed.InvalidBootstrapAppFileException.Verification Scenarios
How can this be tested?
withMiddleware(...)is missing:addMiddlewarePrependToGroup('api', [AutoDocMiddleware::class, 'throttle:60,1']).withMiddleware(function (Middleware $middleware): void { ... })is created inbootstrap/app.php.$middleware->prependToGroup('api', [...])is added inside it.withMiddleware(...)exists, butprependToGroup('api', ...)does not exist:prependToGroup('api', [...])call is added to the end of existing closure.prependToGroup('api', [...])call is added to the start of existing closure.prependToGroup('api', ...)exists and none of the passed middleware are present:prependToGroup('api', ...)exists and some passed middleware are already present:prependToGroup('api', ...)exists and all passed middleware are already present:prependToGroup(...)exists for another group, for exampleweb:api.webgroup is not changed.apicall is added or updated correctly.Pass a single string instead of an array:
addMiddlewarePrependToGroup('api', 'throttle:60,1').Pass duplicated values in the input array:
['throttle:60,1', 'throttle:60,1']File contains class, trait, interface, or enum:
InvalidBootstrapAppFileException.Missing import for
Illuminate\Foundation\Configuration\Middleware:Existing import already present:
Formatting: