Skip to content

Commit 2e94b3f

Browse files
committed
Implement writing and using cache
1 parent 16ce51b commit 2e94b3f

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/Config.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ private function buildGroup(string $group): void
6464
return;
6565
}
6666

67-
// TODO: get from cache here if cache exists
67+
$cachePath = $this->cachePath . '/' . $group . '.php';
68+
if ($this->useCache && file_exists($cachePath)) {
69+
$this->build[$group] = $this->buildFile($group, $cachePath);
70+
return;
71+
}
6872

6973
$this->build[$group] = [];
7074

@@ -88,7 +92,8 @@ private function buildGroup(string $group): void
8892
$matches = glob($path);
8993

9094
foreach ($matches as $match) {
91-
$this->buildFile($group, $match, [$file, $group, $packageName]);
95+
$buildConfig = $this->buildFile($group, $match);
96+
$this->build[$group] = $this->merge([$file, $group, $packageName], '', $this->build[$group], $buildConfig);
9297
}
9398
continue;
9499
}
@@ -97,21 +102,21 @@ private function buildGroup(string $group): void
97102
continue;
98103
}
99104

100-
$this->buildFile($group, $path, [$file, $group, $packageName]);
105+
$buildConfig = $this->buildFile($group, $path);
106+
$this->build[$group] = $this->merge([$file, $group, $packageName], '', $this->build[$group], $buildConfig);
101107
}
102108
}
103109

104110
if ($this->writeCache) {
105-
// This is debug only. Export isn't working correctly (not exporting namespaces).
106-
$filePath = $this->cachePath . '/' . $group . '.php';
107-
file_put_contents($filePath, "<?php\n\ndeclare(strict_types=1);\n\nreturn " . VarDumper::create($this->build[$group])->export(true) . ";\n");
111+
// TODO: export isn't working correctly (not exporting namespaces).
112+
file_put_contents($cachePath, "<?php\n\ndeclare(strict_types=1);\n\nreturn " . VarDumper::create($this->build[$group])->export(true) . ";\n");
108113
}
109114
}
110115

111116
/**
112117
* @psalm-param array{string, string, string} $context $context
113118
*/
114-
private function buildFile(string $group, string $filePath, array $context): void
119+
private function buildFile(string $group, string $filePath): array
115120
{
116121
$scopeRequire = static function (Config $config): array {
117122
/** @psalm-suppress InvalidArgument, MissingClosureParamType */
@@ -136,8 +141,7 @@ private function buildFile(string $group, string $filePath, array $context): voi
136141
}
137142

138143
/** @psalm-suppress TooManyArguments */
139-
$config = $scopeRequire($this, $filePath, $scope);
140-
$this->build[$group] = $this->merge($context, '', $this->build[$group], $config);
144+
return $scopeRequire($this, $filePath, $scope);
141145
}
142146

143147
public function get(string $name): array

0 commit comments

Comments
 (0)