lesphp/psr4-converter
Composer 安装命令:
composer require lesphp/psr4-converter
包简介
A tool to automatically converts PSR-0 and non-standard autoload to PSR-4.
README 文档
README
PSR-4 Converter is a tool to automate the conversion of PSR-0 or non-standard codes to PSR-4 compliance codes. To convert the php code (support codebase with PHP >= 5.0), this tool statically parses the code and then all the code is scanned to find the definitions that will be changed. Making legacy code compiler with PSR-4 involves more steps than renaming namespaces, this tool solves the following problems with this conversion:
- Ensure that there is only one class per php file;
- Possibility of converting names from PSR-0 to PSR-4, converting underscore to namespace;
- Prefix existing namespaces with a vendor namespace;
- Create class_alias from the old class to the new one to ensure dynamic calls (class reference by variables);
- Validation of invalid statements that cannot be converted in php files;
- Creating include files with conditional functions and definitions to be included using composer.
- Automatic generation of autoload file to load old classes, delivering the new class instead.
- Possibility to ignore namespaces and directories.
- Creation of a mapping json file, allowing to use it to automate other steps.
- Rename all references from the old names to the new class and function names.
- Rename all references in DocBlocks from the old names to the new class and function names.
- Refactoring the imports to use the new converted names.
- Risky conversion alert.
- Checksum validation, allowing you to use pre-existing mapping files safely.
Instalation
You can install this library in several ways.
composer
-
Add the dependency to your
composer require lesphp/psr4-converterproject. It is recommended to install globally via the commandcomposer require --global lesphp/psr4-converter. -
Run the PSR-4 Converter via the command
php vendor/bin/psr4-converter helporpsr4-converter helpif installed globally.
Phive
-
Install PSR-4 from phive with the command
phive install --force-accept-unsigned lesphp/psr4-converter -
Run PSR-4 Converter via
psr4-converter helpcommand.
Usage
Mapping
The map command will generate a mapping json file containing all the information needed for the conversion, including all old and new name mappings.
psr4-converter map "App" /path/to/source -m /tmp/.psr4-converter.json --append-namespace --underscore-conversion --ignore-namespaced-underscore --ignore-path="ignored_relative_path"
For example the above command will generate the mapping file /tmp/.psr4-converter.json with all conversions to the /path/to/source directory, will also:
- Use the
Appvendor namespace for the new converted names, so a\Old\Nameclass will be\App\Old\Name; - With the
--underscore-conversionoption anOld_Name2class will become\App\Old\Name2; - With the option
--append-namespacethe vendor namespaceAppwill always be added to the new name, without this option a class\App\Old\Name3would become\App\Old\Name3, with this option it will become\App\App\Old\Name3; - With the
--ignore-namespaced-underscoreoption underscores in old names will be kept for classes that are already namespaced, then a class\Old\Name_Fourwould become\App\Old\Name_Four.
Use the psr4-converter map --help command to get more details on the arguments and possibilities of the command.
Converting
Using the mapping file it is possible to convert the code to a new directory through the following command.
psr4-converter convert /tmp/.psr4-converter.json /path/to/destination -m /tmp/.psr4-converter2.json --ignore-vendor-path --create-aliases --allow-risky
It will convert the code mapped in /tmp/.psr4-converter.json to the /path/to/destination directory, will also:
- Definitions that need to be statically included by composer will be created in
/path/to/destination/includes. - With the
-moption it is possible to add other mapping files to be used only to rename the classes mapped in the additional file, so the conversion will already have the new names of the additional mapping. - With the option
--ignore-vendor-paththe vendor path will be ignored for generating the path of the converted file, so a class with new name\App\New\Namewill be in/path/to/destination/New/Name.php, this is useful for doing psr-4 mappings in composer.json. - With the
--create-aliasesoption a file will be created in/path/to/destination/includes/autoload.php, that can be statically included in composer to autoload the old names, keeping compatibility for dynamic calls of the old names. - With the
--allow-riskyoption the tool will allow the conversion even if there is some risk mapped.
Use the psr4-converter convert --help command to get more details on the arguments and possibilities of the command.
Renaming
Using the mapping file it is possible to rename the mapped references, so the old names will be converted to the new names.
Use the rename command like this psr4-converter rename /tmp/.psr4-converter.json /path/to/destination.
The convert command already renames the converted code, so the rename command is useful only to rename another already existent code.
Use the psr4-converter rename --help command to get more details on the arguments and possibilities of the command.
Inspecting
Using the mapping file it is possible to inspect the mapped changes. Use the inspect command like this psr4-converter inspect /tmp/.psr4-converter.json.
Using the option -o names-changes is possible to get a json from changed names.
Use the psr4-converter inspect --help command to get more details on the arguments and possibilities of the command.
lesphp/psr4-converter 适用场景与选型建议
lesphp/psr4-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 95 次下载、GitHub Stars 达 4, 最近一次更新时间为 2022 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「psr-0」 「autoload」 「convert」 「namespace」 「PSR-4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lesphp/psr4-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lesphp/psr4-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lesphp/psr4-converter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
Provides a simple decimal/roman number converter
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Pop Loader Component for Pop PHP Framework
Convert xml to array
Extension to autoload modules dynamically
统计信息
- 总下载量: 95
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-08