1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE |
6
|
|
|
|
7
|
|
|
This program is free software; you can redistribute it and/or modify |
8
|
|
|
it under the terms of the GNU General Public License as published by |
9
|
|
|
the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
(at your option) any later version. |
11
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
GNU General Public License for more details. |
16
|
|
|
|
17
|
|
|
You should have received a copy of the GNU General Public License |
18
|
|
|
along with this program; if not, write to the Free Software |
19
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace TheCodingMachine\TDBM; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The WeakrefObjectStorage class is used to reference all beans that have been fetched from the database. |
26
|
|
|
* If a bean is requested twice from TDBM, the WeakrefObjectStorage is used to "cache" the bean. |
27
|
|
|
* Unlike the StandardObjectStorage, the WeakrefObjectStorage manages memory in a clever way, using the weakref |
28
|
|
|
* PHP extension. It is used if the "weakref" extension is available. |
29
|
|
|
* Otherwise, the StandardObjectStorage is used instead. |
30
|
|
|
* |
31
|
|
|
* @author David Negrier |
32
|
|
|
*/ |
33
|
|
|
class WeakrefObjectStorage implements ObjectStorageInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* An array of fetched object, accessible via table name and primary key. |
37
|
|
|
* If the primary key is split on several columns, access is done by an array of columns, serialized. |
38
|
|
|
* |
39
|
|
|
* @var \WeakRef[][] |
40
|
|
|
*/ |
41
|
|
|
private $objects = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Every 10000 set in the dataset, we perform a cleanup to ensure the WeakRef instances |
45
|
|
|
* are removed if they are no more valid. |
46
|
|
|
* This is to avoid having memory used by dangling WeakRef instances. |
47
|
|
|
* |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
private $garbageCollectorCount = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sets an object in the storage. |
54
|
|
|
* |
55
|
|
|
* @param string $tableName |
56
|
|
|
* @param string|int $id |
57
|
|
|
* @param DbRow $dbRow |
58
|
|
|
*/ |
59
|
|
|
public function set(string $tableName, $id, DbRow $dbRow): void |
60
|
|
|
{ |
61
|
|
|
$this->objects[$tableName][$id] = new \WeakRef($dbRow); |
|
|
|
|
62
|
|
|
++$this->garbageCollectorCount; |
63
|
|
|
if ($this->garbageCollectorCount === 10000) { |
64
|
|
|
$this->garbageCollectorCount = 0; |
65
|
|
|
$this->cleanupDanglingWeakRefs(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns an object from the storage (or null if no object is set). |
71
|
|
|
* |
72
|
|
|
* @param string $tableName |
73
|
|
|
* @param string|int $id |
74
|
|
|
* |
75
|
|
|
* @return DbRow|null |
76
|
|
|
*/ |
77
|
|
|
public function get(string $tableName, $id) : ?DbRow |
78
|
|
|
{ |
79
|
|
|
if (isset($this->objects[$tableName][$id])) { |
80
|
|
|
return $this->objects[$tableName][$id]->get(); |
81
|
|
|
} |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Removes an object from the storage. |
87
|
|
|
* |
88
|
|
|
* @param string $tableName |
89
|
|
|
* @param string|int $id |
90
|
|
|
*/ |
91
|
|
|
public function remove(string $tableName, $id): void |
92
|
|
|
{ |
93
|
|
|
unset($this->objects[$tableName][$id]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Applies the callback to all objects. |
98
|
|
|
* |
99
|
|
|
* @param callable $callback |
100
|
|
|
*/ |
101
|
|
|
public function apply(callable $callback): void |
102
|
|
|
{ |
103
|
|
|
foreach ($this->objects as $tableName => $table) { |
104
|
|
|
foreach ($table as $id => $ref) { |
105
|
|
|
$obj = $ref->get(); |
106
|
|
|
if ($obj !== null) { |
107
|
|
|
$callback($obj, $tableName, $id); |
108
|
|
|
} else { |
109
|
|
|
unset($this->objects[$tableName][$id]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function cleanupDanglingWeakRefs(): void |
116
|
|
|
{ |
117
|
|
|
foreach ($this->objects as $tableName => $table) { |
118
|
|
|
foreach ($table as $id => $obj) { |
119
|
|
|
if ($obj->valid() === false) { |
120
|
|
|
unset($this->objects[$tableName][$id]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths