Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup build matrix and adjust to semantic branch naming #19

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/static.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Static analysis
on:
push:
branches:
- '[0-9]+.x'
- '[0-9]+.[0-9]+'
- '[0-9]+.[0-9]+.x'
pull_request:

jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: PHPStan
uses: docker://oskarstark/phpstan-ga
env:
REQUIRE_DEV: true
with:
args: analyze --no-progress

php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --dry-run
12 changes: 8 additions & 4 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ on:
pull_request:
push:
branches:
- 'master'
- '[0-9]+.x'
- '[0-9]+.[0-9]+'
- '[0-9]+.[0-9]+.x'

jobs:
test:
name: 'PHP ${{ matrix.php-version }} ${{ matrix.dependencies }}'
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
env:
SYMFONY_DEPRECATIONS_HELPER: weak

Expand All @@ -23,10 +25,12 @@ jobs:
- php-version: '7.4'
- php-version: '8.0'
- php-version: '8.1'
- php-version: '8.2'
- php-version: '8.3'

steps:
- name: Checkout project
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
Expand All @@ -35,7 +39,7 @@ jobs:
tools: 'composer:v2'

- name: Install dependencies with Composer
uses: ramsey/composer-install@v1
uses: ramsey/composer-install@v3
with:
dependency-versions: ${{ matrix.dependencies }}
composer-options: --prefer-dist
Expand Down
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
composer.lock
vendor
tests/data
tests/migrations
.phpunit.result.cache
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.lock
/phpunit.xml
/tests/data
/tests/migrations
/vendor
12 changes: 12 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__);
$config = new PhpCsFixer\Config();

return $config->setFinder($finder)
->setRules([
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
]);
29 changes: 0 additions & 29 deletions .php_cs

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PHPCR Migrations
================

[![Build
Status](https://travis-ci.org/phpcr/phpcr-migrations.svg?branch=master)](https://travis-ci.org/phpcr/phpcr-migrations)
Status](https://github.com/phpcr/phpcr-migrations/actions/workflows/test-application.yaml/badge.svg)](https://github.com/phpcr/phpcr-migrations/actions/workflows/test-application.yaml)

Migrations library for PHPCR influenced by [Doctrine
migrations](https://github.com/doctrine/migrations).
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"require-dev": {
"jackalope/jackalope-fs": "0.0.*",
"handcraftedinthealps/zendsearch": "^2.0",
"phpunit/phpunit": "^8.5 || ^9.4"
"phpunit/phpunit": "^8.5 || ^9.4",
"phpspec/prophecy": "^1.19"
},
"autoload": {
"psr-4": {
Expand All @@ -28,8 +29,5 @@
"psr-4": {
"PHPCR\\Migrations\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {"dev-master": "1.x-dev" }
}
}
37 changes: 15 additions & 22 deletions lib/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class Migrator
private $session;
private $versionCollection;
private $versionStorage;
private $actions = array(
private $actions = [
'up', 'down', 'top', 'bottom',
);
];

public function __construct(
SessionInterface $session,
Expand Down Expand Up @@ -57,15 +57,14 @@ public function initialize()
* If $to is 0 then all migrations will be reverted.
* If $to is null then all migrations will be executed.
*
* @param string|null $to Version to run until
* @param OutputInterface $output
* @param string|int|bool|null $to Version to run until
*
* @return VersionInterface[] Executed migrations
*/
public function migrate($to, OutputInterface $output)
{
if (false === $to) {
return array();
return [];
}

$from = $this->versionStorage->getCurrentVersion();
Expand All @@ -76,17 +75,17 @@ public function migrate($to, OutputInterface $output)
$versionsToExecute = $this->versionCollection->getVersions($from, $to, $direction);

if (!$versionsToExecute) {
return array();
return [];
}

$position = 0;
$output->writeln(sprintf('<comment>%s</comment> %d version(s):', ($direction == 'up' ? 'Upgrading' : 'Reverting'), count($versionsToExecute)));
$output->writeln(sprintf('<comment>%s</comment> %d version(s):', 'up' == $direction ? 'Upgrading' : 'Reverting', count($versionsToExecute)));
foreach ($versionsToExecute as $timestamp => $version) {
$position++;
$output->writeln(sprintf(' %s [<info>%d/%d</info>]: %s', $direction == 'up' ? '+' : '-', $position, count($versionsToExecute), $timestamp));
++$position;
$output->writeln(sprintf(' %s [<info>%d/%d</info>]: %s', 'up' == $direction ? '+' : '-', $position, count($versionsToExecute), $timestamp));
$version->$direction($this->session);

if ($direction === 'down') {
if ('down' === $direction) {
$this->versionStorage->remove($timestamp);
} else {
$this->versionStorage->add($timestamp);
Expand Down Expand Up @@ -117,34 +116,28 @@ private function resolveTo($to, $from)
$to = strtolower($to);
}

if ($to === 'down') {
if ('down' === $to) {
$to = $this->versionCollection->getPreviousVersion($from);
}

if ($to === 'up') {
if ('up' === $to) {
$to = $this->versionCollection->getNextVersion($from);
}

if ($to === 'bottom') {
if ('bottom' === $to) {
$to = 0;
}

if ($to === 'top' || null === $to) {
if ('top' === $to || null === $to) {
$to = $this->versionCollection->getLatestVersion();
}

if (0 !== $to && false === strtotime($to)) {
throw new MigratorException(sprintf(
'Unknown migration action "%s". Known actions: "%s"',
$to,
implode('", "', $this->actions)
));
throw new MigratorException(sprintf('Unknown migration action "%s". Known actions: "%s"', $to, implode('", "', $this->actions)));
}

if (0 !== $to && !$this->versionCollection->has($to)) {
throw new MigratorException(sprintf(
'Unknown version "%s"', $to
));
throw new MigratorException(sprintf('Unknown version "%s"', $to));
}

return $to;
Expand Down
31 changes: 16 additions & 15 deletions lib/MigratorUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MigratorUtil
*/
public static function getClassNameFromFile($file)
{
$fp = fopen($file, 'r');
$fp = fopen($file, 'rb');

$class = $namespace = $buffer = '';
$i = 0;
Expand All @@ -35,40 +35,41 @@ public static function getClassNameFromFile($file)
}

// Read entire lines to prevent keyword truncation
for ($line = 0; $line <= 20; $line++) {
for ($line = 0; $line <= 20; ++$line) {
$buffer .= fgets($fp);
}
$tokens = @token_get_all($buffer);
$tokensCount = count($tokens);

if (strpos($buffer, '{') === false) {
if (false === strpos($buffer, '{')) {
continue;
}

for (;$i < count($tokens);$i++) {
if ($tokens[$i][0] === T_NAMESPACE) {
for ($j = $i + 1;$j < count($tokens); $j++) {
if (\defined('T_NAME_QUALIFIED') && $tokens[$j][0] === T_NAME_QUALIFIED || $tokens[$j][0] === T_STRING) {
$namespace .= '\\' . $tokens[$j][1];
} elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
for (; $i < $tokensCount; ++$i) {
if (T_NAMESPACE === $tokens[$i][0]) {
for ($j = $i + 1; $j < $tokensCount; ++$j) {
if (T_STRING === $tokens[$j][0] || (\defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokens[$j][0])) {
$namespace .= '\\'.$tokens[$j][1];
} elseif ('{' === $tokens[$j] || ';' === $tokens[$j]) {
break;
}
}
}

if ($tokens[$i][0] === T_CLASS) {
for ($j = $i + 1;$j < count($tokens);$j++) {
if ($tokens[$j] === '{') {
if (T_CLASS === $tokens[$i][0]) {
for ($j = $i + 1; $j < $tokensCount; ++$j) {
if ('{' === $tokens[$j]) {
$class = $tokens[$i + 2][1];
}
}
}
}
};
}

if (!$class) {
return;
throw new \RuntimeException('Could not determine class for migration');
}

return $namespace . '\\' . $class;
return $namespace.'\\'.$class;
}
}
23 changes: 8 additions & 15 deletions lib/VersionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ public function getAllVersions()
public function getVersions($from, $to)
{
$direction = $from > $to ? 'down' : 'up';
$result = array();
$result = [];
$versions = $this->versions;

if ($from == $to) {
return array();
return [];
}

if ($direction === 'up') {
if ('up' === $direction) {
ksort($versions, SORT_STRING);
} else {
krsort($versions, SORT_STRING);
}

$found = $from === null ? true : false;
$found = null === $from ? true : false;
foreach ($versions as $timestamp => $version) {
if ($timestamp == $from) {
$found = true;

if ($direction == 'down') {
if ('down' == $direction) {
$result[$timestamp] = $version;
}

Expand All @@ -64,7 +64,7 @@ public function getVersions($from, $to)
}

if ($timestamp == $to) {
if ($direction == 'up') {
if ('up' == $direction) {
$result[$timestamp] = $version;
}
break;
Expand All @@ -86,13 +86,13 @@ public function getLatestVersion()
/**
* Return the version after the given version.
*
* @param string $from
* @param string|null $from
*/
public function getNextVersion($from)
{
$found = false;
foreach (array_keys($this->versions) as $timestamp) {
if ($from === null) {
if (null === $from) {
return $timestamp;
}

Expand All @@ -105,8 +105,6 @@ public function getNextVersion($from)
return $timestamp;
}
}

return;
}

/**
Expand All @@ -126,9 +124,4 @@ public function getPreviousVersion($from)

return 0;
}

private function normalizeTs($ts)
{
return $ts ? $ts : null;
}
}
Loading