1

I installed a 3rd party PHP sdk via composer and need to override a couple files in my own module. However, I'm not quite sure what the best practice is to override these files from the vendor folder.

The 2 files I'm looking to override are located in:

vendor\quickbooks\v3-php-sdk\src\DataService\DataService.php

and

vendor\quickbooks\v3-php-sdk\src\sdk.config

Here is how the composer.json file looks:

{
"name": "quickbooks/v3-php-sdk",
"description": "The Official PHP SDK for QuickBooks Online Accounting API",
"type": "library",
"keywords": ["api", "http", "rest", "quickbooks", "smallbusiness"],
"homepage": "http://developer.intuit.com",
"require": {
    "php": ">=5.6.0"
},
"require-dev": {
    "phpunit/phpunit": "^5.7"
},
"autoload": {
    "psr-4": {
        "QuickBooksOnline\\API\\": "src/"
    }
},
"license": "Apache-2.0",
"authors": [
    {
        "name": "hlu2",
        "email": "[email protected]"
    }
]
}

My question is, how do I override these 2 files in app/code/MyCompany/MyModule/

2 Answers 2

4

as you see on the composer part

"name": "quickbooks/v3-php-sdk"

and

"autoload": {
    "psr-4": {
        "QuickBooksOnline\\API\\": "src/"
    }
},

so the path of the vendor folder extension on the vendor, should be in :

<magento_root>/vendor/quickbooks/v3-php-sdk/

then, as the autoload describe in the psr-4 to be like this :

"QuickBooksOnline\\API\\": "src/"

that's mean you can access class in the folder quickbooks/v3-php-sdk/src/* by defined namespace to be like this :

namespace QuickBooksOnline\API;

since you want override Class file vendor\quickbooks\v3-php-sdk\src\DataService\DataService.php with the my explanation before, so the class should be to be like this :

QuickBooksOnline\API\DataService\Dataservice

to override that class, you just create di.xml inside folder app/code/MyCompany/MyModule/etc/

add this code to override Dataservice.php Class

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="QuickBooksOnline\API\DataService\Dataservice" type="MyCompany\MyModule\Model\DataService" />
</config>

the override class now on the MyCompany\MyModule\Model\DataService.php

and then to override sdk.config I still cannot find to override this xml by similiar xml file again, but as the documentation on this link , you can modified the setting on the sdk.config by write php code, in the magento you can create helper file to connect based your setting

so create Helper Class Data.php on the folder app/code/MyCompany/MyModule/Helper add this code to the Data.php

<?php    
namespace MyCompany\MyModule\Helper;
use QuickBooksOnline\API\DataService\DataService as DataService;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $_dataService;

    public function _construct(
        DataService $dataService
    ){
        $this->_dataService = $dataService;
    }
    public function getConfigurationSetting()
    {
        $settings = $this->_dataService::Configure(array(
        'auth_mode' => 'oauth1',
        'consumerKey' => "ConsumerKey",
        'consumerSecret' => "ConsumerSecret",
        'accessTokenKey' => "CccessTokenKey",
        'accessTokenSecret' => "AccessTokenSecret",
        'QBORealmID' => "RealmID"
        'baseUrl' => "Development/Production"
        ));

        return $settings
    }
}

so you can call the method getConfigurationSetting() from the helper Data.php when you want checking your configuration, you can use this answer to call method on your helper class.

Hope it helps.

5
  • Thank you, the Helper/Data file worked so I'll be able to keep my DataService credentials to a single file! I'm still stuck on the preference override for DataService.php. I've got it all setup, cleared cache but it's still calling from the vendor folder.
    – AJK
    Oct 10, 2018 at 18:49
  • try to execute php bin/magento setup:di:compile, if it still does not work, please show your new override class code, so we can analyze that
    – mrfizh
    Oct 11, 2018 at 0:23
  • php bin/magento setup:di:compile was the trick, thank you so much!
    – AJK
    Oct 11, 2018 at 13:30
  • you're welcome, glad to know it solve your problem :)
    – mrfizh
    Oct 11, 2018 at 14:14
  • "autoload": { "psr-4": { "Pelago\\": "src/" } } I am having only a single namespace Pelago. so not able to override. Any idea what should I do Dec 21, 2021 at 10:14
0

Best and easiest way is to open module's registration.php file and check module name there.

So for example your registration file is something like :

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'QuickBooksOnline_API',
     __DIR__
);

Then your file path, which you want to extend would be :

QuickBooksOnline\API\DataService\DataService.php

2
  • Thanks Naveed, I can try this, however it doesn't look like the Quickbooks php library has a registration.php file.
    – AJK
    Oct 10, 2018 at 17:53
  • This was just an example how to find file path to extend however in your case you have to extend 'QuickBooksOnline\API\DataService\DataService.php' file to extend in your module Oct 10, 2018 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.