jeroen-g/autowire 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

jeroen-g/autowire

Composer 安装命令:

composer require jeroen-g/autowire

包简介

Autowire and configure using PHP 8 Attributes in Laravel.

README 文档

README

Latest Version on Packagist CI

Autowire and configure using PHP 8 Attributes in Laravel.

Installation

Via Composer

composer require jeroen-g/autowire

You will need the configuration file to change where it should look:

php artisan vendor:publish --tag=autowire.config

Usage

Autowiring

Are you tired of binding abstract interfaces to concrete classes all the time?

$this->app->bind(HelloInterface::class, WorldClass::class);

Use the PHP 8 attribute of this package to autowire any of your interfaces:

namespace App\Contracts;

use JeroenG\Autowire\Attribute\Autowire;

#[Autowire]
interface HelloInterface
{
    public function hello(): string;
}

The class that implements that interface does not need any changes:

namespace App;

use App\Contracts\HelloInterface;

class WorldClass implements HelloInterface
{
    public function hello(): string
    {
        return 'world';
    }
}

The Autowire package will crawl through the classes and bind the abstract interface to the concrete class. If there already is a binding in the container it will skip the autowiring.

Tagging

If you quickly want to tag all implementations of an interface, simply add the Tag attribute to the interface:

namespace App\Contracts;

use JeroenG\Autowire\Attribute\Tag;

#[Tag('myTag')]
interface HelloInterface
{
    public function hello(): string;
}

All implementations will now be available under the 'myTag' tag:

$this->app->when(Greeting::class)
	->needs(HelloInterface::class)
	->giveTagged('myTag');

If no tag value is specified in the attribute, the fully-namespaced name of the class will be used as the tag:

namespace App\Contracts;

use JeroenG\Autowire\Attribute\Tag;

#[Tag]
interface GoodbyeInterface
{
    public function goodbye(): string;
}
$this->app->when(Greeting::class)
	->needs(GoodbyeInterface::class)
	->giveTagged(GoodbyeInterface::class);

Configure

Personally I like injection of dependencies over resolving them using make() helpers. However, that means writing binding definitions such as:

$this->app->when($x)->needs($y)->give($z);

Not anymore with the Configure attribute! Here is the WorldClass example again:

namespace App;

use App\Contracts\HelloInterface;

#[Configure(['$message' => 'world'])]
class WorldClass
{
    private $message;

    public function __construct($message)
    {
        $this->message = $message;
    }
}

In this example message is a simple string. However, it can be a reference to a configuration value or other class too! The notations of config and service definitions is the same as used in Symfony.

// Will get the value set in config/app.php
#[Configure(['$message' => '%app.message%'])]

// Will inject an instance of the Message class
#[Configure(['$message' => '@App\Domain\Message'])]

// When you want tagged classes
#[Configure(['$messages' => '#messages'])]

// When you have multiple constructor arguments
#[Configure(['$message' => '%app.message%', '$logger' => '@Psr\Log\LoggerInterface'])]

Listen to events

If you use a lot of events, the EventServiceProvider will very likely become long and messy:

protected $listen = [
    Registered::class => [
        UpdateLastLogin::class,
        ...
    ],
    ...
];

With the PHP 8 attribute of this package you can define the events for a listener alongside each other:

#[Listen(Registered::class)]
#[Listen(Login::class)]
class UpdateLastLoginListener {
   ...
}

The package will crawl through the classes and bind the listeners to the event classes.

Caching

The autowiring, configuration and listeners can be cached with the command php artisan autowire:cache. In a similar fashion it can be cleared with php artisan autowire:clear. Keep in mind that caching means that it won't crawl all the classes and changes to the annotations will not be loaded.

Configuration

The package's configuration can be found in config/autowire.php. It should contain the list of directories where Autowire should look for both interfaces and implementations.

Custom attributes

It is possible to use custom Attribute classes for either or both the autowiring or configuration functionality:

  • Create a custom attribute class, making sure to implement either JeroenG\Autowire\Attribute\AutowireInterface or JeroenG\Autowire\Attribute\ConfigureInterface, depending on the attribute you want to replace.
  • Add a autowire_attribute, configure_attribute, tag_attribute or listen_attribute setting to the config/autowire.php file, containing the fully-namespaced name of your custom attribute class.
  • Use your custom attribute to mark the interface or class you want to autowire or configure.

Speeding up tests

To avoid tests crawling your codebase for autowiring attributes, and thus slowing them down, you can use the WithCachedAutowire trait. This will make sure Autowire will only crawl once for the whole run of the tests.

abstract class TestCase extends BaseTestCase
{
    use WithCachedAutowire;
}

Changelog

Please see the changelog for more information on what has changed recently.

Credits

License

MIT. Please see the license file for more information.

jeroen-g/autowire 适用场景与选型建议

jeroen-g/autowire 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.3k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2022 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「autowire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jeroen-g/autowire 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jeroen-g/autowire 我们能提供哪些服务?
定制开发 / 二次开发

基于 jeroen-g/autowire 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-01