PHP code example of jeroen-g / autowire

1. Go to this page and download the library: Download jeroen-g/autowire library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jeroen-g / autowire example snippets


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

namespace App\Contracts;

use JeroenG\Autowire\Attribute\Autowire;

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

namespace App;

use App\Contracts\HelloInterface;

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

namespace App\Contracts;

use JeroenG\Autowire\Attribute\Tag;

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

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

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);

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

namespace App;

use App\Contracts\HelloInterface;

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

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

// 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'])]

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

#[Listen(Registered::class)]
#[Listen(Login::class)]
class UpdateLastLoginListener {
   ...
}
bash
php artisan vendor:publish --tag=autowire.config