You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: resolve the source location for generators with --path - #592
HasPathSupport::resolveCustomPath() returns only type, path and namespace, while ResolvesLocation::getResolvedFilePath() reads $location['source_path'] (and getResolvedNamespace() reads $location['source_namespace']) unconditionally. Every other resolver (resolveDefaultLocation(), the theme, plugin and module ones) sets both keys, so only the --path location crashes.
Fix
Set source_path (the resolved absolute path) and source_namespace in resolveCustomPath(), matching the shape the other resolvers return, so --path generates the file in the given folder again.
Testing
Added it generates the class in the custom path with --path to tests/Feature/Hook/ActionMakeCommandTest.php: it failed before the fix with Undefined array key "source_path" at ResolvesLocation.php:53 and passes after it, asserting the class file is created under the custom folder.
vendor/bin/pest tests/Feature/Hook/ActionMakeCommandTest.php: 4 passed (the 3 pre-existing tests still pass).
Full vendor/bin/pest run shows the same pre-existing (environment-related) failures as on a clean main; vendor/bin/pint --test passes on both changed files.
Thanks @bfs2021, and thank you for taking on part 1 of #591. I changed the base branch to develop (fixes go through develop and reach main with a release).
The crash is gone and your diagnosis is right. One thing the test can't see: with --path, the generated file gets an invalid namespace, e.g. namespace AppCms\Hooks;. source_namespace is 'App' without a trailing backslash, while resolveDefaultLocation() returns app()->getNamespace() (App\) and getResolvedNamespace() concatenates the sub-path directly. I checked it with make:action, make:filter, make:post-type, make:taxonomy, make:wp-cli, make:role, make:model and make:binding.
Could you set 'source_namespace' => app()->getNamespace() and make the test assert the namespace line of the generated file, not just that the file exists? Whether the sub-path should be appended to --path is a design question I'll settle separately; no need to change it here.
Done in 4c1a63f — resolveCustomPath() now uses app()->getNamespace() for source_namespace, so the sub-path concatenation yields App\Cms\Hooks instead of the invalid AppCms\Hooks.
The --path test now asserts the namespace line of the generated file (namespace App\Cms\Hooks; present, namespace AppCms\ absent) — it fails against the previous commit and passes with this one. Ran the full ActionMakeCommandTest locally (4 passed); the other generators sharing HasPathSupport get the same fix from this one line.
I left the namespace key untouched — the sub-path-vs---path design question stays with you as you proposed.
Merged into develop, thank you @bfs2021: the crash of --path is gone, and the namespace fix and the test on the generated file are exactly what the change needed. It will reach main with the next release.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Running any generator that resolves its location with
--pathcrashes withUndefined array key "source_path", e.g.:Found while checking the documentation against the code (Pollora/documentation#71), part 1 of #591.
Root cause
HasPathSupport::resolveCustomPath()returns onlytype,pathandnamespace, whileResolvesLocation::getResolvedFilePath()reads$location['source_path'](andgetResolvedNamespace()reads$location['source_namespace']) unconditionally. Every other resolver (resolveDefaultLocation(), the theme, plugin and module ones) sets both keys, so only the--pathlocation crashes.Fix
Set
source_path(the resolved absolute path) andsource_namespaceinresolveCustomPath(), matching the shape the other resolvers return, so--pathgenerates the file in the given folder again.Testing
it generates the class in the custom path with --pathtotests/Feature/Hook/ActionMakeCommandTest.php: it failed before the fix withUndefined array key "source_path"atResolvesLocation.php:53and passes after it, asserting the class file is created under the custom folder.vendor/bin/pest tests/Feature/Hook/ActionMakeCommandTest.php: 4 passed (the 3 pre-existing tests still pass).vendor/bin/pestrun shows the same pre-existing (environment-related) failures as on a cleanmain;vendor/bin/pint --testpasses on both changed files.