Skip to content

Commit

Permalink
Add modules to exclude path
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Mar 22, 2024
1 parent 98639bb commit 148cfed
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
57 changes: 48 additions & 9 deletions src/Support/PhpStorm/PhpFrameworkWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 <component name="PhpIncludePathManager"> exists
$component = $config->xpath('//component[@name="PhpIncludePathManager"]');
if (empty($component)) {
$component = $config->addChild('component');
$component->addAttribute('name', 'PhpIncludePathManager');
} else {
$component = $component[0];
}

// Ensure that <exclude_path> exists
$content = $component->xpath('//exclude_path');
if (empty($content)) {
$component->addChild('exclude_path');
}

return $config;
}
}

0 comments on commit 148cfed

Please sign in to comment.