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

Implementation of scopes #80

Merged
merged 9 commits into from
Aug 30, 2024
Merged

Implementation of scopes #80

merged 9 commits into from
Aug 30, 2024

Conversation

msmakouz
Copy link
Member

@msmakouz msmakouz commented Feb 28, 2024

What was changed

Added a temporal scope to the dispatcher. Added opening of the temporal.activity scope when creating an activity. Now it is possible to define a scope for an activity and register dependencies in the container so that they are only available within the temporal.activity scope.

For example, let's create an activity and specify the temporal.activity scope for it:

use Psr\Container\ContainerInterface;
use Spiral\Core\Attribute\Scope;
use Spiral\Framework\Spiral;
use Temporal\Activity\ActivityInterface;
use Temporal\Activity\ActivityMethod;

#[Scope(Spiral::TemporalActivity)] // <------------
#[ActivityInterface]
class PingWebsiteActivity
{
    public function __construct(
        private readonly ContainerInterface $container,
    ) {
        trap($this->container); // container here must be in the temporal.activity scope
    }

    #[ActivityMethod(name: 'ping')]
    public function ping(string $domain): bool
    {
        // ...
    }
}

After that, we will define the binding of the activity within the temporal.activity scope:

use App\Endpoint\Temporal\Activity\PingWebsiteActivity;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Core\BinderInterface;
use Spiral\Framework\Spiral;

final class AppBootloader extends Bootloader
{
    public function __construct(
        private readonly BinderInterface $binder,
    ) {
    }

    public function defineBindings(): array
    {
        $this->binder
            ->getBinder(Spiral::TemporalActivity)
            ->bind(PingWebsiteActivity::class, PingWebsiteActivity::class);

        return [];
    }
}

As a result, the activity will receive dependencies that are defined within the temporal.activity scope.

111

@msmakouz msmakouz marked this pull request as draft February 28, 2024 14:31
@msmakouz msmakouz self-assigned this Feb 28, 2024
@msmakouz msmakouz added the enhancement New feature or request label Feb 28, 2024
Copy link

codecov bot commented Feb 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.71%. Comparing base (38be446) to head (4b81bbc).

Additional details and impacted files
@@             Coverage Diff              @@
##                3.x      #80      +/-   ##
============================================
+ Coverage     99.14%   99.71%   +0.56%     
- Complexity      103      104       +1     
============================================
  Files            15       15              
  Lines           352      354       +2     
============================================
+ Hits            349      353       +4     
+ Misses            3        1       -2     

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

@msmakouz msmakouz requested a review from butschster March 1, 2024 09:51
@msmakouz msmakouz marked this pull request as ready for review March 1, 2024 09:52
@roxblnfk roxblnfk merged commit 815a7d4 into 3.x Aug 30, 2024
4 checks passed
@roxblnfk roxblnfk deleted the feature/scope branch August 30, 2024 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants