Skip to content

Commit

Permalink
Add phpscoper library #439
Browse files Browse the repository at this point in the history
  • Loading branch information
alifallahrn committed Oct 19, 2023
1 parent aeba6cd commit 538209d
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,35 @@
"psr-4": {
"WP_Statistics\\": "src/"
}
},
"scripts": {
"build": [
"composer self-update --2",
"composer install --no-dev --optimize-autoloader --ignore-platform-reqs",
"~/.composer/vendor/bin/php-scoper add-prefix --output-dir ../wp-statistics-namespaced --force",
"composer --working-dir=../wp-statistics-namespaced dump-autoload",
"chmod -R 777 ../wp-statistics-namespaced/storage"
],
"build-zip": [
"composer self-update --2",
"composer install --no-dev --optimize-autoloader --ignore-platform-reqs",
"~/.composer/vendor/bin/php-scoper add-prefix --output-dir ../../build/wp-statistics --force",
"composer --working-dir=../../build/wp-statistics dump-autoload",
"cd ../../build/ && zip -rm wp-statistics.zip wp-statistics/"
],
"linux-build": [
"composer self-update --2",
"composer install --no-dev --optimize-autoloader --ignore-platform-reqs",
"~/.config/composer/vendor/bin/php-scoper add-prefix --output-dir ../wp-statistics-namespaced --force",
"composer --working-dir=../wp-statistics-namespaced dump-autoload",
"chmod -R 777 ../wp-statistics-namespaced/storage"
],
"linux-build-zip": [
"composer self-update --2",
"composer install --no-dev --optimize-autoloader --ignore-platform-reqs",
"~/.config/composer/vendor/bin/php-scoper add-prefix --output-dir ../../build/wp-statistics --force",
"composer --working-dir=../../build/wp-statistics dump-autoload",
"cd ../../build/ && zip -rm wp-statistics.zip wp-statistics/"
]
}
}
125 changes: 125 additions & 0 deletions scoper.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?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.

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

// 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()
->ignoreVCS(true)
->ignoreVCSIgnored(true)
->notName(['scoper.inc.php', 'Vlabs'])
->exclude(['includes/vendor', 'node_modules'])
->in('./'),

Finder::create()
->files()
->ignoreVCS(true)
->notName('/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/')
->exclude([
'doc',
'test',
'test_old',
'tests',
'Tests',
'vendor-bin',
])
->in('includes/vendor'),

Finder::create()->append([
'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' => [
'storage/.htaccess',
],

// 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' => [
/**==============================================
* ! WARNING
* All files belonging to root namespace are
* left untouched, this may cause problems
* since some of these files may not contain
* only declarations.
* For now, we patch'em manually
*=============================================**/

static function (string $filePath, string $prefix, string $contents): string {
if (str_contains($filePath, '/vendor/laminas/laminas-zendframework-bridge/src/autoload.php')) {
return str_replace(
'Laminas\ZendFrameworkBridge\Autoloader::load();',
$prefix . '\Laminas\ZendFrameworkBridge\Autoloader::load();',
$contents
);
}
return $contents;
},
],

// 'whitelist' => [
// 'WP_STATISTICS\*',
// ],

// 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' => [
'~^$~', // The root namespace only
'WP_STATISTICS',
// 'Acme\Foo' // The Acme\Foo namespace (and sub-namespaces)
// '~^PHPUnit\\\\Framework$~', // The whole namespace PHPUnit\Framework (but not sub-namespaces)
// '', // Any namespace
],
'exclude-classes' => [],
'exclude-functions' => [],
'exclude-constants' => [],

// 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' => [],
];

0 comments on commit 538209d

Please sign in to comment.