Skip to content

Commit

Permalink
Merge pull request #50 from edgardmessias/refactored
Browse files Browse the repository at this point in the history
Refactored RegistryFactory
  • Loading branch information
hiqsol committed May 18, 2017
2 parents 1f0abde + dc47aff commit b76fbcd
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 225 deletions.
42 changes: 42 additions & 0 deletions src/components/PackageUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Asset Packagist.
*
* @link https://github.com/hiqdev/asset-packagist
* @package asset-packagist
* @license BSD-3-Clause
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\assetpackagist\components;

use Composer\Semver\Comparator;
use Composer\Semver\VersionParser;

class PackageUtil
{
public static function sort(&$releases)
{
uasort($releases, function ($a, $b) {
if ($a['version'] === $b['version']) {
return 0;
}

$stability_a = VersionParser::parseStability($a['version_normalized']);
$stability_b = VersionParser::parseStability($b['version_normalized']);

// DEV versions to LAST
if ($stability_a === 'dev' && $stability_b !== 'dev') {
return 1;
} elseif ($stability_a !== 'dev' && $stability_b === 'dev') {
return -1;
}

if (Comparator::lessThan($a['version_normalized'], $b['version_normalized'])) {
return 1;
}

return -1;
});
}
}
3 changes: 3 additions & 0 deletions src/config/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
'packageStorage' => [
'class' => \hiqdev\assetpackagist\components\Storage::class,
],
'registryFactory' => [
'class' => hiqdev\assetpackagist\registry\RegistryFactory::class,
],
],
'container' => [
'singletons' => [
Expand Down
88 changes: 12 additions & 76 deletions src/models/AssetPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@

namespace hiqdev\assetpackagist\models;

use Composer\Composer;
use Composer\Factory;
use Composer\Package\Link;
use Exception;
use Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository;
use hiqdev\assetpackagist\components\Storage;
use hiqdev\assetpackagist\log\YiiLogIO;
use hiqdev\assetpackagist\registry\BowerRegistry;
use hiqdev\assetpackagist\registry\NpmRegistry;
use hiqdev\assetpackagist\registry\RegistryFactory;
use Yii;
use yii\base\Object;
Expand All @@ -33,29 +27,12 @@ class AssetPackage extends Object
*/
protected $_releases = [];
protected $_saved;
/**
* @var AssetVcsRepository|BowerRegistry|NpmRegistry
*/
protected $_registry;

/**
* @var integer UNIX Epoch timestamp of the latest package update
*/
protected $_updateTime;

/**
* @var YiiLogIO
*/
protected $_io;
/**
* @var Composer
*/
protected $_composer;
/**
* @var Composer
*/
protected static $_commonComposer;

public static function normalizeName($name)
{
return strtolower($name);
Expand All @@ -82,13 +59,12 @@ public function __construct($type, $name, $config = [])
$this->_name = $name;
}

/**
* @return RegistryFactory
*/
public function getRegistry()
{
if ($this->_registry === null) {
$this->_registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager());
}

return $this->_registry;
return Yii::$app->get('registryFactory');
}

public function checkType($type)
Expand Down Expand Up @@ -149,49 +125,6 @@ public function getHash()
return $this->_hash;
}

/**
* @return Composer
*/
public static function getCommonComposer()
{
if (static::$_commonComposer === null) {
static::$_commonComposer = (new Factory())->createComposer(
new YiiLogIO(),
Yii::getAlias('@composer/composer.json'),
false,
Yii::getAlias('@composer')
);
}

return static::$_commonComposer;
}

public function setComposer($value)
{
$this->_composer = $value;
}

/**
* @return Composer
*/
public function getComposer()
{
if ($this->_composer === null) {
$this->_composer = static::getCommonComposer();
}

return $this->_composer;
}

public function getIO()
{
if ($this->_io === null) {
$this->_io = new YiiLogIO();
}

return $this->_io;
}

/**
* findOne.
*
Expand Down Expand Up @@ -219,21 +152,21 @@ public function load()

public function update()
{
$repo = $this->getRegistry()->buildVcsRepository($this->getName());
$this->_releases = $this->prepareReleases($repo);
$pool = $this->getRegistry()->getPool();
$this->_releases = $this->prepareReleases($pool);
$this->getStorage()->writePackage($this);
$this->load();
}

/**
* @param AssetVcsRepository $repository
* @param \Composer\DependencyResolver\Pool $pool
* @return array
*/
public function prepareReleases($repository)
public function prepareReleases($pool)
{
$releases = [];

foreach ($repository->getPackages() as $package) {
foreach ($pool->whatProvides($this->getFullName()) as $package) {
if ($package instanceof \Composer\Package\AliasPackage) {
continue;
}
Expand Down Expand Up @@ -269,6 +202,9 @@ public function prepareReleases($repository)
}
}

//Sort before save
\hiqdev\assetpackagist\components\PackageUtil::sort($releases);

return $releases;
}

Expand Down
18 changes: 0 additions & 18 deletions src/registry/BowerRegistry.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/registry/NpmRegistry.php

This file was deleted.

Loading

0 comments on commit b76fbcd

Please sign in to comment.