From 136b6309941d117f883fc99e63e43e38e9969e5b Mon Sep 17 00:00:00 2001 From: dimti Date: Sun, 10 Jul 2022 14:35:37 +0300 Subject: [PATCH] * conditionally include vendor autoload & dot-env file if exists --- src/Exceptions/VendorAutoloadException.php | 6 ++++++ wpstudio-media-cloud-transform.php | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 src/Exceptions/VendorAutoloadException.php diff --git a/src/Exceptions/VendorAutoloadException.php b/src/Exceptions/VendorAutoloadException.php new file mode 100644 index 0000000..b518f7e --- /dev/null +++ b/src/Exceptions/VendorAutoloadException.php @@ -0,0 +1,6 @@ + add_submenu_page( // $ilabClassLoader = new Composer\Autoload\ClassLoader(); // $ilabLoader->unregister(); - include 'vendor/autoload.php'; + $localVendorAutoloadPath = 'vendor/autoload.php'; + $projectVendorAutoloadPath = ABSPATH . 'vendor/autoload.php'; - $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); - $dotenv->load(); + if (file_exists($localVendorAutoloadPath)) { + include $localVendorAutoloadPath; + } elseif (file_exists($projectVendorAutoloadPath)) { + include $projectVendorAutoloadPath; + } else { + throw new VendorAutoloadException('Unable to find appropriate composer autoloader'); + } + + $dotEnvFile = __DIR__ . '/.env'; + + if (file_exists($dotEnvFile)) { + $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); + $dotenv->load(); + } if ($_SERVER['REQUEST_METHOD'] == 'POST') { $logger = new Logger();