How to install a library in a custom plugin?

Hello,

i’d like to develop a plugin that reads an iCalendar/ics file and provide the events for further processing/displaying.

As a first step I have created the plugin skeleton as described in the tutorial here: https://learn.getgrav.org/plugins/plugin-tutorial

But I have absolutely no idea how I can add existing libraries to my new plugin as I only have very little experience with PHP and even less with things like composer.

I’d like to use either https://github.com/OzzyCzech/icalparser or https://github.com/u01jmg3/ics-parser. I already got the first one running in a simple PHP script:

<?php
    require ('src/IcalParser.php');
    $cal = new \om\IcalParser();
    $results = $cal->parseFile('events.ics');

    foreach ($cal->getSortedEvents() as $r) {
        echo sprintf('<li>%s - %s</li>'.PHP_EOL, $r['DTSTART']->format('j.n.Y'), $r['SUMMARY']);
    }

But how do I properly embed it in my plugin?

Best regards,
Michael

Use composer, and autoload the libraries (example). Then, in the events which you do the parsing, employ the libraries (example). Composer is just a library-manager, which generates a composer.json, and populates a vendor-folder.

Thank you very much for your reply.

I copied the whole folder of the library manually into /vendor in my plugin and added the line

require DIR . ‘/vendor/autoload.php’;

at the top of my plugins PHP file (event-calendar.php).

When I reload any site now, I get the following error:

**Fatal error** : require(): Failed opening required 'xxx/user/plugins/event-calendar/vendor/autoload.php' (include_path='.:/usr/local/php72/share/php72') in **/xxx/user/plugins/event-calendar/event-calendar.php** on line **4**

Can you confirm that there is a autoload.php in /pluginname/vendor?

No there is no autoload.php. Where do I get that or where does it come from?

When you install with Composer, it creates the autoload.php and requisite folders beside it.

I tried to, but to be honest I have no idea how to do that correctly. I’m coming from programming in C and Python and therefore I have no idea how to use the composer.

Could you help me with the correct command line command? If I type

composer

in the command line it shows me

bash: composer: command not found

Shall I use the composer.phar in /bin or must it be installed by my hosting company? How do I tell it which package it shall install and where it shall be installed.

Since we have Composer available in Grav, in /bin as you note, you can use it from there. Say you’re on the terminal, and in your plugin’s folder (eg. cd /user/grav/user/plugins/pluginname).

You can write php /user/grav/bin/composer.phar require om/icalparser to install it to this folder. That will add it to composer.json, and the contents of the library to /user/grav/user/plugins/pluginname/vendor.

In most environments, you want it installed globally like PHP, but if the host makes that difficult just using composer.phar is sufficient.

1 Like

Thank you very much! That helped me and now the library was installed and seems to work as expected. I’ll proceed now and come back if I have another issue.

Hi myscha,
I’m currently thinking about doing the same as you described here:
writing a Plugin to display Events from ics File(s) :grinning: .
so, short question: is your Plugin finished/working ?
and, if so, available e.g. on GitHub ?