-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathModulesSync.php
More file actions
161 lines (129 loc) · 4.67 KB
/
Copy pathModulesSync.php
File metadata and controls
161 lines (129 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace InterNACHI\Modular\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use InterNACHI\Modular\Support\FinderCollection;
use InterNACHI\Modular\Support\ModuleRegistry;
use InterNACHI\Modular\Support\PhpStorm\LaravelConfigWriter;
use InterNACHI\Modular\Support\PhpStorm\PhpFrameworkWriter;
use InterNACHI\Modular\Support\PhpStorm\ProjectImlWriter;
use InterNACHI\Modular\Support\PhpStorm\WorkspaceWriter;
use Symfony\Component\Finder\SplFileInfo;
class ModulesSync extends Command
{
protected $signature = 'modules:sync {--no-phpstorm : Do not update PhpStorm config files}';
protected $description = 'Sync your project\'s configuration with your current modules';
/**
* @var \Illuminate\Filesystem\Filesystem
*/
protected $filesystem;
/**
* @var \InterNACHI\Modular\Support\ModuleRegistry
*/
protected $registry;
public function handle(ModuleRegistry $registry, Filesystem $filesystem)
{
$this->filesystem = $filesystem;
$this->registry = $registry;
$this->updatePhpUnit();
if (true !== $this->option('no-phpstorm')) {
$this->updatePhpStormConfig();
}
}
protected function updatePhpUnit(): void
{
$config_path = $this->getLaravel()->basePath('phpunit.xml');
if (! $this->filesystem->exists($config_path)) {
$this->warn('No phpunit.xml file found. Skipping PHPUnit configuration.');
return;
}
$modules_directory = config('app-modules.modules_directory', 'app-modules');
$config = simplexml_load_string($this->filesystem->get($config_path));
$existing_nodes = $config->xpath("//phpunit//testsuites//testsuite//directory[text()='./{$modules_directory}/*/tests']");
if (count($existing_nodes)) {
$this->info('Modules test suite already exists in phpunit.xml');
return;
}
$testsuites = $config->xpath('//phpunit//testsuites');
if (! count($testsuites)) {
$this->error('Cannot find <testsuites> node in phpunit.xml file. Skipping PHPUnit configuration.');
return;
}
$testsuite = $testsuites[0]->addChild('testsuite');
$testsuite->addAttribute('name', 'Modules');
$directory = $testsuite->addChild('directory');
$directory->addAttribute('suffix', 'Test.php');
$directory[0] = "./{$modules_directory}/*/tests";
$this->filesystem->put($config_path, $config->asXML());
$this->info('Added "Modules" PHPUnit test suite.');
}
protected function updatePhpStormConfig(): void
{
$this->updatePhpStormLaravelPlugin();
$this->updatePhpStormPhpConfig();
$this->updatePhpStormWorkspaceConfig();
$this->updatePhpStormProjectIml();
}
protected function updatePhpStormLaravelPlugin(): void
{
$config_path = $this->getLaravel()->basePath('.idea/laravel-plugin.xml');
$writer = new LaravelConfigWriter($config_path, $this->registry);
if ($writer->handle()) {
$this->info('Updated PhpStorm/Laravel Plugin config file...');
} else {
$this->info('Did not find/update PhpStorm/Laravel Plugin config.');
if ($this->getOutput()->isVerbose()) {
$this->warn($writer->last_error);
}
}
}
protected function updatePhpStormPhpConfig(): void
{
$config_path = $this->getLaravel()->basePath('.idea/php.xml');
$writer = new PhpFrameworkWriter($config_path, $this->registry);
if ($writer->handle()) {
$this->info('Updated PhpStorm PHP config file...');
} else {
$this->info('Did not find/update PhpStorm PHP config.');
if ($this->getOutput()->isVerbose()) {
$this->warn($writer->last_error);
}
}
}
protected function updatePhpStormWorkspaceConfig(): void
{
$config_path = $this->getLaravel()->basePath('.idea/workspace.xml');
$writer = new WorkspaceWriter($config_path, $this->registry);
if ($writer->handle()) {
$this->info('Updated PhpStorm workspace library roots...');
} else {
$this->info('Did not find/update PhpStorm workspace config.');
if ($this->getOutput()->isVerbose()) {
$this->warn($writer->last_error);
}
}
}
protected function updatePhpStormProjectIml(): void
{
$idea_directory = $this->getLaravel()->basePath('.idea/');
if (! $this->filesystem->isDirectory($idea_directory)) {
return;
}
FinderCollection::forFiles()
->in($idea_directory)
->name('*.iml')
->first(function(SplFileInfo $file) {
$config_path = $file->getPathname();
$writer = new ProjectImlWriter($config_path, $this->registry);
if ($writer->handle()) {
$this->info("Updated PhpStorm project source folders in '{$file->getBasename()}'");
return true;
}
$this->info("Could not update PhpStorm project source folders in '{$file->getBasename()}'");
if ($this->getOutput()->isVerbose()) {
$this->warn($writer->last_error);
}
return false;
});
}
}