Skip to content

Commit

Permalink
Prefix vendor to avoid conflicts (#30)
Browse files Browse the repository at this point in the history
* Prefix vendor to avoid vendor conflicts

---------

Co-authored-by: Adam Wysocki <[email protected]>
  • Loading branch information
s4ddly and Adam Wysocki authored Dec 5, 2024
1 parent 74526de commit 7d1416f
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 18 deletions.
1 change: 1 addition & 0 deletions .dev-tools/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
.php-cs-fixer.cache
tools/*
154 changes: 154 additions & 0 deletions .dev-tools/scoper.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

declare(strict_types=1);

use Isolated\Symfony\Component\Finder\Finder;

// You can do your own things here, e.g. collecting symbols to expose dynamically
// or files to exclude.
// However beware that this file is executed by PHP-Scoper, hence if you are using
// the PHAR it will be loaded by the PHAR. So it is highly recommended to avoid
// to auto-load any code here: it can result in a conflict or even corrupt
// the PHP-Scoper analysis.

// Example of collecting files to include in the scoped build but to not scope
// leveraging the isolated finder.
$excludedFiles = array_map(
static fn (SplFileInfo $fileInfo) => $fileInfo->getPathName(),
iterator_to_array(
Finder::create()->files()->in('../vendor/tpay-com'),
false,
),
);

return [
// The prefix configuration. If a non-null value is used, a random prefix
// will be generated instead.
//
// For more see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#prefix
'prefix' => 'Tpay\\Vendor',

// The base output directory for the prefixed files.
// This will be overridden by the 'output-dir' command line option if present.
'output-dir' => '../vendor_prefixed',

// By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
// directory. You can however define which files should be scoped by defining a collection of Finders in the
// following configuration key.
//
// This configuration entry is completely ignored when using Box.
//
// For more see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#finders-and-paths
'finders' => [
Finder::create()->files()->in('../vendor/composer')->name(['*.php', 'LICENSE', 'composer.json'])->notName('autoload_*.php'),
Finder::create()->files()->in('../vendor/paragonie')->name(['*.php', 'LICENSE', 'composer.json']),
Finder::create()->files()->in('../vendor/phpseclib')->name(['*.php', 'LICENSE', 'composer.json']),
Finder::create()->files()->in('../vendor/psr')->exclude('Test')->name(['*.php', 'LICENSE', 'composer.json']),
],

// List of excluded files, i.e. files for which the content will be left untouched.
// Paths are relative to the configuration file unless if they are already absolute
//
// For more see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#patchers
'exclude-files' => [
// 'src/an-excluded-file.php',
// ...$excludedFiles,
],

// PHP version (e.g. `'7.2'`) in which the PHP parser and printer will be configured into. This will affect what
// level of code it will understand and how the code will be printed.
// If none (or `null`) is configured, then the host version will be used.
'php-version' => null,

// When scoping PHP files, there will be scenarios where some of the code being scoped indirectly references the
// original namespace. These will include, for example, strings or string manipulations. PHP-Scoper has limited
// support for prefixing such strings. To circumvent that, you can define patchers to manipulate the file to your
// heart contents.
//
// For more see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#patchers
'patchers' => [
static function (string $filePath, string $prefix, string $contents): string {
if (strpos($contents, '\'phpseclib3\\')) {
return preg_replace("%'phpseclib3%", "'\Tpay\Vendor\phpseclib3", $contents);
}

if (strpos($contents, '\'\\phpseclib3')) {
return str_replace("'\phpseclib3", "'\Tpay\Vendor\phpseclib3", $contents);
}

// Change logger namespaces

$files = [
'../vendor/tpay-com/tpay-openapi-php/src/Utilities/Logger.php',
'../vendor/tpay-com/tpay-openapi-php/src/Utilities/TpayException.php',
];

foreach ($files as $file) {
if (file_exists($file)) {
$content = file_get_contents($file);

$changed = str_replace(" Psr\\Log\\", " Tpay\Vendor\Psr\Log\\", $content);

file_put_contents($file, $changed);
}
}

// Change phpseclib namespaces

$files = [
'../vendor/tpay-com/tpay-openapi-php/src/Utilities/phpseclib/Crypt/RSA.php',
'../vendor/tpay-com/tpay-openapi-php/src/Utilities/phpseclib/File/X509.php',
];

foreach ($files as $file) {
if (file_exists($file)) {
$content = file_get_contents($file);

$changed = str_replace("'phpseclib3\\", "'Tpay\Vendor\phpseclib3\\", $content);
$changed = str_replace(" \phpseclib3\\", " \Tpay\Vendor\phpseclib3\\", $changed);
$changed = str_replace("'phpseclib\\", "'Tpay\Vendor\phpseclib\\", $changed);
$changed = str_replace(" \phpseclib\\", " \Tpay\Vendor\phpseclib\\", $changed);

file_put_contents($file, $changed);
}
}

return $contents;
},
],

// List of symbols to consider internal i.e. to leave untouched.
//
// For more information see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#excluded-symbols
'exclude-namespaces' => [
// 'Acme\Foo' // The Acme\Foo namespace (and sub-namespaces)
// '~^PHPUnit\\\\Framework$~', // The whole namespace PHPUnit\Framework (but not sub-namespaces)
// '~^$~', // The root namespace only
// '', // Any namespace
],
'exclude-classes' => [
// 'ReflectionClassConstant',
],
'exclude-functions' => [
// 'mb_str_split',
],
'exclude-constants' => [
// 'STDIN',
],

// List of symbols to expose.
//
// For more information see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#exposed-symbols
'expose-global-constants' => true,
'expose-global-classes' => true,
'expose-global-functions' => true,
'expose-namespaces' => [
// 'Acme\Foo' // The Acme\Foo namespace (and sub-namespaces)
// '~^PHPUnit\\\\Framework$~', // The whole namespace PHPUnit\Framework (but not sub-namespaces)
// '~^$~', // The root namespace only
// '', // Any namespace
],
'expose-classes' => [],
'expose-functions' => [],
'expose-constants' => [],
];
41 changes: 40 additions & 1 deletion .github/workflows/create_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ on:
required: true

jobs:
phive:
name: Install tools
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Install tools
uses: ngmy/phive-install-action@master
- name: Upload tools to artifact store
uses: actions/upload-artifact@master
with:
name: tools
path: .dev-tools/tools

create_release:
needs: phive
runs-on: ubuntu-latest
steps:
- name: Checkout source
Expand All @@ -26,14 +41,38 @@ jobs:
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter, json
coverage: none

- run: composer install --no-dev
- run: composer install --prefer-dist --optimize-autoloader --no-dev --no-scripts

- run: chmod 777 -R vendor

- name: Download tools from artifact store
uses: actions/download-artifact@master
with:
name: tools
path: .dev-tools/tools
- name: Set tools as an executable
run: find .dev-tools/tools -type f -print0 | xargs -0 chmod +x

- uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter, json
coverage: none

- run: cd .dev-tools && tools/php-scoper add-prefix -fq --config=./scoper.inc.php

- run: git archive --output=tpay-woocommerce.zip HEAD

- run: rm -rf vendor/paragonie
- run: rm -rf vendor/phpseclib
- run: rm -rf vendor/psr

- run: composer dump-autoload

- run: zip -ur tpay-woocommerce.zip vendor/

- run: zip -ur tpay-woocommerce.zip vendor_prefixed/

- name: Create Github release
uses: ncipollo/release-action@v1
with:
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ permissions:
pull-requests: write

jobs:
phive:
name: Install tools
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Install tools
uses: ngmy/phive-install-action@master
- name: Upload tools to artifact store
uses: actions/upload-artifact@master
with:
name: tools
path: .dev-tools/tools

check:
needs: phive
runs-on: ubuntu-latest

steps:
Expand All @@ -24,10 +39,34 @@ jobs:

- run: chmod 777 -R vendor

- name: Download tools from artifact store
uses: actions/download-artifact@master
with:
name: tools
path: .dev-tools/tools
- name: Set tools as an executable
run: find .dev-tools/tools -type f -print0 | xargs -0 chmod +x

- uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter, json
coverage: none

- run: cd .dev-tools && tools/php-scoper add-prefix -fq --config=./scoper.inc.php

- run: git archive --output=tpay-woocommerce.zip HEAD

- run: rm -rf vendor/paragonie
- run: rm -rf vendor/phpseclib
- run: rm -rf vendor/psr

- run: composer dump-autoload

- run: zip -ur tpay-woocommerce.zip vendor/

- run: zip -ur tpay-woocommerce.zip vendor_prefixed/

- run: mkdir plugin && unzip tpay-woocommerce.zip -d plugin

- uses: actions/upload-artifact@v4
Expand Down
4 changes: 4 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="humbug/php-scoper" version="^0.18.15" installed="0.18.15" location="./.dev-tools/tools/php-scoper" copy="false"/>
</phive>
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.7.9]

### Changed

- Prefix vendor to avoid conflicts

## [1.7.8]

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"Tpay\\": "includes/"
},
"classmap": [
"tpay.php"
"tpay.php",
"vendor_prefixed/"
]
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion includes/TpayLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Tpay;

use Psr\Log\LoggerInterface;
use Tpay\Vendor\Psr\Log\LoggerInterface;

class TpayLogger implements LoggerInterface
{
Expand Down
14 changes: 1 addition & 13 deletions tpay-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,7 @@ function tpay_refresh_checkout_on_payment_methods_change()

function get_package_version(): string
{
$installed = __DIR__ . '/vendor/composer/installed.php';

if (false === file_exists($installed)) {
throw new Exception('Composer installed file not found');
}

$dir = require $installed;

if (isset($dir['versions']['tpay/woocommerce'])) {
return $dir['versions']['tpay/woocommerce']['pretty_version'];
}

return 'n/a';
return \Tpay\Vendor\Composer\InstalledVersions::getPrettyVersion('tpay-com/tpay-openapi-php');
}

function tpay_blik0_transaction_status()
Expand Down
4 changes: 2 additions & 2 deletions tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Tpay Payment Gateway
* Plugin URI: https://tpay.com
* Description: Tpay payment gateway for WooCommerce
* Version: 1.7.8
* Version: 1.7.9
* Author: Krajowy Integrator Płatności S.A.
* Author URI: http://www.tpay.com
* License: LGPL 3.0
Expand Down Expand Up @@ -42,7 +42,7 @@

require_once 'tpay-functions.php';

define('TPAY_PLUGIN_VERSION', '1.7.8');
define('TPAY_PLUGIN_VERSION', '1.7.9');
define('TPAY_PLUGIN_DIR', dirname(plugin_basename(__FILE__)));
add_action('plugins_loaded', 'init_gateway_tpay');
register_activation_hook(__FILE__, 'tpay_on_activate');
Expand Down
4 changes: 4 additions & 0 deletions vendor_prefixed/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

0 comments on commit 7d1416f

Please sign in to comment.