Skip to content

Commit 0e89e38

Browse files
authored
Merge pull request #9 from apacheborys/introduce-optional-container-for-handlers
Introduce optional container for handlers
2 parents 1bdd7b3 + 1f57bb0 commit 0e89e38

18 files changed

Lines changed: 728 additions & 229 deletions

.github/workflows/php.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v3
1919

20+
- name: Install dependencies
21+
run: composer install --prefer-dist --no-progress
22+
23+
- name: Update dependencies
24+
run: composer update
25+
2026
- name: Validate composer.json and composer.lock
2127
run: composer validate --strict
2228

@@ -29,9 +35,6 @@ jobs:
2935
restore-keys: |
3036
${{ runner.os }}-php-
3137
32-
- name: Install dependencies
33-
run: composer install --prefer-dist --no-progress
34-
3538
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
3639
# Docs: https://getcomposer.org/doc/articles/scripts.md
3740

README.md

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,85 @@ Config example:
2929
* here we can define declarator what should register exception handling callback function, if you are plan to use
3030
* standard php function set_exception_handler - you can ignore that section. StandardHandlerExceptionDeclarator is default
3131
**/
32-
"handlerExceptionDefiner": {
32+
"handlerExceptionDeclarator": {
3333
"class": "ApacheBorys\\Retry\\HandlerExceptionDefiner\\StandardHandlerExceptionDeclarator",
3434
"arguments": []
3535
},
3636
"items": {
37+
/* name of retry */
3738
"test": {
38-
/* name of retry */
39-
"exception": "ApacheBorys\\Retry\\Tests\\Functional\\Exceptions\\Mock",
4039
/* what type of Exception we would like to retry */
41-
"maxRetries": 4,
40+
"exception": "ApacheBorys\\Retry\\Tests\\Functional\\Exceptions\\Mock",
4241
/* how many tries we should do */
42+
"maxRetries": 4,
4343
/* here we are describing formula, how next execution time should be calculated. Calculated amount will be added to current time */
4444
"formula": [
4545
{
46-
"operator": "+",
4746
/* here available *, -, + and / operators */
48-
"argument": "QTY_TRIES"
47+
"operator": "+",
4948
/* you can use QTY_TRIES operator or any integer value */
49+
"argument": "QTY_TRIES"
5050
},
5151
{
5252
"operator": "*",
5353
"argument": "5"
5454
}
5555
],
56+
/* here you should define, what kind of transport you would use to deliver re-try messages to worker. Please pay your attention to https://github.com/apacheborys/re-try-php-basics-lib */
5657
"transport": {
57-
/* here you should define, what kind of transport you would use to deliver re-try messages to worker */
5858
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Transport\\FileTransportForTests",
59+
/* each specific transport could have own arguments in constructor. Here you should define it */
5960
"arguments": [
60-
/* each specific transport could have own arguments in constructor. Here you should define it */
6161
"tests\/transport.data"
6262
]
6363
},
64+
/* here you should define, what kind of executor you would use to perform re-try action */
6465
"executor": {
65-
/* here you should define, what kind of executor you would use to perform re-try action */
6666
"class": "ApacheBorys\\Retry\\Tests\\Functional\\Executor\\Runtime",
67-
"arguments": []
6867
/* each specific executor could have own arguments in constructor. Here you should define it */
68+
"arguments": []
6969
}
7070
}
7171
}
7272
}
73-
```
73+
```
74+
75+
The notice about `handlerExceptionDeclarator`, `transport` and `executor`:
76+
77+
As second argument for constructor of `ApacheBorys\Retry\ExceptionHandler` and `ApacheBorys\Retry\MessageHandler` you can send ContainerInterface. In this case, you can define arguments for `handlerExceptionDeclarator`, `transport` and `executor` as instances from runtime. It will be fetched from this injected Container.
78+
79+
For example:
80+
```json
81+
...
82+
"transport": {
83+
"class": "ApacheBorys\\Retry\\BasicTransport\\PdoTransport,
84+
"arguments": [
85+
"@pdoInstanceFromYourContainer"
86+
]
87+
},
88+
...
89+
```
90+
91+
Leading `@` indicates - you are trying to inject some instance from your container.
92+
93+
Also, if you don't want to use Container to inject some instances from runtime. But you still need to create some instances to ensure proper execution for `handlerExceptionDeclarator`, `transport` and `executor`, you can use next construction:
94+
95+
```json
96+
...
97+
"transport": {
98+
"class": "ApacheBorys\\Retry\\BasicTransport\\PdoTransport,
99+
"arguments": [
100+
[
101+
"class": "\\PDO",
102+
"arguments": [
103+
"sqlite:/app/storage/retry-db.data'"
104+
]
105+
]
106+
]
107+
},
108+
...
109+
```
110+
111+
In this case, handler/declarator will try to instantiate described class with arguments. In these arguments you can use same tricks with leading `@`; and `class`, `arguments` constructions.
112+
113+
Leading `@` works with `class` too.

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
],
1212
"require": {
1313
"php": "^7.4||^8.0",
14-
"psr/log": "^1.1"
14+
"psr/log": "^1.1",
15+
"psr/container": "^1.1"
1516
},
1617
"require-dev": {
1718
"phpunit/phpunit": "9.5.21",
1819
"ext-json": "*",
20+
"ext-pdo": "*",
1921
"vimeo/psalm": "^4.30"
2022
},
2123
"scripts": {

composer.lock

Lines changed: 49 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

psalm.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,32 @@
1212
<directory name="vendor" />
1313
</ignoreFiles>
1414
</projectFiles>
15+
<issueHandlers>
16+
<UndefinedClass errorLevel="suppress"/>
17+
<PossiblyInvalidPropertyAssignmentValue>
18+
<errorLevel type="suppress">
19+
<file name="src/AbstractHandler.php" />
20+
</errorLevel>
21+
</PossiblyInvalidPropertyAssignmentValue>
22+
<PossiblyInvalidArgument>
23+
<errorLevel type="suppress">
24+
<file name="src/AbstractHandler.php" />
25+
</errorLevel>
26+
</PossiblyInvalidArgument>
27+
<MoreSpecificReturnType>
28+
<errorLevel type="suppress">
29+
<file name="src/AbstractHandler.php" />
30+
</errorLevel>
31+
</MoreSpecificReturnType>
32+
<InvalidStringClass>
33+
<errorLevel type="suppress">
34+
<file name="src/AbstractHandler.php" />
35+
</errorLevel>
36+
</InvalidStringClass>
37+
<LessSpecificReturnStatement>
38+
<errorLevel type="suppress">
39+
<file name="src/AbstractHandler.php" />
40+
</errorLevel>
41+
</LessSpecificReturnStatement>
42+
</issueHandlers>
1543
</psalm>

0 commit comments

Comments
 (0)