diff --git a/src/Hooks.php b/src/Hooks.php index b34f1d8..d09cbe3 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -3,6 +3,9 @@ namespace WPHooks; +use Composer\Autoload\ClassLoader; +use ReflectionClass; + /** * @phpstan-import-type HookArray from Hook * @phpstan-type HooksArray array @@ -91,6 +94,30 @@ protected static function findFileFromVendor( string $directory, string $path ): ) ); } + /** + * @throws \RuntimeException + */ + public static function getVendorPath(): string { + static $cache; + + if (is_string($cache)) { + return $cache; + } + + $reflector = new ReflectionClass(ClassLoader::class); + $classLoaderPath = $reflector->getFileName(); + if ($classLoaderPath === false) { + throw new \RuntimeException('Unable to find Composer ClassLoader file.'); + } + + $vendorPath = dirname($classLoaderPath, 2); + if (!is_dir($vendorPath)) { + throw new \RuntimeException('Unable to detect vendor path.'); + } + + return $cache = $vendorPath; + } + /** * @phpstan-param HooksArray $data */