Jump to content

Load External PHP Library Using WireClassLoader


Gadgetto
 Share

Recommended Posts

I'd like to use the following PHP library within my module:

https://github.com/ibericode/vat

I have a "vendor" folder within my module's file structure where I'd like to place the library.

How can this be achieved using the WireClassLoader methods?

Would this work:

wire('classLoader')->addNamespace('Ibericode\Vat', __DIR__ . '/vendor/vat');

use Ibericode\Vat;

I don't understand how all the Ibericode\vat subclasses are loaded.

Link to comment
Share on other sites

2 hours ago, Gadgetto said:

I have a "vendor" folder within my module's file structure where I'd like to place the library.

How can this be achieved using the WireClassLoader methods?

Slightly off-topic: There's not really an ideal way to include vendor libraries in ProcessWire modules. You can of course just check the entire vendor folder into your repository, but then you're replicating the ENTIRE source code of ALL vendor libraries in your repository, which feels wrong. Also, in this case you have to check the license of that project; if it has a share-alike clause, you'd have to use the same license for your entire module ...

Honestly, I'd avoid the hassle and just include instructions on how to install the third-party libraries to use with your module. You could even include a composer.json in your module with all dependencies, so the users of your module just have to do a composer install. @bernhard talked about a similar approach here; though in this case, the entire vendor folder created by Composer is included in the repository, which again is a somewhat heavy overhead.

Quote

I don't understand how all the Ibericode\vat subclasses are loaded.

If you are including the repository in your vendor folder, you should be able to load them with the WireClassLoader. Though most PHP libraries are built to work with Composer's autoloader. In theory, every PSR-4 compliant autoloader should work – but I'm not sure how many edge cases there are. I'm also not sure if the WireClassLoader is fully PSR-4 compliant.

A good starting point is the repository's composer.json. Check the autoload information, there you can see that the module follows PSR-4 and maps the Ibericode\Vat\ namespace to the src/ directory. Modify your namespace mapping accordingly and it should (could / might ?) work:

wire('classLoader')->addNamespace('Ibericode\Vat', __DIR__ . '/vendor/vat/src');

Again, I'm not sure if this is the best solution. I'd love to see some core support for Composer dependencies in ProcessWire.

My dream solution would be to be able to use git submodules for dependencies, but not even Github really supports those, oh well ?

  • Like 3
Link to comment
Share on other sites

10 minutes ago, MoritzLost said:

I'd love to see some core support for Composer dependencies in ProcessWire.

Sounds good ? 

10 minutes ago, MoritzLost said:

@bernhard talked about a similar approach here; though in this case, the entire vendor folder created by Composer is included in the repository, which again is a somewhat heavy overhead.

I really don't know much about composer. I just know the basics to get things done - so if you have suggestions for improvements (with a little more details so that a composer noob like me can understand it quickly ? ) I'm happy to change the simple approach used in some of my modules ? 

Link to comment
Share on other sites

1 hour ago, MoritzLost said:

Slightly off-topic: There's not really an ideal way to include vendor libraries in ProcessWire modules. You can of course just check the entire vendor folder into your repository, but then you're replicating the ENTIRE source code of ALL vendor libraries in your repository, which feels wrong. Also, in this case you have to check the license of that project; if it has a share-alike clause, you'd have to use the same license for your entire module ...

The stated library has a MIT license so this should not be a problem.

As the library has no external dependencies I'm only using the content of the "src" folder within my module. I copied it manually into "/vendor/vat". 

This works like a charm:

wire('classLoader')->addNamespace('Ibericode\Vat', __DIR__ . '/vendor/vat');

use Ibericode\Vat\Rates;

...

$cachePath = $this->wire('config')->paths->cache . 'vat-rates.txt';
$rates = new Rates($cachePath);
$rate = $rates->getRateForCountry('NL'); // 21
bd($rate);

Edit: I finally copied over the full library including LICENSE and so on.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...