PHP code example of rumur / autowire

1. Go to this page and download the library: Download rumur/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/ */

    

rumur / autowire example snippets


// plugin-name.php


use Rumur\Autowiring\Autowire;

$autowire = Autowire::create();


// plugin-name.php

// ...

$autowire->singleton(wpdb::class, fn() => $GLOBALS['wpdb']);

// plugin/repositories/IOrderRepository.php

interface IOrderRepository {
    public function find(int $order_id): ?Order
    public function findAll(): array
}

// plugin/repositories/OrderRepository.php
use wpdb;

class OrderRepository implements IOrderRepository {
    protected wpdb $connection;
    
    public function __construct(wpdb $connection) {
        $this->connection = $connection;
    }
    
    public function find(int $order_id): ?Order {
        // ...
    } 
    
    public function findAll(): array {
        // ...
    } 
    
    // ...
}

// Somewhere in the code
$order = $autowire->make(OrderRepository::class)->find(2022);

// Somewhere where service providers are getting injected.
$autowire->bind(IOrderRepository::class, OrderRepository::class)

// app/http/api/OrderController.php
class OrderController {
    protected IOrderRepository $repository;
    
    public function __construct(IOrderRepository $repository) {
        $this->repository;
    }
    
    public function index(): array {
        return $this->repository->findAll();
    }
}

// Somewhere in you app.
$ctrl = app()->autowire->make(OrderController::class);

$orders = $ctrl->index();


$autowire->singleton(IOrderRepository::class, OrderRepository::class)

class OrderController {
    public function index(IOrderRepository $repository): array {
        return $repository->findAll();
    }
}

$ctrl = new OrderController; 

$orders = app()->autowire->call([$ctrl,'index']);

// Or if `index` method is static do the following.
$orders = app()->autowire->call('OrderController::index');