Skip to content

Commit

Permalink
Override config of VCS Repository of asset registry by root config
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Sep 17, 2014
1 parent 6a0b3f0 commit b116949
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 45 deletions.
2 changes: 2 additions & 0 deletions FxpAssetPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ protected function addRepositories(RepositoryManager $rm, array $repositories)
throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a url defined');
}
$name = is_int($index) ? preg_replace('{^https?://}i', '', $repo['url']) : $index;
$name = isset($repo['name']) ? $repo['name'] : $name;

Util::addRepository($rm, $this->repos, $name, $repo);
}
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This allows you to manage asset dependencies in a PHP based project very easily.
- Conversion of [dependencies with URL](Resources/doc/schema.md#url-range-verison-conversion) to the composer dependencies with the creation of VCS repositories
- Conversion of [multiple versions of the same dependency](Resources/doc/schema.md#multiple-version-of-depdendency-in-the-same-project) to different dependencies of composer
- Add manually the [multiple versions of a same dependency in the project](Resources/doc/index.md#usage-with-multiple-version-of-a-same-dependency)
- Override the config of VCS Repository defined by the asset registry directly in extra section of root composer
- VCS drivers for:
- Git
- GitHub
Expand Down
93 changes: 50 additions & 43 deletions Repository/AbstractAssetsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Composer\Repository\ComposerRepository;
use Composer\Repository\RepositoryManager;
use Fxp\Composer\AssetPlugin\Assets;
use Fxp\Composer\AssetPlugin\Converter\SemverUtil;
use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;

/**
Expand Down Expand Up @@ -55,6 +54,11 @@ abstract class AbstractAssetsRepository extends ComposerRepository
*/
protected $fallbackProviders;

/**
* @var RepositoryManager
*/
protected $repositoryManager;

/**
* Constructor.
*
Expand All @@ -68,6 +72,7 @@ public function __construct(array $repoConfig, IOInterface $io, Config $config,
$repoConfig = array_merge($repoConfig, array(
'url' => $this->getUrl(),
));
$this->repositoryManager = $repoConfig['repository-manager'];

parent::__construct($repoConfig, $io, $config, $eventDispatcher);

Expand All @@ -91,7 +96,7 @@ public function search($query, $mode = 0)
return array();
}

$url = str_replace('%query%', urlencode($this->cleanPackageName($query)), $this->searchUrl);
$url = str_replace('%query%', urlencode(Util::cleanPackageName($query)), $this->searchUrl);
$hostname = (string) parse_url($url, PHP_URL_HOST) ?: $url;
$json = (string) $this->rfs->getContents($hostname, $url, false);
$data = JsonFile::parseJson($json, $url);
Expand All @@ -110,23 +115,17 @@ public function search($query, $mode = 0)
*/
public function whatProvides(Pool $pool, $name)
{
$assetPrefix = $this->assetType->getComposerVendorName() . '/';

if (false === strpos($name, $assetPrefix)) {
return array();
}

if (isset($this->providers[$name])) {
return $this->providers[$name];
if (null !== $provides = $this->findWhatProvides($name)) {
return $provides;
}

try {
$repoName = $this->convertAliasName($name);
$packageName = $this->cleanPackageName($repoName);
$repoName = Util::convertAliasName($name);
$packageName = Util::cleanPackageName($repoName);
$packageUrl = str_replace('%package%', $packageName, $this->lazyProvidersUrl);
$cacheName = $packageName . '-' . sha1($packageName) . '-package.json';
$data = $this->fetchFile($packageUrl, $cacheName);
$repo = $this->createVcsRepositoryConfig($data, $this->cleanPackageName($name));
$repo = $this->createVcsRepositoryConfig($data, Util::cleanPackageName($name));

Util::addRepository($this->rm, $this->repos, $name, $repo, $pool);

Expand All @@ -148,53 +147,61 @@ public function getMinimalPackages()
}

/**
* {@inheritDoc}
*/
protected function loadRootServerFile()
{
return array(
'providers' => array(),
);
}

/**
* Cleans the package name, removing the Composer prefix if present.
* Finds what provides in cache or return empty array if the
* name is not a asset package.
*
* @param string $name
*
* @return string
* @return array|null
*/
protected function cleanPackageName($name)
protected function findWhatProvides($name)
{
$prefix = $this->assetType->getComposerVendorName() . '/';
$assetPrefix = $this->assetType->getComposerVendorName() . '/';

if (0 === strpos($name, $prefix)) {
$name = substr($name, strlen($prefix));
if (false === strpos($name, $assetPrefix)) {
return array();
}

if (isset($this->providers[$name])) {
return $this->providers[$name];
}

if ($this->hasVcsRepository($name)) {
$this->providers[$name] = array();

return $this->providers[$name];
}

return $name;
return null;
}

/**
* Converts the alias of asset package name by the real asset package name.
* Checks if the package vcs repository is already include in repository manager.
*
* @param string $name
* @param string $name The package name of the vcs repository
*
* @return string
* @return bool
*/
protected function convertAliasName($name)
protected function hasVcsRepository($name)
{
$pos = strrpos($name, '-');

if (false !== $pos) {
$version = substr($name, $pos + 1);

if (preg_match(SemverUtil::createPattern(''), $version)) {
return substr($name, 0, $pos);
foreach ($this->repositoryManager->getRepositories() as $mRepo) {
if ($mRepo instanceof AssetVcsRepository
&& $name === $mRepo->getComposerPackageName()) {
return true;
}
}

return $name;
return false;
}

/**
* {@inheritDoc}
*/
protected function loadRootServerFile()
{
return array(
'providers' => array(),
);
}

/**
Expand Down Expand Up @@ -244,7 +251,7 @@ protected function fallbackWathProvides(Pool $pool, $name, TransportException $e

if (404 === $ex->getCode() && !$this->fallbackProviders) {
$this->fallbackProviders = true;
$repoName = $this->convertAliasName($name);
$repoName = Util::convertAliasName($name);
$results = $this->search($repoName);

foreach ($results as $item) {
Expand Down
16 changes: 15 additions & 1 deletion Repository/AssetVcsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,27 @@ public function __construct(array $repoConfig, IOInterface $io, Config $config,
parent::__construct($repoConfig, $io, $config, $dispatcher, $drivers);
}

/**
* Gets the package name of this repository.
*
* @return string
*/
public function getComposerPackageName()
{
if (null === $this->packages) {
$this->initialize();
}

return $this->assetType->formatComposerName($this->packageName);
}

/**
* {@inheritdoc}
*/
protected function initialize()
{
$this->packages = array();
$this->packageName = isset($this->repoConfig['name']) ? $this->repoConfig['name'] : null;
$this->packageName = isset($this->repoConfig['name']) ? Util::cleanPackageName($this->repoConfig['name']) : null;
$driver = $this->initDriver();

$this->initLoader();
Expand Down
39 changes: 39 additions & 0 deletions Repository/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Composer\DependencyResolver\Pool;
use Composer\Repository\RepositoryManager;
use Fxp\Composer\AssetPlugin\Converter\SemverUtil;

/**
* Helper for Repository.
Expand Down Expand Up @@ -40,4 +41,42 @@ public static function addRepository(RepositoryManager $rm, array &$repos, $name
}
}
}

/**
* Cleans the package name, removing the Composer prefix if present.
*
* @param string $name
*
* @return string
*/
public static function cleanPackageName($name)
{
if (preg_match('/^[a-z]+\-asset\//', $name, $matches)) {
$name = substr($name, strlen($matches[0]));
}

return $name;
}

/**
* Converts the alias of asset package name by the real asset package name.
*
* @param string $name
*
* @return string
*/
public static function convertAliasName($name)
{
$pos = strrpos($name, '-');

if (false !== $pos) {
$version = substr($name, $pos + 1);

if (preg_match(SemverUtil::createPattern(''), $version)) {
return substr($name, 0, $pos);
}
}

return $name;
}
}
37 changes: 37 additions & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,43 @@ and/or Bower.
}
```

### Override the config of VCS Repository

If you must use a repository other than that indicated by the registry of NPM or Bower,
you must specify the name of the package with the asset prefix in the config of the VCS
Repository.

**Example:**

```json
{
"extra": {
"asset-repositories": [
{
"type": "bower-vcs",
"url": "https://github.com/vendor/exemple-asset-name.git",
"name": "bower-asset/exemple-asset-name"
}
]
}
}
```

You can also use the standard format of Composer for naming your VCS Repository:

```json
{
"extra": {
"asset-repositories": {
"bower-asset/exemple-asset-name": {
"type": "bower-vcs",
"url": "https://github.com/vendor/exemple-asset-name.git"
}
}
}
}
```

### Usage with multiple version of a same dependency

If you need to use multiple version of the same asset, You can do this by
Expand Down
21 changes: 21 additions & 0 deletions Tests/Repository/AbstractAssetsRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,25 @@ public function testSearchWithSearchDisabled()

$this->assertCount(0, $this->registry->search('query'));
}

public function testOverridingVcsRepositoryConfig()
{
$name = $this->getType() . '-asset/foobar';
$rfs = $this->replaceRegistryRfsByMock();
$rfs->expects($this->any())
->method('getContents')
->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));

$repo = $this->getMockBuilder('Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository')
->disableOriginalConstructor()
->getMock();

$repo->expects($this->any())
->method('getComposerPackageName')
->will($this->returnValue($name));

$this->rm->addRepository($repo);

$this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
}
}
13 changes: 13 additions & 0 deletions Tests/Repository/AssetVcsRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ public function getMockDriversWithVersions()
);
}

/**
* @dataProvider getMockDriversWithVersions
*/
public function testRepositoryPackageName($type, $url, $class, $verbose)
{
$packageName = 'asset-package-name';
$valid = str_replace('-mock', '-asset', $type) . '/' . $packageName;

$this->init(true, $type, $url, $class, $verbose, null, $packageName);

$this->assertEquals($valid, $this->repository->getComposerPackageName());
}

/**
* @dataProvider getMockDriversWithVersions
*/
Expand Down
1 change: 0 additions & 1 deletion Tests/Repository/Vcs/GitDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function getAssetTypes()

/**
* @dataProvider getAssetTypes
* @group bug
*/
public function testPublicRepositoryWithEmptyComposer($type, $filename)
{
Expand Down

0 comments on commit b116949

Please sign in to comment.