Skip to content

Commit

Permalink
Merge pull request #24 from Sergej-Tihonov/5.x
Browse files Browse the repository at this point in the history
changed resolve of the muted class to allow constructor injection
  • Loading branch information
monooso authored Jun 6, 2022
2 parents 04425a7 + adfb5e2 commit c1b77d5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 36 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [[X.X.X] - 2022-05-26](https://github.com/monooso/unobserve/releases/tag/vX.X.X)
### Changed
- changed resolve of the muted class to allow constructor injection

### Added
- PHP 8 conform type hinting

## [[5.0.1] - 2022-05-26](https://github.com/monooso/unobserve/releases/tag/v5.0.1)
### Changed
- Improved documentation, courtesy of [Sergej Tihonov](https://github.com/Sergej-Tihonov)
Expand Down
11 changes: 6 additions & 5 deletions src/CanMute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

trait CanMute
{
public static function mute($events = null)
/** @param string|string[] $events */
public static function mute(string|array $events = null): void
{
$instance = new static();
$instance = resolve(static::class);
resolve(ProxyManager::class)->register($instance, static::normalizeEvents($events));
}

protected static function normalizeEvents($events): array
protected static function normalizeEvents(null|string|array $events): array
{
if (is_null($events)) {
$events = ['*'];
Expand All @@ -23,8 +24,8 @@ protected static function normalizeEvents($events): array
return $events;
}

public static function unmute()
public static function unmute(): void
{
resolve(ProxyManager::class)->unregister(new static);
resolve(ProxyManager::class)->unregister(static::class);
}
}
8 changes: 3 additions & 5 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

class Proxy
{
/** @var object */
private $target;
private object $target;

/** @var array */
private $events;
private array $events;

public function __construct($target, array $events = [])
public function __construct(object $target, array $events = [])
{
$this->target = $target;
$this->events = $events;
Expand Down
14 changes: 5 additions & 9 deletions src/ProxyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@

namespace Monooso\Unobserve;

use Illuminate\Contracts\Container\Container;
use Illuminate\Container\Container;

class ProxyManager
{
/** @var Container */
private $app;

public function __construct(Container $app)
public function __construct(private Container $app)
{
$this->app = $app;
}

public function register($target, array $events)
public function register(object $target, array $events): void
{
$proxy = $this->app->make(Proxy::class, ['target' => $target, 'events' => $events]);

$this->app->instance(get_class($target), $proxy);
}

public function unregister($target)
public function unregister(string $targetClass): void
{
$this->app->instance(get_class($target), $target);
$this->app->forgetInstance($targetClass);
}
}
4 changes: 2 additions & 2 deletions src/UnobserveServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class UnobserveServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function register()
public function register(): void
{
parent::register();

Expand All @@ -20,7 +20,7 @@ public function register()
});
}

public function provides()
public function provides(): array
{
return [ProxyManager::class, Proxy::class];
}
Expand Down
53 changes: 47 additions & 6 deletions tests/CanMuteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Monooso\Unobserve\Tests;

use Monooso\Unobserve\CanMute;
use Monooso\Unobserve\Proxy;
use Orchestra\Testbench\TestCase;

class CanMuteTest extends TestCase
{
/** @test */
public function it_mutes_an_array_of_events()
public function it_mutes_an_array_of_events(): void
{
CanMuteTarget::mute(['cloaked']);

Expand All @@ -19,7 +20,7 @@ public function it_mutes_an_array_of_events()
}

/** @test */
public function it_mutes_a_single_event()
public function it_mutes_a_single_event(): void
{
CanMuteTarget::mute('cloaked');

Expand All @@ -30,7 +31,7 @@ public function it_mutes_a_single_event()
}

/** @test */
public function it_mutes_all_events()
public function it_mutes_all_events(): void
{
CanMuteTarget::mute();

Expand All @@ -41,7 +42,7 @@ public function it_mutes_all_events()
}

/** @test */
public function it_unmutes_all_events()
public function it_unmutes_all_events(): void
{
CanMuteTarget::mute();
CanMuteTarget::unmute();
Expand All @@ -51,19 +52,59 @@ public function it_unmutes_all_events()
$this->assertSame('cloaked', $target->cloaked());
$this->assertSame('uncloaked', $target->uncloaked());
}

/** @test */
public function it_mutes_class_with_constructor_injection(): void
{
WithConstructorInjection::mute();

$target = resolve(WithConstructorInjection::class);

$this->assertInstanceOf(Proxy::class, $target);
}

/** @test */
public function it_resolves_to_proxy_on_mute(): void
{
CanMuteTarget::mute();

$target = resolve(CanMuteTarget::class);

$this->assertInstanceOf(Proxy::class, $target);
}

/** @test */
public function it_resolves_back_to_class_on_unmute(): void
{
CanMuteTarget::mute();
CanMuteTarget::unmute();

$target = resolve(CanMuteTarget::class);

$this->assertInstanceOf(CanMuteTarget::class, $target);
}
}

class CanMuteTarget
{
use CanMute;

public function cloaked()
public function cloaked(): string
{
return 'cloaked';
}

public function uncloaked()
public function uncloaked(): string
{
return 'uncloaked';
}
}

class WithConstructorInjection
{
use CanMute;

public function __construct(private CanMuteTarget $injection)
{
}
}
6 changes: 3 additions & 3 deletions tests/ProxyManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ProxyManagerTest extends TestCase
{
/** @test */
public function it_registers_a_proxy_with_the_service_container()
public function it_registers_a_proxy_with_the_service_container(): void
{
$app = $this->resolveApplication();

Expand All @@ -20,12 +20,12 @@ public function it_registers_a_proxy_with_the_service_container()
}

/** @test */
public function it_unregisters_a_proxy()
public function it_unregisters_a_proxy(): void
{
$app = $this->resolveApplication();

$manager = new ProxyManager($app);
$manager->unregister(new ProxyManagerTarget);
$manager->unregister(ProxyManagerTarget::class);

$this->assertInstanceOf(ProxyManagerTarget::class, $app->make(ProxyManagerTarget::class));
}
Expand Down
12 changes: 6 additions & 6 deletions tests/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ProxyTest extends TestCase
{
/** @test */
public function it_swallows_cloaked_events()
public function it_swallows_cloaked_events(): void
{
$target = new ProxyTarget;

Expand All @@ -19,7 +19,7 @@ public function it_swallows_cloaked_events()
}

/** @test */
public function it_passes_uncloaked_events_to_the_observer()
public function it_passes_uncloaked_events_to_the_observer(): void
{
$target = new ProxyTarget;

Expand All @@ -29,7 +29,7 @@ public function it_passes_uncloaked_events_to_the_observer()
}

/** @test */
public function it_swallows_all_events()
public function it_swallows_all_events(): void
{
$target = new ProxyTarget;
$proxy = new Proxy($target, ['*']);
Expand All @@ -39,7 +39,7 @@ public function it_swallows_all_events()
}

/** @test */
public function it_raises_an_exception_for_unknown_methods()
public function it_raises_an_exception_for_unknown_methods(): void
{
$target = new ProxyTarget;

Expand All @@ -51,12 +51,12 @@ public function it_raises_an_exception_for_unknown_methods()

class ProxyTarget
{
public function uncloaked()
public function uncloaked(): string
{
return 'uncloaked';
}

public function cloaked()
public function cloaked(): string
{
return 'cloaked';
}
Expand Down

0 comments on commit c1b77d5

Please sign in to comment.