How can I search all plugins for composer's vendor/autoload.php?

I've got projects (both themes and plugins) set up as composer projects. These projects can be dev-dependencies of each other. Right now I'm working on a theme which requires the vendor/autoload.php file. However, when I bring that theme into another project (like a plugin), I still need access to the autoload.php. The problem is that the autoload.php file is now inside the directory structure for the plugin and not the theme.

define('FL_CHILD_THEME_DIR', get_stylesheet_directory());
define('FL_CHILD_THEME_URL', get_stylesheet_directory_uri());

if (!file_exists(FL_CHILD_THEME_DIR . '/vendor/autoload.php')) {
  // somehow check all the directories within the plugins directory 
  // for vendor/autoload.php
} else {
    require FL_CHILD_THEME_DIR . '/vendor/autoload.php';
  }
}

I've tried using things like scandir and glob but I'm not getting any closer. I thought something like this would work, but I'm still no closer.

foreach(glob(plugin_dir_path() . 'vendor/autoload.php') as $file {
  require $file;
}

Topic composer php plugin-development theme-development Wordpress

Category Web


Autoloader setup:

"autoload"   : {
   "files"   : ["functions.php"]
}

Once you have the autoloader setup properly, you just need to include that one file, like this:

require_once( 'vendor/autoload.php' );

Full tutorial here - https://torquemag.io/2014/11/improving-wordpress-plugin-development-composer/

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.