Skip to content

Commit ae6e7cf

Browse files
committed
Add function to keep the stub files for the console
1 parent c623fe0 commit ae6e7cf

6 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎.github/workflows/tests.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ jobs:
7070
grep -rq 'function Themeum\\Framework\\app' libraries/themeum/framework/Facade.php
7171
grep -q 'use Carbon\\Carbon as BaseCarbon' libraries/themeum/framework/Supports/Carbon.php
7272
! grep -rq 'Themeum\\Carbon\\' libraries/themeum/framework/
73+
74+
- name: Verify console stubs are present and prefixed
75+
run: |
76+
test -f libraries/themeum/framework/Console/stubs/provider.stub
77+
grep -q 'use Themeum\\Framework\\ServiceProvider' libraries/themeum/framework/Console/stubs/provider.stub

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ return [
7575
->files()
7676
->in(__DIR__ . '/vendor/themeum/framework/src')
7777
->exclude(['test', 'tests', 'Tests'])
78-
->name('*.php'),
78+
->name(['*.php', '*.stub']),
7979
],
8080
'exclude-files' => [],
8181
'exclude-namespaces' => [
@@ -90,6 +90,8 @@ return [
9090

9191
PHP-Scoper rewrites `Framework\` to `Kirki\Framework\` and writes the result to `libraries/kirki/framework/`. The `exclude-namespaces` regex keeps every namespace except `Framework\` unprefixed (Composer vendor code stays on its original PSR-4 paths).
9292

93+
WP-CLI generator stubs live under `src/Console/stubs/` as `*.stub` files. Include them in the finder (`->name(['*.php', '*.stub'])`) so they are copied into the scoped tree. Stub templates use placeholders such as `{{namespace}}`, which PHP-Scoper cannot parse; after scoping, run `scripts/prefix-scoped-console-stubs.php` on `libraries/<vendor>/framework/Console/stubs` with your prefix (the example plugin does this via `scripts/post-scope-console-stubs.php` in its `scope` Composer script).
94+
9395
### 3. Autoload the scoped copy only
9496

9597
```json

‎example/composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
},
3838
"scripts": {
3939
"scope": [
40-
"@php vendor/bin/php-scoper add-prefix --config=scoper.config.php --force"
40+
"@php vendor/bin/php-scoper add-prefix --config=scoper.config.php --force",
41+
"@php scripts/post-scope-console-stubs.php"
4142
]
4243
},
4344
"minimum-stability": "dev",

‎example/scoper.config.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
->files()
1313
->in(__DIR__ . '/vendor/themeum/framework/src')
1414
->exclude(['test', 'tests', 'Tests'])
15-
->name('*.php')
15+
->name(['*.php', '*.stub'])
1616
],
1717
'exclude-files' => [],
1818

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$plugin_root = dirname(__DIR__);
6+
$stubs_dir = $plugin_root . '/libraries/themeum/framework/Console/stubs';
7+
$prefix = $argv[1] ?? 'Themeum';
8+
9+
$candidates = [
10+
'/var/www/html/framework-library/scripts/prefix-scoped-console-stubs.php',
11+
dirname(__DIR__, 2) . '/scripts/prefix-scoped-console-stubs.php',
12+
];
13+
14+
$prefix_script = null;
15+
16+
foreach ($candidates as $candidate) {
17+
if (is_file($candidate)) {
18+
$prefix_script = $candidate;
19+
break;
20+
}
21+
}
22+
23+
if ($prefix_script === null) {
24+
fwrite(STDERR, "Could not find prefix-scoped-console-stubs.php.\n");
25+
exit(1);
26+
}
27+
28+
$command = sprintf(
29+
'php %s %s %s',
30+
escapeshellarg($prefix_script),
31+
escapeshellarg($stubs_dir),
32+
escapeshellarg($prefix)
33+
);
34+
35+
passthru($command, $exit_code);
36+
exit((int) $exit_code);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$stubs_dir = $argv[1] ?? null;
6+
$prefix = $argv[2] ?? 'Themeum';
7+
8+
if ($stubs_dir === null || $stubs_dir === '') {
9+
fwrite(STDERR, "Usage: php prefix-scoped-console-stubs.php <stubs-dir> [prefix]\n");
10+
exit(1);
11+
}
12+
13+
if (!is_dir($stubs_dir)) {
14+
exit(0);
15+
}
16+
17+
$prefixed_framework = $prefix . '\\Framework\\';
18+
$pattern = '/(?<!' . preg_quote($prefix, '/') . '\\\\)Framework\\\\/';
19+
20+
$iterator = new RecursiveIteratorIterator(
21+
new RecursiveDirectoryIterator($stubs_dir, FilesystemIterator::SKIP_DOTS)
22+
);
23+
24+
foreach ($iterator as $file) {
25+
if (!$file->isFile() || $file->getExtension() !== 'stub') {
26+
continue;
27+
}
28+
29+
$path = $file->getPathname();
30+
$contents = file_get_contents($path);
31+
32+
if ($contents === false) {
33+
continue;
34+
}
35+
36+
$updated = preg_replace($pattern, $prefixed_framework, $contents);
37+
38+
if (!is_string($updated) || $updated === $contents) {
39+
continue;
40+
}
41+
42+
file_put_contents($path, $updated);
43+
}

0 commit comments

Comments
 (0)