|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ChangelogParser\Manager; |
|
4
|
|
|
|
|
5
|
|
|
class ChangelogManager { |
|
6
|
|
|
/** @var \ChangelogParser\Driver\Driver **/ |
|
7
|
|
|
protected $driver; |
|
8
|
|
|
/** @var \ChangelogParser\Manager\CacheManager **/ |
|
9
|
|
|
protected $cacheManager; |
|
10
|
|
|
/** @var string **/ |
|
11
|
|
|
protected $storagePath; |
|
12
|
|
|
|
|
13
|
2 |
|
public function __construct() { |
|
14
|
2 |
|
$this->cacheManager = new CacheManager(); |
|
15
|
2 |
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $storagePath |
|
19
|
|
|
* @return \ChangelogParser\Driver\Driver |
|
20
|
|
|
*/ |
|
21
|
2 |
|
public function setStoragePath($storagePath) { |
|
22
|
2 |
|
$this->storagePath = $storagePath; |
|
23
|
|
|
|
|
24
|
2 |
|
return $this; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getStoragePath() { |
|
31
|
|
|
return $this->storagePath; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $filepath |
|
36
|
|
|
* @param string $format |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function getLastVersion($filepath, $format = 'json') { |
|
40
|
1 |
|
$cacheFile = "last-version.$format"; |
|
41
|
1 |
|
if(($cache = $this->cacheManager->getCache($filepath, $cacheFile)) !== false) { |
|
|
|
|
|
|
42
|
|
|
return $cache; |
|
43
|
|
|
} |
|
44
|
1 |
|
$this->initializeDriver($format); |
|
45
|
1 |
|
$this->driver->convert($filepath); |
|
46
|
1 |
|
$content = $this->driver->getLastVersion(); |
|
|
|
|
|
|
47
|
1 |
|
$this->cacheManager->generateCache($filepath, $cacheFile, $content); |
|
48
|
1 |
|
return $content; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $filepath |
|
53
|
|
|
* @param string $format |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function getAllVersions($filepath, $format = 'json') { |
|
57
|
1 |
|
$cacheFile = "all-versions.$format"; |
|
58
|
1 |
|
if(($cache = $this->cacheManager->getCache($filepath, $cacheFile)) !== false) { |
|
|
|
|
|
|
59
|
|
|
return $cache; |
|
60
|
|
|
} |
|
61
|
1 |
|
$this->initializeDriver($format); |
|
62
|
1 |
|
$this->driver->convert($filepath); |
|
63
|
1 |
|
$content = $this->driver->getAllVersions(); |
|
|
|
|
|
|
64
|
1 |
|
$this->cacheManager->generateCache($filepath, $cacheFile, $content); |
|
65
|
1 |
|
return $content; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $format |
|
70
|
|
|
* @throws \InvalidArgumentException |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function initializeDriver($format) { |
|
73
|
2 |
|
$driverClass = 'ChangelogParser\\Driver\\' . ucfirst($format) . 'Driver'; |
|
74
|
|
|
|
|
75
|
2 |
|
if(!class_exists($driverClass)) { |
|
|
|
|
|
|
76
|
|
|
throw new \InvalidArgumentException('The requested format is not supported'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
$this->driver = new $driverClass(); |
|
80
|
2 |
|
if($this->storagePath !== null) { |
|
|
|
|
|
|
81
|
2 |
|
$this->driver->setStoragePath($this->storagePath); |
|
82
|
2 |
|
} |
|
83
|
2 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return \ChangelogParser\Manager\CacheManager |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getCacheManager() { |
|
89
|
|
|
return $this->cacheManager; |
|
90
|
|
|
} |
|
91
|
|
|
} |