Skip to content

Commit 7ba8a8a

Browse files
authored
Merge pull request #200 from BrianHenryIE/fix-vendor-autoloader
Fix vendor autoloader dev entries when target is vendor
2 parents 0c5e48c + dd3af8d commit 7ba8a8a

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"brianhenryie/simple-php-code-parser": "^0.15.2",
2020
"composer-runtime-api": "^2.0",
2121
"composer/class-map-generator": "^1.6.0",
22-
"composer/composer": "*",
22+
"composer/composer": "^2.6.0",
2323
"elazar/flystream": "^0.5.0|^1",
2424
"json-mapper/json-mapper": ">=2.0.0 <=2.22.3",
2525
"league/flysystem": "^2.1|^3.0",

src/Pipeline/Autoload/DumpAutoload.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ protected function generatedMainAutoloader(): void
111111
// $generator->setApcu($apcu, $apcuPrefix);
112112
// $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input));
113113
$optimize = true; // $input->getOption('optimize') || $config->get('optimize-autoloader');
114-
$generator->setDevMode(false);
114+
115+
/**
116+
* If the target directory is different to the vendor directory, then we do not want to include dev
117+
* dependencies, but if it is vendor, then unless composer install was run with --no-dev, we do want them.
118+
*/
119+
if ($this->config->getVendorDirectory() !== $this->config->getTargetDirectory()) {
120+
$generator->setDevMode(false);
121+
}
115122

116123
$localRepo = new InstalledFilesystemRepository(new JsonFile($this->config->getTargetDirectory() . 'composer/installed.json'));
117124

@@ -179,6 +186,10 @@ protected function createInstalledVersionsFiles(): void
179186

180187
protected function prefixNewAutoloader(): void
181188
{
189+
if ($this->config->getVendorDirectory() === $this->config->getTargetDirectory()) {
190+
return;
191+
}
192+
182193
$this->logger->debug('Prefixing the new Composer autoloader.');
183194

184195
$projectFiles = $this->fileEnumerator->compileFileListForPaths([
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* @see https://github.com/BrianHenryIE/strauss/pull/200
4+
*/
5+
6+
namespace BrianHenryIE\Strauss\Tests\Issues;
7+
8+
use BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase;
9+
10+
/**
11+
* @package BrianHenryIE\Strauss\Tests\Issues
12+
* @coversNothing
13+
*/
14+
class StraussIssue200Test extends IntegrationTestCase
15+
{
16+
public function test_does_not_remove_vendor_autoload_dev_entries()
17+
{
18+
19+
$composerJsonString = <<<'EOD'
20+
{
21+
"require": {
22+
"psr/log": "*"
23+
},
24+
"require-dev": {
25+
"psr/simple-cache": "*"
26+
},
27+
"extra": {
28+
"strauss": {
29+
"target_directory": "vendor",
30+
"namespace_prefix": "Company\\Project\\"
31+
}
32+
}
33+
}
34+
EOD;
35+
36+
chdir($this->testsWorkingDir);
37+
38+
file_put_contents($this->testsWorkingDir . '/composer.json', $composerJsonString);
39+
40+
exec('composer install');
41+
$exitCode = $this->runStrauss($output);
42+
assert(0 === $exitCode, $output);
43+
44+
$php_string = file_get_contents($this->testsWorkingDir . 'vendor/composer/installed.json');
45+
$this->assertStringContainsString("Company\\\\Project\\\\Psr\\\\Log\\\\", $php_string);
46+
$this->assertStringContainsString("\"Psr\\\\SimpleCache\\\\", $php_string);
47+
48+
$php_string = file_get_contents($this->testsWorkingDir . 'vendor/composer/autoload_psr4.php');
49+
$this->assertStringContainsString("Company\\\\Project\\\\Psr\\\\Log\\\\", $php_string);
50+
$this->assertStringContainsString("Psr\\\\SimpleCache\\\\", $php_string);
51+
}
52+
}

tests/Unit/Pipeline/Autoload/DumpAutoloadTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public function test_create_installed_versions_files(): void
8989
$logger = new NullLogger();
9090

9191
$config->expects('isDryRun')->times(1)->andReturn(true);
92-
$config->expects('getVendorDirectory')->times(3)->andReturn('mem://project/vendor');
93-
$config->expects('getTargetDirectory')->times(4)->andReturn('mem://project/vendor-prefixed');
92+
$config->expects('getVendorDirectory')->times(4)->andReturn('mem://project/vendor');
93+
$config->expects('getTargetDirectory')->times(5)->andReturn('mem://project/vendor-prefixed');
9494

9595
$installedVersions = <<<EOD
9696
<?php // a core Composer file that is not unique per install.

tests/Unit/Pipeline/FileSymbolScannerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public function test_it_parses_classes_after_semicolon()
479479
{
480480

481481
$contents = "
482-
myvar = 123; class Pear { };
482+
\$myvar = 123; class Pear { };
483483
";
484484

485485
$filesystemReaderMock = Mockery::mock(Filesystem::class);

0 commit comments

Comments
 (0)