Skip to content

Commit bfa7a9d

Browse files
committed
Merge branch 'dev'
2 parents 00da0cf + 47904de commit bfa7a9d

16 files changed

+200
-105
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ class GetString extends \QuixLabs\LaravelHookSystem\Hook
4343
In the `register` method of your ServiceProvider:
4444

4545
```php
46-
use QuixLabs\LaravelHookSystem\Facades\HookManager;
4746
use Workbench\App\Hooks\GetString;
4847

4948
class YourProvider{
5049
public function register()
5150
{
52-
HookManager::registerHook(GetString::class);
51+
\QuixLabs\LaravelHookSystem\HookRegistry::registerHook(GetString::class);
5352
}
5453
}
5554

@@ -65,7 +64,7 @@ class YourController
6564
public function index()
6665
{
6766
$string = "";
68-
\Workbench\App\Hooks\GetString::send($string);
67+
\Workbench\App\Hooks\GetString::send($string);
6968
return $string;
7069
}
7170
}
@@ -111,12 +110,10 @@ class AppendRandomString
111110
In the `boot` method of your ServiceProvider:
112111

113112
```php
114-
use QuixLabs\LaravelHookSystem\Facades\HookManager;
115-
116113
class YourProvider{
117114
public function boot()
118115
{
119-
HookManager::registerInterceptor(\App\Interceptors\AppendRandomString::class);
116+
\QuixLabs\LaravelHookSystem\HookRegistry::registerInterceptor(\App\Interceptors\AppendRandomString::class);
120117
}
121118
}
122119
```

src/Facades/HookManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @method static array<Hook|string> getHooks()
1212
* @method static array<int,callable[]> getInterceptorsForHook(Hook|string $hook)
1313
* @method static string getCacheFilepath()
14-
* @method static void loadCache()
14+
* @method static void reloadCache()
1515
* @method static void createCache()
1616
* @method static void clearCache()
1717
* @method static bool isCached()

src/HookManager.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ class HookManager
1313
*/
1414
protected array $hooks = [];
1515

16+
/**
17+
* @var true
18+
*/
19+
private bool $cached = false;
20+
21+
/**
22+
* @throws \Exception
23+
*/
1624
public function __construct()
1725
{
18-
if ($this->isCached()) {
19-
$this->loadCache();
20-
}
26+
$this->reloadCache();
2127
}
2228

2329
public function registerHook(Hook|string $hook): void
@@ -92,12 +98,20 @@ public function getCacheFilepath(): string
9298
return base_path('bootstrap/cache/hooks.php');
9399
}
94100

95-
public function loadCache(): void
101+
public function reloadCache(): void
96102
{
103+
if (! File::exists($this->getCacheFilepath())) {
104+
$this->cached = false;
105+
106+
return;
107+
}
108+
97109
try {
98110
$this->hooks = require $this->getCacheFilepath();
111+
$this->cached = true;
99112
} catch (\Throwable $e) {
100-
throw new \RuntimeException('Unable to load hooks cache: '.$e->getMessage());
113+
$this->clearCache();
114+
$this->cached = false;
101115
}
102116
}
103117

@@ -112,10 +126,11 @@ public function clearCache(): void
112126
if (File::exists($this->getCacheFilepath())) {
113127
File::delete($this->getCacheFilepath());
114128
}
129+
$this->cached = false;
115130
}
116131

117132
public function isCached(): bool
118133
{
119-
return File::exists($this->getCacheFilepath());
134+
return $this->cached;
120135
}
121136
}

src/HookRegistry.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace QuixLabs\LaravelHookSystem;
4+
5+
use QuixLabs\LaravelHookSystem\Facades\HookManager;
6+
7+
class HookRegistry
8+
{
9+
protected static array $hooks = [];
10+
11+
protected static array $interceptors = [];
12+
13+
public static function registerHook($hook): void
14+
{
15+
if (static::booted() === true && ! HookManager::isCached()) {
16+
Hookmanager::registerHook($hook);
17+
18+
return;
19+
}
20+
static::$hooks[] = $hook;
21+
}
22+
23+
public static function registerInterceptor($interceptor): void
24+
{
25+
if (static::booted() === true && ! HookManager::isCached()) {
26+
Hookmanager::registerInterceptor($interceptor);
27+
28+
return;
29+
}
30+
static::$interceptors[] = $interceptor;
31+
}
32+
33+
public static function getHooks(): array
34+
{
35+
return static::$hooks;
36+
}
37+
38+
public static function getInterceptors(): array
39+
{
40+
return static::$interceptors;
41+
}
42+
43+
public static function clear(): void
44+
{
45+
static::$hooks = [];
46+
static::$interceptors = [];
47+
}
48+
49+
public static function booted(): bool
50+
{
51+
return app()->resolved('hooks_manager');
52+
}
53+
}

src/Providers/ServiceProvider.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,35 @@
22

33
namespace QuixLabs\LaravelHookSystem\Providers;
44

5-
use Illuminate\Foundation\Application;
65
use Illuminate\Foundation\Console\AboutCommand;
76
use QuixLabs\LaravelHookSystem\Console\Commands\HooksCacheCommand;
87
use QuixLabs\LaravelHookSystem\Console\Commands\HooksClearCommand;
98
use QuixLabs\LaravelHookSystem\Console\Commands\HooksStatusCommand;
109
use QuixLabs\LaravelHookSystem\Facades\HookManager as HookManagerFacade;
1110
use QuixLabs\LaravelHookSystem\HookManager;
11+
use QuixLabs\LaravelHookSystem\HookRegistry;
1212
use QuixLabs\LaravelHookSystem\Hooks\GetHooksTable;
1313

1414
class ServiceProvider extends \Illuminate\Support\ServiceProvider
1515
{
1616
public function register(): void
1717
{
18-
$this->_registerHookManager();
18+
$this->app->singleton('hooks_manager', function () {
19+
return new HookManager();
20+
});
1921
$this->_registerCommands();
2022
$this->_registerHooks();
2123
}
2224

2325
public function boot(): void
2426
{
27+
$this->_bootHooksRegistry();
2528
$this->_appendInformationToAboutCommand();
2629
}
2730

28-
private function _registerHookManager(): void
29-
{
30-
$this->app->singleton('hooks_manager', function (Application $app) {
31-
return new HookManager();
32-
});
33-
}
34-
3531
private function _registerHooks(): void
3632
{
37-
if (! HookManagerFacade::isCached()) {
38-
HookManagerFacade::registerHook(GetHooksTable::class);
39-
}
33+
HookRegistry::registerHook(GetHooksTable::class);
4034
}
4135

4236
private function _registerCommands(): void
@@ -57,8 +51,21 @@ private function _appendInformationToAboutCommand(): void
5751
}
5852
AboutCommand::add('Cache', fn () => [
5953
'Hooks' => HookManagerFacade::isCached()
60-
? '<fg=green;options=bold>CACHED</>'
61-
: '<fg=yellow;options=bold>NOT CACHED</>',
54+
? '<fg=green;options=bold>CACHED</>'
55+
: '<fg=yellow;options=bold>NOT CACHED</>',
6256
]);
6357
}
58+
59+
private function _bootHooksRegistry(): void
60+
{
61+
if (! HookManagerFacade::isCached()) {
62+
foreach (HookRegistry::getHooks() as $hook) {
63+
HookManagerFacade::registerHook($hook);
64+
}
65+
foreach (HookRegistry::getInterceptors() as $interceptor) {
66+
HookManagerFacade::registerInterceptor($interceptor);
67+
}
68+
}
69+
HookRegistry::clear();
70+
}
6471
}
File renamed without changes.
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
<?php
22

33
use QuixLabs\LaravelHookSystem\Facades\HookManager;
4+
use QuixLabs\LaravelHookSystem\HookRegistry;
45
use Workbench\App\Hooks\GetString;
56
use Workbench\App\Interceptors\AppendRandomString;
67

7-
afterEach(function () {
8-
HookManager::clearCache();
9-
$this->app->forgetInstance('hooks_manager');
10-
HookManager::clearResolvedInstances();
11-
});
12-
138
test('Can generate cache using facade', function () {
149
HookManager::createCache();
10+
HookManager::reloadCache();
1511
expect(HookManager::isCached())->toBeTrue();
1612
});
1713

1814
test('Can clear cache using facade', function () {
1915
HookManager::createCache();
16+
HookManager::reloadCache();
2017
expect(HookManager::isCached())->toBeTrue();
2118
HookManager::clearCache();
19+
HookManager::reloadCache();
20+
2221
expect(HookManager::isCached())->toBeFalse();
2322
});
2423

2524
test('Remove hook when has no interceptors', function () {
26-
HookManager::registerHook(GetString::class);
25+
HookRegistry::registerHook(GetString::class);
2726
HookManager::createCache();
28-
HookManager::loadCache();
27+
HookManager::reloadCache();
2928
expect(HookManager::getHooks())->not->toContain(GetString::class);
3029
});
3130

3231
test('Can cache single interceptor', function () {
33-
HookManager::registerHook(GetString::class);
34-
HookManager::registerInterceptor(AppendRandomString::class);
32+
HookRegistry::registerHook(GetString::class);
33+
HookRegistry::registerInterceptor(AppendRandomString::class);
3534
HookManager::createCache();
36-
HookManager::loadCache();
35+
HookManager::reloadCache();
3736
expect(HookManager::getInterceptorsForHook(GetString::class))->toHaveCount(1);
3837
});
3938

4039
test('Can cache same interceptor twice', function () {
41-
HookManager::registerHook(GetString::class);
42-
HookManager::registerInterceptor(AppendRandomString::class);
43-
HookManager::registerInterceptor(AppendRandomString::class);
40+
HookRegistry::registerHook(GetString::class);
41+
HookRegistry::registerInterceptor(AppendRandomString::class);
42+
HookRegistry::registerInterceptor(AppendRandomString::class);
4443
HookManager::createCache();
45-
HookManager::loadCache();
44+
HookManager::reloadCache();
4645
expect(collect(HookManager::getInterceptorsForHook(GetString::class))->flatten(1))->toHaveCount(2);
4746
});
4847

@@ -53,7 +52,8 @@
5352
expect(HookManager::isCached())->toBeTrue();
5453
});
5554

56-
test('Throw error when cache file cannot be loaded', function () {
55+
test('Corrupted cache file will be removed', function () {
5756
file_put_contents(HookManager::getCacheFilepath(), '<?php return teer;');
58-
HookManager::loadCache();
59-
})->throws('Unable to load hooks cache');
57+
HookManager::reloadCache();
58+
expect(HookManager::isCached())->toBeFalse();
59+
});

tests/CommandTest.php renamed to tests/Ordered/CommandTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,24 @@
66
use QuixLabs\LaravelHookSystem\Console\Commands\HooksClearCommand;
77
use QuixLabs\LaravelHookSystem\Console\Commands\HooksStatusCommand;
88
use QuixLabs\LaravelHookSystem\Facades\HookManager;
9+
use QuixLabs\LaravelHookSystem\HookRegistry;
910
use Workbench\App\Hooks\GetArray;
1011
use Workbench\App\Hooks\GetString;
1112
use Workbench\App\Interceptors\AppendPriority2;
1213
use Workbench\App\Interceptors\AppendPriority4;
1314

14-
afterEach(function () {
15-
HookManager::clearCache();
16-
$this->app->forgetInstance('hooks_manager');
17-
HookManager::clearResolvedInstances();
18-
});
19-
2015
test('Can call hooks:cache', function () {
2116
$this->artisan(HooksCacheCommand::class)->assertSuccessful();
17+
HookManager::reloadCache();
2218
expect(HookManager::isCached())->toBeTrue();
2319
});
2420

2521
test('Can call hooks:clear', function () {
2622
HookManager::createCache();
23+
HookManager::reloadCache();
2724
expect(HookManager::isCached())->toBeTrue();
2825
$this->artisan(HooksClearCommand::class)->assertSuccessful();
26+
HookManager::reloadCache();
2927
expect(HookManager::isCached())->toBeFalse();
3028
});
3129

@@ -35,6 +33,7 @@
3533

3634
test('hooks:status show warning when cached', function () {
3735
HookManager::createCache();
36+
HookManager::reloadCache();
3837
expect(HookManager::isCached())->toBeTrue();
3938
$this->artisan(HooksStatusCommand::class)->assertSuccessful()->expectsOutputToContain('Hooks are actually cached!');
4039
});
@@ -44,18 +43,18 @@
4443
});
4544

4645
test('hooks:status show all hooks', function () {
47-
HookManager::registerHook(GetString::class);
48-
HookManager::registerHook(GetArray::class);
46+
HookRegistry::registerHook(GetString::class);
47+
HookRegistry::registerHook(GetArray::class);
4948

5049
// Keep below multiple call, not work due to termwind multiline if expects are chained
5150
$this->artisan(HooksStatusCommand::class)->assertSuccessful()->expectsOutputToContain(GetArray::class);
5251
$this->artisan(HooksStatusCommand::class)->assertSuccessful()->expectsOutputToContain(GetString::class);
5352
});
5453

5554
test('hooks:status show all interceptors', function () {
56-
HookManager::registerHook(GetString::class);
57-
HookManager::registerInterceptor(AppendPriority2::class);
58-
HookManager::registerInterceptor(AppendPriority4::class);
55+
HookRegistry::registerHook(GetString::class);
56+
HookRegistry::registerInterceptor(AppendPriority2::class);
57+
HookRegistry::registerInterceptor(AppendPriority4::class);
5958

6059
// Keep below multiple call, not work due to termwind multiline if expects are chained
6160
$this->artisan(HooksStatusCommand::class)->assertSuccessful()->expectsOutputToContain(AppendPriority2::class);
@@ -77,7 +76,7 @@
7776

7877
test('Display hooks cached information when cached in about config', function () {
7978
HookManager::createCache();
80-
HookManager::loadCache();
79+
HookManager::reloadCache();
8180
Artisan::call(AboutCommand::class);
8281
$output = Artisan::output();
8382
expect(preg_match("/Hooks\s[.]+\sCACHED/", $output))->toBe(1);

0 commit comments

Comments
 (0)