From 148cfedda25202f086a8fe58014250f860583af2 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Fri, 22 Mar 2024 14:24:01 -0400 Subject: [PATCH] Add modules to exclude path --- CHANGELOG.md | 4 ++ src/Support/PhpStorm/PhpFrameworkWriter.php | 57 +++++++++++++++++---- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b519082..f87eb12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- The modules sync command now adds modules to PhpStorm exclude path, preventing double-registration of modules + +### Added + - Added support for Laravel 11 - Added support for event discovery diff --git a/src/Support/PhpStorm/PhpFrameworkWriter.php b/src/Support/PhpStorm/PhpFrameworkWriter.php index 120e799..235d029 100644 --- a/src/Support/PhpStorm/PhpFrameworkWriter.php +++ b/src/Support/PhpStorm/PhpFrameworkWriter.php @@ -4,15 +4,13 @@ use Illuminate\Support\Str; use InterNACHI\Modular\Support\ModuleConfig; +use SimpleXMLElement; class PhpFrameworkWriter extends ConfigWriter { public function write(): bool { - $config = simplexml_load_string(file_get_contents($this->config_path)); - if (empty($config->xpath('//component[@name="PhpIncludePathManager"]//include_path//path'))) { - return true; - } + $config = $this->getNormalizedPluginConfig(); $namespace = config('app-modules.modules_namespace', 'Modules'); $vendor = config('app-modules.modules_vendor') ?? Str::kebab($namespace); @@ -21,14 +19,55 @@ public function write(): bool return '$PROJECT_DIR$/vendor/'.$vendor.'/'.$module->name; }); - $include_paths = $config->xpath('//component[@name="PhpIncludePathManager"]//include_path//path'); - - foreach ($include_paths as $key => $existing) { - if ($module_paths->contains((string) $existing['value'])) { - unset($include_paths[$key][0]); + // Remove modules from include_path + if (! empty($config->xpath('//component[@name="PhpIncludePathManager"]//include_path//path'))) { + $include_paths = $config->xpath('//component[@name="PhpIncludePathManager"]//include_path//path'); + foreach ($include_paths as $key => $existing) { + if ($module_paths->contains((string) $existing['value'])) { + unset($include_paths[$key][0]); + } } } + // Add modules to exclude_path + $exclude_paths = $config->xpath('//component[@name="PhpIncludePathManager"]//exclude_path//path'); + $existing_values = collect($exclude_paths)->map(function($node) { + return (string) $node['value']; + }); + + // Now add all missing modules to the config + $content = $config->xpath('//component[@name="PhpIncludePathManager"]//exclude_path')[0]; + $module_paths->each(function(string $module_path) use (&$content, $existing_values) { + if ($existing_values->contains($module_path)) { + return; + } + + $path_node = $content->addChild('path'); + $path_node->addAttribute('value', $module_path); + }); + return false !== file_put_contents($this->config_path, $this->formatXml($config)); } + + protected function getNormalizedPluginConfig(): SimpleXMLElement + { + $config = simplexml_load_string(file_get_contents($this->config_path)); + + // Ensure that exists + $component = $config->xpath('//component[@name="PhpIncludePathManager"]'); + if (empty($component)) { + $component = $config->addChild('component'); + $component->addAttribute('name', 'PhpIncludePathManager'); + } else { + $component = $component[0]; + } + + // Ensure that exists + $content = $component->xpath('//exclude_path'); + if (empty($content)) { + $component->addChild('exclude_path'); + } + + return $config; + } }