Skip to content

Commit c1c87df

Browse files
committed
Use composer-preload-files
1 parent d285164 commit c1c87df

File tree

7 files changed

+161
-199
lines changed

7 files changed

+161
-199
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ composer require craftcms/cloud
1414

1515
### [`craftcms/cloud-ops`](packages/cloud-ops)
1616

17-
Standalone Yii2 extension + Composer plugin for Cloud infrastructure. Configures cache, queue, session, and other runtime components. Automatically installed as a dependency of `craftcms/cloud`.
17+
Yii2 extension for Cloud infrastructure. Configures cache, queue, session, and other runtime components. Automatically installed as a dependency of `craftcms/cloud`.
1818

19-
## Composer Plugin
20-
21-
`cloud-ops` includes a Composer plugin that reorders `vendor/composer/autoload_files.php` after install/update. This ensures the `cloud-ops` autoload file (which defines `craft_modify_app_config()`) is loaded before any other package that might define the same function—including older versions of `craftcms/cloud`.
22-
23-
This allows projects to safely migrate from the legacy single-package setup to the new split architecture without bootstrap conflicts.
19+
Uses [`codezero/composer-preload-files`](https://github.com/codezero-be/composer-preload-files) to ensure the `cloud-ops` autoload file (which defines `craft_modify_app_config()`) is loaded before any other package that might define the same function—including older versions of `craftcms/cloud`. This allows projects to safely migrate from the legacy single-package setup to the new split architecture without bootstrap conflicts.
2420

2521
## Releasing
2622

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"allow-plugins": {
4545
"yiisoft/yii2-composer": true,
4646
"craftcms/plugin-installer": true,
47-
"craftcms/cloud-ops": true
47+
"codezero/composer-preload-files": true
4848
}
4949
}
5050
}

composer.lock

Lines changed: 67 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cloud-ops/composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"name": "craftcms/cloud-ops",
33
"description": "Craft Cloud infrastructure and runtime extension",
4-
"type": "composer-plugin",
4+
"type": "yii2-extension",
55
"license": "proprietary",
66
"require": {
77
"php": "^8.2",
8-
"composer-plugin-api": "^2.0",
98
"craftcms/cms": "^4.6 || ^5.0",
109
"bref/bref": "^2.0",
1110
"bref/extra-php-extensions": "^1.0",
1211
"craftcms/yii2-cache-cascade": "^1.0",
1312
"yiisoft/yii2-redis": "^2.0",
1413
"yiisoft/yii2-queue": "^2.3.7",
15-
"aws/aws-sdk-php": "^3.342.6"
14+
"aws/aws-sdk-php": "^3.342.6",
15+
"codezero/composer-preload-files": "^1.0"
1616
},
1717
"autoload": {
1818
"psr-4": {
@@ -26,12 +26,15 @@
2626
"branch-alias": {
2727
"dev-main": "1.x-dev"
2828
},
29-
"class": "craft\\cloud\\ops\\composer\\Plugin",
30-
"bootstrap": "craft\\cloud\\ops\\Module"
29+
"bootstrap": "craft\\cloud\\ops\\Module",
30+
"preload-files": [
31+
"autoload.php"
32+
]
3133
},
3234
"config": {
3335
"allow-plugins": {
34-
"yiisoft/yii2-composer": true
36+
"yiisoft/yii2-composer": true,
37+
"codezero/composer-preload-files": true
3538
}
3639
}
3740
}

packages/cloud-ops/src/Composer.php

Lines changed: 0 additions & 87 deletions
This file was deleted.

packages/cloud-ops/src/cli/controllers/AssetBundlesController.php

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Craft;
66
use craft\cloud\ops\cli\AssetBundlePublisher;
7-
use craft\cloud\ops\Composer;
87
use craft\console\Controller;
98
use craft\helpers\App;
9+
use craft\helpers\Json;
1010
use craft\web\assets\datepickeri18n\DatepickerI18nAsset;
11+
use Illuminate\Support\Collection;
12+
use League\Uri\Components\HierarchicalPath;
1113
use ReflectionClass;
1214
use yii\console\Exception;
1315
use yii\console\ExitCode;
@@ -33,9 +35,9 @@ public function beforeAction($action): bool
3335
}
3436

3537
if (App::env('CRAFT_NO_DB')) {
36-
Composer::getModuleAliases()
37-
->merge(Composer::getPluginAliases())
38-
->merge(Composer::getRootAliases())
38+
$this->getModuleAliases()
39+
->merge($this->getPluginAliases())
40+
->merge($this->getRootAliases())
3941
->each(function($path, $alias) {
4042
return Craft::setAlias($alias, $path);
4143
});
@@ -105,4 +107,80 @@ public function actionPublish(): int
105107

106108
return ExitCode::OK;
107109
}
110+
111+
private function getPluginAliases(): Collection
112+
{
113+
$pluginInfo = Craft::$app->getPlugins()->getComposerPluginInfo();
114+
115+
return Collection::make($pluginInfo)
116+
->flatMap(fn(array $plugin) => $plugin['aliases'] ?? []);
117+
}
118+
119+
private function getModuleAliases(): Collection
120+
{
121+
$data = Json::decode(file_get_contents(Craft::$app->getComposer()->getLockPath()));
122+
$packages = new Collection($data['packages'] ?? null);
123+
124+
return $packages
125+
->flatMap(function(array $package) {
126+
$packageName = $package['name'] ?? null;
127+
$packageType = $package['type'] ?? null;
128+
129+
if (
130+
!$packageName ||
131+
!$packageType ||
132+
!preg_match('/^(craft|yii)/', $packageType)
133+
) {
134+
return null;
135+
}
136+
137+
$basePath = HierarchicalPath::fromAbsolute(
138+
Craft::$app->getVendorPath(),
139+
$packageName,
140+
);
141+
142+
return $this->psr4ToAliases(
143+
$package['autoload']['psr-4'] ?? [],
144+
$basePath,
145+
);
146+
});
147+
}
148+
149+
private function getRootAliases(): Collection
150+
{
151+
$jsonPath = Craft::$app->getComposer()->getJsonPath();
152+
$root = dirname($jsonPath);
153+
$data = Json::decode(file_get_contents($jsonPath));
154+
155+
return $this->psr4ToAliases(
156+
$data['autoload']['psr-4'] ?? [],
157+
$root,
158+
);
159+
}
160+
161+
private function psr4ToAliases(iterable $psr4, string $basePath): Collection
162+
{
163+
return Collection::make($psr4)
164+
->mapWithKeys(function($path, $namespace) use ($basePath) {
165+
166+
// Yii doesn't support aliases that point to multiple base paths
167+
if (is_array($path)) {
168+
return null;
169+
}
170+
171+
$normalizedPath = HierarchicalPath::new($path);
172+
173+
if (!$normalizedPath->isAbsolute()) {
174+
$normalizedPath = HierarchicalPath::fromAbsolute(
175+
$basePath,
176+
$path,
177+
);
178+
}
179+
180+
$alias = '@' . str_replace('\\', '/', trim($namespace, '\\'));
181+
$normalizedPath = $normalizedPath->withoutTrailingSlash()->value();
182+
183+
return [$alias => $normalizedPath];
184+
});
185+
}
108186
}

0 commit comments

Comments
 (0)