Skip to content

Commit

Permalink
Add Support for composer's config.vendor-dir setting
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Jun 13, 2023
1 parent 39eb04e commit 177e95d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Release 1.28.0
* Add support for composer's `vendor-dir` setting when building autoloader based on composer.json and custom vendor directory location

## Release 1.27.2
* Fix/Avoid deprecation notices in PHP 8.2

Expand Down
2 changes: 1 addition & 1 deletion composer/bin/phpab
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ foreach ($files as $file) {
require __DIR__ . '/../../src/autoload.php';

$factory = new \TheSeer\Autoload\Factory();
$factory->getCLI()->run();
$factory->getCLI()->run($_SERVER);
exit(0);
2 changes: 1 addition & 1 deletion phpab.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
require __DIR__ . '/src/autoload.php';

$factory = new \TheSeer\Autoload\Factory();
$factory->getCLI()->run();
$factory->getCLI()->run($_SERVER);
exit(0);
14 changes: 11 additions & 3 deletions src/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(Factory $factory) {
*
* @return void
*/
public function run() {
public function run(array $env) {

try {

Expand All @@ -93,7 +93,7 @@ public function run() {
exit(CLI::RC_OK);
}

$config = $this->configure($input);
$config = $this->configure($env, $input);
$this->factory->setConfig($config);
if (!$config->isQuietMode()) {
$this->showVersion();
Expand Down Expand Up @@ -140,7 +140,7 @@ public function run() {
*
* @return \TheSeer\Autoload\Config
*/
private function configure(\ezcConsoleInput $input) {
private function configure(array $env, \ezcConsoleInput $input) {
$config = new Config($input->getArguments());
if ($input->getOption('quiet')->value) {
$config->setQuietMode(TRUE);
Expand Down Expand Up @@ -262,6 +262,14 @@ private function configure(\ezcConsoleInput $input) {
$config->setTrusting(FALSE);
}

if (isset($env['HOME'])) {
$config->setHomeDirectory($env['HOME']);
} elseif (isset($env['HOMEDRIVE']) && isset($env['HOMEPATH'])) {
$config->setHomeDirectory($env['HOMEDRIVE'] . $env['HOMEPATH']);
} else {
$config->setHomeDirectory('/');
}

return $config;
}

Expand Down
18 changes: 14 additions & 4 deletions src/ComposerIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ComposerIterator implements \Iterator {

private $pos = 0;

public function __construct(\SplFileInfo $composerFile) {
public function __construct(\SplFileInfo $composerFile, $homeDirectory) {
if (!$composerFile->isFile() || !$composerFile->isReadable()) {
throw new ComposerIteratorException(
sprintf('Composer file "%s" not found or not readable', $composerFile->getPathname()),
Expand All @@ -21,12 +21,22 @@ public function __construct(\SplFileInfo $composerFile) {
}
$composerDir = dirname($composerFile->getRealPath());
$composerData = json_decode(file_get_contents($composerFile->getRealPath()), true);

$vendorDir = $composerDir . '/vendor';
if (isset($composerData['config']['vendor-dir'])) {
$vendorDir = preg_replace(
'/($HOME|~)/',
$homeDirectory,
$composerData['config']['vendor-dir']
);
}

if (isset($composerData['require'])) {
foreach($composerData['require'] as $require => $version) {
if ($require === 'php' || strpos($require, 'ext-') === 0) {
continue;
}
$this->processRequire($composerDir, $require);
$this->processRequire($vendorDir, $require);
}
}
if (isset($composerData['autoload'])) {
Expand Down Expand Up @@ -66,7 +76,8 @@ private function processRequire($basedir, $require) {
}
$this->seen[$require] = true;

$requireDir = $basedir . '/vendor/' . $require;
$requireDir = $basedir . '/' . $require;

$jsonFile = $this->findComposerJson($requireDir);
if ($jsonFile === null) {
return;
Expand Down Expand Up @@ -168,7 +179,6 @@ public function valid() {
public function rewind() {
$this->pos = 0;
}

}

class ComposerIteratorException extends \Exception {
Expand Down
12 changes: 11 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Config {
private $prepend = false;
private $exceptions = true;

private $homeDirectory = '';

public function __construct(Array $directories) {
$this->directories = $directories;
$this->php = (PHP_OS === 'WIN' ? 'C:\php\php.exe' : '/usr/bin/php');
Expand All @@ -82,6 +84,14 @@ public function setBaseDirectory($baseDirectory) {
$this->baseDirectory = $baseDirectory;
}

public function setHomeDirectory($homeDir) {
$this->homeDirectory = $homeDir;
}

public function getHomeDirectory(): string {
return $this->homeDirectory;
}

public function getBaseDirectory() {
if ($this->baseDirectory !== NULL) {
return realpath($this->baseDirectory);
Expand Down Expand Up @@ -391,7 +401,7 @@ public function getDirectories() {
$list = array();
foreach($this->directories as $dir) {
if (is_file($dir) && basename($dir) == 'composer.json') {
foreach(new ComposerIterator(new \SplFileInfo($dir)) as $d) {
foreach(new ComposerIterator(new \SplFileInfo($dir), $this->getHomeDirectory()) as $d) {
$list[] = $d;
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/ComposerIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ComposerIteratorTest extends TestCase {

public function testRecursionIsHandledProperly() {
$iterator = new ComposerIterator(new \SplFileInfo(__DIR__ . '/_data/recursion/composer.json'));
$iterator = new ComposerIterator(new \SplFileInfo(__DIR__ . '/_data/recursion/composer.json'), array());
$expected = array(
__DIR__ . '/_data/recursion/vendor/foo/bar',
__DIR__ . '/_data/recursion/vendor/bar/foo'
Expand All @@ -17,7 +17,7 @@ public function testRecursionIsHandledProperly() {
}

public function testPSR14ArrayIsSupported() {
$iterator = new ComposerIterator(new \SplFileInfo(__DIR__ . '/_data/composer-array-issue-98/composer.json'));
$iterator = new ComposerIterator(new \SplFileInfo(__DIR__ . '/_data/composer-array-issue-98/composer.json'), array());
$expected = array(
__DIR__ . '/_data/composer-array-issue-98/../src',
__DIR__ . '/_data/composer-array-issue-98/modules',
Expand Down

0 comments on commit 177e95d

Please sign in to comment.