2015-08-18 13:40:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Repository;
|
|
|
|
|
2015-08-18 14:08:27 +00:00
|
|
|
use Composer\Config;
|
|
|
|
use Composer\IO\IOInterface;
|
2015-08-18 13:40:48 +00:00
|
|
|
use Composer\Json\JsonFile;
|
|
|
|
use Composer\Package\Loader\ArrayLoader;
|
2015-11-14 13:30:37 +00:00
|
|
|
use Composer\Package\Locker;
|
2015-08-18 14:08:27 +00:00
|
|
|
use Composer\Package\Version\VersionGuesser;
|
2015-09-24 14:32:36 +00:00
|
|
|
use Composer\Semver\VersionParser;
|
2015-08-18 14:08:27 +00:00
|
|
|
use Composer\Util\ProcessExecutor;
|
2015-08-18 13:40:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This repository allows installing local packages that are not necessarily under their own VCS.
|
|
|
|
*
|
|
|
|
* The local packages will be symlinked when possible, else they will be copied.
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* "require": {
|
|
|
|
* "<vendor>/<local-package>": "*"
|
|
|
|
* },
|
|
|
|
* "repositories": [
|
|
|
|
* {
|
|
|
|
* "type": "path",
|
|
|
|
* "url": "../../relative/path/to/package/"
|
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* "type": "path",
|
|
|
|
* "url": "/absolute/path/to/package/"
|
2015-09-15 15:39:55 +00:00
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* "type": "path",
|
|
|
|
* "url": "/absolute/path/to/several/packages/*"
|
2015-08-18 13:40:48 +00:00
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* @author Samuel Roze <samuel.roze@gmail.com>
|
|
|
|
* @author Johann Reinke <johann.reinke@gmail.com>
|
|
|
|
*/
|
2015-11-23 22:18:24 +00:00
|
|
|
class PathRepository extends ArrayRepository implements ConfigurableRepositoryInterface
|
2015-08-18 13:40:48 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ArrayLoader
|
|
|
|
*/
|
|
|
|
private $loader;
|
|
|
|
|
2015-08-18 14:08:27 +00:00
|
|
|
/**
|
|
|
|
* @var VersionGuesser
|
|
|
|
*/
|
|
|
|
private $versionGuesser;
|
|
|
|
|
|
|
|
/**
|
2015-09-12 21:28:03 +00:00
|
|
|
* @var string
|
2015-08-18 14:08:27 +00:00
|
|
|
*/
|
2015-09-14 08:58:07 +00:00
|
|
|
private $url;
|
2015-08-18 14:08:27 +00:00
|
|
|
|
2015-11-23 22:18:24 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $repoConfig;
|
|
|
|
|
2015-08-18 13:40:48 +00:00
|
|
|
/**
|
2015-09-12 21:28:03 +00:00
|
|
|
* @var ProcessExecutor
|
2015-08-18 13:40:48 +00:00
|
|
|
*/
|
2015-09-12 21:28:03 +00:00
|
|
|
private $process;
|
2015-08-18 13:40:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes path repository.
|
|
|
|
*
|
2015-09-28 09:51:14 +00:00
|
|
|
* @param array $repoConfig
|
2015-08-18 14:08:27 +00:00
|
|
|
* @param IOInterface $io
|
2015-09-28 09:51:14 +00:00
|
|
|
* @param Config $config
|
2015-08-18 13:40:48 +00:00
|
|
|
*/
|
2015-09-12 21:28:03 +00:00
|
|
|
public function __construct(array $repoConfig, IOInterface $io, Config $config)
|
2015-08-18 13:40:48 +00:00
|
|
|
{
|
2015-09-12 21:28:03 +00:00
|
|
|
if (!isset($repoConfig['url'])) {
|
2015-08-18 13:40:48 +00:00
|
|
|
throw new \RuntimeException('You must specify the `url` configuration for the path repository');
|
|
|
|
}
|
|
|
|
|
2015-09-12 21:28:03 +00:00
|
|
|
$this->loader = new ArrayLoader();
|
2015-09-14 08:58:07 +00:00
|
|
|
$this->url = $repoConfig['url'];
|
2015-09-12 21:28:03 +00:00
|
|
|
$this->process = new ProcessExecutor($io);
|
|
|
|
$this->versionGuesser = new VersionGuesser($config, $this->process, new VersionParser());
|
2015-11-23 22:18:24 +00:00
|
|
|
$this->repoConfig = $repoConfig;
|
2015-09-12 21:28:03 +00:00
|
|
|
|
|
|
|
parent::__construct();
|
2015-08-18 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 22:18:24 +00:00
|
|
|
public function getRepoConfig()
|
|
|
|
{
|
|
|
|
return $this->repoConfig;
|
|
|
|
}
|
|
|
|
|
2015-08-18 13:40:48 +00:00
|
|
|
/**
|
|
|
|
* Initializes path repository.
|
|
|
|
*
|
|
|
|
* This method will basically read the folder and add the found package.
|
|
|
|
*/
|
|
|
|
protected function initialize()
|
|
|
|
{
|
|
|
|
parent::initialize();
|
|
|
|
|
2015-09-23 15:51:41 +00:00
|
|
|
foreach ($this->getUrlMatches() as $url) {
|
2016-01-25 23:33:47 +00:00
|
|
|
$path = realpath($url) . DIRECTORY_SEPARATOR;
|
2015-09-15 14:41:07 +00:00
|
|
|
$composerFilePath = $path.'composer.json';
|
2015-09-28 09:51:14 +00:00
|
|
|
|
2015-09-15 14:41:07 +00:00
|
|
|
if (!file_exists($composerFilePath)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = file_get_contents($composerFilePath);
|
|
|
|
$package = JsonFile::parseJson($json, $composerFilePath);
|
|
|
|
$package['dist'] = array(
|
|
|
|
'type' => 'path',
|
2015-09-23 15:51:41 +00:00
|
|
|
'url' => $url,
|
2015-09-15 14:41:07 +00:00
|
|
|
'reference' => '',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset($package['version'])) {
|
|
|
|
$package['version'] = $this->versionGuesser->guessVersion($package, $path) ?: 'dev-master';
|
|
|
|
}
|
2016-01-25 23:33:47 +00:00
|
|
|
$output = '';
|
|
|
|
if (is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H', $output, $path)) {
|
2015-09-15 14:41:07 +00:00
|
|
|
$package['dist']['reference'] = trim($output);
|
2015-10-18 09:57:43 +00:00
|
|
|
} else {
|
2015-11-14 13:30:37 +00:00
|
|
|
$package['dist']['reference'] = Locker::getContentHash($json);
|
2015-09-15 14:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$package = $this->loader->load($package);
|
|
|
|
$this->addPackage($package);
|
2015-08-18 13:40:48 +00:00
|
|
|
}
|
2015-09-15 16:28:17 +00:00
|
|
|
|
2015-09-18 15:55:07 +00:00
|
|
|
if (count($this->getPackages()) == 0) {
|
2015-09-15 16:28:17 +00:00
|
|
|
throw new \RuntimeException(sprintf('No `composer.json` file found in any path repository in "%s"', $this->url));
|
|
|
|
}
|
2015-08-18 13:40:48 +00:00
|
|
|
}
|
2015-09-14 08:58:07 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-23 15:51:41 +00:00
|
|
|
* Get a list of all (possibly relative) path names matching given url (supports globbing).
|
2015-09-15 14:41:07 +00:00
|
|
|
*
|
|
|
|
* @return string[]
|
2015-09-14 08:58:07 +00:00
|
|
|
*/
|
2015-09-23 15:51:41 +00:00
|
|
|
private function getUrlMatches()
|
2015-09-14 08:58:07 +00:00
|
|
|
{
|
2016-01-25 23:33:47 +00:00
|
|
|
// Ensure environment-specific path separators are normalized to URL separators
|
|
|
|
return array_map(function($val) {
|
|
|
|
return str_replace(DIRECTORY_SEPARATOR, '/', $val);
|
|
|
|
}, glob($this->url, GLOB_MARK | GLOB_ONLYDIR));
|
2015-09-14 08:58:07 +00:00
|
|
|
}
|
2015-08-18 13:40:48 +00:00
|
|
|
}
|