php - 在 Laravel Framework 7.29.3 中覆盖供应商文件
问题描述
我正在尝试覆盖位于“vendor\cimpleo\omnipay-authorizenetrecurring\src\Objects\Schedule.php”的供应商文件以纠正一些问题。
composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Cimpleo\\": "app/Overrides/"
},
"classmap": [
"database/seeds",
"database/factories",
"vendor/google/apiclient/src",
"vendor/google/apiclient-services/src/Google"
],
"exclude-from-classmap": ["vendor\\cimpleo\\omnipay-authorizenetrecurring\\src\\Objects\\Schedule.php"]
}
然后我将 Schedule.php 复制并编辑到文件夹"app\Overrides"
。
namespace Cimpleo;
use Academe\AuthorizeNet\PaymentInterface;
use Academe\AuthorizeNet\AbstractModel;
use Omnipay\Common\Exception\InvalidRequestException;
use DateTime;
class Schedule extends AbstractModel
{
...
供应商 Schedule.php 文件如下所示。
namespace Omnipay\AuthorizeNetRecurring\Objects;
use Academe\AuthorizeNet\PaymentInterface;
use Academe\AuthorizeNet\AbstractModel;
use Omnipay\Common\Exception\InvalidRequestException;
use DateTime;
class Schedule extends AbstractModel
{
const SCHEDULE_UNIT_DAYS = 'days';
const SCHEDULE_UNIT_MONTHS = 'months';
protected $intervalLength;
protected $intervalUnit;
protected $startDate;
protected $totalOccurrences;
protected $trialOccurrences;
public function __construct($parameters = null) {
parent::__construct();
$this->setIntervalLength($parameters['intervalLength']);
$this->setIntervalUnit($parameters['intervalUnit']);
$this->setStartDate($parameters['startDate']);
$this->setTotalOccurrences($parameters['totalOccurrences']);
if (isset($parameters['trialOccurrences'])) {
$this->setTrialOccurrences($parameters['trialOccurrences']);
}
}
public function jsonSerialize() {
$data = [];
if ($this->hasIntervalLength()) {
$data['interval']['length'] = $this->getIntervalLength();
}
if ($this->hasIntervalUnit()) {
$data['interval']['unit'] = $this->getIntervalUnit();
}
if ($this->hasStartDate()) {
$data['startDate'] = $this->getStartDate();
}
if ($this->hasTotalOccurrences()) {
$data['totalOccurrences'] = $this->getTotalOccurrences();
}
if ($this->hasTrialOccurrences()) {
$data['trialOccurrences'] = $this->getTrialOccurrences();
}
return $data;
}
protected function setIntervalLength(int $value) {
if ($value < 7 || $value > 365) {
throw new InvalidRequestException('Interval Length must be a string, between "7" and "365".');
}
$this->intervalLength = (string)$value;
}
...
类在这里被实例化
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Omnipay\Omnipay;
use Omnipay\AuthorizeNetRecurring;
use Omnipay\AuthorizeNetRecurring\Objects\Schedule;
use Omnipay\Common\CreditCard;
class AuthorizeNetRecurringController extends Controller
{
private $gateway;
public function __construct() {
$this->gateway = Omnipay::create('AuthorizeNetRecurring_Recurring');
$this->gateway->setAuthName('3KJZb44jR');
$this->gateway->setTransactionKey('2fFqRA7w22a2G7He');
$this->gateway->setTestMode(true);
}
//
public function createSubscription(Request $request) {
$schedule = new Schedule([
//For a unit of days, use an integer between 7 and 365, inclusive. For a unit of months, use an integer between 1 and 12, inclusive.
'intervalLength' => '1',
// use values 'days' or 'months'
'intervalUnit' => 'months',
//date in format 'YYYY-mm-dd'
'startDate' => date("Y-m-d"), //'2020-03-10',
//To create an ongoing subscription without an end date, set totalOccurrences to "9999".
'totalOccurrences' => '12',
//If a trial period is specified, include the number of payments during the trial period in totalOccurrences.
'trialOccurrences' => '1',
]);
...
然后运行composer dump-autoload
。运行脚本后,应用程序仍在调用导致以下错误的供应商文件。作曲家更改似乎不起作用。
Omnipay\Common\Exception\InvalidRequestException
Interval Length must be a string, between "7" and "365".
Omnipay\AuthorizeNetRecurring\Objects\Schedule::setIntervalLength
D:\xampp\htdocs\SBF_app_version1.5\vendor\cimpleo\omnipay-authorizenetrecurring\src\Objects\Schedule.php:56
谢谢
解决方案
我认为你必须导入被覆盖的类,而不是原来的类。
use Cimpleo\Schedule;
// use Omnipay\AuthorizeNetRecurring\Objects\Schedule;
但是解决这个问题的更好方法是使用继承:
namespace App\Overrides\Cimpleo;
use Omnipay\AuthorizeNetRecurring\Objects\Schedule as BaseSchedule;
class Schedule extends BaseSchedule
{
...
}
然后在控制器中导入新Schedule
类:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Omnipay\Omnipay;
use Omnipay\AuthorizeNetRecurring;
use App\Overrides\Cimpleo\Schedule;
use Omnipay\Common\CreditCard;
class AuthorizeNetRecurringController extends Controller
{
...
}
此外,您还必须删除新的自动加载指令以及exclude-from-classmap
作曲家。只需自动加载app
目录就足够了:
"autoload": {
"psr-4": {
"App\\": "app/"
},
推荐阅读
- java - Spring 与 JPA 集成返回错误::查询有参数但未提供参数源
- powershell - 使用 powershell 在桌面上创建“mydocuments”快捷方式
- s4sdk - 使用 S/4 HANA SDK 生成 OData VDM 会导致错误
- python - Replace() 方法在函数中不起作用
- c++ - 使用元组遍历地图
- tableau-api - Tableau 不计算所有记录
- c++ - 在模板类中删除二维数组时遇到问题
- aws-lambda - Slack 斜杠命令从 lambda 获取 400_client_error
- excel - VBA如何自动化CTRL + F搜索
- linux - 使用 awk 从 grep 匹配中提取文本并并排放置