3rd Party Library

Permalink
Hi all,

I've had a search and can't find exactly what I'm after, hence the new thread.

I am building a site where I need to include 3rd party libararies such as those to do things with Amazon's AWS or processing payments through Stripe for instance.

Such classes from these libraries would be called within single page controllers and therefore it seems like overhead to build a package as I would normally just place the libraries under /libraries/3rdParty pre 5.7 and use the loader.

Could someone provide me a little guidance on something that I may have missed or a best-practice I should be learning/adopting to help with this pattern I've used many times pre 5.7.

Thanks,

Dan

dbaggs
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi dbaggs,

Have you looked over the package documentation?

"Advanced: Adding Custom Code Libraries and Third Party Libraries in a Package"
http://www.concrete5.org/documentation/developers/5.7/packages/addi...

I've found that there are problems with third party libraries that don't use namespaces and rely on being included. I would be interested in how to deal with them.
dbaggs replied on at Permalink Reply
dbaggs
Hi and thanks for your reply.

Yes - I did read through that article but as I mentioned above, my 5.6 brain is resisting putting things into a package as that feels like an overhead in this case.

I have just re-read the following autoloading article https://www.concrete5.org/documentation/developers/5.7/environment/a... and am about to try creating my own application/src directory and use the appropriate namespace.

I'll post back here to let you know how I get on.

Dan
dbaggs replied on at Permalink Reply
dbaggs
ok - like with the way things work when someone looks over your shoulder, posting this thread appears to have given me greater focus to resolve the issue.

After some initial hesitance to use composer (as I'm a little old school and want to do more work first, understand the problem something like composer solves, and then use it), I realised that it was the answer.

I added a composer.json file to my application folder, which contained the following for me:

{
    "require": {
        "stripe/stripe-php": "2.*"
    }
}


After that, I installed composer to the application folder as per the instructions here -https://getcomposer.org/doc/00-intro.md...

curl -sS https://getcomposer.org/installer | php


I then ran composer

php composer.phar install


This created an application/vendor directory and the final step was to include the autoload.php generated within.

I chose to add this to the /application/bootstrap/autoload.php file:

<?php defined('C5_EXECUTE') or die('Access Denied.');
// My Addition
require_once(DIR_APPLICATION . '/vendor/autoload.php');
// My Addition
/**
 * ----------------------------------------------------------------------------
 * Load all composer autoload items.
 * ----------------------------------------------------------------------------
 */
if (!@include(DIR_BASE_CORE . '/' . DIRNAME_VENDOR . '/autoload.php')) {
    die('Third party libraries not installed. Make sure that composer has required libraries in the concrete/ directory.');
}


I would appreciate it if others could guide me on whether this seems right. It feels like it mirrors the core and also reflects the advice for packages.
MrKDilkington replied on at Permalink Reply
MrKDilkington
Thank you for documenting your process.

I forgot to include this link in my other reply. This was written by one of the core team members. This might be what you are looking for.

"concrete5 Composer Package Example
This package is an example for how a concrete5 package that uses composer should be structured."
https://github.com/Buttress/addon_composer_sample/...
dbaggs replied on at Permalink Reply
dbaggs
Thanks.
MrKDilkington replied on at Permalink Reply
MrKDilkington
@dbaggs

Using your example, I was able to use libraries in blocks and single pages in the application and packages directories.

When trying to use a library in a single page controller though, they were not found.

Have you been able to use a library in a single page controller?

Also, here is another autoloading/composer post I had bookmarked. I think this is what you are looking for.
https://www.concrete5.org/community/forums/5-7-discussion/add-3rdpar...


EDIT
It wasn't working because I was missing a backslash (the backslash creates an object of the class defined in global scope/global space).
Example: this works in a single page controller
$mpdf = new \mPDF();
Example: this does not work in a single page controller
$mpdf = new mPDF();
janwidmer replied on at Permalink Reply
janwidmer
Hey Karl,

I followed this tutorial:https://documentation.concrete5.org/developers/packages/advanced-inc...

(Also tried this one..https://www.concrete5.org/community/forums/customizing_c5/adding-com... :-))

But when I try to use my 3rd Party Library, I also get a Class not Found error

// package controller on_start
require $this->getPackagePath() . '/vendor/autoload.php';
// or package controller before class definition
require_once __DIR__ . '/vendor/autoload.php';
// single page controller
$pdf = new \tcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// Error
Class 'tcpdf' not found


What would you recommend, when I have a 3rd party library which I need to configure after installing it. (TcPDF) I need to extend the base class of tcpdf and also set config values in an existing config file..

I guess it's not the idea to change files which get installed via composer, that would kind of betray the idea of a package manager..
MrKDilkington replied on at Permalink Reply
MrKDilkington
@janwidmer

Does the version of TCPDF you are using have an autoloader? Are you using Composer to install it?

Before extending it, is the class found?
janwidmer replied on at Permalink Reply
janwidmer
Hi Karl,

OK, I figured it out, my Class Name was wrong at the place where I used it.

For now, I just added my extended class to the same folder where the base class lives. I also added a custom config to the config folder of the tcpdf folder.

Is there a general advice how to handle extending vendor libraries?
MrKDilkington replied on at Permalink Reply
MrKDilkington
@janwidmer

I am not sure.

You could try asking in #general on Slack.
https://www.concrete5.org/slack...