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

Slip stream in composer 2.0 #18

Merged
merged 20 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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
2 changes: 0 additions & 2 deletions .coveralls.yml

This file was deleted.

1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/.coveralls.yml export-ignore
/.gitattributes export-ignore
/.github/ export-ignore
/.gitignore export-ignore
Expand Down
52 changes: 32 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,64 @@ cache:

env:
global:
- COMPOSER_VERSION="1"
- COMPAT=false
- TEST=false
- CS_CHECK=false
- COMPOSER_ARGS="--no-interaction"
- COVERAGE_DEPS="php-coveralls/php-coveralls"
- NO_TEST_DEPS="phpunit/phpunit webimpress/coding-standard"
- TEST_DEPS="phpunit/phpunit:^9.1 phpspec/prophecy-phpunit webimpress/coding-standard:^1.0"

matrix:
include:
- php: 5.6
- php: 5.6
env:
- COMPAT_VERSION=5.6
- NO_TEST=1
- COMPOSER_VERSION=2
- php: 7.0
- php: 7.0
env:
- COMPAT_VERSION=7.0
- NO_TEST=1
- COMPOSER_VERSION=2
- php: 7.1
- php: 7.1
env:
- COMPAT_VERSION=7.1
- NO_TEST=1
- COMPOSER_VERSION=2
- php: 7.2
- php: 7.2
env:
- TEST_COVERAGE=true
- COMPAT_VERSION=7.2
- COMPOSER_VERSION=2
- php: 7.3
env:
- COMPAT_VERSION=7.3
- CS_CHECK=1
- COMPOSER_VERSION=2
- php: 7.3
env:
- TEST=true
- CS_CHECK=true
- php: 7.4
env:
- COMPAT_VERSION=7.4
- COMPOSER_VERSION=2
- TEST_COVERAGE=true
- php: 7.4
env:
- TEST_COVERAGE=true
- COMPAT=true

before_install:
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi

install:
- if [[ $NO_TEST == '1' ]]; then travis_retry composer remove --dev $COMPOSER_ARGS $NO_TEST_DEPS ; fi
- travis_retry composer install $COMPOSER_ARGS
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS $COVERAGE_DEPS ; fi
- if [[ $COMPOSER_VERSION == "1" ]]; then travis_retry composer update composer/* --prefer-lowest ; fi
- if [[ $TEST == 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS $TEST_DEPS ; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS $TEST_DEPS ; fi
- stty cols 120 && composer show

script:
- if [[ $TEST_COVERAGE == 'true' ]]; then composer test-coverage ; else if [[ $NO_TEST != '1' ]]; then composer test ; fi ; fi
- if [[ $COMPAT_VERSION != '0' ]]; then composer cs-check -- -p src --standard=PHPCompatibility --runtime-set testVersion $COMPAT_VERSION ; fi
- if [[ $CS_CHECK == '1' ]]; then composer cs-check ; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then composer test-coverage ; else if [[ $TEST != 'false' ]]; then composer test ; fi ; fi
- if [[ "$COMPAT" == 'true' ]]; then composer compat ; fi
- if [[ $CS_CHECK == 'true' ]]; then composer cs-check ; fi

after_script:
- if [[ $TEST_COVERAGE == 'true' ]]; then vendor/bin/php-coveralls -v ; fi
after_success:
- bash <(curl -s https://codecov.io/bash)

notifications:
email: false
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![Build Status](https://travis-ci.com/laminas/laminas-dependency-plugin.svg?branch=master)](https://travis-ci.com/laminas/laminas-dependency-plugin)

[![codecov](https://codecov.io/gh/laminas/laminas-dependency-plugin/branch/master/graph/badge.svg)](https://codecov.io/gh/laminas/laminas-dependency-plugin)

This Composer plugin, when enabled in a project, intercepts requests to install
packages from the zendframework and zfcampus vendors, and will replace them with
the equivalents from the Laminas Project.
Expand Down
7 changes: 7 additions & 0 deletions autoload/composer-2.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Composer\Plugin;

if (!class_exists(PrePoolCreateEvent::class)) {
class PrePoolCreateEvent {}
}
22 changes: 13 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
},
"require": {
"php": "^5.6 || ^7.0",
"composer-plugin-api": "^1.1"
"composer-plugin-api": "^1.1 || ^2.0"
},
"require-dev": {
"composer/composer": "^1.9",
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"composer/composer": "^1.9 || ^2.0@dev",
"composer/semver": "^1.5 || ^2.1@dev",
boesing marked this conversation as resolved.
Show resolved Hide resolved
"mikey179/vfsstream": "^1.6",
"phpcompatibility/php-compatibility": "^9.3",
"phpunit/phpunit": "^8.4",
"roave/security-advisories": "dev-master",
"webimpress/coding-standard": "^1.0"
"roave/security-advisories": "dev-master"
},
"autoload": {
"psr-4": {
Expand All @@ -26,23 +25,28 @@
"autoload-dev": {
"psr-4": {
"LaminasTest\\DependencyPlugin\\": "test/"
}
},
"files": ["autoload/composer-2.0.php"]
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev",
"dev-develop": "1.1.x-dev"
},
"class": "Laminas\\DependencyPlugin\\DependencyRewriterPlugin"
"class": "Laminas\\DependencyPlugin\\DependencyRewriterPluginDelegator"
},
"scripts": {
"check": [
"@cs-check",
"@compat",
"@test"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"post-install-cmd": "phpcs --config-set installed_paths vendor/phpcompatibility/php-compatibility",
"post-update-cmd" : "phpcs --config-set installed_paths vendor/phpcompatibility/php-compatibility",
"compat": "phpcs --standard=PHPCompatibility src/ -- --runtime-set testVersion 5.6-"
}
}
12 changes: 8 additions & 4 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?xml version="1.0"?>
<ruleset name="Webimpress Coding Standard">
<rule ref="./vendor/webimpress/coding-standard/ruleset.xml"/>
<rule ref="./vendor/webimpress/coding-standard/ruleset.xml">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably move to Laminas Coding Standard v2, but that can wait for another PR. 😄

<exclude name="WebimpressCodingStandard.Functions.NullableTypehint"/>
</rule>

<!-- Paths to check -->
<file>src</file>
<file>test</file>

<exclude-pattern>test/*/TestAsset/</exclude-pattern>

<rule ref="WebimpressCodingStandard.PHP.DeclareStrictTypes">
<exclude-pattern>src/DependencyRewriterPlugin.php</exclude-pattern>
<exclude-pattern>src/*</exclude-pattern>
</rule>

<rule ref="Generic.NamingConventions.CamelCapsFunctionName">
<exclude-pattern>test/*</exclude-pattern>
</rule>
</ruleset>
156 changes: 156 additions & 0 deletions src/AbstractDependencyRewriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

/**
* @see https://github.com/laminas/laminas-dependency-plugin for the canonical source repository
* @copyright https://github.com/laminas/laminas-dependency-plugin/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-dependency-plugin/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\DependencyPlugin;

use Composer\Composer;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
use Composer\Plugin\PreCommandRunEvent;

use function array_map;
use function array_shift;
use function get_class;
use function in_array;
use function preg_split;
use function reset;
use function sprintf;

abstract class AbstractDependencyRewriter implements RewriterInterface
{
/** @var Composer */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be null

protected $composer;

/** @var IOInterface */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be null

protected $io;

/**
* @var Replacements
*/
private $replacements;

public function __construct()
{
$this->replacements = new Replacements();
}

public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
$this->output(sprintf('<info>Activating %s</info>', get_class($this)), IOInterface::DEBUG);
}

/**
* When a ZF package is requested, replace with the Laminas variant.
*
* When a `require` operation is requested, and a ZF package is detected,
* this listener will replace the argument with the equivalent Laminas
* package. This ensures that the `composer.json` file is written to
* reflect the package installed.
*/
public function onPreCommandRun(PreCommandRunEvent $event)
{
$this->output(
sprintf(
'<info>In %s::%s</info>',
get_class($this),
__FUNCTION__
),
IOInterface::DEBUG
);

if (! in_array($event->getCommand(), ['require', 'update'], true)) {
// Nothing to do here.
return;
}

$input = $event->getInput();
if (! $input->hasArgument('packages')) {
return;
}

$input->setArgument(
'packages',
array_map([$this, 'updatePackageArgument'], $input->getArgument('packages'))
);
}

abstract public function onPrePackageInstallOrUpdate(PackageEvent $event);

/**
* @param string $message
* @param int $verbosity
*/
protected function output($message, $verbosity = IOInterface::NORMAL)
{
$this->io->write($message, true, $verbosity);
}

/**
* Parses a package argument from the command line, replacing it with the
* Laminas variant if it exists.
*
* @param string $package Package specification from command line
* @return string Modified package specification containing Laminas
* substitution, or original if no changes required.
*/
private function updatePackageArgument($package)
{
$result = preg_split('/[ :=]/', $package, 2);
if ($result === false) {
return $package;
}
$name = array_shift($result);

if (! $this->isZendPackage($name)) {
return $package;
}

$replacementName = $this->transformPackageName($name);
if ($replacementName === $name) {
return $package;
}

$this->io->write(
sprintf(
'Changing package in current command from %s to %s',
$name,
$replacementName
),
true,
IOInterface::DEBUG
);

$version = reset($result);

if ($version === false) {
return $replacementName;
}

return sprintf('%s:%s', $replacementName, $version);
}

/**
* @param string $name
* @return bool
*/
protected function isZendPackage($name)
{
return $this->replacements->isZendPackage($name);
}

/**
* @param string $name
* @return string
*/
protected function transformPackageName($name)
{
return $this->replacements->transformPackageName($name);
}
}
16 changes: 16 additions & 0 deletions src/AutoloadDumpCapableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @see https://github.com/laminas/laminas-dependency-plugin for the canonical source repository
* @copyright https://github.com/laminas/laminas-dependency-plugin/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-dependency-plugin/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\DependencyPlugin;

use Composer\Script\Event;

interface AutoloadDumpCapableInterface extends RewriterInterface
{
public function onPostAutoloadDump(Event $event);
}
Loading