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

[FEATURE] Compatibility with PHP 7.3+ and Composer 2.0 #1

Merged
merged 7 commits into from
Jan 12, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Tests
on: [push, pull_request]

jobs:
tests:
name: PHP ${{ matrix.php-version }} & Composer ${{ matrix.composer-version }}

runs-on: ubuntu-18.04
strategy:
fail-fast: false
matrix:
php-version: [8.0, 7.4, 7.3, 7.2, 7.1]
composer-version: [2, 1]
include:
- php-version: 8.0
composer-version: 2
coverage: 1

steps:
- uses: actions/checkout@v2

# Prepare environment
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: composer:v${{ matrix.composer-version }}
coverage: xdebug

# Define Composer cache
- name: Define Composer cache
uses: actions/cache@v1
with:
path: ~/.composer/cache
key: tests-php-${{ matrix.php-version }}-composer-${{ matrix.composer-version }}
restore-keys: tests-php-${{ matrix.php-version }}-composer-

# Install Composer dependencies
- name: Install Composer dependencies
run: composer require --dev composer/composer:"^${{ matrix.composer-version }}" --no-progress

# Run tests
- name: Run tests
run: composer test -- --coverage-text

# Report coverage
- name: Fix coverage path
working-directory: .build/log/coverage
run: sed -i 's/\/home\/runner\/work\/auditor\/auditor\//\/github\/workspace\//g' clover.xml
if: ${{ matrix.coverage }}
- name: Run SonarCloud scan
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
if: ${{ matrix.coverage }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
composer.phar
/vendor/
.build/
.phpunit.result.cache

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
composer.lock
src/BundleDescriber.php
/tests/Build/test-application/vendor/
/tests/Build/test-application/composer.lock
30 changes: 15 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ before_script:
- composer install --no-progress

script:
- >
if [[ "$COVERAGE" != "1" ]]; then
echo;
echo "Running unit tests";
.build/bin/phpunit --colors tests/Unit/
fi

- >
if [[ "$COVERAGE" == "1" ]]; then
echo;
echo "Running unit tests";
.build/bin/phpunit --colors -c tests/Build/UnitTests.xml tests/Unit/
rm -rf .build/vendor/ .build/bin/
sonar-scanner -Dproject.settings=.sonar-project.properties
fi
- >
if [[ "$COVERAGE" != "1" ]]; then
echo;
echo "Running unit tests";
.build/bin/phpunit --colors tests/Unit/
fi

- >
if [[ "$COVERAGE" == "1" ]]; then
echo;
echo "Running unit tests";
.build/bin/phpunit --colors -c tests/Build/UnitTests.xml tests/Unit/
rm -rf .build/vendor/ .build/bin/
sonar-scanner -Dproject.settings=sonar-project.properties
fi
31 changes: 16 additions & 15 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
Auditor
=========
# Auditor

This is a composer plugin. It allows to access information about the current (root) package.
This is a Composer plugin. It allows to access information about the current (root) package.

### Requirements
* composer
## Requirements

* Composer
* PHP >= 7.1

### Usage
### Version matrix

| | PHP 8.0 | PHP 7.4 | PHP 7.3 | PHP 7.2 | PHP 7.1 |
| ------------ | ------- | ------- | ------- | ------- | ------- |
| Composer 1.x | 0.5.x | 0.5.x | 0.5.x | 0.1.0 - 0.5.x | 0.1.0 - 0.5.x
| Composer 2.x | 0.5.x | 0.5.x | 0.5.x | 0.5.x | 0.5.x |

## Usage

In your project root

````bash
```bash
composer require cpsit/auditor
````

**Note:** The package is not yet available on packagist.org.
Please add `https://github.com/CPS-IT/auditor` to the list of repositories in your
`composer.json`
```

After installation or update via composer a class `CPSIT\Auditor\BundleDescriber` is generated.
After installation or update via Composer a class `CPSIT\Auditor\BundleDescriber` is generated.

It allows to access each property of your bundle (root package).

```php
$name = \CPSIT\Auditor\BundleDescriber::getProperty('name');

```

14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"license": "GPL-2.0-or-later",
"type": "composer-plugin",
"require": {
"php": ">=7.0.0 <=7.3.0",
"composer-plugin-api": "^1.0.0",
"ocramius/package-versions": "^1.3.0"
"php": "^7.1.0 || ~8.0.0",
"composer-plugin-api": "^1.0.0 || ^2.0.0",
"composer/package-versions-deprecated": "~1.11.99"
},
"authors": [
{
Expand All @@ -21,11 +21,13 @@
"scripts": {
"post-autoload-dump": [
"mkdir -p .build/log/coverage/"
]
],
"test": "phpunit -c tests/Build/UnitTests.xml"
},
"require-dev": {
"phpunit/phpunit": "^6.5.0 || ^7.0.0",
"composer/composer": "^1.6.3"
"phpunit/phpunit": "^7.5.0 || ^8.0.0 || ^9.0.0",
"composer/composer": "^1.6.3 || ^2.0.0",
"symfony/filesystem": "~4.4.0 || ~5.2.0"
},
"autoload": {
"psr-4": {
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions src/Dto/NullPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getName(): string
*/
public function setName(string $name): Package
{
// We ignore $name here since the package name is static
return $this;
}

Expand All @@ -59,6 +60,7 @@ public function getVersion(): string
*/
public function setVersion(string $version): Package
{
// We ignore $version here since the package version is static
return $this;
}

Expand All @@ -76,6 +78,7 @@ public function getSourceReference(): string
*/
public function setSourceReference(string $sourceReference): Package
{
// We ignore $sourceReference here since the packages' source reference is static
return $this;
}
}
11 changes: 6 additions & 5 deletions src/Generator/BundleDescriberClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class BundleDescriberClassGenerator
%s implements DescriberInterface
{
use PropertiesTrait;
static protected $properties = %s;
static protected $installedPackages = %s;
static protected $properties = '%s';
static protected $installedPackages = '%s';

private function __construct()
{
Expand Down Expand Up @@ -103,12 +103,13 @@ public function writeFile(array $properties = [], array $installedPackages = [])
* @param array $installedPackages
* @return string
*/
protected function generateSource(array $properties, array $installedPackages): string {
protected function generateSource(array $properties, array $installedPackages): string
{
return sprintf(
self::$generatedClassTemplate,
'fin' . 'al ' . 'cla' . 'ss ' . SI::BUNDLE_DESCRIBER_CLASS, // note: workaround for regex-based code parsers :-(
var_export($properties, true),
var_export($installedPackages, true)
addcslashes(serialize($properties), "'"),
addcslashes(serialize($installedPackages), "'")
);
}

Expand Down
36 changes: 26 additions & 10 deletions src/InstalledPackagesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
*/
trait InstalledPackagesTrait
{
/**
* @var array|null
*/
protected static $resolvedPackages;

/**
* @return array
*/
Expand All @@ -50,26 +55,24 @@ static public function propertyExists(string $propertyName): bool
1557047757
);
}


if (!static::arePackagesResolved()) {
static::resolvePackages();
}
return true;
}

/**
* Get a representation of a installed package
* Get a representation of an installed package
*
* @param string $name
* @return Package|null
*/
static public function getInstalledPackage(string $name): Package
static public function getInstalledPackage(string $name): ?Package
{
if (!self::isPackageInstalled($name)) {
return null;
}

$package = new Package(self::$installedPackages[$name]);

return $package;

return new Package(self::$installedPackages[$name]);
}

/**
Expand All @@ -83,4 +86,17 @@ static public function isPackageInstalled(string $name): bool
self::propertyExists(DescriberInterface::INSTALLED_PACKAGES);
return (array_key_exists($name, self::$installedPackages));
}
}

protected static function arePackagesResolved(): bool
{
return is_array(static::$resolvedPackages);
}

protected static function resolvePackages(): void
{
if (!property_exists(static::class, DescriberInterface::INSTALLED_PACKAGES)) {
return;
}
static::$resolvedPackages = unserialize(static::${DescriberInterface::INSTALLED_PACKAGES}) ?: [];
}
}
15 changes: 9 additions & 6 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@
***************************************************************/

use Composer\Composer;
use Composer\Config;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Package\AliasPackage;
use Composer\Package\Locker;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use CPSIT\Auditor\Generator\BundleDescriberClassGenerator;
use CPSIT\Auditor\Reflection\RootPackageReflection;
use CPSIT\Auditor\SettingsInterface as SI;

/**
* Class Installer
Expand Down Expand Up @@ -73,4 +67,13 @@ public static function dumpBundleDescriberClass(Event $composerEvent)
$generator->writeFile($properties, $installedPackages);
}

public function deactivate(Composer $composer, IOInterface $io): void
{
// Intentionally left blank.
}

public function uninstall(Composer $composer, IOInterface $io): void
{
// Intentionally left blank.
}
}
32 changes: 23 additions & 9 deletions src/PropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

trait PropertiesTrait
{
/**
* @var array|null
*/
protected static $resolvedProperties;

/**
* @param string $key
* @return mixed
Expand All @@ -33,18 +38,27 @@ public static function getProperty(string $key)
1557047730
);
}
return self::$properties[$key];
return static::$resolvedProperties[$key];
}

/**
* @param string $key
* @return boolean
*/
public static function hasProperty(string $key):bool
{
return (
property_exists(self::class, 'properties')
&& array_key_exists($key, self::$properties)
);
if (!static::arePropertiesResolved()) {
static::resolveProperties();
}
return array_key_exists($key, static::$resolvedProperties ?? []);
}

protected static function arePropertiesResolved(): bool
{
return is_array(static::$resolvedProperties);
}

protected static function resolveProperties(): void
{
if (!property_exists(self::class, 'properties')) {
return;
}
static::$resolvedProperties = unserialize(self::$properties, ['allowed_classes' => false]) ?: [];
}
}