PHP code example of a2workspace / laravel-automount

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

    

a2workspace / laravel-automount example snippets


use A2Workspace\AutoMount\AutoMountDependencies;

class ProductController extends Controller
{
    protected ProductService $productService;

    protected ProductRepository $productRepository;

    protected ProductStockManager $productStockManager;

    public function __construct(
        ProductService $productService,
        ProductRepository $productRepository,
        ProductStockManager $productStockManager
    ) {
        $this->productService = $productService;
        $this->productRepository = $productRepository;
        $this->productStockManager = $productStockManager;
    }

    public function __invoke(Request $request)
    {
        $this->productService->doSomething();
    }
}

use A2Workspace\AutoMount\AutoMountDependencies;

class ProductController extends Controller
{
    use AutoMountDependencies; // Add this.

    protected ProductService $productService;

    protected ProductRepository $productRepository;

    protected ProductStockManager $productStockManager;

    // We don't need the constructor anymore...

    public function __invoke(Request $request)
    {
        $this->productService->doSomething(); // Still works!
    }
}

use A2Workspace\AutoMount\AutoMountDependencies;

class PaymentService
{
    use AutoMountDependencies;
}

use A2Workspace\AutoMount\AutoMountDependencies;

class PaymentService
{
    use AutoMountDependencies;

    protected PaymentGatewayFactory $factory;
}

use A2Workspace\AutoMount\AutoMountDependencies;

class PaymentService
{
    use AutoMountDependencies;

    protected int $amount; // Pass.

    protected array $gateways; // Pass.

    protected $something; // Pass.

    protected ?PaymentGateway $gateway = null; // Pass.

    protected PaymentGatewayFactory $factory; // Do inject.
}

use A2Workspace\AutoMount\AutoMountDependencies;

class PaymentService
{
    use AutoMountDependencies;

    public function __construct()
    {
        $this->mountDependencies(); // Add this line.

        // ...
    }
}

use A2Workspace\AutoMount\AutoMountDependencies;

abstract class BasePaymentGateway
{
    use AutoMountDependencies;

    private PaymentServiceProvider $service;
}

class PaymentGateway extends BasePaymentGateway
{
    // ...
}

$gateway = new PaymentGateway; // 拋出 PaymentGateway::$service 未被初始之錯誤