承接 zorac/php-di-migration 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zorac/php-di-migration

最新稳定版本:1.0.0

Composer 安装命令:

composer require zorac/php-di-migration

包简介

Eases migration from PHP-DI 6 annotations to PHP-DI 7 attributes

README 文档

README

Software license PHP version Latest release

Eases migration from PHP-DI 6 annotations to PHP-DI 7 attributes.

Rationale

PHP-DI 7 replaces the usage of PHPDoc @Inject and @Injectable annotations with native PHP 8 #[Inject] and #[Injectable] attributes. This is very much a change for the better, but all of your code needs to be updated at once, which can be difficult or dangerous across a large codebase with multiple contributors. This project provides a smoother migration path by supporting attributes alongside annotations while you migrate your code.

Warning

This code has not been extensively tested for all use cases and edge cases. Use at your own risk!

Usage

Install the migration package alongside PHP-DI 6:

composer require zorac/php-di-migration

Replace ContainerBuilder with MigrationContainerBuilder and enable annotations:

// Before:
$builder = new DI\ContainerBuilder();
$builder->useAnnotations(true);
// etc...
$container = $builder->build();

// After:
$builder = new DI\MigrationContainerBuilder();
$builder->useAnnotations(true);
$builder->useAttributes(true);
// etc...
$container = $builder->build();

Commit and merge the changes, and you're ready to start migrating!

Over time, migrate your code from attributes to annotations, remembering to add strong PHP types anywhere you're currently relying on PHPDoc typing, and to remove any imports of DI\Annotation\Inject etc.

Once your code is all migrated, remove the useAnnotations(false) call from the container builder for final testing. If you missed any typing changes, this is when things will break.

Once you're happy with your fully migrated code, you can remove zorac/php-di-migration and upgrade to PHP-DI 7, switching back to using DI\ContainerBuilder. You may also be able to remove doctrine/annotations if you're not relying on it for anything else.

Congratulations! Your PHP-DI migration is complete.

Implementation Notes

  • MigrationContainerBuilder is a modified version of PHP-DI 6's ContainerBuilder with changes ported from PHP-DI 7's version to add support for useAttributes, and additional changes to support annotations and attributes in parallel.
  • AttributeAndAnnotationBasedAutowiring is a modified version of PHP-DI 6's AnnotationBasedAutowiring with changes ported from PHP-DI 7's AttributeBasedAutowiring to support annotations and attributes in parallel.
  • All other classes are copied verbatim from PHP-DI 7.

统计信息

  • 总下载量: 400
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-20