Adding PSR-4 support#1552
Conversation
JohnVillalovos
left a comment
There was a problem hiding this comment.
This PR is seeming to undo recent commits. Please fix that. Thanks.
6e452cd to
0a39ce6
Compare
It seems I have mistakenly branched off from a feature branch I was working on instead of from develop. Cleaned this up today with a rebase. Sorry for the mess. |
There was a problem hiding this comment.
Pull request overview
This PR introduces initial PSR-4 autoloading support via Composer and begins migrating the Microsoft OAuth callback handler to a proper namespace-based import workflow, reducing reliance on require_once barrel files.
Changes:
- Add PSR-4 autoload mapping for the
lib\namespace incomposer.json. - Namespace
MicrosoftOAuthCallback(and related types) and switch call sites touseimports. - Update the Microsoft OAuth callback web entrypoint to bootstrap via Composer autoload.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| Web/microsoft-auth.php | Switches bootstrap and imports for the Microsoft OAuth callback handler. |
| tests/Application/Authentication/MicrosoftOAuthCallbackTest.php | Updates test to use namespaced MicrosoftOAuthCallback import. |
| lib/Application/Authentication/MicrosoftOAuthCallback.php | Adds namespace declaration to support PSR-4 autoloading. |
| composer.json | Introduces PSR-4 autoload configuration for lib\ => lib/. |
JohnVillalovos
left a comment
There was a problem hiding this comment.
@barakiva What do you think of these Codex review comments?
PR 1552 review
Recommendation
Do not merge PR 1552 yet. Request changes for the production OAuth
regression and the PSR-4 design problems below.
Findings
P1: Microsoft OAuth error callbacks now return HTTP 500
Web/microsoft-auth.php replaces lib/Common/namespace.php with only the
Composer autoloader and lib/Common/Logging/Log.php. Log::Error() depends on
global classes such as Configuration, ConfigKeys, BooleanConverter, and
ServiceLocator, which are no longer loaded.
Reproducing an Azure error callback fails before the intended redirect:
Fatal error: Class "Configuration" not found
User cancellation and provider errors therefore return an HTTP 500 instead of
redirecting home. Keep the existing common bootstrap until all of its required
dependencies are migrated, or explicitly bootstrap every required dependency.
Add an entry-point test that exercises the OAuth error and logging path; the
handler unit test does not cover this integration.
P2: The auth entry point no longer registers the global exception handler
lib/Common/namespace.php also required
lib/Common/Logging/ExceptionHandler.php, which registers
set_exception_handler(['ExceptionHandler', 'Handle']). Loading only Log.php
drops that registration, so this endpoint no longer logs uncaught exceptions
the same way as sibling auth entry points such as google-auth.php and
oauth2-auth.php.
If the common bootstrap is not restored, the exception handler and all of its
dependencies must be loaded explicitly.
P2: Mapping the entire legacy lib/ tree breaks strict optimized autoload generation
composer.json declares "lib\\": "lib/", although nearly everything under
lib/ remains in the global namespace or uses an unrelated namespace. The
mapped tree also contains bundled third-party Slim code.
The following command exits with status 1 and reports 841 PSR-4 violations:
composer dump-autoload --optimize --strict-psrThis undermines the intended gradual migration. Put migrated code in an
isolated PSR-4 source subtree, such as src/, rather than declaring the whole
legacy tree PSR-4-compliant.
P2: Two newly namespaced types are not independently autoloadable
lib/Application/Authentication/MicrosoftOAuthCallback.php declares all three
of these types:
MicrosoftOAuthCallbackOutcomeMicrosoftOAuthCallbackResultMicrosoftOAuthCallback
Only MicrosoftOAuthCallback matches the filename. With a normal,
non-optimized Composer autoloader, directly resolving the result type fails:
class_exists('lib\\Application\\Authentication\\MicrosoftOAuthCallbackResult');
// falseSplit the three types into files matching their class or enum names.
P3: PR and commit metadata needs cleanup
The PR title is not in conventional-commit format. Commit subjects such as
fixed typo and passed unit tests use past tense and do not clearly describe
the changes. The PR also remains blocked by an existing CHANGES_REQUESTED
review.
Before merge, use an imperative conventional title such as:
refactor(autoload): add initial PSR-4 support
|
Here is Claude's code review PR 1552 code reviewMerge recommendationDo not merge yet. The PR is mergeable, currently has a maintainer approval, FindingsP1: Microsoft OAuth provider errors now cause an HTTP 500Location: The entry point previously loaded The normal provider-error path can be reproduced with: cd Web
php -d display_errors=1 -r '$_GET = ["error" => "access_denied"]; include "microsoft-auth.php";'It terminates with: An Azure error or user cancellation therefore produces a server error instead P2: The auth entry point silently loses the global exception handlerLocation:
Restoring the common bootstrap fixes both this problem and the missing logging P2: The PSR-4 mapping covers the entire non-PSR-4 legacy treeLocation: The mapping After a fresh composer dump-autoload --optimize --strict-psrThis makes strict optimized autoload generation unusable and creates a noisy P2: Two migrated types cannot be autoloaded by their class namesLocation: The file declares three namespaced types:
Only require 'vendor/autoload.php';
class_exists('lib\\Application\\Authentication\\MicrosoftOAuthCallbackResult');
// falseThe current callback happens to work because loading P3: PR title and commit subjects do not follow repository conventionsThe PR title, If this will be squash-merged, rename the PR to an imperative conventional Otherwise, clean up the individual commit messages before merge. ValidationThe following were run at head commit
GitHub CI is green across PHP 8.2-8.5, linting, static analysis, |
Can you please explain P2 to me? I've went over a few PSR-4 articles as well as composer docs, and had claude explain it to me. I was under the impression migrations are gradual. So why is it that adding 'lib' breaks 800+ files if I didn't add use and namepsace to them? I've prompted the AI over and over to explain p2 to me but it's all just nonsense I need a human to explain this to me cause literally the reason I've made the PR was due to multiple articles explaning PSR-4 code and require can codexist. |
|
@barakiva I'll attempt to take a look at it more this weekend. No guarantees as the family might have other plans 😄 Thanks. |
I used Codex and Claude to help me research this. This is my initial research but I need to do more. Run this command with the current code in this PR: So doing some research it sounds like we might should do is to create a new directory off of the root called For example would take The one file becomes three files because each class is supposed to be in its own file. |
Codex PR 1552 PSR-4 Migration PlanPrompt
ObjectiveConfigure a clean PSR-4 source root and migrate the Microsoft OAuth callback PSR-4 migrates named PHP types rather than arbitrary files. The existing 1. Establish the PSR-4 source rootCreate a top-level "autoload": {
"psr-4": {
"LibreBooking\\": "src/"
}
}The mapping means: Do not map the legacy 2. Split and move the three typesThe existing file declares:
Move those declarations into separate files: Each file should contain namespace LibreBooking\Application\Authentication;Because the types share a namespace, their references to one another do not After the declarations have moved, delete: 3. Generate the Composer autoloaderRun: composer dump-autoloadThis regenerates Composer's derived autoload configuration with the new 4. Update the web entry pointUpdate use LibreBooking\Application\Authentication\MicrosoftOAuthCallback;The entry point should explicitly load both autoloading systems: require_once ROOT_DIR . 'vendor/autoload.php';
require_once ROOT_DIR . 'lib/Common/namespace.php';They have separate responsibilities:
Remove the old direct include: require_once ROOT_DIR . 'lib/Application/Authentication/MicrosoftOAuthCallback.php';That file no longer exists after the migration. 5. Update the unit testReplace the test's direct use LibreBooking\Application\Authentication\MicrosoftOAuthCallback;Confirm that the test runner loads The existing callback tests should naturally exercise autoloading of all three 6. Add a bootstrap regression testAdd coverage for the actual The test should demonstrate that an input such as: can:
This test would catch the current PR's 7. Validate the new boundaryFirst verify PSR-4 compliance: composer dump-autoload --optimize --strict-psrBecause Composer now scans only the clean Then run the normal project checks: composer validate
composer phpcsfixer:lint
composer phpstan
composer phpstan_next
composer phpunitExpected resultThe Microsoft OAuth callback component uses PSR-4, while the rest of |
|
I added the |
Establish a Composer PSR-4 autoload mapping for the LibreBooking namespace rooted at the new src/ directory and migrate the first component: the Microsoft OAuth callback handler. - Map LibreBooking\ to src/ in composer.json, leaving the legacy lib/ tree on the existing include system so the gradual migration passes `composer dump-autoload --optimize --strict-psr`. - Split MicrosoftOAuthCallback, MicrosoftOAuthCallbackResult, and MicrosoftOAuthCallbackOutcome into one file per type under src/Application/Authentication/ and delete the legacy file. - Keep the lib/Common/namespace.php bootstrap in Web/microsoft-auth.php alongside vendor/autoload.php so Log's dependencies and the global exception handler keep working; dropping it caused OAuth error callbacks to fatal with "Class 'Configuration' not found" in an earlier attempt (PR #1552). - Add an entry-point regression test that runs Web/microsoft-auth.php in a subprocess to catch missing-class fatals in the bootstrap. - Map LibreBooking\Tests\ to tests/src/ via autoload-dev and move the handler test there as a namespaced class; the strict CI autoload check now enforces that test names match their paths. Add a "src" PHPUnit testsuite for running these tests in isolation. - Add src to both PHPStan configurations. - Document the migration policy in AGENTS.md: what belongs in src/, the one-type-per-file and namespace rules, the test location and naming convention, and the entry-point bootstrap requirements. Also update `commitlint.config.cjs` as the `footer-leading-blank` check is broken. Related: #1501 Co-authored-by: barakiva <barakiva1@gmail.com> Assisted-by: Claude:claude-fable-5
Establish a Composer PSR-4 autoload mapping for the LibreBooking namespace rooted at the new src/ directory and migrate the first component: the Microsoft OAuth callback handler. - Map LibreBooking\ to src/ in composer.json, leaving the legacy lib/ tree on the existing include system so the gradual migration passes `composer dump-autoload --optimize --strict-psr`. - Split MicrosoftOAuthCallback, MicrosoftOAuthCallbackResult, and MicrosoftOAuthCallbackOutcome into one file per type under src/Application/Authentication/ and delete the legacy file. - Keep the lib/Common/namespace.php bootstrap in Web/microsoft-auth.php alongside vendor/autoload.php so Log's dependencies and the global exception handler keep working; dropping it caused OAuth error callbacks to fatal with "Class 'Configuration' not found" in an earlier attempt (PR #1552). - Add an entry-point regression test that runs Web/microsoft-auth.php in a subprocess to catch missing-class fatals in the bootstrap. - Map LibreBooking\Tests\ to tests/src/ via autoload-dev and move the handler test there as a namespaced class; the strict CI autoload check now enforces that test names match their paths. Add a "src" PHPUnit testsuite for running these tests in isolation. - Add src to both PHPStan configurations. - Document the migration policy in AGENTS.md: what belongs in src/, the one-type-per-file and namespace rules, the test location and naming convention, and the entry-point bootstrap requirements. Also update `commitlint.config.cjs` as the `footer-leading-blank` check is broken. Related: #1501 Co-authored-by: barakiva <barakiva1@gmail.com> Assisted-by: Claude:claude-fable-5
Establish a Composer PSR-4 autoload mapping for the LibreBooking namespace rooted at the new src/ directory and migrate the first component: the Microsoft OAuth callback handler. - Map LibreBooking\ to src/ in composer.json, leaving the legacy lib/ tree on the existing include system so the gradual migration passes `composer dump-autoload --optimize --strict-psr`. - Split MicrosoftOAuthCallback, MicrosoftOAuthCallbackResult, and MicrosoftOAuthCallbackOutcome into one file per type under src/Application/Authentication/ and delete the legacy file. - Keep the lib/Common/namespace.php bootstrap in Web/microsoft-auth.php alongside vendor/autoload.php so Log's dependencies and the global exception handler keep working; dropping it caused OAuth error callbacks to fatal with "Class 'Configuration' not found" in an earlier attempt (PR #1552). - Add an entry-point regression test that runs Web/microsoft-auth.php in a subprocess to catch missing-class fatals in the bootstrap. - Map LibreBooking\Tests\ to tests/src/ via autoload-dev and move the handler test there as a namespaced class; the strict CI autoload check now enforces that test names match their paths. Add a "src" PHPUnit testsuite for running these tests in isolation. - Add src to both PHPStan configurations. - Document the migration policy in AGENTS.md: what belongs in src/, the one-type-per-file and namespace rules, the test location and naming convention, and the entry-point bootstrap requirements. - Add src to the PHPUnit coverage source filter and PHPDocumentor scan paths. Also update `commitlint.config.cjs` as the `footer-leading-blank` check is broken. Related: #1501 Co-authored-by: barakiva <barakiva1@gmail.com> Assisted-by: Claude:claude-fable-5
Establish a Composer PSR-4 autoload mapping for the LibreBooking namespace rooted at the new src/ directory and migrate the first component: the Microsoft OAuth callback handler. - Map LibreBooking\ to src/ in composer.json, leaving the legacy lib/ tree on the existing include system so the gradual migration passes `composer dump-autoload --optimize --strict-psr`. - Split MicrosoftOAuthCallback, MicrosoftOAuthCallbackResult, and MicrosoftOAuthCallbackOutcome into one file per type under src/Application/Authentication/ and delete the legacy file. - Keep the lib/Common/namespace.php bootstrap in Web/microsoft-auth.php alongside vendor/autoload.php so Log's dependencies and the global exception handler keep working; dropping it caused OAuth error callbacks to fatal with "Class 'Configuration' not found" in an earlier attempt (PR #1552). - Add an entry-point regression test that runs Web/microsoft-auth.php in a subprocess to catch missing-class fatals in the bootstrap. - Map LibreBooking\Tests\ to tests/src/ via autoload-dev and move the handler test there as a namespaced class; the strict CI autoload check now enforces that test names match their paths. Add a "src" PHPUnit testsuite for running these tests in isolation. - Add src to both PHPStan configurations. - Document the migration policy in AGENTS.md: what belongs in src/, the one-type-per-file and namespace rules, the test location and naming convention, and the entry-point bootstrap requirements. - Add src to the PHPUnit coverage source filter and PHPDocumentor scan paths. Also update `commitlint.config.cjs` as the `footer-leading-blank` check is broken. Related: #1501 Co-authored-by: barakiva <barakiva1@gmail.com> Assisted-by: Claude:claude-fable-5
Addressing #1501 , this PR adds PSR-4 to the project. I've went in greater depth with regards to the necessity of PSR-4 in the issue. But in general, PSR-4 gives us the following: