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

[1.x] Respect configuration changes #81

Merged
merged 2 commits into from
Jan 10, 2024
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
33 changes: 28 additions & 5 deletions src/Drivers/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Laravel\Pennant\Drivers;

use Illuminate\Config\Repository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Laravel\Pennant\Contracts\Driver;
Expand All @@ -16,17 +18,24 @@ class DatabaseDriver implements Driver
/**
* The database connection.
*
* @var \Illuminate\Database\Connection
* @var \Illuminate\Database\DatabaseManager
*/
protected $db;

/**
* The user configuration.
*
* @var array{connection?: string|null, table?: string|null}
* @var \Illuminate\Config\Repository
*/
protected $config;

/**
* The store's name.
*
* @var string
*/
protected $name;

/**
* The event dispatcher.
*
Expand All @@ -51,15 +60,15 @@ class DatabaseDriver implements Driver
/**
* Create a new driver instance.
*
* @param array{connection?: string|null, table?: string|null} $config
* @param array<string, (callable(mixed $scope): mixed)> $featureStateResolvers
* @return void
*/
public function __construct(Connection $db, Dispatcher $events, $config, $featureStateResolvers)
public function __construct(DatabaseManager $db, Dispatcher $events, Repository $config, string $name, $featureStateResolvers)
{
$this->db = $db;
$this->events = $events;
$this->config = $config;
$this->name = $name;
$this->featureStateResolvers = $featureStateResolvers;

$this->unknownFeatureValue = new stdClass;
Expand Down Expand Up @@ -313,6 +322,20 @@ public function purge($features): void
*/
protected function newQuery()
{
return $this->db->table($this->config['table'] ?? 'features');
return $this->connection()->table(
$this->config->get("pennant.stores.{$this->name}.table") ?? 'features'
);
}

/**
* The database connection.
*
* @return \Illuminate\Database\Connection
*/
protected function connection()
{
return $this->db->connection(
$this->config->get("pennant.stores.{$this->name}.connection") ?? null
);
}
}
9 changes: 5 additions & 4 deletions src/FeatureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function resolve($name)
$driverMethod = 'create'.ucfirst($config['driver']).'Driver';

if (method_exists($this, $driverMethod)) {
$driver = $this->{$driverMethod}($config);
$driver = $this->{$driverMethod}($config, $name);
} else {
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
}
Expand Down Expand Up @@ -163,12 +163,13 @@ public function createArrayDriver()
*
* @return \Laravel\Pennant\Drivers\DatabaseDriver
*/
public function createDatabaseDriver(array $config)
public function createDatabaseDriver(array $config, string $name)
{
return new DatabaseDriver(
$this->container['db']->connection($config['connection'] ?? null),
$this->container['db'],
$this->container['events'],
$config,
$this->container['config'],
$name,
[]
);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/DatabaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use InvalidArgumentException;
use Laravel\Pennant\Contracts\FeatureScopeable;
use Laravel\Pennant\Events\AllFeaturesPurged;
use Laravel\Pennant\Events\DynamicallyRegisteringFeatureClass;
Expand Down Expand Up @@ -1261,6 +1262,20 @@ public function test_it_can_use_eloquent_morph_map_for_scope_serialization()
'value' => 'true',
]);
}

public function test_it_respects_updated_connection_configuration()
{
Feature::flushCache();
Config::set('pennant.stores.database.connection', null);
Feature::store('database')->active('feature-name');

Feature::flushCache();
Config::set('pennant.stores.database.connection', 'xxxx');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Database connection [xxxx] not configured.');
Feature::store('database')->active('feature-name');
}
}

class UnregisteredFeature
Expand Down