1

I ran into the bug seen here: https://github.com/magento/magento2/issues/9016. Basically, the library was updated but the Sftp.php file was not. I don't think I'm in a position or ready to run a major update at the moment so I need to patch this file or override it. I didn't see anything in the documentation as to what the preferred method is.

Can I simply copy the vendor/magento/framework/Filesystem/Io/Sftp.php file to app/code/magento/framework/Filesystem/Io/Sftp.php and make the change there as I would have in Magento 1 to override the file? What is considered best practice?

Edit- per Asrar's answer I've added:

Vendor/Namespace/etc/di.xml:

<?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="Magento\Framework\Filesystem\Io\Sftp" type="Vendor\Namespace\Model\Filesystem\Io\Sftp" />
</config>

Vendor/Namespace/Model/Filesystem/Io/Sftp.php:

...

/**
 * @var \phpseclib\Net\SFTP $_connection
 */
protected $_connection = null;

...


...

/**
 * Write a file
 *
 * @param string $filename
 * @param string $source string data or local file name
 * @param int $mode ignored parameter
 * @return bool
 */
public function write($filename, $source, $mode = null)
{
    $mode = is_readable($source) ? \phpseclib\Net\SFTP::SOURCE_LOCAL_FILE : \phpseclib\Net\SFTP::SOURCE_STRING;
    return $this->_connection->put($filename, $source, $mode);
}

...

But I'm still throwing the same error.

Notice: Use of undefined constant NET_SFTP_LOCAL_FILE - assumed 'NET_SFTP_LOCAL_FILE' in .../magento/vendor/magento/framework/Filesystem/Io/Sftp.php on line 187

This must mean it's not reading my overrides. Can you see what I'm doing wrong?

3
  • which M2 version are you on?
    – Asrar
    Commented Dec 29, 2017 at 21:31
  • Sorry, I'm on 2.1.9 Commented Dec 29, 2017 at 22:41
  • See my answer below
    – Asrar
    Commented Dec 30, 2017 at 11:26

1 Answer 1

0

You can do something like this in your etc/di.xml file:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\Filesystem\Io\Sftp" type="Vendor\Namespace\Model\Filesystem\Io\Sftp" />
</config>

Now create that model class Vendor\Namespace\Model\Filesystem\Io\Sftp and add methods as required. This is feasible approach unless you want to upgrade to v2.2.

2
  • Thank you very much. This is what I need. Unfortunately, I can't seem to get it to work. I feel like I've l followed everything perfectly. I'll post my changes above as an edit. Commented Dec 30, 2017 at 16:25
  • Did you solved the issue
    – Jaisa
    Commented Jan 23, 2019 at 12:03

Your Answer

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

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