bearsunday /
BEAR.QueryRepository
| 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; |
||
|
0 ignored issues
–
show
|
|||
| 13 | use Override; |
||
| 14 | use Ray\Di\AbstractModule; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Provides HttpCacheInterface and derived bindings |
||
| 18 | * |
||
| 19 | * The following bindings are provided: |
||
| 20 | * |
||
| 21 | * HttpCacheInterface |
||
| 22 | * -Commands |
||
| 23 | * RefreshInterceptor |
||
| 24 | * StorageExpiryModule |
||
| 25 | */ |
||
| 26 | final class CacheableModule extends AbstractModule |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * {@inheritDoc} |
||
| 30 | */ |
||
| 31 | #[Override] |
||
| 32 | protected function configure(): void |
||
| 33 | { |
||
| 34 | $this->bind(HttpCacheInterface::class)->to(HttpCache::class); |
||
| 35 | $this->bind()->annotatedWith(Commands::class)->toProvider(CommandsProvider::class); |
||
| 36 | $this->bind(RefreshInterceptor::class); |
||
| 37 | $this->install(new StorageExpiryModule(60, 60 * 60, 60 * 60 * 24)); |
||
| 38 | $this->installAopModule(); |
||
| 39 | } |
||
| 40 | |||
| 41 | protected function installAopModule(): void |
||
| 42 | { |
||
| 43 | $this->bindPriorityInterceptor( |
||
| 44 | $this->matcher->annotatedWith(Cacheable::class), |
||
| 45 | $this->matcher->startsWith('onGet'), |
||
| 46 | [CacheInterceptor::class], |
||
| 47 | ); |
||
| 48 | $this->bindInterceptor( |
||
| 49 | $this->matcher->annotatedWith(Cacheable::class), |
||
| 50 | $this->matcher->logicalOr( |
||
| 51 | $this->matcher->startsWith('onPut'), |
||
| 52 | $this->matcher->logicalOr( |
||
| 53 | $this->matcher->startsWith('onPatch'), |
||
| 54 | $this->matcher->startsWith('onDelete'), |
||
| 55 | ), |
||
| 56 | ), |
||
| 57 | [CommandInterceptor::class], |
||
| 58 | ); |
||
| 59 | $this->bindInterceptor( |
||
| 60 | $this->matcher->logicalNot( |
||
| 61 | $this->matcher->annotatedWith(Cacheable::class), |
||
| 62 | ), |
||
| 63 | $this->matcher->logicalOr( |
||
| 64 | $this->matcher->annotatedWith(Purge::class), |
||
| 65 | $this->matcher->annotatedWith(Refresh::class), |
||
| 66 | ), |
||
| 67 | [RefreshInterceptor::class], |
||
| 68 | ); |
||
| 69 | |||
| 70 | $this->bindInterceptor( |
||
| 71 | $this->matcher->annotatedWith(AbstractCacheControl::class), |
||
| 72 | $this->matcher->startsWith('onGet'), |
||
| 73 | [HttpCacheInterceptor::class], |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | } |
||
| 77 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: