Skip to content

Commit 1549f67

Browse files
committed
Fix glob does not work in phar archive
Fix missing built-in definitions (not copied in)
1 parent 191be12 commit 1549f67

File tree

4 files changed

+93
-48
lines changed

4 files changed

+93
-48
lines changed

.idea/csv-plugin.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Commands/Services/DockerComposeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
190190

191191
$dumper->store($compose, $service->getFileInProject('docker-compose.yml'));
192192

193-
$this->tools()->success('done');
193+
$this->tools()->success('done - be sure to check your <info>docker-compose.yml</info> file');
194194

195195
return 0;
196196
} catch (DockerComposeException $e) {

src/Services/Compiler.php

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use function file_exists;
1010
use function shell_exec;
1111
use function str_replace;
12+
use function strcmp;
1213
use function strtr;
1314

1415
/**
@@ -42,37 +43,10 @@ public function compile($pharFile = 'somnambulist-project-manager.phar')
4243
$phar = new Phar($pharFile, 0, 'somnambulist-project-manager.phar');
4344
$phar->startBuffering();
4445

45-
$finderSort = function (SplFileInfo $a, SplFileInfo $b) {
46-
return strcmp(strtr($a->getRealPath(), '\\', '/'), strtr($b->getRealPath(), '\\', '/'));
47-
};
48-
4946
$basePath = $this->basePath();
5047

51-
$finder = new Finder();
52-
$finder->files()
53-
->ignoreVCS(true)
54-
->name(['*.php', '*.yaml', '*.yml', '*.md', '*.xml'])
55-
->name('LICENSE')
56-
->name('dockerignore')
57-
->name('gitignore')
58-
->exclude('Tests')
59-
->exclude('tests')
60-
->exclude('docs')
61-
->notName('create-phar.php')
62-
->in($basePath . '/config/')
63-
->in($basePath . '/src/')
64-
->in($basePath . '/var/cache/prod/')
65-
->in($basePath . '/vendor/pragmarx/')
66-
->in($basePath . '/vendor/psr/')
67-
->in($basePath . '/vendor/somnambulist/')
68-
->in($basePath . '/vendor/symfony/')
69-
->in($basePath . '/vendor/voku/')
70-
->sort($finderSort)
71-
;
72-
73-
foreach ($finder as $file) {
74-
$this->addFile($phar, $file);
75-
}
48+
$this->addApplicationFiles($phar, $basePath);
49+
$this->addDefinitionFiles($phar, $basePath);
7650

7751
$testFor = [
7852
'include_paths.php', 'platform_check.php', 'installed.php', 'InstalledVersions.php', 'installed.json',
@@ -106,6 +80,57 @@ public function compile($pharFile = 'somnambulist-project-manager.phar')
10680
chmod($pharFile, 0755);
10781
}
10882

83+
private function addApplicationFiles(Phar $phar, string $basePath): void
84+
{
85+
$finder = new Finder();
86+
$finder
87+
->files()
88+
->ignoreVCS(true)
89+
->name(['*.php', '*.yaml', '*.yml', '*.md', '*.xml'])
90+
->name('LICENSE')
91+
->name('dockerignore')
92+
->name('gitignore')
93+
->exclude('Tests')
94+
->exclude('tests')
95+
->exclude('docs')
96+
->exclude('config/definitions')
97+
->notName('create-phar.php')
98+
->in($basePath . '/config/')
99+
->in($basePath . '/src/')
100+
->in($basePath . '/var/cache/prod/')
101+
->in($basePath . '/vendor/pragmarx/')
102+
->in($basePath . '/vendor/psr/')
103+
->in($basePath . '/vendor/somnambulist/')
104+
->in($basePath . '/vendor/symfony/')
105+
->in($basePath . '/vendor/voku/')
106+
->sort(function (SplFileInfo $a, SplFileInfo $b) {
107+
return strcmp(strtr($a->getRealPath(), '\\', '/'), strtr($b->getRealPath(), '\\', '/'));
108+
})
109+
;
110+
111+
foreach ($finder as $file) {
112+
$this->addFile($phar, $file);
113+
}
114+
}
115+
116+
private function addDefinitionFiles(Phar $phar, string $basePath): void
117+
{
118+
$finder = new Finder();
119+
$finder
120+
->files()
121+
->in($basePath . '/config/definitions/')
122+
->name('*')
123+
->ignoreVCS(true)
124+
->sort(function (SplFileInfo $a, SplFileInfo $b) {
125+
return strcmp(strtr($a->getRealPath(), '\\', '/'), strtr($b->getRealPath(), '\\', '/'));
126+
})
127+
;
128+
129+
foreach ($finder as $file) {
130+
$this->addFile($phar, $file);
131+
}
132+
}
133+
109134
private function addFile(Phar $phar, SplFileInfo $file, $strip = true): void
110135
{
111136
$path = strtr(str_replace(realpath($this->basePath()), '', $file->getRealPath()), '\\', '/');

src/Services/Docker/ServiceDefinitionLocator.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
use Somnambulist\Collection\MutableCollection;
66
use Somnambulist\ProjectManager\Exceptions\DefinitionNotFound;
77
use Somnambulist\ProjectManager\Models\Definitions\ServiceDefinition;
8-
use function basename;
8+
use SplFileInfo;
9+
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
10+
use Symfony\Component\Finder\Finder;
911
use function dirname;
10-
use function file_get_contents;
11-
use function glob;
12-
use function is_dir;
12+
use function sprintf;
1313
use function str_replace;
14-
use const GLOB_BRACE;
1514

1615
/**
1716
* Class ServiceDefinitionLocator
@@ -39,11 +38,11 @@ public function findAll(): MutableCollection
3938
private function findInternalDefinitions(): MutableCollection
4039
{
4140
$ret = new MutableCollection();
42-
$files = glob(dirname(__DIR__, 3) . '/config/definitions/*.yaml');
41+
$files = (new Finder())->files()->in(dirname(__DIR__, 3) . '/config/definitions')->depth(0)->name('*.yaml')->sortByName();
4342

4443
foreach ($files as $file) {
45-
$name = basename($file, '.yaml');
46-
$ret->set($name, new ServiceDefinition($name, file_get_contents($file), $this->findRelatedFilesForService($name, $file)));
44+
$name = $file->getBasename('.yaml');
45+
$ret->set($name, new ServiceDefinition($name, $file->getContents(), $this->findRelatedFilesForService($name, $file)));
4746
}
4847

4948
return $ret;
@@ -52,26 +51,40 @@ private function findInternalDefinitions(): MutableCollection
5251
private function findExternalDefinitions(): MutableCollection
5352
{
5453
$ret = new MutableCollection();
55-
$files = glob($p = $_SERVER['SOMNAMBULIST_PROJECTS_CONFIG_DIR'] . '/definitions/*.yaml');
54+
$files = (new Finder())->files()->ignoreDotFiles(true)->ignoreVCS(true)->name('*.yaml')->sortByName();
55+
$f = false;
56+
57+
foreach ([$_SERVER['SOMNAMBULIST_ACTIVE_PROJECT'] . '/definitions', '/definitions'] as $path) {
58+
try {
59+
$files->in($d = sprintf('%s/%s', $_SERVER['SOMNAMBULIST_PROJECTS_CONFIG_DIR'], $path));
60+
$f = true;
61+
} catch (DirectoryNotFoundException $e) {
62+
// SF Finder throws exceptions if the dir does not exist
63+
}
64+
}
5665

57-
foreach ($files as $file) {
58-
$name = basename($file, '.yaml');
59-
$ret->set($name, new ServiceDefinition($name, file_get_contents($file), $this->findRelatedFilesForService($name, $file)));
66+
if ($f) {
67+
foreach ($files as $file) {
68+
$name = $file->getBasename('.yaml');
69+
$ret->set($name, new ServiceDefinition($name, $file->getContents(), $this->findRelatedFilesForService($name, $file)));
70+
}
6071
}
6172

6273
return $ret;
6374
}
6475

65-
private function findRelatedFilesForService(string $name, string $file): array
76+
private function findRelatedFilesForService(string $name, SplFileInfo $file): array
6677
{
67-
$root = sprintf('%s/%s', dirname($file), $name);
68-
$files = glob($p = sprintf('%s/{,*/,*/*/}*', $root), GLOB_BRACE);
78+
$path = sprintf('%s/%s', $file->getPath(), $name);
6979
$return = [];
7080

71-
foreach ($files as $f) {
72-
if (is_dir($f)) continue;
81+
try {
82+
$files = (new Finder())->files()->in($path)->name('*')->ignoreVCS(true)->ignoreDotFiles(true);
7383

74-
$return[] = new ServiceDefinition(str_replace($root . '/', '', $f), file_get_contents($f));
84+
foreach ($files as $f) {
85+
$return[] = new ServiceDefinition(str_replace($path . '/', '', $f->getPathname()), $f->getContents());
86+
}
87+
} catch (DirectoryNotFoundException $e) {
7588
}
7689

7790
return $return;

0 commit comments

Comments
 (0)