diff --git a/src/GitDriver.php b/src/GitDriver.php index eb83037..113accc 100644 --- a/src/GitDriver.php +++ b/src/GitDriver.php @@ -2,6 +2,7 @@ namespace Drupal\ParseComposer; +use Composer\Downloader\TransportException; use Composer\Repository\Vcs\GitDriver as BaseDriver; use Composer\Package\Version\VersionParser; use Drupal\ParseComposer\DrupalOrg\DistUrl; @@ -19,35 +20,60 @@ class GitDriver extends BaseDriver implements FileFinderInterface */ private $versionFactory = false; + /** + * @var string identifier + */ + private $identifier = ''; + + /** + * @var string drupalProjectName + */ + private $drupalProjectName = ''; + + /** + * @var bool isCore + */ + private $isCore = false; + + /** + * @var string[] paths + */ + private $paths; + /** * {@inheritDoc} */ public function getComposerInformation($identifier) { $this->identifier = $identifier; - try { - $composer = parent::getComposerInformation($identifier); - } catch (TransportException $e) { - // There is not composer.json file in the root - } - $composer = is_array($composer) ? $composer : array(); $ref = strlen($this->identifier) == 40 ? $this->lookUpRef($this->identifier) : $this->identifier; if ($version = $this->getVersion($ref)) { $core = $version->getCore(); - $majorSlug = $this->isCore ? '' : "{$version->getMajor()}.x"; - $devBranch = "dev-$core.x-$majorSlug"; - $composer['extra']['branch-alias'][$devBranch] = $core.'.' - .($majorSlug ?: 'x').'-dev'; + if ($core < 7) { + return []; + } } else { return []; } - // TODO: make configurable? - if ($core < 7) { - return []; + + try { + $composer = parent::getComposerInformation($identifier); + } catch (TransportException $e) { + // There is not composer.json file in the root + } + $composer = is_array($composer) ? $composer : array(); + foreach (array('replace', 'require', 'suggest') as $link) { + $composer[$link] = isset($composer[$link]) + ? $composer[$link] + : array(); } + $majorSlug = $this->isCore ? '' : "{$version->getMajor()}.x"; + $devBranch = "dev-$core.x-$majorSlug"; + $composer['extra']['branch-alias'][$devBranch] = $core.'.' + .($majorSlug ?: 'x').'-dev'; $project = new Project($this->drupalProjectName, $this, $core); if (null != ($drupalInformation = $project->getDrupalInformation())) { if (isset($drupalInformation[$this->drupalProjectName])) { @@ -55,11 +81,6 @@ public function getComposerInformation($identifier) } else { $topInformation = current($drupalInformation); } - foreach (array('replace', 'require', 'suggest') as $link) { - $composer[$link] = isset($composer[$link]) - ? $composer[$link] - : array(); - } foreach (array_keys($drupalInformation) as $name) { if ($name != $this->drupalProjectName) { $composer['replace']["drupal/$name"] = 'self.version'; @@ -104,7 +125,6 @@ public function getComposerInformation($identifier) unset($composer['suggest'][$composer['name']]); unset($composer['suggest']['drupal/drupal']); } - return $composer; } @@ -181,9 +201,11 @@ public function getBranches() if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch) && preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+) .*$}', $branch, $match) - && $this->getVersion($name = @end(explode('/', $match[1]))) ) { - $branches[$name] = $match[2]; + $parts = explode('/', $match[1]); + if ($this->getVersion($name = end($parts))) { + $branches[$name] = $match[2]; + } } } $this->branches = $branches; diff --git a/src/Project.php b/src/Project.php index 4927df7..e14ca23 100644 --- a/src/Project.php +++ b/src/Project.php @@ -8,10 +8,10 @@ class Project { - private $makeFiles = []; - private $infoFiles = []; - private $isTheme; - private $hasDrush; + private $makeFiles = []; + private $infoFiles = []; + private $isTheme = false; + private $hasDrush = false; /** * @param string $name @@ -43,85 +43,91 @@ public function getName() /** * Get composer information for Drupal project. * - * @return array|void + * @return array|null */ public function getDrupalInformation() { $projectMap = $composerMap = $make = array(); - $this->hasDrush = $this->hasModule = false; - $this->finder->pathMatch( - function($path) { - if (strpos($path, 'test') !== false) { - return false; - } - $parts = explode('.', basename($path)); - if (end($parts) === 'info' - || array_slice($parts, -2) == ['info', 'yml'] - ) { - $this->infoFiles[] = $path; - - return true; - } elseif (end($parts) === 'make') { - $this->makeFiles[] = $path; - - return true; - } elseif (end($parts) === 'module' - ) { - $this->hasModule = true; - } elseif (basename($path) === 'template.php') { - $this->isTheme = true; - } elseif (array_slice($parts, -2) == ['drush', 'inc'] - ) { - $this->hasDrush = true; - } - } - ); - foreach ($this->infoFiles as $infoPath) { - $info = new InfoFile( - basename($infoPath), - $this->finder->fileContents($infoPath), - $this->core - ); - $projectMap[$info->getProjectName()] = $info; - } - foreach ($this->makeFiles as $makePath) { - $make[] = new Makefile( - $this->finder->fileContents($makePath) - ); - } - if (empty($projectMap) && !$this->hasDrush) { + $this->finder->pathMatch($this->getPathMatcher()); + if (empty($this->projectMap) && !$this->hasDrush) { return; } if ('drupal' === $this->name) { $projectMap['drupal'] = clone($projectMap['system']); } - foreach ($projectMap as $name => $info) { - $composerMap[$name] = $info->packageInfo(); - foreach ($make as $makefile) { - foreach (($makefile->getDrupalProjects()) as $name => $project) { - $composerMap[$this->name]['require']['drupal/'.$name] = $makefile->getConstraint($name); - } - } + foreach ($this->projectMap as $name => $info) { + $composerMap[$name] = $info; } $top = isset($composerMap[$this->name]) ? $this->name : current(array_keys($composerMap)); + foreach ($this->makeFiles as $makefile) { + foreach (($makefile->getDrupalProjects()) as $name => $project) { + $composerMap[$top]['require']['drupal/'.$name] = $makefile->getConstraint($name); + } + } + if (!isset($composerMap[$top]['name'])) { + $composerMap[$top]['name'] = $this->getName(); + } + $composerMap[$top]['type'] = $this->getProjectType($composerMap[$top]); + if ($composerMap[$top]['type'] === 'drupal-drush') { + $composerMap[$top]['require']['drush/drush'] = '>=6'; + } + + return $composerMap; + } + + /** + * @return a function for use with FileFinderInterface::pathMatch() + */ + private function getPathMatcher() + { + return function($path) { + if (strpos($path, 'test') !== false) { + return false; + } + $parts = explode('.', basename($path)); + if (end($parts) === 'info' + || array_slice($parts, -2) == ['info', 'yml'] + ) { + $info = new InfoFile( + basename($path), + $this->finder->fileContents($path), + $this->core + ); + $this->projectMap[$info->getProjectName()] = $info->packageInfo(); + } elseif (end($parts) === 'make') { + $this->makeFiles[] = new Makefile( + $this->finder->fileContents($path) + ); + } elseif (end($parts) === 'module') { + $this->hasModule = true; + } elseif (basename($path) === 'template.php') { + $this->isTheme = true; + } elseif (array_slice($parts, -2) == ['drush', 'inc'] + ) { + $this->hasDrush = true; + } + }; + } + + /** + * @return the type for the composer project + */ + private function getProjectType() + { if ('drupal' === $this->name) { - $composerMap[$top]['type'] = 'drupal-core'; + return 'drupal-core'; } elseif ($releaseInfo = $this->getReleaseInfo($this->core)) { - $composerMap[$top]['type'] = $releaseInfo->getProjectType(); - if ($composerMap[$top]['type'] === 'drupal-module' + $type = $releaseInfo->getProjectType(); + if ($type === 'drupal-module' && !$this->hasModule && !$this->isTheme && $this->hasDrush ) { - if (!isset($composerMap[$top]['name'])) { - $composerMap[$top]['name'] = $this->getName(); - } - $composerMap[$top]['type'] = 'drupal-drush'; - $composerMap[$top]['require']['drush/drush'] = '>=6'; + $type = 'drupal-drush'; } - } - return $composerMap; + return $type; + } } /**