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

feat: feature context interaction #3

Merged
merged 2 commits into from
Mar 1, 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
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
}
],
"autoload": {
"files": [
"./helpers.php"
],
"psr-4": {
"YouCanShop\\Foggle\\": "src/"
}
Expand Down
4 changes: 1 addition & 3 deletions src/helpers.php → helpers.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php

use Illuminate\Contracts\Container\BindingResolutionException;
use YouCanShop\Foggle\Foggle;

if (!function_exists('foggle')) {
/**
* @param string|null $feature
*
* @return mixed
* @throws BindingResolutionException
* @return Foggle|mixed
*/
function foggle(string $feature = null)
{
Expand Down
5 changes: 0 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
20 changes: 15 additions & 5 deletions src/Drivers/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace YouCanShop\Foggle\Drivers;

use Closure;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Str;
use Symfony\Component\Finder\Finder;
use YouCanShop\Foggle\Contracts\Driver;
use YouCanShop\Foggle\FeatureInteraction;
use YouCanShop\Foggle\Lazily;

/**
* @mixin FeatureInteraction
*/
class Decorator implements Driver
{
private string $name;
Expand All @@ -28,9 +31,6 @@ public function __construct(
$this->container = $container;
}

/**
* @throws BindingResolutionException
*/
public function discover(string $namespace = 'App\\Features', ?string $path = null): void
{
$namespace = Str::finish($namespace, '\\');
Expand All @@ -49,7 +49,6 @@ public function discover(string $namespace = 'App\\Features', ?string $path = nu
* @param callable|null $resolver
*
* @return void
* @throws BindingResolutionException
*/
public function define(string $name, callable $resolver = null): void
{
Expand Down Expand Up @@ -96,4 +95,15 @@ public function defined(): array
{
return $this->driver->defined();
}

/**
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public function __call($name, $arguments)
{
return (new FeatureInteraction($this))->$name(...$arguments);
}
}
58 changes: 58 additions & 0 deletions src/FeatureInteraction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace YouCanShop\Foggle;

use Illuminate\Support\Collection;
use YouCanShop\Foggle\Drivers\Decorator;

class FeatureInteraction
{
protected Decorator $driver;
protected array $context = [];

public function __construct(Decorator $driver)
{
$this->driver = $driver;
}

/**
* @param mixed $scope
*/
public function for($scope): self
{
$this->context = array_merge(
$this->context,
Collection::wrap($scope)->all()
);

return $this;
}

/**
* @return mixed
*/
public function value(string $feature)
{
return $this->driver->get($feature, $this->context()[0]);
}

public function active(string $feature): bool
{
return Collection::make($feature)
->crossJoin($this->context())
->every(fn($args) => $this->driver->get(...$args) !== false);
}

public function inactive(string $feature): bool
{
return !$this->active($feature);
}

/**
* @return array
*/
protected function context(): array
{
return $this->context ?: [null];
}
}
3 changes: 1 addition & 2 deletions src/Foggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ protected function resolve(string $name): Decorator

return new Decorator($name, $driver, $this->container);
}



protected function getDriverConfig(string $name): ?array
{
return $this->container['config']["foggle.stores.$name"];
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/FoggleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@

expect($foggle->get('always-true', null))->toBeTrue();
});

it('resolves with a context', function () {
foggle()->discover(
'Workbench\\App\\Features',
workbench_path('app/Features')
);

expect(foggle()->for(true)->active('resolves-to-context'))->toBeTrue()
->and(foggle()->for(false)->active('resolves-to-context'))->toBeFalse();
});
13 changes: 13 additions & 0 deletions workbench/app/Features/ResolvesToContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Workbench\App\Features;

class ResolvesToContext
{
public string $name = 'resolves-to-context';

public function resolve(bool $context): bool
{
return $context;
}
}
Loading