1
|
|
|
<?php
|
2
|
|
|
namespace Kir\Services\Cmd\Dispatcher\Builder;
|
3
|
|
|
|
4
|
|
|
use Ioc\MethodInvoker;
|
5
|
|
|
use Kir\Services\Cmd\Dispatcher\AttributeRepositories\MySQLAttributeRepository;
|
6
|
|
|
use Kir\Services\Cmd\Dispatcher\Dispatchers\DefaultDispatcher;
|
7
|
|
|
use PDO;
|
8
|
|
|
|
9
|
|
|
class MySQLBuilder {
|
10
|
|
|
/** @var PDO */
|
11
|
|
|
private $pdo;
|
12
|
|
|
/** @var MethodInvoker|null */
|
13
|
|
|
private $methodInvoker;
|
14
|
|
|
/** @var bool */
|
15
|
|
|
private $useLocking = true;
|
16
|
|
|
/** @var string */
|
17
|
|
|
private $lockPrefix = '';
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @param PDO $pdo
|
21
|
|
|
*/
|
22
|
|
|
public function __construct(PDO $pdo) {
|
23
|
|
|
$this->pdo = $pdo;
|
24
|
|
|
}
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* This can be used to enable automatic provisioning of DI objects when service methods are called. (Autowiring)
|
28
|
|
|
*
|
29
|
|
|
* @param MethodInvoker|null $methodInvoker
|
30
|
|
|
* @return $this
|
31
|
|
|
*/
|
32
|
|
|
public function setMethodInvoker(MethodInvoker $methodInvoker = null): self {
|
33
|
|
|
$this->methodInvoker = $methodInvoker;
|
34
|
|
|
return $this;
|
35
|
|
|
}
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* Enabled by default.
|
39
|
|
|
* You can disable global locking (using MySQL's GET_LOCK) per service-run.
|
40
|
|
|
* A Lock will be optained before a service runs and will be released, is the service is done
|
41
|
|
|
* (or an exception was thrown during the run)
|
42
|
|
|
*
|
43
|
|
|
* @param bool $useLocking
|
44
|
|
|
* @return $this
|
45
|
|
|
*/
|
46
|
|
|
public function useLocking(bool $useLocking = true): self {
|
47
|
|
|
$this->useLocking = $useLocking;
|
48
|
|
|
return $this;
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* Set the lock-prefix for the MySQL-`GET_LOCK` name.
|
53
|
|
|
*
|
54
|
|
|
* You can think of it as calling the function like this:
|
55
|
|
|
* `SELECT GET_LOCK(CONCAT(<lock-prefix>, <registered-service-name>), 0)`
|
56
|
|
|
*
|
57
|
|
|
* Something like `my-app:` would result in `my-app:service-xy` if the particular service was registered as
|
58
|
|
|
* `service-xy`. The Lock is per service. Between two services, the lock will be released and reoptained.
|
59
|
|
|
*
|
60
|
|
|
* @param string $lockPrefix
|
61
|
|
|
* @return $this
|
62
|
|
|
*/
|
63
|
|
|
public function setLockPrefix(string $lockPrefix = ''): self {
|
64
|
|
|
$this->lockPrefix = $lockPrefix;
|
65
|
|
|
return $this;
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* @return DefaultDispatcher
|
70
|
|
|
*/
|
71
|
|
|
public function build(): DefaultDispatcher {
|
72
|
|
|
$repos = new MySQLAttributeRepository($this->pdo, [
|
|
|
|
|
73
|
|
|
'use-locking' => $this->useLocking,
|
74
|
|
|
'lock-prefix' => $this->lockPrefix,
|
75
|
|
|
]);
|
76
|
|
|
return new DefaultDispatcher($repos, $this->methodInvoker);
|
77
|
|
|
}
|
78
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: