-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCumulativeResource.php
144 lines (126 loc) · 4.03 KB
/
CumulativeResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Oro\Component\Config;
use Oro\Component\Config\Loader\CumulativeResourceLoader;
use Oro\Component\Config\Loader\CumulativeResourceLoaderCollection;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
/**
* Represents a resource which can be located in any bundle
* and does not required any special registration in a bundle.
*/
class CumulativeResource implements ResourceInterface, \Serializable, SelfCheckingResourceInterface
{
/** @var string */
private $resource;
/** @var CumulativeResourceLoaderCollection */
private $resourceLoaders;
/**
* The list of found the resource
*
* @var array [bundle class => [resource path => TRUE, ...], ...]
*/
private $found = [];
/** @var int not serializable */
private $isFreshTimestamp;
/** @var bool not serializable */
private $isFresh;
/**
* @param string $resource The unique name of a configuration resource
* @param CumulativeResourceLoaderCollection $resourceLoaders The resource loaders
*/
public function __construct(string $resource, CumulativeResourceLoaderCollection $resourceLoaders)
{
$this->resource = $resource;
$this->resourceLoaders = $resourceLoaders;
}
/**
* Gets the unique name of a configuration resource.
*/
public function getResource(): string
{
return $this->resource;
}
/**
* {@inheritdoc}
*/
public function isFresh($timestamp)
{
if ($this->isFreshTimestamp !== $timestamp) {
$this->isFreshTimestamp = $timestamp;
$this->isFresh = true;
$manager = CumulativeResourceManager::getInstance();
$bundles = $manager->getBundles();
foreach ($bundles as $bundleName => $bundleClass) {
$bundleDir = $manager->getBundleDir($bundleClass);
$bundleAppDir = $manager->getBundleAppDir($bundleName);
/** @var CumulativeResourceLoader $loader */
foreach ($this->resourceLoaders as $loader) {
if (!$loader->isResourceFresh($bundleClass, $bundleDir, $bundleAppDir, $this, $timestamp)) {
$this->isFresh = false;
break;
}
}
if (!$this->isFresh) {
break;
}
}
}
return $this->isFresh;
}
/**
* Registers a resource as found one
*
* @param string $bundleClass The full name of bundle class
* @param string $path The full path to the resource
*/
public function addFound(string $bundleClass, string $path): void
{
$this->found[$bundleClass][$path] = true;
}
/**
* Checks if a resource was registered as found one
*
* @param string $bundleClass The full name of bundle class
* @param string $path The full path to the resource
*
* @return bool
*/
public function isFound(string $bundleClass, string $path): bool
{
return isset($this->found[$bundleClass][$path]);
}
/**
* Gets all found resources for the given bundle
*
* @param string $bundleClass The full name of bundle class
*
* @return string[] A list of resources' full paths
*/
public function getFound(string $bundleClass): array
{
return isset($this->found[$bundleClass])
? array_keys($this->found[$bundleClass])
: [];
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return (string)$this->resource;
}
/**
* {@inheritdoc}
*/
public function serialize()
{
return serialize([$this->resource, $this->found, $this->resourceLoaders]);
}
/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
list($this->resource, $this->found, $this->resourceLoaders) = unserialize($serialized);
}
}