Skip to content

Commit c0f96a3

Browse files
committed
package
1 parent 58c677a commit c0f96a3

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

src/Command/DumpCommand.php

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3434

3535
$localConfig = $composer->getConfig();
3636

37-
$localPsr4Paths = $localPackage->getAutoload()['psr-4'];
38-
if (count($localPsr4Paths) < 2) {
37+
$localPsr4Autoload = $localPackage->getAutoload()['psr-4'] ?? [];
38+
$hasCacheDirectory = false;
39+
foreach ($localPsr4Autoload as $directories) {
40+
if (!is_array($directories)) {
41+
continue;
42+
}
43+
if (count($directories) < 2) {
44+
continue;
45+
}
46+
$hasCacheDirectory = true;
47+
break;
48+
}
49+
if (!$hasCacheDirectory) {
3950
$this->getIO()->writeError("<bg=red>You must set cache directory</>");
4051

4152
return 1;
@@ -97,55 +108,65 @@ protected function execute(InputInterface $input, OutputInterface $output): int
97108

98109
$filesCount = 0;
99110

100-
$sourceDirectory = $filesystem->normalizePath($basePath . '/' . $localPsr4Paths[1]);
101-
$cacheDirectory = $filesystem->normalizePath($basePath . '/' . $localPsr4Paths[0]);
102-
$filesystem->emptyDirectory($cacheDirectory);
111+
foreach ($localPsr4Autoload as $directories) {
112+
if (!is_array($directories)) {
113+
continue;
114+
}
103115

104-
try {
105-
$result = $compiler->compile($sourceDirectory);
116+
if (count($directories) < 2) {
117+
continue;
118+
}
106119

107-
foreach ($result->getConcreteClasses() as $concreteClass) {
108-
$genericFilePath = $classLoader->findFile($concreteClass->genericFqn);
109-
if (!$genericFilePath) {
110-
throw new \RuntimeException(sprintf('Can\'t find file for class "%s"', $concreteClass->genericFqn));
111-
}
120+
$sourceDirectory = $filesystem->normalizePath($basePath . '/' . $directories[1]);
121+
$cacheDirectory = $filesystem->normalizePath($basePath . '/' . $directories[0]);
122+
$filesystem->emptyDirectory($cacheDirectory);
112123

113-
$package = self::findPackageByFilePath($genericPackages, $genericFilePath);
114-
if ($package === null) {
115-
throw new \RuntimeException(sprintf('Can\'t find package for file "%s"', $genericFilePath));
116-
}
124+
try {
125+
$result = $compiler->compile($sourceDirectory);
117126

118-
$cacheDirectory = $package->getCacheDirectory($genericFilePath);
127+
foreach ($result->getConcreteClasses() as $concreteClass) {
128+
$genericFilePath = $classLoader->findFile($concreteClass->genericFqn);
129+
if (!$genericFilePath) {
130+
throw new \RuntimeException(sprintf('Can\'t find file for class "%s"', $concreteClass->genericFqn));
131+
}
119132

120-
if (array_key_exists($cacheDirectory, $emptiedCacheDirectories)) {
121-
$filesystem->emptyDirectory($cacheDirectory);
122-
$emptiedCacheDirectories[$cacheDirectory] = true;
123-
}
133+
$package = self::findPackageByFilePath($genericPackages, $genericFilePath);
134+
if ($package === null) {
135+
throw new \RuntimeException(sprintf('Can\'t find package for file "%s"', $genericFilePath));
136+
}
124137

125-
$concreteFilePath = rtrim($cacheDirectory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($package->getRelativeFilePathByClassFqn($concreteClass->fqn), DIRECTORY_SEPARATOR);
126-
$filesystem->ensureDirectoryExists(dirname($concreteFilePath));
127-
if (file_put_contents($concreteFilePath, $printer->printFile($concreteClass->ast)) === false) {
128-
throw new \RuntimeException(sprintf('Can\'t write into file "%s"', $concreteFilePath));
129-
}
130-
$filesCount++;
138+
$cacheDirectory = $package->getCacheDirectory($genericFilePath);
139+
140+
if (array_key_exists($cacheDirectory, $emptiedCacheDirectories)) {
141+
$filesystem->emptyDirectory($cacheDirectory);
142+
$emptiedCacheDirectories[$cacheDirectory] = true;
143+
}
131144

132-
if ($this->getIO()->isVerbose()) {
133-
$line = sprintf(' - %s', $concreteClass->fqn);
134-
if ($this->getIO()->isVeryVerbose()) {
135-
$line .= sprintf(' <comment>%s</comment>', $concreteFilePath);
145+
$concreteFilePath = rtrim($cacheDirectory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($package->getRelativeFilePathByClassFqn($concreteClass->fqn), DIRECTORY_SEPARATOR);
146+
$filesystem->ensureDirectoryExists(dirname($concreteFilePath));
147+
if (file_put_contents($concreteFilePath, $printer->printFile($concreteClass->ast)) === false) {
148+
throw new \RuntimeException(sprintf('Can\'t write into file "%s"', $concreteFilePath));
149+
}
150+
$filesCount++;
151+
152+
if ($this->getIO()->isVerbose()) {
153+
$line = sprintf(' - %s', $concreteClass->fqn);
154+
if ($this->getIO()->isVeryVerbose()) {
155+
$line .= sprintf(' <comment>%s</comment>', $concreteFilePath);
156+
}
157+
$this->getIO()->write($line);
136158
}
137-
$this->getIO()->write($line);
138159
}
139-
}
140160

141-
} catch (\Exception $exception) {
142-
$this->getIO()->writeError(sprintf("<bg=red>%s</>", $exception->getMessage()));
143-
if ($exception->getPrevious() !== null) {
144-
$this->getIO()->writeError(sprintf("<bg=red>%s</>", $exception->getPrevious()->getMessage()));
145-
}
146-
$this->getIO()->writeError(sprintf("<bg=red>%s</>", $exception->getTraceAsString()));
161+
} catch (\Exception $exception) {
162+
$this->getIO()->writeError(sprintf("<bg=red>%s</>", $exception->getMessage()));
163+
if ($exception->getPrevious() !== null) {
164+
$this->getIO()->writeError(sprintf("<bg=red>%s</>", $exception->getPrevious()->getMessage()));
165+
}
166+
$this->getIO()->writeError(sprintf("<bg=red>%s</>", $exception->getTraceAsString()));
147167

148-
return 1;
168+
return 1;
169+
}
149170
}
150171

151172
$timeFin = microtime(true);

0 commit comments

Comments
 (0)