From 0aa8042b676df910ee10ca7f3868dd1a256c91f0 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 31 Aug 2023 17:19:40 -0700 Subject: [PATCH] phpstorm - Prefer self-same folder for output, but fallback to [civicrm.files] --- .../Civi/PhpStorm/PhpStormMetadata.php | 3 ++ tools/extensions/phpstorm/phpstorm.php | 40 ++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/tools/extensions/phpstorm/Civi/PhpStorm/PhpStormMetadata.php b/tools/extensions/phpstorm/Civi/PhpStorm/PhpStormMetadata.php index 9e88fa2c25..55642eff87 100644 --- a/tools/extensions/phpstorm/Civi/PhpStorm/PhpStormMetadata.php +++ b/tools/extensions/phpstorm/Civi/PhpStorm/PhpStormMetadata.php @@ -98,6 +98,9 @@ class PhpStormMetadata { */ public function write(): void { $path = phpstorm_metadata_dir(); + if ($path === NULL) { + return; + } if (file_exists($path) && !is_dir($path)) { unlink($path); diff --git a/tools/extensions/phpstorm/phpstorm.php b/tools/extensions/phpstorm/phpstorm.php index 015e91dec5..9e12439452 100644 --- a/tools/extensions/phpstorm/phpstorm.php +++ b/tools/extensions/phpstorm/phpstorm.php @@ -17,8 +17,25 @@ use CRM_PhpStorm_ExtensionUtil as E; * * @return string */ -function phpstorm_metadata_dir(): string { - return \Civi::paths()->getPath('[civicrm.files]/.phpstorm.meta.php'); +function phpstorm_metadata_dir(): ?string { + $candidates = _phpstorm_metadata_dirs(); + foreach ($candidates as $candidate) { + if (file_exists($candidate) && is_writable($candidate)) { + return $candidate; + } + if (!file_exists($candidate) && is_writable(dirname($candidate))) { + return $candidate; + } + } + \Civi::log()->error("Failed to find writeable folder for PhpStorm metadata. Candidates: " . implode(",", $candidates)); + return NULL; +} + +function _phpstorm_metadata_dirs(): array { + return [ + E::path('.phpstorm.meta.php'), + \Civi::paths()->getPath('[civicrm.files]/.phpstorm.meta.php'), + ]; } /** @@ -43,9 +60,20 @@ function phpstorm_civicrm_managed(&$entities, $modules) { } } -function phpstorm_civicrm_uninstall() { - $dir = phpstorm_metadata_dir(); - if (file_exists($dir)) { - CRM_Utils_File::cleanDir($dir, TRUE); +function phpstorm_civicrm_enable(): void { + // Remove any stale files from old versions. + _phpstorm_cleanup(); +} + +function phpstorm_civicrm_uninstall(): void { + _phpstorm_cleanup(); +} + +function _phpstorm_cleanup(): void { + $dirs = _phpstorm_metadata_dirs(); + foreach ($dirs as $dir) { + if (file_exists($dir)) { + CRM_Utils_File::cleanDir($dir, TRUE); + } } } -- 2.25.1