Repository navigation
fix: prevent double registration of breadcrumbs in service provider - #1
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens BreadcrumbsServiceProvider against duplicate breadcrumb-file registration (e.g., when a deferrable provider boots more than once), and adds a regression test to ensure repeated registration calls don’t break breadcrumb registration.
Changes:
- Add a
$breadcrumbsRegisteredflag to short-circuit repeatedregisterBreadcrumbs()calls. - Switch breadcrumb file loading from
requiretorequire_once. - Add a PHPUnit test covering calling
registerBreadcrumbs()twice.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/BreadcrumbsServiceProvider.php |
Adds an internal “already registered” guard and uses require_once for breadcrumb files. |
tests/BreadcrumbsTest.php |
Adds a test to validate double-invocation of breadcrumb registration is safe. |
Comments suppressed due to low confidence (1)
src/BreadcrumbsServiceProvider.php:47
$breadcrumbsRegisteredis set totruebefore verifying that any breadcrumb files are actually configured/available. IfregisterBreadcrumbs()is called whenconfig('breadcrumbs.files')is falsy (or when the defaultroutes/breadcrumbs.phppath is missing), the method returns early but permanently disables future registration on the same provider instance. Move the flag assignment to after the early-return checks (or only set it once files have been successfully required).
if ($this->breadcrumbsRegistered) {
return;
}
$this->breadcrumbsRegistered = true;
$files = config('breadcrumbs.files');
if (! $files) {
return;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $file = tempnam(sys_get_temp_dir(), 'breadcrumbs_test_').'.php'; | ||
| file_put_contents($file, '<?php Breadcrumbs::for("double-boot-test", fn($t) => $t->push("Test", "/"));'); | ||
|
|
||
| try { | ||
| config(['breadcrumbs.files' => $file]); | ||
|
|
||
| $provider->registerBreadcrumbs(); | ||
| $provider->registerBreadcrumbs(); // second call must be a no-op | ||
|
|
||
| $this->assertTrue(Breadcrumbs::exists('double-boot-test')); | ||
| } finally { | ||
| unlink($file); | ||
| } |
There was a problem hiding this comment.
tempnam() already creates a file; appending '.php' produces a second filename and leaves the original temp file behind, so this test leaks a temp file on disk. Use the path returned by tempnam() directly (no suffix) or rename/unlink the original temp file so both are cleaned up in the finally block.
This pull request improves the robustness of the
BreadcrumbsServiceProviderby ensuring that breadcrumb files are only registered once, even if the registration method is called multiple times. It also adds a test to verify this behavior.Service provider improvements:
$breadcrumbsRegisteredflag toBreadcrumbsServiceProviderto prevent duplicate registration of breadcrumbs.registerBreadcrumbs()to check the$breadcrumbsRegisteredflag and return early if registration has already occurred.requiretorequire_onceto further guard against duplicate file loading.Testing enhancements:
test_register_breadcrumbs_called_twice_does_not_throw, to confirm that callingregisterBreadcrumbs()multiple times does not cause errors or duplicate breadcrumb registration.