1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\QueryRepository; |
6
|
|
|
|
7
|
|
|
use BEAR\RepositoryModule\Annotation\AbstractCacheControl; |
8
|
|
|
use BEAR\RepositoryModule\Annotation\Cacheable; |
9
|
|
|
use BEAR\RepositoryModule\Annotation\Commands; |
10
|
|
|
use BEAR\RepositoryModule\Annotation\Purge; |
11
|
|
|
use BEAR\RepositoryModule\Annotation\Refresh; |
12
|
|
|
use BEAR\Sunday\Extension\Transfer\HttpCacheInterface; |
|
|
|
|
13
|
|
|
use Ray\Di\AbstractModule; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Provides HttpCacheInterface and derived bindings |
17
|
|
|
* |
18
|
|
|
* The following bindings are provided: |
19
|
|
|
* |
20
|
|
|
* HttpCacheInterface |
21
|
|
|
* -Commands |
22
|
|
|
* RefreshInterceptor |
23
|
|
|
* StorageExpiryModule |
24
|
|
|
*/ |
25
|
|
|
final class CacheableModule extends AbstractModule |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritDoc} |
29
|
|
|
*/ |
30
|
|
|
protected function configure(): void |
31
|
|
|
{ |
32
|
|
|
$this->bind(HttpCacheInterface::class)->to(HttpCache::class); |
33
|
|
|
$this->bind()->annotatedWith(Commands::class)->toProvider(CommandsProvider::class); |
34
|
|
|
$this->bind(RefreshInterceptor::class); |
35
|
|
|
$this->install(new StorageExpiryModule(60, 60 * 60, 60 * 60 * 24)); |
36
|
|
|
$this->installAopModule(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function installAopModule(): void |
40
|
|
|
{ |
41
|
|
|
$this->bindPriorityInterceptor( |
42
|
|
|
$this->matcher->annotatedWith(Cacheable::class), |
43
|
|
|
$this->matcher->startsWith('onGet'), |
44
|
|
|
[CacheInterceptor::class], |
45
|
|
|
); |
46
|
|
|
$this->bindInterceptor( |
47
|
|
|
$this->matcher->annotatedWith(Cacheable::class), |
48
|
|
|
$this->matcher->logicalOr( |
49
|
|
|
$this->matcher->startsWith('onPut'), |
50
|
|
|
$this->matcher->logicalOr( |
51
|
|
|
$this->matcher->startsWith('onPatch'), |
52
|
|
|
$this->matcher->startsWith('onDelete'), |
53
|
|
|
), |
54
|
|
|
), |
55
|
|
|
[CommandInterceptor::class], |
56
|
|
|
); |
57
|
|
|
$this->bindInterceptor( |
58
|
|
|
$this->matcher->logicalNot( |
59
|
|
|
$this->matcher->annotatedWith(Cacheable::class), |
60
|
|
|
), |
61
|
|
|
$this->matcher->logicalOr( |
62
|
|
|
$this->matcher->annotatedWith(Purge::class), |
63
|
|
|
$this->matcher->annotatedWith(Refresh::class), |
64
|
|
|
), |
65
|
|
|
[RefreshInterceptor::class], |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->bindInterceptor( |
69
|
|
|
$this->matcher->annotatedWith(AbstractCacheControl::class), |
70
|
|
|
$this->matcher->startsWith('onGet'), |
71
|
|
|
[HttpCacheInterceptor::class], |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: