|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/manager 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\Manager\Cache; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
use Puli\Manager\Api\Cache\CacheFile; |
|
16
|
|
|
use Puli\Manager\Api\Module\InstallInfo; |
|
17
|
|
|
use Puli\Manager\Assert\Assert; |
|
18
|
|
|
use stdClass; |
|
19
|
|
|
use Webmozart\Json\Conversion\JsonConverter; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Converts cache file from and to json. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 1.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @author Mateusz Sojda <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class CacheFileConverter implements JsonConverter |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var JsonConverter |
|
32
|
|
|
*/ |
|
33
|
|
|
private $moduleFileConverter; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates a new cache file converter. |
|
37
|
|
|
* |
|
38
|
|
|
* @param JsonConverter $moduleFileConverter The module file converter. |
|
39
|
|
|
*/ |
|
40
|
5 |
|
public function __construct(JsonConverter $moduleFileConverter) |
|
41
|
|
|
{ |
|
42
|
5 |
|
$this->moduleFileConverter = $moduleFileConverter; |
|
43
|
5 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function toJson($cacheFile, array $options = array()) |
|
49
|
|
|
{ |
|
50
|
2 |
|
Assert::isInstanceOf($cacheFile, 'Puli\Manager\Api\Cache\CacheFile'); |
|
51
|
|
|
|
|
52
|
2 |
|
$jsonData = new stdClass(); |
|
53
|
2 |
|
$jsonData->modules = array(); |
|
54
|
2 |
|
$jsonData->installInfos = array(); |
|
55
|
|
|
|
|
56
|
2 |
|
foreach ($cacheFile->getModuleFiles() as $moduleFile) { |
|
57
|
1 |
|
$jsonData->modules[] = $this->moduleFileConverter->toJson($moduleFile, array( |
|
58
|
1 |
|
'targetVersion' => $moduleFile->getVersion(), |
|
59
|
|
|
)); |
|
60
|
|
|
} |
|
61
|
2 |
|
foreach ($cacheFile->getInstallInfos() as $installInfo) { |
|
62
|
1 |
|
$jsonData->installInfos[$installInfo->getModuleName()] = $installInfo->getInstallPath(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
return $jsonData; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function fromJson($jsonData, array $options = array()) |
|
72
|
|
|
{ |
|
73
|
3 |
|
Assert::isInstanceOf($jsonData, 'stdClass'); |
|
74
|
|
|
|
|
75
|
3 |
|
$cacheFile = new CacheFile(isset($options['path']) ? $options['path'] : null); |
|
76
|
|
|
|
|
77
|
3 |
|
foreach ($jsonData->modules as $module) { |
|
78
|
1 |
|
$moduleFile = $this->moduleFileConverter->fromJson($module); |
|
79
|
1 |
|
$cacheFile->addModuleFile($moduleFile); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
foreach ($jsonData->installInfos as $packageName => $installPath) { |
|
83
|
1 |
|
$cacheFile->addInstallInfo(new InstallInfo($packageName, $installPath)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
return $cacheFile; |
|
87
|
|
|
} |
|
88
|
|
|
} |