-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce php-scoper to scope our complete plugin dependencies …
…(#4jnk84)
- Loading branch information
1 parent
1e985dc
commit c9513b3
Showing
21 changed files
with
551 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { readFileSync } from "fs"; | ||
import phpParser from "php-parser"; | ||
|
||
const ALLOWED = ["class", "interface", "function", "trait"]; | ||
|
||
function findKinds(obj: any, key: string, namespace = "") { | ||
let list: any[] = []; | ||
if (!obj) return list; | ||
if (obj instanceof Array) { | ||
for (const i in obj) { | ||
list = list.concat(findKinds(obj[i], key, namespace)); | ||
} | ||
return list; | ||
} | ||
|
||
if (ALLOWED.indexOf(obj[key]) > -1 && obj.name) list.push({ ...obj.name, inNamespace: namespace }); | ||
|
||
if (typeof obj == "object" && obj !== null) { | ||
const children = Object.keys(obj); | ||
if (children.length > 0) { | ||
// Correctly set namespace for next children | ||
const appendNamespace = | ||
obj.kind === "namespace" && typeof obj.name === "string" | ||
? `${obj.name.split("\\").filter(Boolean).join("\\")}\\` | ||
: namespace; | ||
for (let i = 0; i < children.length; i++) { | ||
list = list.concat(findKinds(obj[children[i]], key, appendNamespace)); | ||
} | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
/** | ||
* Due to the fact that php-scoper does not support external global dependencies like WordPress | ||
* functions and classes we need to whitelist them. The best approach is to use the already | ||
* used stubs. Stubs are needed for PHP Intellisense so they need to be up2date, too. | ||
* | ||
* @see https://github.com/humbug/php-scoper/issues/303 | ||
* @see https://github.com/humbug/php-scoper/issues/378 | ||
*/ | ||
function extractGlobalStubIdentifiers(files: string[]) { | ||
const result: string[] = []; | ||
|
||
const parser = new phpParser({ | ||
parser: { | ||
extractDoc: true | ||
}, | ||
ast: { | ||
withSource: false, | ||
withPositions: false | ||
} | ||
}); | ||
|
||
for (const file of files) { | ||
const parsed = parser.parseCode(readFileSync(file, { encoding: "UTF-8" })); | ||
result.push( | ||
...findKinds(parsed, "kind").map((id: { name: string; inNamespace: string }) => id.inNamespace + id.name) | ||
); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export { extractGlobalStubIdentifiers }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Isolated\Symfony\Component\Finder\Finder; | ||
//use Symfony\Component\Finder\Finder; | ||
|
||
// Obtain original namespace (must be the first entry in autoload.psr-4) | ||
$composerJson = json_decode(file_get_contents('composer.json'), true); | ||
$psr4 = array_keys($composerJson['autoload']['psr-4'])[0]; | ||
|
||
// Obtain stubs generated by grunt task "php:scope" | ||
$stubsJson = json_decode(file_get_contents('php-scoper.php.json'), true); | ||
|
||
// Whitelist all available monorepo-plugins | ||
$whiteListPlugins = glob('../../../*', GLOB_ONLYDIR); | ||
foreach ($whiteListPlugins as $key => $plugin) { | ||
$composerJson = json_decode(file_get_contents($plugin . '/composer.json'), true); | ||
$whiteListPlugins[$key] = array_keys($composerJson['autoload']['psr-4'])[0] . '*'; | ||
} | ||
|
||
return [ | ||
'prefix' => $psr4 . 'Vendor', | ||
'finders' => [ | ||
Finder::create() | ||
->files() | ||
->in('inc'), | ||
Finder::create() | ||
->files() | ||
->notName('/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/') | ||
->exclude(['doc', 'test', 'test_old', 'tests', 'Tests', 'vendor-bin']) | ||
->in('vendor'), | ||
Finder::create()->append(['composer.json']) | ||
], | ||
'patchers' => [ | ||
function ($filePath, $prefix, $content) use ($stubsJson) { | ||
$prefixDoubleSlashed = str_replace('\\', '\\\\', $prefix); | ||
$quotes = ['\'', '"', '`']; | ||
|
||
foreach ($stubsJson as $identifier) { | ||
$identifierDoubleSlashed = str_replace('\\', '\\\\', $identifier); | ||
$content = str_replace($prefix . '\\' . $identifier, $identifier, $content); // "PREFIX\foo()", or "foo extends nativeClass" | ||
|
||
// Replace in strings, e. g. "if( function_exists('PREFIX\\foo') )" | ||
foreach ($quotes as $quote) { | ||
$content = str_replace( | ||
$quote . $prefixDoubleSlashed . '\\\\' . $identifierDoubleSlashed . $quote, | ||
$quote . $identifierDoubleSlashed . $quote, | ||
$content | ||
); | ||
} | ||
} | ||
return $content; | ||
} | ||
], | ||
'whitelist' => $whiteListPlugins, | ||
'whitelist-global-constants' => true, | ||
'whitelist-global-classes' => false, | ||
'whitelist-global-functions' => false | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
namespace { | ||
// Put your global namespaces here. | ||
// Learn more about it here: https://www.php.net/manual/en/language.namespaces.definitionmultiple.php | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.