Manages multiple instances, handling default selection, creation, caching, and extension.
Table of Contents
You can install the package via Composer:
composer require nacosvel/locator<?php
return [
'default' => 'alipay',
'alipay' => [
'default' => '2021004102600103',
// 'driver' => AlipayPaymentManager::class,
'2021004102600102' => [
'app_id' => env('ALIPAY_APP_ID', '2021004102600102****'),
],
'2021004102600103' => [
'app_id' => env('ALIPAY_APP_ID', '2021004102600103****'),
],
],
'wechat' => [
'mch_id' => env('WECHAT_MCH_ID', '190000****'),
],
];interface Payment
{
}use Nacosvel\Locator\MultipleInstanceManager;
class PaymentManager extends MultipleInstanceManager
{
public function __construct(array $config = [])
{
parent::__construct($config);
}
#[ReturnTypeWillChange]
public function instance(string $name = null): Payment
{
return parent::instance($name);
}
}
PaymentManager::macro('other', function () {
//
});
$payment = new PaymentManager(require __DIR__ . '/config.php');
$payment->extend('alipay', function (array $config) {
return new Alipay($config);
});
$payment->extend('wechat', function (array $config) {
return new Wechat($config);
});$payment->instance(); // alipayvar_dump($payment->getDefaultInstance());// alipay
$payment->using('wechat', function () {
// wechat
});
var_dump($payment->getDefaultInstance());// alipayContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Thanks goes to these wonderful people:
Contributions of any kind are welcome!
Distributed under the MIT License (MIT). Please see License File for more information.