๐Ÿ”Œ Autowire and configure using PHP 8 Attributes in Laravel.

Overview

๐Ÿ”Œ Autowire for Laravel

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.

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 have multiple constructor arguments
#[Configure(['$message' => '%app.message%', '$logger' => '@Psr\Log\LoggerInterface'])]

Caching

The autowiring and configuration 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.

Changelog

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

Credits

License

MIT. Please see the license file for more information.

Comments
  • Allow for extending of Attributes

    Allow for extending of Attributes

    • feat: removed final keyword from Attributes
    • feat: changed Electrician to recognise child Attributes.

    This allows extending the base Autowire and Configure attributes while still retaining full functionality.

    opened by bitwise-operators 5
  • Fix codestyle issue and changelog

    Fix codestyle issue and changelog

    style: move private method to correct position docs: moved new feature to 1.4.0 header in changelog

    With this, it should be safe to publish and release a new '1.4.0' tag to trigger Packagist.

    Thanks!

    opened by bitwise-operators 4
  • fix: Make caching work with custom attributes

    fix: Make caching work with custom attributes

    When I added the custom attributes, I overlooked the fact that cache command does its own initialization of the Electrician, so it won't load the correct attributes.

    This commit should fix that.

    PS: I had a quick look at adding tests for the console commands, but since the package doesn't load a complete Laravel installation, testing Artisan commands is tricky. So for now, I'm afraid this is it.

    opened by bitwise-operators 3
  • Add option to use custom attribute classes

    Add option to use custom attribute classes

    • feat: Add option to use custom attribute classes
    • test: Add tests for custom attributes
    • test: Update CrawlerTest to ignore ordering of classes.
    • docs: Update readme

    This change allows users to create custom attribute classes, implementing the interface supplied by the package, and use those to mark their interfaces and/or classes.

    opened by bitwise-operators 3
  • Add configure attribute

    Add configure attribute

    // 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 have multiple constructor arguments
    #[Configure(['$message' => '%app.message%', '$logger' => '@Psr\Log\LoggerInterface'])]
    
    opened by Jeroen-G 0
  • fix: Crawler should only return interfaces and non-abstract classes

    fix: Crawler should only return interfaces and non-abstract classes

    Right now, the Crawler will return any class-like file, including traits and abstract classes.

    This means (for instance) if an abstract class implements an interface, and is the first file found by the Electrician, the plugin will try to inject the abstract class into the Service Provider.

    As traits and abstract classes are basically never valid targets for autowiring or configuring, they should probably be filtered out.

    opened by bitwise-operators 1
  • Feature: attribute to automatically tag implementations of interface

    Feature: attribute to automatically tag implementations of interface

    Hi there,

    This is a 'simple' addition of a new Attribute (and interface) that allows for instant tagging of all implementations of the interface the attribute is added to.

    In the included implementation, the fully namespaced interface name is used as the tag name by default, but can be overridden by a string argument to the attribute.

    The interface for the attribute specifies passing the ReflectionClass of the interface to the getTag() method, so that implementations can generate their tags in whatever way they desire.

    I'm also working on an expansion of the Configure functionality to use tags, but once I'm happy with that, I'll submit it as a separate PR.

    opened by bitwise-operators 4
  • Allow to use #[Autowire] on implemntations

    Allow to use #[Autowire] on implemntations

    So in our code we personally like to keep interfaces clean, we are using hexagonal architecture and all our service providers are kept in infrastracture code, and interfaces don't have any framework related code. So is it possible to maybe allow marking implementations with #[Autowire] and then it could get interfaces of this class and bind them to the container? Also as optional parameter maybe we could tell there which exactly interface we want to bind in case of multiple interfaces.

    opened by Jurigag 5
Releases(1.5.0)
Owner
JeroenG
JeroenG
A simple pure PHP RADIUS client supporting Standard and Vendor-Specific Attributes in single file

BlockBox-Radius A simple pure PHP RADIUS client supporting Standard and Vendor-Specific Attributes in single file Author: Daren Yeh [email protected]

null 2 Oct 2, 2022
Guess attributes for Laravel model factories

Eloquent Populator This package provides default attributes for Laravel model factories by guessing the best Faker formatters from columns' names and

Guido Cella 68 Aug 11, 2022
Make your own custom cast type for Laravel model attributes

Laravel Custom Casts Make your own cast type for Laravel model attributes Laravel custom casts works similarly to Eloquent attribute casting, but with

Vladimir Koviฤ‡ 220 Oct 28, 2022
Laravel-model-mapper - Map your model attributes to class properties with ease.

Laravel Model-Property Mapper This package provides functionality to map your model attributes to local class properties with the same names. The pack

Michael Rubel 15 Oct 29, 2022
Provide all attributes (including irregular patterns) to Laravel Blade class components.

blade-wants-attributes blade-wants-attributes offers you the ability to use Blade/HTML-defined attributes within the constructors of Laravel Blade cla

Stephan Casas 4 Sep 15, 2022
Easily validate data attributes through a remote request

Laravel Remote Rule Easily validate data attributes through a remote request. This package allows you to define a subset of custom rules to validate a

H-FARM Innovation 27 Nov 20, 2022
Cast your Eloquent model attributes to Value Objects with ease.

Laravel Value Objects Cast your Eloquent model attributes to value objects with ease! Requirements This package requires PHP >= 5.4. Using the latest

Red Crystal Code 23 Dec 30, 2022
Generate previous attributes when saving Eloquent models

This package provides a trait that will generate previous attributes when saving any Eloquent model.

Ricardo Sawir 33 Nov 6, 2022
This package allows you to render livewire components like a blade component, giving it attributes, slots etc

X-livewire This package allows you to render livewire components like a blade component, giving it attributes, slots etc. Assuming you wanted to creat

null 7 Nov 15, 2022
Backend application using Laravel 9.x REST APIs for games topup from digiflazz.com and payment gateway using xendit.co

TOPUP - Laravel 9.x REST API Documentation is still on progress. For now, you can fork this postman collection Installation Clone this project git clo

Muhammad Athhar Kautsar 46 Dec 17, 2022
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Jan 2, 2023
A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

Adnane Kadri 49 Jun 22, 2022
โšก Laravel Charts โ€” Build charts using laravel. The laravel adapter for Chartisan.

What is laravel charts? Charts is a Laravel library used to create Charts using Chartisan. Chartisan does already have a PHP adapter. However, this li

Erik C. Forรฉs 31 Dec 18, 2022
Twitter clone project being developed by using PHP Laravel Framework and tailwind.css

Twits! About Twits! We, as enthusiastic learners and new developers, kicked of this project in order to improve our skills and capabilities in PhP Lar

Furkan MeraloฤŸlu 10 Aug 29, 2022
Attendize is an open-source ticketing and event management application built using the Laravel PHP framework

Attendize is an open-source ticketing and event management application built using the Laravel PHP framework. Attendize allows event organisers to sel

Attendize 3.6k Dec 27, 2022
Video Chat application built using Metered Video SDK, with PHP Laravel Backend and JavaScript Front-End

Group Video Chat App with PHP Laravel and JavaScript Powered by Metered Video SDK Overview This application is a highly scalable group video calling a

null 2 Aug 18, 2022
Laravel Video Chat using Socket.IO and WebRTC

Laravel Video Chat Laravel Video Chat using Socket.IO and WebRTC Installation composer require php-junior/laravel-video-chat Laravel 5.5 uses Package

Nyi Nyi Lwin 757 Dec 26, 2022
A simple API documentation package for Laravel using OpenAPI and Stoplight Elements

Laravel Stoplight Elements Easily publish your API documentation using your OpenAPI document in your Laravel Application. Installation You can install

Steve McDougall 24 Nov 17, 2022
REST API with auth using Laravel 8 and Sanctum

Laravel REST API with Sanctum This is an example of a REST API using auth tokens with Laravel Sanctum Usage Change the .env.example to .env and add yo

Brad Traversy 251 Dec 29, 2022