Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Contracts/CanSetManyFeaturesForScopes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Laravel\Pennant\Contracts;

interface CanSetManyFeaturesForScopes
{
/**
* Set multiple feature flag values.
*
* @param list<array{ feature: string, scope: mixed, value: mixed }> $features
*/
public function setAll(array $features): void;
}
12 changes: 12 additions & 0 deletions src/Drivers/ArrayDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ public function set($feature, $scope, $value): void
$this->resolvedFeatureStates[$feature][Feature::serializeScope($scope)] = $value;
}

/**
* Set multiple feature flag values.
*
* @param list<array{ feature: string, scope: mixed, value: mixed }> $features
*/
public function setAll(array $features): void
{
foreach ($features as $featureData) {
$this->set($featureData['feature'], $featureData['scope'], $featureData['value']);
}
}

/**
* Set a feature flag's value for all scopes.
*
Expand Down
21 changes: 20 additions & 1 deletion src/Drivers/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Laravel\Pennant\Contracts\CanListStoredFeatures;
use Laravel\Pennant\Contracts\CanSetManyFeaturesForScopes;
use Laravel\Pennant\Contracts\Driver;
use Laravel\Pennant\Events\UnknownFeatureResolved;
use Laravel\Pennant\Feature;
use RuntimeException;
use stdClass;

class DatabaseDriver implements CanListStoredFeatures, Driver
class DatabaseDriver implements CanListStoredFeatures, CanSetManyFeaturesForScopes, Driver
{
/**
* The database connection.
Expand Down Expand Up @@ -281,6 +282,24 @@ public function set($feature, $scope, $value): void
], uniqueBy: ['name', 'scope'], update: ['value', static::UPDATED_AT]);
}

/**
* Set multiple feature flag values.
*
* @param list<array{ feature: string, scope: mixed, value: mixed }> $features
*/
public function setAll(array $features): void
{
$now = Carbon::now();

$this->newQuery()->upsert(array_map(fn (array $feature) => [
'name' => $feature['feature'],
'scope' => Feature::serializeScope($feature['scope']),
'value' => json_encode($feature['value'], flags: JSON_THROW_ON_ERROR),
static::CREATED_AT => $now,
static::UPDATED_AT => $now,
], $features), uniqueBy: ['name', 'scope'], update: ['value', static::UPDATED_AT]);
}

/**
* Set a feature flag's value for all scopes.
*
Expand Down
41 changes: 40 additions & 1 deletion src/Drivers/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Laravel\Pennant\Contracts\CanListStoredFeatures;
use Laravel\Pennant\Contracts\CanSetManyFeaturesForScopes;
use Laravel\Pennant\Contracts\DefinesFeaturesExternally;
use Laravel\Pennant\Contracts\Driver;
use Laravel\Pennant\Contracts\FeatureScopeable;
Expand Down Expand Up @@ -37,7 +38,7 @@
/**
* @mixin \Laravel\Pennant\PendingScopedFeatureInteraction
*/
class Decorator implements CanListStoredFeatures, Driver, HasFlushableCache
class Decorator implements CanListStoredFeatures, CanSetManyFeaturesForScopes, Driver, HasFlushableCache
{
use Macroable {
__call as macroCall;
Expand Down Expand Up @@ -487,6 +488,44 @@ public function set($feature, $scope, $value): void
Event::dispatch(new FeatureUpdated($feature, $scope, $value));
}

/**
* Set multiple feature flag values.
*
* @internal
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marked as internal so it doesn't show up on the Facade. This isn't meant to be called directly by the consuming application.

*
* @param list<array{ feature: string, scope: mixed, value: mixed }> $features
*/
public function setAll(array $features): void
{
$features = array_map(fn ($feature) => [
'feature' => $this->resolveFeature($feature['feature']),
'scope' => $this->resolveScope($feature['scope']),
'value' => $feature['value'],
], $features);

$updated = false;

if ($this->driver instanceof CanSetManyFeaturesForScopes) {
$this->driver->setAll($features);

$updated = true;
}

foreach ($features as $feature) {
if (! $updated) {
$this->driver->set($feature['feature'], $feature['scope'], $feature['value']);
}

$this->putInCache($feature['feature'], $feature['scope'], $feature['value']);

Event::dispatch(new FeatureUpdated(
$feature['feature'],
$feature['scope'],
$feature['value'],
));
}
}

/**
* Activate the feature for everyone.
*
Expand Down
14 changes: 10 additions & 4 deletions src/PendingScopedFeatureInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@ public function unless($feature, $whenInactive, $whenActive = null)
*/
public function activate($feature, $value = true)
{
Collection::wrap($feature)
$features = Collection::wrap($feature)
->crossJoin($this->scope())
->each(fn ($bits) => $this->driver->set($bits[0], $bits[1], $value));
->map(fn ($bits) => ['feature' => $bits[0], 'scope' => $bits[1], 'value' => $value])
->all();

$this->driver->setAll($features);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->driver is our decorator which implements the new contract, so calling setAll here is safe.

}

/**
Expand All @@ -262,9 +265,12 @@ public function activate($feature, $value = true)
*/
public function deactivate($feature)
{
Collection::wrap($feature)
$features = Collection::wrap($feature)
->crossJoin($this->scope())
->each(fn ($bits) => $this->driver->set($bits[0], $bits[1], false));
->map(fn ($bits) => ['feature' => $bits[0], 'scope' => $bits[1], 'value' => false])
->all();

$this->driver->setAll($features);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->driver is our decorator which implements the new contract, so calling setAll here is safe.

}

/**
Expand Down
90 changes: 90 additions & 0 deletions tests/Feature/ArrayDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,96 @@ public function test_it_can_handles_float_scopes_correctly()
FloatScopeFeature::class => false,
], Feature::for(10.00)->all());
}

public function test_it_dispatches_events_when_activating_multiple_features_and_scopes()
{
Event::fake([FeatureUpdated::class]);

$first = new User(['id' => 1]);
$second = new User(['id' => 2]);

Feature::for([$first, $second])->activate(['foo', 'bar']);

Event::assertDispatchedTimes(FeatureUpdated::class, 4);

$events = [];
Event::assertDispatched(function (FeatureUpdated $event) use (&$events) {
$events[] = [
'feature' => $event->feature,
'scope' => $event->scope,
'value' => $event->value,
];

return true;
});

$this->assertCount(4, $events);
$this->assertContains([
'feature' => 'foo',
'scope' => $first,
'value' => true,
], $events);
$this->assertContains([
'feature' => 'foo',
'scope' => $second,
'value' => true,
], $events);
$this->assertContains([
'feature' => 'bar',
'scope' => $first,
'value' => true,
], $events);
$this->assertContains([
'feature' => 'bar',
'scope' => $second,
'value' => true,
], $events);
}

public function test_it_dispatches_events_when_deactivating_multiple_features_and_scopes()
{
Event::fake([FeatureUpdated::class]);

$first = new User(['id' => 1]);
$second = new User(['id' => 2]);

Feature::for([$first, $second])->deactivate(['foo', 'bar']);

Event::assertDispatchedTimes(FeatureUpdated::class, 4);

$events = [];
Event::assertDispatched(function (FeatureUpdated $event) use (&$events) {
$events[] = [
'feature' => $event->feature,
'scope' => $event->scope,
'value' => $event->value,
];

return true;
});

$this->assertCount(4, $events);
$this->assertContains([
'feature' => 'foo',
'scope' => $first,
'value' => false,
], $events);
$this->assertContains([
'feature' => 'foo',
'scope' => $second,
'value' => false,
], $events);
$this->assertContains([
'feature' => 'bar',
'scope' => $first,
'value' => false,
], $events);
$this->assertContains([
'feature' => 'bar',
'scope' => $second,
'value' => false,
], $events);
}
}

class MyFeature
Expand Down
Loading