feat: framework refactor + decouple from Hyperf#349
feat: framework refactor + decouple from Hyperf#349binaryfire wants to merge 1127 commits intohypervel:0.4from
Conversation
|
@albertcht To illustrate how much easier it will be to keep Hypervel in sync with Laravel after this refactor, I asked Claude how long it would take to merge laravel/framework#58461 (as an example) into this branch. This is what it said: So just 5-10 minutes of work with the help of AI tooling! Merging individual PRs is inefficient - merging releases would be better. I can set up a Discord channel where new releases are automatically posted via webhooks. Maybe someone in your team can be responsible for monitoring that channel's notifications and merging updates ever week or 2? I'll only be 1-2 hours of work once the codebases are 1:1. We should be diligent about staying on top of merging updates. Otherwise we'll end up in in the same as Hyperf - i.e. the codebase being completely out of date with the current Laravel API. |
|
Hi @binaryfire , Thank you for submitting this PR and for the detailed explanation of the refactor. After reading through it, I strongly agree that this is the best long-term direction for Hypervel. Refactoring Hypervel into a standalone framework and striving for 1:1 parity with Laravel will indeed solve the current issues regarding deep coupling with Hyperf, maintenance difficulties, outdated versions, and inefficient AI assistance. While this is a difficult step, it is absolutely necessary for the future of the project. Regarding this refactor and the planning for the v0.4 branch, I have a few thoughts to verify with you:
Thank you again for dedicating so much effort to driving this forward; this is a massive undertaking. Let's move forward gradually on this branch with ongoing Code Reviews. |
|
Hi @albertcht Thanks for the detailed response! I'm glad we're aligned on the direction. Let me address each point:
Let me know your thoughts! |
8cec3bf to
bfffa6f
Compare
|
Hi @albertcht. The All the Laravel tests have been ported over and are passing (the unit tests, as well as the integration tests for MySQL, MariaDB, Postgres and SQLite). I've implemented Context-based coroutine safety, static caching for performance and modernised all the types. The code passes PHPStan level 5. Let me know if there's anything I've missed, if you have any ideas or you have any questions. The other packages aren't ready for review yet - many of them are mid-migration and contain temporary code. So please don't review the others yet :) I'll let you know when each one is ready. A few points:
|
|
|
||
| class FreshCommand extends Command | ||
| { | ||
| use ConfirmableTrait; |
There was a problem hiding this comment.
@albertcht Done. I'll be re-working these commands once I've ported illuminate/console, but no harm in adding it now too.
|
|
||
| class RefreshCommand extends Command | ||
| { | ||
| use ConfirmableTrait; |
|
|
||
| class ResetCommand extends BaseCommand | ||
| { | ||
| use ConfirmableTrait; |
|
|
||
| class RollbackCommand extends BaseCommand | ||
| { | ||
| use ConfirmableTrait; |
| @@ -6,9 +6,9 @@ | |||
|
|
|||
| use Hyperf\Command\Concerns\Prohibitable; | |||
There was a problem hiding this comment.
change to Hypervel's Prohibitable here?
80f3ef2 to
94c115e
Compare
Add Hypervel\Support\ClassInvoker for accessing protected/private members via reflection. Modernized with strict parameter and return types. Refactor HeartbeatConnectionTest to use it instead of raw ReflectionProperty calls.
LogManager switches to concrete class for typed property. HyperfLogFactory switches to 'config' string alias.
Remove contract import, bind under 'config' in raw Container,
switch all get(Repository::class) to get('config').
MailManager and MarkdownFactory switch to concrete class for typed properties. Mailable drops import and uses 'config' string alias.
All mail tests use Testbench TestCase - drop contract import and
switch $this->app->get(Repository::class) to $this->app->get('config').
MailChannel drops import and uses 'config' string alias. SlackWebApiChannel switches to concrete class for typed property.
Raw Container - bind under 'config', remove contract import.
Drop config contract import, use 'config' string alias for inline resolution. Cache Repository import is unaffected.
QueueManager, MonitorCommand, ListenCommand, WorkCommand switch to concrete class for typed properties. ClearCommand, FailedJobProviderFactory, RateLimitedWithRedis, ThrottlesExceptionsWithRedis drop import and use 'config' string alias.
Console commands switch to concrete class for DI-injected type hints. All other files drop import and use 'config' string alias.
Switcher switches to concrete class for typed property. All other files drop import and use 'config' string alias.
Remove contract imports and replace ::class resolution with 'config' string alias across all 20 telescope test files including FeatureTestCase, TelescopeTest, AvatarTest, and all watcher tests.
Replace contract import and ::class resolution with 'config' string alias in TranslatorFactory.
Update HasherTest, HttpClientTest, SocialiteManagerTest, Horizon IntegrationTestCase and worker.php to use 'config' string alias instead of contract ::class resolution.
Update PoolConnectionManagementTest, SQLiteFilePoolingTest, InMemorySqliteSharedPdoTest, and EventConnectionEstablishedTest.
…lias Update EvalWithShaCacheIntegrationTest, TransformIntegrationTest, RedisConnectionIntegrationTest, RedisSubscribeIntegrationTest, SubscriberIntegrationTest, SafeScanIntegrationTest, RedisProxyIntegrationTest, and RedisCacheIntegrationTestCase.
Update MeilisearchIndexSettingsIntegrationTest, MeilisearchSoftDeleteIntegrationTest, TypesenseSoftDeleteIntegrationTest, and TypesenseConfigIntegrationTest.
Hi @albertcht. This isn't ready yet but I'm opening it as a draft so we can begin discussions and code reviews. The goal of this PR is to refactor Hypervel to be a fully standalone framework that is as close to 1:1 parity with Laravel as possible.
Why one large PR
Sorry about the size of this PR. I tried spreading things across multiple branches but it made my work a lot more difficult. This is effectively a framework refactor - the database package is tightly coupled to many other packages (collections, pagination, pool) as well as several support classes, so all these things need to be updated together. Splitting it across branches would mean each branch needs multiple temporary workarounds + would have failing tests until merged together, making review and CI impractical.
A single large, reviewable PR is less risky than a stack of dependent branches that can't pass CI independently.
Reasons for the refactor
1. Outdated Hyperf packages
It's been difficult to migrate existing Laravel projects to Hypervel because Hyperf's database packages are quite outdated. There are almost 100 missing methods, missing traits, it doesn't support nested transactions, there are old Laravel bugs which haven't been fixed (eg. JSON indices aren't handled correctly), coroutine safety issues (eg. model
unguard(),withoutTouching()). Other packages like pagination, collections and support are outdated too.Stringablewas missing a bunch of methods and traits, for example. There are just too many to PR to Hyperf at this point.2. Faster framework development
We need to be able to move quickly and waiting for Hyperf maintainers to merge things adds a lot of friction to framework development. Decoupling means we don't need to work around things like PHP 8.4 compatibility while waiting for it to be added upstream. Hyperf's testing package uses PHPUnit 10 so we can't update to PHPUnit 13 (and Pest 4 in the skeleton) when it releases in a couple of weeks. v13 has the fix that allows
RunTestsInCoroutineto work with newer PHPUnit versions. There are lots of examples like this.3. Parity with Laravel
We need to avoid the same drift from Laravel that's happened with Hyperf since 2019. If we're not proactive with regularly merging Laravel updates every week we'll end up in the same situation. Having a 1:1 directory and code structure to Laravel whenever possible will make this much easier. Especially when using AI tools.
Most importantly, we need to make it easier for Laravel developers to use and contribute to the framework. That means following the same APIs and directory structures and only modifying code when there's a good reason to (coroutine safety, performance, type modernisation etc).
Right now the Hypervel codebase is confusing for both Laravel developers and AI tools:
hypervel/contractspackage, the Hyperf database code is split across 3 packages, the Hyperf pagination package ishyperf/paginatorand nothyperf/pagination)static::registerCallback('creating')vsstatic::creating())ConfigProviderand LaravelServiceProviderpatterns across different packages is confusing for anyone who doesn't know HyperfThis makes it difficult for Laravel developers to port over apps and to contribute to the framework.
4. AI
The above issues mean that AI needs a lot of guidance to understand the Hypervel codebase and generate Hypervel boilerplate. A few examples:
hypervel/contractsfor contracts) and then have to spend a lot of time grepping for things to find them.And so on... This greatly limits the effectiveness of building Hypervel apps with AI. Unfortunately MCP docs servers and CLAUDE.md rules don't solve all these problems - LLMs aren't great at following instructions well and the sheer volume of Laravel data they've trained on means they always default to Laravel-style code. The only solution is 1:1 parity. Small improvements such as adding native type hints are fine - models can solve that kind of thing quickly from exception messages.
What changed so far
New packages
illuminate/databaseportilluminate/collectionsportilluminate/paginationportilluminate/contracts)hyperf/pool)Macroableto a separate package for Laravel parityRemoved Hyperf dependencies so far
Database package
The big task was porting the database package, making it coroutine safe, implementing performance improvements like static caching and modernising the types.
whereLike,whereNot,groupLimit,rawValue,soleValue, JSON operations, etc.Collections package
Contracts package
Support package
hyperf/tappable,hyperf/stringable,hyperf/macroable,hyperf/codecdependenciesStr,Envand helper classes from LaravelHypervel\Contextwrappers (will be portinghyperf/contextsoon)Number::useCurrency()wasn't actually setting the currency)Coroutine safety
withoutEvents(),withoutBroadcasting(),withoutTouching()now use Context instead of static propertiesUnsetContextInTaskWorkerListenerto clear database context in task workersConnection::resetForPool()to prevent state leaks between coroutinesDatabaseTransactionsManagercoroutine-safeBenefits
Testing status so far
What's left (WIP)
The refactor process
Hyperf's Swoole packages like
pool,coroutine,contextandhttp-serverhaven't changed in many years so porting these is straightforward. A lot of the code can be simplified since we don't need SWOW support. And we can still support the ecosystem by contributing any improvements we make back to Hyperf in separate PRs.Eventually I'll refactor the bigger pieces like the container (contextual binding would be nice!) and the config system (completely drop
ConfigProviderand move entirely to service providers). But those will be future PRs. For now the main refactors are the database layer, collections and support classes + the simple Hyperf packages. I'll just port the container and config packages as-is for now.Let me know if you have any feedback, questions or suggestions. I'm happy to make any changes you want. I suggest we just work through this gradually, as an ongoing task over the next month or so. I'll continue working in this branch and ping you each time I add something new.