覆盖Laravel框架7.29.3中的供应商文件



我试图覆盖位于"vendorcimpleo omnipayauthorizenetrecurring srcObjectsSchedule.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到文件夹"appOverrides"

namespace Cimpleo;
use AcademeAuthorizeNetPaymentInterface;
use AcademeAuthorizeNetAbstractModel;
use OmnipayCommonExceptionInvalidRequestException;
use DateTime;
class Schedule extends AbstractModel
{
...

vendor Schedule.php文件如下所示。

namespace OmnipayAuthorizeNetRecurringObjects;
use AcademeAuthorizeNetPaymentInterface;
use AcademeAuthorizeNetAbstractModel;
use OmnipayCommonExceptionInvalidRequestException;
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 AppHttpControllers;
use IlluminateHttpRequest;
use OmnipayOmnipay;
use OmnipayAuthorizeNetRecurring;
use OmnipayAuthorizeNetRecurringObjectsSchedule;
use OmnipayCommonCreditCard;
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。运行脚本后,应用程序仍然调用导致下面错误的供应商文件。编辑器更改似乎不起作用。

OmnipayCommonExceptionInvalidRequestException
Interval Length must be a string, between "7" and "365".
OmnipayAuthorizeNetRecurringObjectsSchedule::setIntervalLength
D:xampphtdocsSBF_app_version1.5vendorcimpleoomnipay-authorizenetrecurringsrcObjectsSchedule.php:56

感谢

我认为你必须导入被覆盖的类,而不是原来的那个。

use CimpleoSchedule;
// use OmnipayAuthorizeNetRecurringObjectsSchedule;

但是这个问题更好的解决方案是使用继承:

namespace AppOverridesCimpleo;
use OmnipayAuthorizeNetRecurringObjectsSchedule as BaseSchedule;
class Schedule extends BaseSchedule
{
...
}

然后在控制器中导入新的Schedule类:

namespace AppHttpControllers;
use IlluminateHttpRequest;
use OmnipayOmnipay;
use OmnipayAuthorizeNetRecurring;
use AppOverridesCimpleoSchedule;
use OmnipayCommonCreditCard;
class AuthorizeNetRecurringController extends Controller
{
...
}

你也必须删除新的自动加载指令和exclude-from-classmap在作曲家。只要自动加载app目录就足够了:

"autoload": {
"psr-4": {
"App\": "app/"
},

最新更新