nacosvel / locator
Manages multiple instances, handling default selection, creation, caching, and extension.
Installs: 4
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/nacosvel/locator
Requires
- php: ^8.0
- nacosvel/macroable: ^1.0
Requires (Dev)
- code-lts/doctum: ^5.0
This package is auto-updated.
Last update: 2025-12-11 05:01:19 UTC
README
Locator
Manages multiple instances, handling default selection, creation, caching, and extension.
Table of Contents
Installation
You can install the package via Composer:
composer require nacosvel/locator
Usage
config.php
<?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****'), ], ];
Payment::class
interface Payment
{
}
PaymentManager::class
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); });
PaymentManager::instance
$payment->instance(); // alipay
PaymentManager::using
var_dump($payment->getDefaultInstance());// alipay $payment->using('wechat', function () { // wechat }); var_dump($payment->getDefaultInstance());// alipay
Contributing
Contributions 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
Contributors
Thanks goes to these wonderful people:
Contributions of any kind are welcome!
License
Distributed under the MIT License (MIT). Please see License File for more information.