Skip to content

fix: prevent double registration of breadcrumbs in service provider - #1

Merged
roble merged 2 commits into
mainfrom
fix/breadcrumb-registered-twice
Apr 25, 2026
Merged

roble merged 2 commits into
mainfrom
fix/breadcrumb-registered-twice

Conversation

@roble

@roble roble commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

This pull request improves the robustness of the BreadcrumbsServiceProvider by 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:

  • Added a private $breadcrumbsRegistered flag to BreadcrumbsServiceProvider to prevent duplicate registration of breadcrumbs.
  • Updated registerBreadcrumbs() to check the $breadcrumbsRegistered flag and return early if registration has already occurred.
  • Changed the file inclusion from require to require_once to further guard against duplicate file loading.

Testing enhancements:

  • Added a test, test_register_breadcrumbs_called_twice_does_not_throw, to confirm that calling registerBreadcrumbs() multiple times does not cause errors or duplicate breadcrumb registration.

Copilot AI review requested due to automatic review settings April 25, 2026 09:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 $breadcrumbsRegistered flag to short-circuit repeated registerBreadcrumbs() calls.
  • Switch breadcrumb file loading from require to require_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

  • $breadcrumbsRegistered is set to true before verifying that any breadcrumb files are actually configured/available. If registerBreadcrumbs() is called when config('breadcrumbs.files') is falsy (or when the default routes/breadcrumbs.php path 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.

Comment thread tests/BreadcrumbsTest.php
Comment on lines +289 to +301
$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);
}

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@roble
roble merged commit 1ff34a6 into main Apr 25, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants