1
0
Fork 0

Read vendor directory from project's composer.json, if set.

If not set, default to "vendor".
Also sort found vendor directories so we obtain the correct
composer.json and composer.lock files
pull/281/head
Trevor Bortins 2017-02-05 17:55:59 -08:00
parent 5100d89617
commit f43e0dc34a
2 changed files with 31 additions and 3 deletions

View File

@ -59,6 +59,11 @@ class Indexer
*/ */
private $composerLock; private $composerLock;
/**
* @var \stdClasss
*/
private $composerJson;
/** /**
* @param FilesFinder $filesFinder * @param FilesFinder $filesFinder
* @param string $rootPath * @param string $rootPath
@ -77,7 +82,8 @@ class Indexer
DependenciesIndex $dependenciesIndex, DependenciesIndex $dependenciesIndex,
Index $sourceIndex, Index $sourceIndex,
PhpDocumentLoader $documentLoader, PhpDocumentLoader $documentLoader,
\stdClass $composerLock = null \stdClass $composerLock = null,
\stdClass $composerJson = null
) { ) {
$this->filesFinder = $filesFinder; $this->filesFinder = $filesFinder;
$this->rootPath = $rootPath; $this->rootPath = $rootPath;
@ -87,6 +93,7 @@ class Indexer
$this->sourceIndex = $sourceIndex; $this->sourceIndex = $sourceIndex;
$this->documentLoader = $documentLoader; $this->documentLoader = $documentLoader;
$this->composerLock = $composerLock; $this->composerLock = $composerLock;
$this->composerJson = $composerJson;
} }
/** /**
@ -109,8 +116,12 @@ class Indexer
$source = []; $source = [];
/** @var string[][] */ /** @var string[][] */
$deps = []; $deps = [];
$vendorDir = str_replace('/', '\/', @$this->composerJson->config->{'vendor-dir'} ?: 'vendor');
$this->client->window->logMessage(MessageType::INFO, "Vendor dir: $vendorDir");
foreach ($uris as $uri) { foreach ($uris as $uri) {
if ($this->composerLock !== null && preg_match('/\/vendor\/([^\/]+\/[^\/]+)\//', $uri, $matches)) { if ($this->composerLock !== null && preg_match("/\/$vendorDir\/([^\/]+\/[^\/]+)\//", $uri, $matches)) {
// Dependency file // Dependency file
$packageName = $matches[1]; $packageName = $matches[1];
if (!isset($deps[$packageName])) { if (!isset($deps[$packageName])) {
@ -174,6 +185,8 @@ class Indexer
if ($cacheKey !== null) { if ($cacheKey !== null) {
$this->client->window->logMessage(MessageType::INFO, "Storing $packageKey in cache"); $this->client->window->logMessage(MessageType::INFO, "Storing $packageKey in cache");
$this->cache->set($cacheKey, $index); $this->cache->set($cacheKey, $index);
} else {
$this->client->window->logMessage(MessageType::INFO, "Cannot cache $packageName, cache key was null. Either you are using a 'dev-' version or your composer.lock is missing references.");
} }
} }
} }

View File

@ -206,6 +206,13 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
// Find composer.json // Find composer.json
if ($this->composerJson === null) { if ($this->composerJson === null) {
$composerJsonFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.json', $rootPath)); $composerJsonFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.json', $rootPath));
// If we sort our findings by string length (shortest to longest),
// the first entry will be the project's root composer.json.
usort($composerJsonFiles, function ($a, $b) {
return strlen($a) - strlen($b);
});
if (!empty($composerJsonFiles)) { if (!empty($composerJsonFiles)) {
$this->composerJson = json_decode(yield $this->contentRetriever->retrieve($composerJsonFiles[0])); $this->composerJson = json_decode(yield $this->contentRetriever->retrieve($composerJsonFiles[0]));
} }
@ -214,6 +221,13 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
// Find composer.lock // Find composer.lock
if ($this->composerLock === null) { if ($this->composerLock === null) {
$composerLockFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.lock', $rootPath)); $composerLockFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.lock', $rootPath));
// If we sort our findings by string length (shortest to longest),
// the first entry will be the project's root composer.lock.
usort($composerLockFiles, function ($a, $b) {
return strlen($a) - strlen($b);
});
if (!empty($composerLockFiles)) { if (!empty($composerLockFiles)) {
$this->composerLock = json_decode(yield $this->contentRetriever->retrieve($composerLockFiles[0])); $this->composerLock = json_decode(yield $this->contentRetriever->retrieve($composerLockFiles[0]));
} }
@ -230,7 +244,8 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
$dependenciesIndex, $dependenciesIndex,
$sourceIndex, $sourceIndex,
$this->documentLoader, $this->documentLoader,
$this->composerLock $this->composerLock,
$this->composerJson
); );
$indexer->index()->otherwise('\\LanguageServer\\crash'); $indexer->index()->otherwise('\\LanguageServer\\crash');
} }