Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spiral/core] Advanced Context Handling in Injector Implementations #1041

Merged
merged 7 commits into from
Jan 3, 2024

Conversation

roxblnfk
Copy link
Member

@roxblnfk roxblnfk commented Dec 22, 2023

Injectors

This pull request presents a significant update to the injector system, focusing on the createInjection method of the Spiral\Core\Container\InjectorInterface. The key enhancement lies in the augmented ability of the injector to handle context more effectively.

Previously, the createInjection method accepted two parameters: the ReflectionClass object of the requested class and a context, which was limited to being either a string or null. This approach, while functional, offered limited flexibility in dynamically resolving dependencies based on the calling context.

The updated createInjection method can now accept an extended range of context types including Stringable|string|null, mixed, or ReflectionParameter|string|null. This broadening allows the injector to receive more detailed contextual information, enhancing its capability to make more informed decisions about which implementation to provide.

Now you can do something like this:

<?php

declare(strict_types=1);

namespace App\Application;

final class SomeService
{
    public function __construct(
        #[DatabaseDriver(name: 'mysql')]
        public DatabaseInterface $database,

        #[DatabaseDriver(name: 'sqlite')]
        public DatabaseInterface $database1,
    ) {
    }
}

And example of injector

<?php

declare(strict_types=1);

namespace App\Application;

use Spiral\Core\Container\InjectorInterface;

final class DatabaseInjector implements InjectorInterface
{
    public function createInjection(\ReflectionClass $class, \ReflectionParameter|null|string $context = null): object
    {
        $driver = $context?->getAttributes(DatabaseDriver::class)[0]?->newInstance()?->name ?? 'mysql';

        return match ($driver) {
            'sqlite' => new Sqlite(),
            'mysql' => new Mysql(),
            default => throw new \InvalidArgumentException('Invalid database driver'),
        };
    }
}

Container Factory

Now the container factory accepts a Stringable context. This can be either reflection or any other Stringable.

$container->make('foo', new StringableContext());

Other container methods with an extended context will be available in a separate PR.

Q A
Bugfix?
Breaks BC?
New feature? ✔️
Issues Closes #888

@roxblnfk
Copy link
Member Author

2627 Segmentation fault (core dumped) vendor/bin/phpunit --coverage-clover=coverage.clover

😟

@roxblnfk roxblnfk mentioned this pull request Dec 28, 2023
Copy link

codecov bot commented Jan 2, 2024

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (dcb61c9) 89.51% compared to head (b80910f) 89.49%.
Report is 8 commits behind head on master.

Files Patch % Lines
src/Core/src/Internal/Factory.php 95.12% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1041      +/-   ##
============================================
- Coverage     89.51%   89.49%   -0.03%     
- Complexity     6234     6238       +4     
============================================
  Files           819      819              
  Lines         17555    17568      +13     
============================================
+ Hits          15714    15722       +8     
- Misses         1841     1846       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@roxblnfk roxblnfk marked this pull request as ready for review January 2, 2024 09:42
@butschster butschster self-assigned this Jan 3, 2024
@butschster butschster added this to the 3.12 milestone Jan 3, 2024
@butschster butschster merged commit 44459b0 into master Jan 3, 2024
13 of 14 checks passed
@roxblnfk roxblnfk deleted the core/extended-injector branch January 3, 2024 12:53
@butschster butschster changed the title Extended injector Advanced Context Handling in Injector Implementations Jan 5, 2024
@butschster butschster changed the title Advanced Context Handling in Injector Implementations [spiral/core] Advanced Context Handling in Injector Implementations Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[spiral/core] Injectors context
4 participants