/** * @param string $constraint See {@link https://getcomposer.org/doc/articles/versions.md} * @param bool $transform if true, will transform the simple constraint (v1, 1.2, ...) in a constraint with an interval (^v1, ^1.2, ...) * * @return array ordered from the last release to the first */ public function match($constraint, $transform = true) { if ($transform && preg_match('/^v?[0-9]+(\\.[0-9]+)*$/', $constraint)) { $constraint = '^' . $constraint; } return Semver::satisfiedBy($this->versions, $constraint); }
/** * Matches actual version with preset version. * * @param string $actual * @param array $preset * * @return bool */ private function matchVersion($actual, array $preset) { if (!$actual || !array_key_exists('version', $preset)) { return false; } return Semver::satisfies($actual, $preset['version']); }
/** * Return all versions that satisfy given constraints. * * @param array $versions * @param string $constraints * * @return array */ public static function satisfiedBy(array $versions, $constraints) { $versions = array_filter($versions, function ($version) use($constraints) { return Semver::satisfies($version, $constraints); }); return array_values($versions); }
/** @inheritdoc */ public function satisfiedBy($packageName, $versionContraint) { $versions = Semver::satisfiedBy($packageName, $versionContraint); // Unlike composer, we allow dependant packages to define stability // flags. Why? Because we can have multiple versions of the same // package. So if foo requires bar@dev & baz requires bar@beta this is // totally fine. if (preg_match("/dev/i", $versionContraint)) { return $versions; } if (preg_match("/alpha/i", $versionContraint)) { return $this->filterDev($versions); } if (preg_match("/beta/i", $versionContraint)) { return $this->filterAlpha($versions); } if (preg_match("/rc/i", $versionContraint)) { return $this->filterBeta($versions); } // If the version contraint does not define a stability // then we defer to our "root" minimum-stability setting. switch ($this->minimumStability) { case 'dev': return $versions; case 'alpha': return $this->filterDev($versions); case 'beta': return $this->filterAlpha($versions); case 'rc': return $this->filterBeta($versions); default: return $this->filterRc($versions); } }
private function matchAllVersionsViaSemver($composerVersion) { $matches = []; foreach ($this->tagsReleasesBranches as $ref) { $matching = $this->semver->satisfies($ref, $composerVersion); if ($matching) { $matches[] = $ref; break; } } return !empty($matches) ? $matches : false; }
public function testCheckRequire() { $rootJson = $this->getJsonFromFile($this->rootComposerJson); $rootRequires = array_merge($rootJson['require'], $rootJson['require-dev'], $rootJson['replace']); foreach (array_merge($this->jsonFiles) as $jsonFile) { $subJson = $this->getJsonFromFile($jsonFile); if (isset($subJson['require'])) { foreach ($subJson['require'] as $subRequireName => $subRequireVersion) { static::assertArrayHasKey($subRequireName, $rootRequires); // root require contains all of sub require package if ($rootRequires[$subRequireName] === 'self.version') { static::assertTrue(Semver::satisfies(Application::VERSION, $subRequireVersion)); // self.version version check } else { static::assertEquals($rootRequires[$subRequireName], $subRequireVersion); } } } } }
/** * @return array */ public static function getCoverageFileProvider() { $version = 'CodeCoverage >4.0'; $filenames = array('coverage-tests/runner_test.cov', 'coverage-tests/result_printer_test.cov'); $coverageClass = 'SebastianBergmann\\CodeCoverage\\CodeCoverage'; if (class_exists('PHP_CodeCoverage')) { $version = 'Legacy CodeCoverage'; $filenames = array('coverage-tests/runner_test.cov4', 'coverage-tests/result_printer_test.cov4'); $coverageClass = 'PHP_CodeCoverage'; if (Semver::satisfies(static::getPhpUnitVersion(), '3.7.*')) { $version = 'PHPUnit 3.7'; $filenames = array('coverage-tests/runner_test.cov3', 'coverage-tests/result_printer_test.cov3'); } } return array($version => array('filenames' => $filenames, 'expected coverage class' => $coverageClass)); }
public function processNotifications($notifications) { // Sort by date usort($notifications, function ($a, $b) { return strcmp($a->date, $b->date); }); $notifications = array_reverse($notifications); // Make adminNicetimeFilter available require_once __DIR__ . '/../twig/AdminTwigExtension.php'; $adminTwigExtension = new AdminTwigExtension(); $filename = $this->grav['locator']->findResource('user://data/notifications/' . $this->grav['user']->username . YAML_EXT, true, true); $read_notifications = CompiledYamlFile::instance($filename)->content(); $notifications_processed = []; foreach ($notifications as $key => $notification) { $is_valid = true; if (in_array($notification->id, $read_notifications)) { $notification->read = true; } if ($is_valid && isset($notification->permissions) && !$this->authorize($notification->permissions)) { $is_valid = false; } if ($is_valid && isset($notification->dependencies)) { foreach ($notification->dependencies as $dependency => $constraints) { if ($dependency == 'grav') { if (!Semver::satisfies(GRAV_VERSION, $constraints)) { $is_valid = false; } } else { $packages = array_merge($this->plugins()->toArray(), $this->themes()->toArray()); if (!isset($packages[$dependency])) { $is_valid = false; } else { $version = $packages[$dependency]['version']; if (!Semver::satisfies($version, $constraints)) { $is_valid = false; } } } if (!$is_valid) { break; } } } if ($is_valid) { $notifications_processed[] = $notification; } } // Process notifications $notifications_processed = array_map(function ($notification) use($adminTwigExtension) { $notification->date = $adminTwigExtension->adminNicetimeFilter($notification->date); return $notification; }, $notifications_processed); return $notifications_processed; }
protected function getVersionStyle(PackageInterface $latestPackage, PackageInterface $package) { if ($latestPackage->getFullPrettyVersion() === $package->getFullPrettyVersion()) { // print green as it's up to date return 'info'; } $constraint = $package->getVersion(); if (0 !== strpos($constraint, 'dev-')) { $constraint = '^' . $constraint; } if ($latestPackage->getVersion() && Semver::satisfies($latestPackage->getVersion(), $constraint)) { // print red as it needs an immediate semver-compliant upgrade return 'highlight'; } // print yellow as it needs an upgrade but has potential BC breaks so is not urgent return 'comment'; }
/** * Compare two version strings to get the named semantic version. * * @access public * * @param string $new_version * @param string $original_version * @return string $name 'major', 'minor', 'patch' */ function get_named_sem_ver($new_version, $original_version) { if (!Comparator::greaterThan($new_version, $original_version)) { return ''; } $parts = explode('-', $original_version); $bits = explode('.', $parts[0]); $major = $bits[0]; if (isset($bits[1])) { $minor = $bits[1]; } if (isset($bits[2])) { $patch = $bits[2]; } if (!is_null($minor) && Semver::satisfies($new_version, "{$major}.{$minor}.x")) { return 'patch'; } else { if (Semver::satisfies($new_version, "{$major}.x.x")) { return 'minor'; } else { return 'major'; } } }
/** * Check if the extension is valid for loading, i.e has a class and is withing version constraints. * * @param Composer $composer * @param string|null $class * @param string|null $constraint * * @return bool */ private static function parseValid(Composer $composer, $class, $constraint) { if ($constraint === null) { return false; } $provides = $composer->getPackage()->getProvides(); $boltVersion = isset($provides['bolt/bolt']) ? $provides['bolt/bolt'] : new Link('__root__', 'bolt/bolt', new Constraint('=', '0.0.0')); return $class && Semver::satisfies($boltVersion->getPrettyConstraint(), $constraint); }
* you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ require_once __DIR__ . '/vendor/autoload.php'; use Composer\Semver\Semver; // Available versions in the order we want to check. $available_versions = [getenv('PHP70_VERSION'), getenv('PHP56_VERSION')]; if (count($argv) < 2) { die("Usage:\n" . $argv[0] . " filename\n"); } $composer = json_decode(file_get_contents($argv[1]), true); $php_version = ''; if (is_array($composer) && array_key_exists('require', $composer) && array_key_exists('php', $composer['require'])) { $constraints = $composer['require']['php']; foreach ($available_versions as $version) { if (Semver::satisfies($version, $constraints)) { // The first match wins, picking the highest version possible. $php_version = substr($version, 0, strrpos($version, '.')); break; } } } echo $php_version;
$metapackage_repository->run('config', ['user.email', '*****@*****.**']); $branches = array_filter($repository->getReferences()->getRemoteBranches(), function (Branch $branch) { if ($branch->isRemote() && preg_match('/^origin\\/8\\./', $branch->getName(), $matches)) { return TRUE; } return FALSE; }); $tags = array_filter($repository->getReferences()->getTags(), function (Tag $tag) { return preg_match('/^8\\.[0-9]+\\.[0-9]+/', $tag->getName()); }); $refs = $tags + $branches; $refs_array = []; foreach ($refs as $ref) { $name = str_replace('origin/', '', $ref->getName()); if ($ref instanceof Branch) { $name .= '-dev'; } $refs_array[$name] = $ref; } $sorted = \Composer\Semver\Semver::sort(array_keys($refs_array)); foreach ($sorted as $version) { /** @var Gitonomy\Git\Reference\Tag|Gitonomy\Git\Reference\Branch $ref */ $ref = $refs_array[$version]; $repository->run('reset', ['--hard', $ref->getCommitHash()]); $path = $repository->getPath() . '/composer.lock'; if (file_exists($path)) { $packageBuilder = PackageBuilder::fromLockfile($path); $dump = new Dumper($ref, $packageBuilder->buildPackage(), $metapackage_repository); $dump->write(); } }
/** * @return array */ public static function getReporterProvider() { $version = 'CodeCoverage >4.0'; $filenames = array('coverage-tests/runner_test.cov', 'coverage-tests/result_printer_test.cov'); $reporterClass = 'ParaTest\\Coverage\\CoverageReporter'; if (class_exists('\\PHP_CodeCoverage')) { $version = 'Legacy CodeCoverage'; $filenames = array('coverage-tests/runner_test.cov4', 'coverage-tests/result_printer_test.cov4'); $reporterClass = 'ParaTest\\Coverage\\CoverageReporterLegacy'; if (Semver::satisfies(static::getPhpUnitVersion(), '3.7.*')) { $version = 'PHPUnit 3.7'; $filenames = array('coverage-tests/runner_test.cov3', 'coverage-tests/result_printer_test.cov3'); } } return array($version => array('filenames' => $filenames, 'expected reporter class' => $reporterClass)); }
/** * Selects the upgrade functions applicable for this upgrade. * * The upgrade functions are specified by the `upgradeList` * hook. This variable is an associative array containing version numbers * as keys and an array of upgrade function names as values. This function * merges all the upgrade function names of the version between the current * installed version and the upgraded version. * * @param string $version the version of SimpleID to upgrade from, calls * {@link getVersion()} if not specified * @return array an array of strings, containing the list of upgrade functions * to call. The functions should be called in the same order as they appear * in this array * @see SimpleID\API\ModuleHooks::upgradeListHook() */ protected function getUpgradeList($version = NULL) { $mgr = ModuleManager::instance(); $upgrade_data = array(); foreach ($mgr->getModules() as $name => $module) { $data = $mgr->invoke($name, 'upgradeList'); if ($data != NULL) { $upgrade_data = array_merge_recursive($upgrade_data, $data); } } if ($version == NULL) { $version = $this->getVersion(); } $list = array(); // Sorts versions from newest to oldest $versions = array_keys($upgrade_data); $versions = Semver::rsort($versions); foreach ($versions as $upgrade_version) { if (Comparator::lessThan($version, $upgrade_version)) { $list = array_merge($list, $upgrade_data[$upgrade_version]); } } if (Comparator::lessThan($version, SIMPLEID_VERSION)) { $list[] = 'SimpleID\\Upgrade->setVersion'; } return $list; }
/** * Compare two version strings to get the named semantic version. * * @access public * * @param string $new_version * @param string $original_version * @return string $name 'major', 'minor', 'patch' */ function get_named_sem_ver($new_version, $original_version) { if (!Comparator::greaterThan($new_version, $original_version)) { return ''; } $parts = explode('-', $original_version); list($major, $minor, $patch) = explode('.', $parts[0]); if (Semver::satisfies($new_version, "{$major}.{$minor}.x")) { return 'patch'; } else { if (Semver::satisfies($new_version, "{$major}.x.x")) { return 'minor'; } else { return 'major'; } } }
/** * Resolves a Wordpress Version Contraint. * * This is a private helper method. It takes a semantic version contraint, * parsable by [Composer's Semver](https://github.com/composer/semver) and * resolves an actual wordpress version number. * * We use this page: http://wordpress.org/download/release-archive/ * As an offical list of released versions. * * @param string $versionContraint A semantic version contraint. * @return string A semantic version number. */ private function wpResolveVersionNo($versionContraint) { // Remove a v at the start if it exists $versionContraint = str_replace('v', '', $versionContraint); // If the constraint it a single wildcard, lets just // return the latest stable release of wordpress. if ($versionContraint == '*') { $json = (new Http())->request('GET', self::$WP_VERSION_URL)->getBody(); return json_decode($json, true)['offers'][0]['version']; } // Download the releases from the wordpress site. $html = (new Http())->request('GET', self::$WP_RELEASES_URL)->getBody(); // Extract a list of download links, these contain the versions. preg_match_all("#><a href='https://wordpress\\.org/wordpress-[^>]+#", $html, $matches); // Filter the links to obtain a list of just versions $versions = Linq::from($matches[0])->select(function ($v) { return s::create($v); })->where(function ($v) { return $v->endsWith(".zip'"); })->where(function ($v) { return !$v->contains('IIS'); })->where(function ($v) { return !$v->contains('mu'); })->select(function ($v) { return $v->between('wordpress-', '.zip'); })->where(function ($v) { if ($v->contains('-')) { return preg_match("#.*-(dev|beta|alpha|rc).*#i", $v) === 1; } return true; })->toArray(); // Let semver take over and work it's magic return (string) Semver::satisfiedBy($versions, $versionContraint)[0]; }