1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/repository package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\Repository\ChangeStream; |
13
|
|
|
|
14
|
|
|
use Puli\Repository\Api\ChangeStream\ChangeStream; |
15
|
|
|
use Puli\Repository\Api\ChangeStream\VersionList; |
16
|
|
|
use Puli\Repository\Api\NoVersionFoundException; |
17
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
18
|
|
|
use Puli\Repository\Api\ResourceRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A change stream stored in memory. |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* |
25
|
|
|
* @author Titouan Galopin <[email protected]> |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class InMemoryChangeStream implements ChangeStream |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $versions = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
323 |
|
public function append(PuliResource $resource) |
39
|
|
|
{ |
40
|
323 |
|
if (!isset($this->versions[$resource->getPath()])) { |
41
|
323 |
|
$this->versions[$resource->getPath()] = array(); |
42
|
|
|
} |
43
|
|
|
|
44
|
323 |
|
$this->versions[$resource->getPath()][] = $resource; |
45
|
323 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
83 |
|
public function purge($path) |
51
|
|
|
{ |
52
|
83 |
|
unset($this->versions[$path]); |
53
|
83 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
113 |
|
public function clear() |
59
|
|
|
{ |
60
|
113 |
|
$this->versions = array(); |
61
|
113 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
4 |
|
public function contains($path) |
67
|
|
|
{ |
68
|
4 |
|
return isset($this->versions[$path]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
91 |
|
public function getVersions($path, ResourceRepository $repository = null) |
75
|
|
|
{ |
76
|
91 |
|
if (!isset($this->versions[$path])) { |
77
|
23 |
|
throw NoVersionFoundException::forPath($path); |
78
|
|
|
} |
79
|
|
|
|
80
|
68 |
|
$versions = array(); |
81
|
|
|
|
82
|
68 |
View Code Duplication |
foreach ($this->versions[$path] as $resource) { |
|
|
|
|
83
|
68 |
|
if (null !== $repository) { |
84
|
65 |
|
$resource = clone $resource; |
85
|
65 |
|
$resource->attachTo($repository, $path); |
86
|
|
|
} |
87
|
|
|
|
88
|
68 |
|
$versions[] = $resource; |
89
|
|
|
} |
90
|
|
|
|
91
|
68 |
|
return new VersionList($path, $versions); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.