Common Custom PHP Library: Drupal 8 and Stand-alone Shell Scripts

by Greg Sims   Last Updated April 21, 2018 18:07 PM

I have a Drupal 8 custom module that contains 5K of php in 10 classes. This code is production ready for the launch of a new website.

I also have code that runs from non-drush shell scripts. One example is the creation and distribution of a newsletter that runs from crontab. This pulls data from Drupal and creates a multi-part (html & plain text) email in multiple languages.

There is overlap between these two sets of code that would like to eliminate. How can I create a single php code library that can be shared between Drupal 8 and also used in standalone shell scripts?

There seems to be two approaches here:

(1) place all code in the custom module and Include it in shell scripts

I understand how to accomplish the custom module part of this. I don't know how to do the include into a stand-alone shell script from the Drupal directory.

(2) place all code in an external library and Include it in the custom module, the shell scripts and anywhere else I need it

This approach seems to have more flexibility. It addresses my original issue of sharing code between Drupal custom modules and stand-along scripts. It also allows the same code to be used in multiple projects -- separate Drupal 8 installs perhaps for difference clients.

How can I pull a custom php library (not on into my Drupal custom module? If this is possible, I will likely move most/all of my Drupal and Shell Script PHP code to a common external library that is under github change control.

Related Questions

Mandrill API drupal 8

Updated March 31, 2017 09:07 AM

Bootstrap from external script

Updated May 24, 2016 08:03 AM

Event Calendar didn't show correctly

Updated May 08, 2017 09:07 AM