PHPackages                             jeroen-g/autowire - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. jeroen-g/autowire

ActiveLibrary

jeroen-g/autowire
=================

Autowire and configure using PHP 8 Attributes in Laravel.

2.0.0(8mo ago)2319.2k↓35%4[2 PRs](https://github.com/Jeroen-G/autowire/pulls)MITPHPPHP ^8.0CI passing

Since Jan 1Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/Jeroen-G/autowire)[ Packagist](https://packagist.org/packages/jeroen-g/autowire)[ Docs](https://github.com/jeroen-g/autowire)[ RSS](/packages/jeroen-g-autowire/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (14)Used By (0)

🔌 Autowire for Laravel
======================

[](#-autowire-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/66c9caca11422cdce9485a5ea3c6ef8bca4d9947062e7cd25a93ea2d35a3302c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a65726f656e2d672f6175746f776972652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jeroen-g/autowire)[![CI](https://camo.githubusercontent.com/e370ac0eade0fd10677d59c049644d688869d42720739926a494c9eeeb9171bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f4a65726f656e2d472f6175746f776972652f63692e796d6c3f6c6162656c3d43492532464344267374796c653d666c61742d737175617265)](https://github.com/Jeroen-G/autowire/actions/workflows/ci.yml)

Autowire and configure using PHP 8 Attributes in Laravel.

Installation
------------

[](#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
-----

[](#usage)

### Autowiring

[](#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

[](#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

[](#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

[](#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

[](#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
-------------

[](#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
-----------------

[](#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.

Changelog
---------

[](#changelog)

Please see the [changelog](changelog.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Jeroen](https://github.com/jeroen-g)
- [All Contributors](../../contributors)

License
-------

[](#license)

MIT. Please see the [license file](license.md) for more information.

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance76

Regular maintenance activity

Popularity36

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 67.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~111 days

Recently: every ~231 days

Total

13

Last Release

264d ago

Major Versions

1.9.0 → 2.0.02025-08-27

PHP version history (3 changes)1.0.0PHP 8.0.\*||8.1.\*

1.7.0PHP 8.0.\*||8.1.\*||8.2.\*

1.8.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d8700d69abe733de151f8cf49084e99ded7b9d34d7b0d1cd8f3825f5d925ff3?d=identicon)[JeroenG](/maintainers/JeroenG)

---

Top Contributors

[![Jeroen-G](https://avatars.githubusercontent.com/u/1116853?v=4)](https://github.com/Jeroen-G "Jeroen-G (49 commits)")[![Xammie](https://avatars.githubusercontent.com/u/13081809?v=4)](https://github.com/Xammie "Xammie (11 commits)")[![BJScharp](https://avatars.githubusercontent.com/u/82942240?v=4)](https://github.com/BJScharp "BJScharp (10 commits)")[![bitwise-operators](https://avatars.githubusercontent.com/u/67020940?v=4)](https://github.com/bitwise-operators "bitwise-operators (1 commits)")[![niladam](https://avatars.githubusercontent.com/u/4151765?v=4)](https://github.com/niladam "niladam (1 commits)")[![svenluijten](https://avatars.githubusercontent.com/u/11269635?v=4)](https://github.com/svenluijten "svenluijten (1 commits)")

---

Tags

autowirelaravellaravelautowire

###  Code Quality

TestsPHPUnit

Code StyleECS

### Embed Badge

![Health badge](/badges/jeroen-g-autowire/health.svg)

```
[![Health](https://phpackages.com/badges/jeroen-g-autowire/health.svg)](https://phpackages.com/packages/jeroen-g-autowire)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[laravel/ui

Laravel UI utilities and presets.

2.7k134.9M601](/packages/laravel-ui)[laravel/jetstream

Tailwind scaffolding for the Laravel framework.

4.1k19.8M136](/packages/laravel-jetstream)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[laravel-notification-channels/discord

Laravel notification driver for Discord.

2371.3M11](/packages/laravel-notification-channels-discord)[erag/laravel-disposable-email

A Laravel package to detect and block disposable email addresses.

226102.4k](/packages/erag-laravel-disposable-email)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
