feat: implement composer classmap autoloading (#49)#1563
Open
KorsarOfficial wants to merge 3 commits intoVKCOM:masterfrom
Open
feat: implement composer classmap autoloading (#49)#1563KorsarOfficial wants to merge 3 commits intoVKCOM:masterfrom
KorsarOfficial wants to merge 3 commits intoVKCOM:masterfrom
Conversation
Adds support for the "classmap" key in composer.json autoload sections. Composer's classmap autoloading lets packages map arbitrary directories or individual .php files to classes without requiring PSR-0/4 naming conventions. Implementation: - ComposerJsonData gains AutoloadClassmapEntry to hold raw paths from composer.json (mirroring the existing AutoloadFileItem/AutoloadPsr4Item pattern for JSON parsing in composer-json-data.cpp). - ComposerAutoloader gains a new autoload_classmap_ flat map populated during load_file() by scanning the listed paths with a heuristic PHP class-declaration scanner (collect_php_class_names / scan_classmap_path in composer.cpp). The scanner handles namespace declarations and class/interface/trait/enum definitions; it uses std::filesystem for recursive directory traversal. - classmap_lookup() is consulted in collect-required-and-classes.cpp after psr0_lookup(), completing the lookup chain: PSR-4 → PSR-0 → classmap → default. Also adds: - tests/python/tests/composer/php/test_autoload_classmap/ — fixture with a classmap-based package (class Logger and Api\ApiClient) - test_classmap_autoloading in test_composer.py
…anner The collect_php_class_names scanner had two issues: 1. Single-line block comments like /* @var int */ left in_block_comment set to true, causing the next line to be incorrectly skipped. Now checks for */ on the same line as /* before entering multi-line mode. 2. PHP # line comments were not stripped, so # class Foo would be incorrectly detected as a class declaration. Now strips both // and # comments (taking whichever appears first on the line).
Replace the heuristic line-by-line PHP class scanner with a token-based implementation that reuses KPHP's own lexer via php_text_to_tokens(). The previous scanner manually handled block comments, line comments, namespace parsing, and class keyword detection with ~120 lines of fragile string matching. The new implementation tokenizes each file with the same lexer used by the compiler pipeline and walks the token stream looking for tok_namespace, tok_class, tok_interface, and tok_trait — correctly handling all PHP syntax (strings, heredoc, comments, escape sequences) with ~30 lines of straightforward code. lexer_init() is guaranteed to run before init_composer_class_loader() in the compiler startup sequence (compiler.cpp:202 vs :234).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
KPHP's Composer autoloader supports PSR-0, PSR-4, and
autoload/files, but notautoload/classmap. Packages using classmap (common for legacy or non-PSR code) silently fail, producingclass not foundcompile errors.Design Decision: Reusing KPHP's Lexer
The classmap strategy requires scanning PHP files to discover which classes they declare. Rather than writing a custom parser, this implementation reuses KPHP's own lexer via
php_text_to_tokens()— the same tokenizer used by the compiler pipeline.Why the lexer and not the full parser?
The classmap scanner runs during
init_composer_class_loader(), before the compilation pipeline starts. At this stage we only need class names — not full AST nodes, type inference, or semantic analysis. The lexer is the minimal sufficient tool:tok_namespace,tok_class,tok_interface,tok_trait,tok_func_name) — no fragile string matching neededlexer_init()is guaranteed to run beforeinit_composer_class_loader()in the startup sequence (compiler.cpp:202vs:234)Formal Analysis: Autoload Resolution Chain
Define the autoload resolution as a priority-ordered function:
Classmap construction — built at load time by tokenizing files from composer.json paths:
Complexity:
Implementation
compiler/data/composer-json-data.hAutoloadClassmapEntrystruct +autoload_classmapvectorcompiler/data/composer-json-data.cppclassmapkey inparse_composer_json_autoload()compiler/composer.hclassmap_lookup()method +autoload_classmap_mapcompiler/composer.cppcollect_php_class_names()viaphp_text_to_tokens()+scan_classmap_path()+classmap_lookup()compiler/pipes/collect-required-and-classes.cppclassmap_lookup()afterpsr0_lookup()Tests
tests/python/tests/composer/php/test_autoload_classmap/— fixture with classmap-based packagetest_classmap_autoloadingintest_composer.py— end-to-end build-and-compare testTechnical Report
Full report with mathematical formalization (PDF)
Closes #49