mirror of
https://github.com/Vateron-Media/XC_VM.git
synced 2026-09-29 12:02:04 +02:00
2.8 KiB
2.8 KiB
description
| description |
|---|
| Use when migrating legacy code to services, replacing global variables, extracting logic from god-objects, or refactoring admin pages. Covers safe migration patterns for XC_VM. |
Migration Patterns — XC_VM
Strategy
Iterative refactoring. Each step must be backward-compatible. No big-bang rewrites.
Replacing Global Variables
Pattern: Wrap global access in a service method
// BEFORE (legacy):
global $db;
$rStreams = $db->query('SELECT * FROM streams WHERE id = ?;', $rID);
// AFTER (migrated):
// In StreamRepository:
class StreamRepository {
public function __construct(private Database $db) {}
public function getById(int $id): ?array {
return $this->db->query('SELECT * FROM streams WHERE id = ?;', $id);
}
}
Migration order for globals:
$db→ injectDatabasevia constructor into Repository classes$rSettings→ inject via constructor or useConfigLoader::get()$rUserInfo→ inject viaSessionManageror pass as parameter$rServers→ inject viaServerRepository
Safety rules:
- Do NOT remove
global $xxxfrom a file until ALL usages in that file are replaced - Mark partial migrations:
// @migration-in-progress — $rSettings still used on line 45 - Test the file after each global removal (php -l at minimum)
Extracting from God-Objects
Pattern: One method group → one Service
// CoreUtilities had: getStreamInfo(), updateStream(), deleteStream(), ...
// Extract to: domain/Stream/StreamService.php
// Then replace call sites:
// BEFORE: CoreUtilities::getStreamInfo($id)
// AFTER: StreamService::getById($id) (or instance method via DI)
Extraction checklist:
- Identify method cluster by domain (all stream-related, all user-related, etc.)
- Create Service + Repository in
src/domain/{Context}/ - Move methods, preserving signatures initially
- Update all call sites (grep for old method name)
- Remove from god-object
- Run
php -lon all changed files
Admin Page Migration
Pattern: Extract SQL from presentation
// BEFORE (legacy page/template logic):
global $db;
$rStreams = $db->query('SELECT * FROM streams;');
foreach ($rStreams as $rStream) {
echo "<tr><td>{$rStream['name']}</td></tr>";
}
// AFTER:
// In StreamRepository: getAllStreams() method
// In Admin controller/View: $rStreams = StreamRepository::getAll();
// HTML stays in public/Views/admin/* (presentation layer)
Batch Migration Script Pattern
When migrating many files at once:
- Create a migration script (not committed) that does search-replace
- Run
grep -rn "OLD_PATTERN" src/to count affected files before - Run the script
- Run
grep -rn "OLD_PATTERN" src/to verify count is 0 - Run
find src/ -name "*.php" -exec php -l {} \;for syntax verification - Delete the migration script