Post by louposk » Thu Oct 18, 2018 1:16 am

Hi all,
i am trying use composer for installing new libraries in opencart 3.0.
Inside folder storage/vendor, it seems that there are some libraries pre-installed using composer when you download initial zip package. Also, if you go to composer.json in root installation folder (outside upload folder), there is a property that says to install new libraries in upload/system/storage/vendor folder.

Code: Select all


...
"config": {
        "vendor-dir": "./upload/system/storage/vendor/"
    },
...
Have you ever tries to use composer *after* installing Opencart? If yes, how did you manage make it work?

Because i've tried multiple ways, none of them have worked.

Thank you for your time
-Konstantinos

Newbie

Posts

Joined
Tue Oct 16, 2018 2:12 am

Post by louposk » Sat Oct 20, 2018 2:52 pm

Anyone?

Newbie

Posts

Joined
Tue Oct 16, 2018 2:12 am

Post by opencart.my » Sat Oct 20, 2018 9:59 pm

Hi Konstantinos,

Yes, I'm able to do "composer require" and install new libraries without any problem. E.g.:

Code: Select all

$ composer require phpseclib/phpseclib
$ composer install
I even changed the "vendor-dir" to another directory and it just works.

However, I did have my composer path set in my bash on macOS, so I'm not using "php composer.phar".

Developing Quality OpenCart Extensions since 2011. View my Extensions.


User avatar
New member

Posts

Joined
Mon Jan 22, 2018 6:03 pm


Post by louposk » Sat Oct 20, 2018 10:19 pm

Hi and thank you for your answer,
from which location do you run the above commands?

-Konstantinos

Newbie

Posts

Joined
Tue Oct 16, 2018 2:12 am

Post by opencart.my » Sat Oct 20, 2018 10:49 pm

I ran the composer commands from the same location where my composer.json file is situated.

From OpenCart perspective it will be outside or parent of the 'upload' directory.

Developing Quality OpenCart Extensions since 2011. View my Extensions.


User avatar
New member

Posts

Joined
Mon Jan 22, 2018 6:03 pm


Post by louposk » Sun Oct 21, 2018 2:39 am

If I understand correct, in production the upload folder exist and if you want to install any new dependency you edit the composer file which exist in root folder?

In installation description of opencart its says that you should copy all files from upload folder to your root folder.

Newbie

Posts

Joined
Tue Oct 16, 2018 2:12 am

Post by opencart.my » Sun Oct 21, 2018 5:30 pm

If you are just installing new library you don't need to edit composer.json file directly. Calling "composer require" will automatically do include the dependency for you.

"Root folder" usually means your web root directory, where you can still have the "composer.json" file reside in the parent of the web root directory. Not advisable to have "composer.json" file reside in the same directory as your OpenCart store directory.

Example:

Code: Select all

/
/var/www/mystore.com/upload/index.php (OpenCart installation)
/var/www/mystore.com/composer.json

Developing Quality OpenCart Extensions since 2011. View my Extensions.


User avatar
New member

Posts

Joined
Mon Jan 22, 2018 6:03 pm


Post by harmonybrew » Thu Aug 12, 2021 8:24 pm

One thing that I believe is missing here is that OpenCart deletes the composer.json and composer.lock files after installation. So, you won't find them anywhere. I simply grabbed those two files from the uninstalled OpenCart version in quest and placed them into the storage directory before adding the new package (otherwise composer will create the files and delete all the old vendors) - along with changing the vendor directory as mentioned above. I imagine it is probably a good idea to delete these two files again once the new package is installed.

The new package will be autoloaded and you can instantiate the class throughout.

Be sure to use the files from your exact version otherwise I am sure conflicts will arise.

Maybe there is more to this then I know, so someone please respond if this wasn't the way to go.

Newbie

Posts

Joined
Wed Sep 14, 2016 1:08 am

Post by treii28 » Wed Feb 21, 2024 1:33 am

Continue reading for OC 4.x here: viewtopic.php?p=867361#p867361

This is something I have been looking into while evaluating opencart v4.0.2.3

It seems they use their own autoloader and grab files they wish to include via composer with a file:

Code: Select all

system/vendor.php
This grabs a bunch of composer installed files and vendor tree libraries created in the initial install which are placed in your 'storage' directory outside of the web root. I'm not entirely sure how they install these files as they are essentially 'broken' in the initial creation of the storage folder on a vanilla install of opencart4. (if you try to run composer update in that directory, it fails in v4 due to a conflict between phpdocumentor and twig >v3. It will also try to upgrade guzzlehttp/promises to a version that no longer includes the functions_include.php file. See the 'issues' on the github repository https://github.com/opencart/opencart/issues/13699 for more details.)

What I basically did (in version 4 of opencart) was replace the require line for phpdocumentor/phpdocumentor with phpdocumentor/shim: "^3"
Then I commented out almost the entire system/vendor.php (everything that referenced the storage/vendor files) and replaced it with:

Code: Select all

 require_once(DIR_STORAGE.'vendor/autoload.php');
This way, the composer autoloader will handle loading any and all files installed with composer. (this will include the new layout of guzzlehttp/promises).

But, it should be noted, you only need to worry about this if you intend to change the core files which will likely break your ability to do a stock upgrade in the future. For adding extensions, it is possible and recommended to include a system/ directory in the extension itself to load any composer files your code additions may need.

I was playing with this to see if I could use something like a dotenv loader for credentials such as AWS or the database -- unfortunately, the vendor.php is called 'after' config.php so I'm likely going to fall back to using the .htaccess for specifying the credentials as environment variables instead. I can then reference the environment variable instead of putting the credential(s) directly in the config files.

Newbie

Posts

Joined
Wed Jan 31, 2024 5:48 am
Who is online

Users browsing this forum: No registered users and 93 guests