Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesvdvreken committed Sep 18, 2015
2 parents e9bc47f + 846cb4e commit faa9620
Show file tree
Hide file tree
Showing 23 changed files with 1,709 additions and 90 deletions.
38 changes: 18 additions & 20 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
<?php
use Symfony\CS\Config\Config;
use Symfony\CS\Finder\DefaultFinder;
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;
use Symfony\CS\FixerInterface;

$finder = DefaultFinder::create()->in('app');
$finder = DefaultFinder::create()->in(['app', 'tests/unit/Semver']);

return Config::create()
->level(FixerInterface::SYMFONY_LEVEL)
->fixers([
'-yoda_conditions',
'align_double_arrow',
'align_equals',
'ereg_to_preg',
'header_comment',
'multiline_spaces_before_semicolon',
'no_blank_lines_before_namespace',
'ordered_use',
'phpdoc_order',
'phpdoc_var_to_type',
'short_array_syntax',
'strict',
'strict_param',
])
->setUsingCache(true)
->finder($finder);
->level(FixerInterface::SYMFONY_LEVEL)
->fixers([
'ereg_to_preg',
'multiline_spaces_before_semicolon',
'newline_after_open_tag',
'no_blank_lines_before_namespace',
'ordered_use',
'php4_constructor',
'phpdoc_order',
'-phpdoc_params',
'short_array_syntax',
'short_echo_tag',
'strict',
'strict_param',
])
->setUsingCache(true)
->finder($finder);
11 changes: 6 additions & 5 deletions app/Semver/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
class Application
{
/**
* @type ContainerInterface
* @var ContainerInterface
*/
private $container;

/**
* @type string[]
* @var string[]
*/
protected $serviceProviders = [
Http\Support\RoutesServiceProvider::class,
Expand All @@ -25,6 +25,7 @@ class Application
Services\Packagist\ServiceProvider::class,
Services\Views\ServiceProvider::class,
Services\Cache\ServiceProvider::class,
Services\Repositories\ServiceProvider::class,
];

/**
Expand All @@ -42,10 +43,10 @@ public function __construct(ContainerInterface $container)
*/
public function run()
{
/** @type Dispatcher $dispatcher */
/** @var Dispatcher $dispatcher */
/* @type Request $request */
$dispatcher = $this->container->get(RouteCollection::class)->getDispatcher();
$request = $this->container->get(Request::class);
$request = $this->container->get(Request::class);

$response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());

Expand All @@ -58,7 +59,7 @@ public function run()
private function setupProviders()
{
foreach ($this->serviceProviders as &$serviceProvider) {
/** @type ServiceProvider $serviceProvider */
/** @var ServiceProvider $serviceProvider */
$serviceProvider = new $serviceProvider();
$serviceProvider->setContainer($this->container);
}
Expand Down
14 changes: 14 additions & 0 deletions app/Semver/Contracts/Repositories/PackageVersionsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Semver\Contracts\Repositories;

use Packagist\Api\Result\Package\Version;

interface PackageVersionsRepository
{
/**
* @param string $package vendor/package
*
* @return Version[]
*/
public function getVersions($package);
}
2 changes: 1 addition & 1 deletion app/Semver/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class HomeController
{
/**
* @type Engine
* @var Engine
*/
private $views;

Expand Down
12 changes: 6 additions & 6 deletions app/Semver/Http/Controllers/PackageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
class PackageController
{
/**
* @type Request
* @var Request
*/
private $request;

/**
* @type Packagist
* @var Packagist
*/
private $packagist;

Expand All @@ -26,7 +26,7 @@ class PackageController
public function __construct(Packagist $packagist, Request $request)
{
$this->packagist = $packagist;
$this->request = $request;
$this->request = $request;
}

/**
Expand All @@ -39,7 +39,7 @@ public function versions($vendor, $package)
{
$this->packagist->setMinimumStability('dev');

$versions = $this->packagist->getVersions($vendor, $package);
$versions = $this->packagist->getVersions($vendor, $package);
$defaultConstraint = $this->packagist->getDefaultConstraint($vendor, $package);

return new JsonResponse(['default_constraint' => $defaultConstraint, 'versions' => $versions]);
Expand All @@ -56,13 +56,13 @@ public function matchVersions($vendor, $package)
$this->configureMinimumStability();

$constraint = $this->request->get('constraint', '*');
$versions = $this->packagist->getMatchingVersions($vendor, $package, $constraint);
$versions = $this->packagist->getMatchingVersions($vendor, $package, $constraint);

return new JsonResponse($versions);
}

/**
* Configured the minimum stability to be used when fetching versions
* Configured the minimum stability to be used when fetching versions.
*/
protected function configureMinimumStability()
{
Expand Down
2 changes: 1 addition & 1 deletion app/Semver/Http/Support/RequestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class RequestServiceProvider extends ServiceProvider
{
/**
* @type array
* @var array
*/
protected $provides = [
Request::class,
Expand Down
2 changes: 1 addition & 1 deletion app/Semver/Http/Support/RoutesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class RoutesServiceProvider extends ServiceProvider
{
/**
* @type array
* @var array
*/
protected $provides = [
RouteCollection::class,
Expand Down
9 changes: 4 additions & 5 deletions app/Semver/Services/Cache/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
namespace Semver\Services\Cache;

use Illuminate\Cache\FileStore;
use Illuminate\Cache\Repository;
use Illuminate\Cache\Repository as IlluminateCache;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Filesystem\Filesystem;
use League\Container\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
/**
* @type array
* @var array
*/
protected $provides = [
Repository::class,
Expand All @@ -19,15 +20,13 @@ class ServiceProvider extends BaseServiceProvider
* Use the register method to register items with the container via the
* protected $this->container property or the `getContainer` method
* from the ContainerAwareTrait.
*
* @return void
*/
public function register()
{
$this->container->singleton(Repository::class, function () {
$store = new FileStore(new Filesystem(), $this->container->get('paths.cache'));

return new Repository($store);
return new IlluminateCache($store);
});
}
}
2 changes: 1 addition & 1 deletion app/Semver/Services/Error/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class ServiceProvider extends BaseServiceProvider
{
/**
* @type array
* @var array
*/
protected $provides = [
Run::class,
Expand Down
93 changes: 51 additions & 42 deletions app/Semver/Services/Packagist/Packagist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,34 @@
use Composer\Package\Package;
use Composer\Package\Version\VersionParser;
use Composer\Package\Version\VersionSelector;
use Illuminate\Cache\Repository;
use Packagist\Api\Client;
use Packagist\Api\Result\Package\Version;
use Semver\Contracts\Repositories\PackageVersionsRepository;

class Packagist
{
/**
* @type Client
*/
private $client;

/**
* @type VersionParser
* @var VersionParser
*/
private $parser;

/**
* @type string
* @var string
*/
protected $minimumStability = 'stable';

/**
* @type Repository
* @var PackageVersionsRepository
*/
private $cache;
private $versionsRepository;

/**
* @param Client $client
* @param VersionParser $parser
* @param Repository $cache
* @param PackageVersionsRepository $versionsRepository
*/
public function __construct(Client $client, VersionParser $parser, Repository $cache)
public function __construct(VersionParser $parser, PackageVersionsRepository $versionsRepository)
{
$this->client = $client;
$this->parser = $parser;
$this->cache = $cache;
$this->versionsRepository = $versionsRepository;
}

/**
Expand Down Expand Up @@ -71,19 +63,13 @@ public function getVersions($vendor, $package)
{
$versions = $this->getRawVersions($vendor, $package);

foreach ($versions as $key => &$version) {
/* @type Version $version */
$versions[$version->getVersion()] = [
'source' => substr($version->getSource()->getUrl(), 0, -4),
'version' => $version->getVersion(),
];
}
return array_map(function (Version $version) {
// Transform the output to only include fields we need.
$source = substr($version->getSource()->getUrl(), 0, -4); // Strip .git
$version = $this->replaceBranchAlias($version->getVersion(), $version->getExtra());

usort($versions, function ($a, $b) {
return -1 * version_compare($a['version'], $b['version']);
});

return $versions;
return compact('source', 'version');
}, $versions);
}

/**
Expand All @@ -99,14 +85,19 @@ public function getMatchingVersions($vendor, $package, $constraint = '*')

$constraint = $this->parser->parseConstraints($constraint);

return array_keys(array_filter($versions, function (Version $version) use ($constraint) {
$matching = array_filter($versions, function (Version $version) use ($constraint) {
return $constraint->matches(new VersionConstraint('==', $this->parser->normalize($version->getVersion())));
}));
});

return array_values(array_map(function (Version $version) {
return $this->replaceBranchAlias($version->getVersion(), $version->getExtra());
}, $matching));
}

/**
* Given a concrete package, this returns a ~ constraint (in the future a ^ constraint)
* that should be used, for example, in composer.json.
*
* For example:
* * 1.2.1 -> ~1.2
* * 1.2 -> ~1.2
Expand All @@ -121,7 +112,11 @@ public function getMatchingVersions($vendor, $package, $constraint = '*')
public function getDefaultConstraint($vendor, $package)
{
// Get versions.
$versions = array_keys($this->getRawVersions($vendor, $package));
$versions = $this->getRawVersions($vendor, $package);

$versions = array_map(function (Version $version) {
return $version->getVersion();
}, $versions);

// Get highest version.
$highestVersion = reset($versions);
Expand All @@ -141,7 +136,7 @@ public function getDefaultConstraint($vendor, $package)

// Let version selector format the constraint.
$selector = new VersionSelector(new Pool());
$package = new Package("$vendor/$package", $this->parser->normalize($highestVersion), $highestVersion);
$package = new Package("$vendor/$package", $this->parser->normalize($highestVersion), $highestVersion);

return $selector->findRecommendedRequireVersion($package);
}
Expand All @@ -150,20 +145,12 @@ public function getDefaultConstraint($vendor, $package)
* @param string $vendor
* @param string $package
*
* @return array
* @return Version[]
*/
private function getRawVersions($vendor, $package)
{
$handle = "$vendor/$package";
$lifetime = 60;

/* @type Version[] $versions */
$versions = $this->cache->remember($handle, $lifetime, function () use ($handle) {
$package = $this->client->get($handle);
$versions = $package->getVersions();

return $versions;
});
$versions = $this->versionsRepository->getVersions("$vendor/$package");

return array_filter(
$versions,
Expand Down Expand Up @@ -202,4 +189,26 @@ private function isMoreStable($version, $requiredStability)

return BasePackage::$stabilities[$stability] < BasePackage::$stabilities[$requiredStability];
}

/**
* Replace 'dev-*' version names with their branch aliases.
*
* @param string $version
* @param array $extras
*
* @return string
*/
private function replaceBranchAlias($version, array $extras = null)
{
if ($extras && isset($extras['branch-alias'])) {
foreach ($extras['branch-alias'] as $branch => $alias) {
if ($version === $branch) {
$version = $alias;
break;
}
}
}

return $version;
}
}
Loading

0 comments on commit faa9620

Please sign in to comment.