Skip to content

Commit 74dd478

Browse files
committed
[ADD] Priority registration Service Providers.
1 parent 34f4a77 commit 74dd478

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
## History
1919

20-
Initially inspired by **Etomite 0.6**, then it has been **MODX Evolution 0.7 - 1.0.8** is an ongoing project written by *Raymond Irving* and a core team of contributors **MODX**, and now its **Evolution CMS** maintained by *Dmytro Lukianenko* and a core team of contributors at the **Evolution CMS Project**.
20+
Initially inspired by **Etomite 0.6**, then it has been **MODX Evolution 0.7 - 1.0.8** is an ongoing project written by *Raymond Irving* and a core team of contributors **MODX**, and now its **Evolution CMS** maintained by *Dmytro Lukianenko*, *Serhii Korneliuk* and a core team of contributors at the **Evolution CMS Project**.
2121

2222
## License
2323

@@ -35,7 +35,7 @@ Most significant, though, is **Evolution CMS's** ability to empower you to quick
3535

3636
## Install
3737
You can use the single click installer: [Evolution CMS Installer](https://github.com/evolution-cms/installer)
38-
Evolution CMS 3.2.x requires **PHP >= 8.2**
38+
Evolution CMS 3.3.x requires **PHP >= 8.3**
3939

4040
## Docker
4141

core/src/Console/Packages/PackageCommand.php

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,36 @@
44
use \EvolutionCMS;
55
use Illuminate\Support\Facades\File;
66

7+
/**
8+
* PackageCommand - Discover and register ServiceProviders from custom packages
9+
*
10+
* This command scans composer.json files and generates provider config files
11+
* in core/custom/config/app/providers/ directory.
12+
*
13+
* Provider Priority System:
14+
* -------------------------
15+
* Providers can specify load priority in their composer.json:
16+
*
17+
* "extra": {
18+
* "laravel": {
19+
* "providers": ["Vendor\\Package\\SomeServiceProvider"],
20+
* "priority": {
21+
* "Vendor\\Package\\SomeServiceProvider": 1
22+
* }
23+
* }
24+
* }
25+
*
26+
* Priority 1-999: Creates file with prefix (e.g., 001_SomeServiceProvider.php)
27+
* Priority 0 (or not specified): Creates file without prefix
28+
*
29+
* Files are loaded in alphabetical order, so:
30+
* - 001_sLangServiceProvider.php loads first (priority: 1)
31+
* - 050_SomeOtherProvider.php loads second (priority: 50)
32+
* - sCommerceServiceProvider.php loads third (priority: 0 or not specified)
33+
* - etc.
34+
*
35+
* @package EvolutionCMS\Console\Packages
36+
*/
737
class PackageCommand extends Command
838
{
939
/**
@@ -80,7 +110,7 @@ public function handle()
80110
if (count($this->require) > 0) {
81111
$this->loadRequire();
82112
}
83-
unlink(EVO_CORE_PATH.'storage/bootstrap/services.php');
113+
unlink(EVO_CORE_PATH . 'storage/bootstrap/services.php');
84114
}
85115

86116
/**
@@ -90,8 +120,13 @@ public function parseComposer(string $composer)
90120
{
91121
$data = json_decode(file_get_contents($composer), true);
92122
if (isset($data['extra']['laravel']['providers'])) {
123+
// Get priorities if defined (default to empty array if not present)
124+
$priorities = $data['extra']['laravel']['priority'] ?? [];
125+
93126
foreach ($data['extra']['laravel']['providers'] as $value) {
94-
$this->process($value);
127+
// Get priority for this provider (default 0 if not specified)
128+
$priority = $priorities[$value] ?? 0;
129+
$this->process($value, $priority);
95130
}
96131
}
97132
if (isset($data['require'])) {
@@ -123,8 +158,13 @@ public function parseComposerServiceProvider(string $composer)
123158
{
124159
$data = json_decode(file_get_contents($composer), true);
125160
if (isset($data['extra']['laravel']['providers']) && is_array($data['extra']['laravel']['providers'])) {
161+
// Get priorities if defined (default to empty array if not present)
162+
$priorities = $data['extra']['laravel']['priority'] ?? [];
163+
126164
foreach ($data['extra']['laravel']['providers'] as $value) {
127-
$this->process($value);
165+
// Get priority for this provider (default 0 if not specified)
166+
$priority = $priorities[$value] ?? 0;
167+
$this->process($value, $priority);
128168
}
129169
}
130170
if (isset($data['extra']['laravel']['files'])) {
@@ -135,15 +175,43 @@ public function parseComposerServiceProvider(string $composer)
135175
}
136176

137177
/**
138-
* @param string $value
178+
* Generate provider config file with optional priority prefix
179+
*
180+
* @param string $value Provider class name
181+
* @param int $priority Priority (1-999), used for load order. Lower numbers load first.
139182
*/
140-
protected function process(string $value)
183+
protected function process(string $value, int $priority = 0)
141184
{
142185
$arrNamespace = explode('\\', $value);
143-
$fileName = end($arrNamespace) . '.php';
144-
$fileContent = "<?php \nreturn " . $value . "::class;";
186+
$className = end($arrNamespace);
187+
188+
// Add priority prefix if specified (001_, 002_, ..., 999_)
189+
if ($priority > 0 && $priority < 1000) {
190+
$prefix = str_pad($priority, 3, '0', STR_PAD_LEFT) . '_';
191+
$fileName = $prefix . $className . '.php';
192+
$fileContent = "<?php\n// Priority {$priority} - loads before other providers\nreturn " . $value . "::class;";
193+
194+
// Remove old files with different prefixes for the same provider
195+
$patterns = [
196+
$this->configDir . $className . '.php', // without prefix
197+
$this->configDir . '*_' . $className . '.php', // with any prefix
198+
];
199+
200+
foreach ($patterns as $pattern) {
201+
foreach (glob($pattern) as $oldFile) {
202+
if ($oldFile !== $this->configDir . $fileName) {
203+
@unlink($oldFile);
204+
$this->getOutput()->write('<comment>Removed old ' . basename($oldFile) . ' (using priority prefix)</comment>');
205+
}
206+
}
207+
}
208+
} else {
209+
$fileName = $className . '.php';
210+
$fileContent = "<?php \nreturn " . $value . "::class;";
211+
}
212+
145213
if (file_put_contents($this->configDir . $fileName, $fileContent)) {
146-
$this->getOutput()->write('<info>' . $value . '</info>');
214+
$this->getOutput()->write('<info>' . $value . ($priority > 0 ? " (priority: {$priority})" : '') . '</info>');
147215
} else {
148216
$this->getOutput()->write('<error>Error create config for: ' . $value . '</error>');
149217
}

0 commit comments

Comments
 (0)