覆盖 Laravel Framework 7.29.3 中的供应商文件
Overriding a vendor file in 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;
}
...
这里实例化了class
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
谢谢
我认为您必须导入覆盖的 class,而不是原始的。
use Cimpleo\Schedule;
// use Omnipay\AuthorizeNetRecurring\Objects\Schedule;
但是这个问题更好的解决方案是使用继承:
namespace App\Overrides\Cimpleo;
use Omnipay\AuthorizeNetRecurring\Objects\Schedule as BaseSchedule;
class Schedule extends BaseSchedule
{
...
}
然后在控制器中导入新的 Schedule
class:
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
{
...
}
此外,您还必须删除新的自动加载指令以及 composer 中的 exclude-from-classmap
。只需自动加载 app
目录就足够了:
"autoload": {
"psr-4": {
"App\": "app/"
},
我正在尝试覆盖位于“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;
}
...
这里实例化了class
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
谢谢
我认为您必须导入覆盖的 class,而不是原始的。
use Cimpleo\Schedule;
// use Omnipay\AuthorizeNetRecurring\Objects\Schedule;
但是这个问题更好的解决方案是使用继承:
namespace App\Overrides\Cimpleo;
use Omnipay\AuthorizeNetRecurring\Objects\Schedule as BaseSchedule;
class Schedule extends BaseSchedule
{
...
}
然后在控制器中导入新的 Schedule
class:
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
{
...
}
此外,您还必须删除新的自动加载指令以及 composer 中的 exclude-from-classmap
。只需自动加载 app
目录就足够了:
"autoload": {
"psr-4": {
"App\": "app/"
},