Skip to content

Commit

Permalink
Fix #37 Allow psr-4 autoload paths to be arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Dec 3, 2022
1 parent fdce24f commit 35d0043
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 48 deletions.
102 changes: 54 additions & 48 deletions src/FileEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,72 +106,78 @@ public function compileFileList()
$this->filesAutoloaders[$dependency->getRelativePath()] = $value;
}

foreach ($value as $namespace => $namespace_relative_path) {
foreach ($value as $namespace => $namespace_relative_paths) {
if (!empty($namespace) && in_array($namespace, $this->excludeNamespaces)) {
continue;
}

if (is_file($packagePath . $namespace_relative_path)) {
// $finder->files()->name($file)->in($source_path);
if (! is_array($namespace_relative_paths)) {
$namespace_relative_paths = array( $namespace_relative_paths );
}

$sourceAbsoluteFilepath = $packagePath . $namespace_relative_path;

$outputRelativeFilepath = str_replace($prefixToRemove, '', $sourceAbsoluteFilepath);
$outputRelativeFilepath = preg_replace('#[\\\/]+#', DIRECTORY_SEPARATOR, $outputRelativeFilepath);
foreach ($namespace_relative_paths as $namespace_relative_path) {
if (is_file($packagePath . $namespace_relative_path)) {
// $finder->files()->name($file)->in($source_path);

$file = array(
'dependency' => $dependency,
'sourceAbsoluteFilepath' => $sourceAbsoluteFilepath,
'targetRelativeFilepath' => $outputRelativeFilepath,
);
$this->filesWithDependencies[$outputRelativeFilepath] = $file;
continue;
} else {
// else it is a directory.
$sourceAbsoluteFilepath = $packagePath . $namespace_relative_path;

// trailingslashit().
$namespace_relative_path = rtrim($namespace_relative_path, DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR;
$outputRelativeFilepath = str_replace($prefixToRemove, '', $sourceAbsoluteFilepath);
$outputRelativeFilepath = preg_replace('#[\\\/]+#', DIRECTORY_SEPARATOR, $outputRelativeFilepath);

$sourcePath = $packagePath . $namespace_relative_path;
$file = array(
'dependency' => $dependency,
'sourceAbsoluteFilepath' => $sourceAbsoluteFilepath,
'targetRelativeFilepath' => $outputRelativeFilepath,
);
$this->filesWithDependencies[ $outputRelativeFilepath ] = $file;
continue;
} else {
// else it is a directory.

// trailingslashit(). (to remove duplicates).
$sourcePath = rtrim($sourcePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
// trailingslashit().
$namespace_relative_path = rtrim($namespace_relative_path, DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR;

$finder = new Finder();
$finder->files()->in($sourcePath)->followLinks();
$sourcePath = $packagePath . $namespace_relative_path;

foreach ($finder as $foundFile) {
$sourceAbsoluteFilepath = $foundFile->getPathname();
// trailingslashit(). (to remove duplicates).
$sourcePath = rtrim($sourcePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

$outputRelativeFilepath = str_replace($prefixToRemove, '', $sourceAbsoluteFilepath);
$finder = new Finder();
$finder->files()->in($sourcePath)->followLinks();

// For symlinked packages.
if( $outputRelativeFilepath == $sourceAbsoluteFilepath ) {
$outputRelativeFilepath = str_replace($packagePath, $dependency->getPackageName() . DIRECTORY_SEPARATOR, $sourceAbsoluteFilepath);
}
foreach ($finder as $foundFile) {
$sourceAbsoluteFilepath = $foundFile->getPathname();

// TODO: Is this needed here?! If anything, it's the prefix that needs to be normalised a few
// lines above before being used.
// Replace multiple \ and/or / with OS native DIRECTORY_SEPARATOR.
$outputRelativeFilepath = preg_replace('#[\\\/]+#', DIRECTORY_SEPARATOR, $outputRelativeFilepath);
$outputRelativeFilepath = str_replace($prefixToRemove, '', $sourceAbsoluteFilepath);

foreach ($this->excludeFilePatterns as $excludePattern) {
if (1 === preg_match($excludePattern, $outputRelativeFilepath)) {
continue 2;
// For symlinked packages.
if ($outputRelativeFilepath == $sourceAbsoluteFilepath) {
$outputRelativeFilepath = str_replace($packagePath, $dependency->getPackageName() . DIRECTORY_SEPARATOR, $sourceAbsoluteFilepath);
}
}

if (is_dir($sourceAbsoluteFilepath)) {
continue;
}
// TODO: Is this needed here?! If anything, it's the prefix that needs to be normalised a few
// lines above before being used.
// Replace multiple \ and/or / with OS native DIRECTORY_SEPARATOR.
$outputRelativeFilepath = preg_replace('#[\\\/]+#', DIRECTORY_SEPARATOR, $outputRelativeFilepath);

$file = array(
'dependency' => $dependency,
'sourceAbsoluteFilepath' => $sourceAbsoluteFilepath,
'targetRelativeFilepath' => $outputRelativeFilepath,
);
$this->filesWithDependencies[$outputRelativeFilepath] = $file;
foreach ($this->excludeFilePatterns as $excludePattern) {
if (1 === preg_match($excludePattern, $outputRelativeFilepath)) {
continue 2;
}
}

if (is_dir($sourceAbsoluteFilepath)) {
continue;
}

$file = array(
'dependency' => $dependency,
'sourceAbsoluteFilepath' => $sourceAbsoluteFilepath,
'targetRelativeFilepath' => $outputRelativeFilepath,
);
$this->filesWithDependencies[ $outputRelativeFilepath ] = $file;
}
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions tests/Issues/StraussIssue37Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
*
* @see https://github.com/BrianHenryIE/strauss/issues/37
*/

namespace BrianHenryIE\Strauss\Tests\Issues;

use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
use BrianHenryIE\Strauss\Console\Commands\Compose;
use BrianHenryIE\Strauss\Prefixer;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @package BrianHenryIE\Strauss\Tests\Issues
* @coversNothing
*/
class StraussIssue37Test extends \BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase
{

/**
*/
public function test_can_handle_psr_namespace_with_path_array()
{

$composerJsonString = <<<'EOD'
{
"name": "brianhenryie/strauss-psr-4-path-array",
"minimum-stability": "dev",
"require": {
"automattic/woocommerce": "*"
},
"extra": {
"strauss": {
"namespace_prefix": "BrianHenryIE\\Strauss\\",
"classmap_prefix": "BH_Strauss_"
}
}
}
EOD;

file_put_contents($this->testsWorkingDir . 'composer.json', $composerJsonString);

chdir($this->testsWorkingDir);

exec('composer install');

$inputInterfaceMock = $this->createMock(InputInterface::class);
$outputInterfaceMock = $this->createMock(OutputInterface::class);

$strauss = new Compose();

$result = $strauss->run($inputInterfaceMock, $outputInterfaceMock);

$this->assertNotEquals(1, $result);
}
}

0 comments on commit 35d0043

Please sign in to comment.