diff --git a/src/Composer/Extra/StraussConfig.php b/src/Composer/Extra/StraussConfig.php index ea56435..2ad97c4 100644 --- a/src/Composer/Extra/StraussConfig.php +++ b/src/Composer/Extra/StraussConfig.php @@ -68,7 +68,7 @@ class StraussConfig implements * * @var ?string[] */ - protected ?array $updateCallSites = null; + protected ?array $updateCallSites = array(); /** * Packages to copy and (maybe) prefix. @@ -398,9 +398,16 @@ public function getUpdateCallSites(): ?array /** * @param string[]|null $updateCallSites */ - public function setUpdateCallSites(?array $updateCallSites): void + public function setUpdateCallSites($updateCallSites): void { - $this->updateCallSites = $updateCallSites; + if (is_array($updateCallSites) && count($updateCallSites) === 1 && $updateCallSites[0] === true) { + // Setting `null` instructs Strauss to update call sites in the project's autoload key. + $this->updateCallSites = null; + } elseif (is_array($updateCallSites) && count($updateCallSites) === 1 && $updateCallSites[0] === false) { + $this->updateCallSites = array(); + } else { + $this->updateCallSites = $updateCallSites; + } } /** diff --git a/src/Composer/ProjectComposerPackage.php b/src/Composer/ProjectComposerPackage.php index a0aec98..ff13b01 100644 --- a/src/Composer/ProjectComposerPackage.php +++ b/src/Composer/ProjectComposerPackage.php @@ -19,13 +19,8 @@ class ProjectComposerPackage extends ComposerPackage * @param string $absolutePath * @param ?array{files?:array,classmap?:array,"psr-4"?:array>} $overrideAutoload */ - public function __construct(string $absolutePath, ?array $overrideAutoload = null) + public function __construct(string $absolutePathFile, ?array $overrideAutoload = null) { - $absolutePathFile = is_dir($absolutePath) - ? rtrim($absolutePath, DIRECTORY_SEPARATOR) . '/composer.json' - : $absolutePath; - unset($absolutePath); - $composer = Factory::create(new NullIO(), $absolutePathFile, true); parent::__construct($composer, $overrideAutoload); diff --git a/src/Console/Commands/DependenciesCommand.php b/src/Console/Commands/DependenciesCommand.php index 101e9d1..88e233c 100644 --- a/src/Console/Commands/DependenciesCommand.php +++ b/src/Console/Commands/DependenciesCommand.php @@ -160,7 +160,7 @@ protected function loadProjectComposerPackage(): void { $this->logger->info('Loading package...'); - $this->projectComposerPackage = new ProjectComposerPackage($this->workingDir); + $this->projectComposerPackage = new ProjectComposerPackage($this->workingDir . 'composer.json'); // TODO: Print the config that Strauss is using. // Maybe even highlight what is default config and what is custom config. @@ -293,14 +293,14 @@ protected function performReplacementsInComposerFiles(): void $this->filesystem ); - $files = $fileEnumerator->compileFileListForPaths([$this->config->getVendorDirectory() . 'composer']); + $files = $fileEnumerator->compileFileListForPaths([$this->config->getVendorDirectory()]); - $composerPhpFileRelativePaths = array_map( - fn($file) => $file->getSourcePath($this->workingDir . $this->config->getVendorDirectory()), + $composerPhpFilePaths = array_map( + fn($file) => $file->getSourcePath(), $files->getFiles() ); - $projectReplace->replaceInProjectFiles($this->discoveredSymbols, $composerPhpFileRelativePaths); + $projectReplace->replaceInProjectFiles($this->discoveredSymbols, $composerPhpFilePaths); } protected function performReplacementsInProjectFiles(): void @@ -328,15 +328,15 @@ protected function performReplacementsInProjectFiles(): void $phpFiles = $fileEnumerator->compileFileListForPaths($callSitePaths); - $phpFilesRelativePaths = array_map( - fn($file) => $file->getSourcePath($this->workingDir), + $phpFilesAbsolutePaths = array_map( + fn($file) => $file->getSourcePath(), $phpFiles->getFiles() ); // TODO: Warn when a file that was specified is not found // $this->logger->warning('Expected file not found from project autoload: ' . $absolutePath); - $projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesRelativePaths); + $projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesAbsolutePaths); } protected function writeClassAliasMap(): void diff --git a/src/Console/Commands/ReplaceCommand.php b/src/Console/Commands/ReplaceCommand.php index 0d2c329..259d994 100644 --- a/src/Console/Commands/ReplaceCommand.php +++ b/src/Console/Commands/ReplaceCommand.php @@ -203,17 +203,17 @@ protected function performReplacementsInProjectFiles(ReplaceConfigInterface $con $this->filesystem ); - $phpFiles = $fileEnumerator->compileFileListForPaths($callSitePaths); - - $phpFilesRelativePaths = array_map( - fn($file) => $file->getSourcePath($this->workingDir), - $phpFiles->getFiles() - ); + $phpFilePaths = $fileEnumerator->compileFileListForPaths($callSitePaths); // TODO: Warn when a file that was specified is not found (during config validation). // $this->logger->warning('Expected file not found from project autoload: ' . $absolutePath); - $projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesRelativePaths); + $phpFilesAbsolutePaths = array_map( + fn($file) => $file->getSourcePath(), + $phpFilePaths->getFiles() + ); + + $projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesAbsolutePaths); } diff --git a/src/Helpers/FileSystem.php b/src/Helpers/FileSystem.php index 8a33c69..b9b9665 100644 --- a/src/Helpers/FileSystem.php +++ b/src/Helpers/FileSystem.php @@ -51,6 +51,9 @@ public function findAllFilesAbsolutePaths(string $workingDir, array $relativeFil public function isDir(string $path): bool { + if (strpos($path, '/.') === strlen($path) - 2) { + $path = rtrim($path, '.'); + } $attributes = $this->getAttributes($path); return $attributes instanceof DirectoryAttributes; } diff --git a/src/Pipeline/FileEnumerator.php b/src/Pipeline/FileEnumerator.php index 7081c74..0b97629 100644 --- a/src/Pipeline/FileEnumerator.php +++ b/src/Pipeline/FileEnumerator.php @@ -119,9 +119,7 @@ public function compileFileListForDependencies(array $dependencies): DiscoveredF foreach ($namespace_relative_paths as $namespaceRelativePath) { $sourceAbsolutePath = $dependency->getPackageAbsolutePath() . $namespaceRelativePath; - if (is_file($sourceAbsolutePath)) { - $this->addFileWithDependency($dependency, $namespaceRelativePath, $type); - } elseif ($this->filesystem->isDir($sourceAbsolutePath)) { + if ($this->filesystem->isDir($sourceAbsolutePath)) { // trailingslashit(). (to remove duplicates). $sourcePath = Path::normalize($sourceAbsolutePath); @@ -145,6 +143,8 @@ public function compileFileListForDependencies(array $dependencies): DiscoveredF $type ); } + } else { + $this->addFileWithDependency($dependency, $namespaceRelativePath, $type); } } } diff --git a/src/Pipeline/Prefixer.php b/src/Pipeline/Prefixer.php index ff3e3a7..74fb6bc 100644 --- a/src/Pipeline/Prefixer.php +++ b/src/Pipeline/Prefixer.php @@ -73,25 +73,30 @@ public function replaceInFiles(DiscoveredSymbols $discoveredSymbols, array $file /** * @param DiscoveredSymbols $discoveredSymbols - * @param string[] $relativeFilePaths + * @param string[] $absoluteFilePathsArray + * * @return void * @throws FilesystemException */ - public function replaceInProjectFiles(DiscoveredSymbols $discoveredSymbols, array $relativeFilePaths): void + public function replaceInProjectFiles(DiscoveredSymbols $discoveredSymbols, array $absoluteFilePathsArray): void { - foreach ($relativeFilePaths as $workingDirRelativeFilepath) { - if (! $this->filesystem->fileExists($this->workingDir . $workingDirRelativeFilepath)) { + foreach ($absoluteFilePathsArray as $workingDirRelativeFilepath) { + $fileAbsolutePath = 0 === strpos($workingDirRelativeFilepath, $this->workingDir) + ? $workingDirRelativeFilepath + : $this->workingDir . $workingDirRelativeFilepath; + + if (! $this->filesystem->fileExists($fileAbsolutePath)) { continue; } // Throws an exception, but unlikely to happen. - $contents = $this->filesystem->read($this->workingDir . $workingDirRelativeFilepath); + $contents = $this->filesystem->read($fileAbsolutePath); $updatedContents = $this->replaceInString($discoveredSymbols, $contents); if ($updatedContents !== $contents) { $this->changedFiles[ $workingDirRelativeFilepath ] = null; - $this->filesystem->write($this->workingDir . $workingDirRelativeFilepath, $updatedContents); + $this->filesystem->write($fileAbsolutePath, $updatedContents); } } } diff --git a/tests/Integration/CopierIntegrationTest.php b/tests/Integration/CopierIntegrationTest.php index 6c8eca0..21a6ec0 100644 --- a/tests/Integration/CopierIntegrationTest.php +++ b/tests/Integration/CopierIntegrationTest.php @@ -47,7 +47,7 @@ public function testsPrepareTarget() exec('composer install'); - $projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir); + $projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir . 'composer.json'); $dependencies = array_map(function ($element) { $composerFile = $this->testsWorkingDir . 'vendor'. DIRECTORY_SEPARATOR . $element . '/composer.json'; @@ -110,7 +110,7 @@ public function testsCopy() exec('composer install'); - $projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir); + $projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir . 'composer.json'); $dependencies = array_map(function ($element) { $composerFile = $this->testsWorkingDir . 'vendor'. DIRECTORY_SEPARATOR . $element . '/composer.json'; diff --git a/tests/Integration/FileEnumeratorIntegrationTest.php b/tests/Integration/FileEnumeratorIntegrationTest.php index 2c20720..9cf0bda 100644 --- a/tests/Integration/FileEnumeratorIntegrationTest.php +++ b/tests/Integration/FileEnumeratorIntegrationTest.php @@ -43,7 +43,7 @@ public function testBuildFileList() exec('composer install'); - $projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir); + $projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir . 'composer.json'); // Only one because we haven't run "flat dependency list". $dependencies = array_map(function ($element) { diff --git a/tests/Integration/FileScannerIntegrationTest.php b/tests/Integration/FileScannerIntegrationTest.php index 9dc8de8..ba9380e 100644 --- a/tests/Integration/FileScannerIntegrationTest.php +++ b/tests/Integration/FileScannerIntegrationTest.php @@ -50,7 +50,7 @@ public function testOne() exec('composer install'); - $projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir); + $projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir . 'composer.json'); $dependencies = array_map(function ($element) { $composerFile = $this->testsWorkingDir . 'vendor'. DIRECTORY_SEPARATOR . $element . '/composer.json'; diff --git a/tests/Issues/StraussIssue108Test.php b/tests/Issues/StraussIssue108Test.php index d12b48a..b0adfb7 100644 --- a/tests/Issues/StraussIssue108Test.php +++ b/tests/Issues/StraussIssue108Test.php @@ -40,7 +40,8 @@ public function test_correct_directory_permission() "." ] } - } + }, + "update_call_sites": true } } }