1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of phpDocumentor. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @copyright 2010-2015 Mike van Riel<[email protected]> |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
11
|
|
|
* @link http://phpdoc.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\Parser\Middleware; |
15
|
|
|
|
16
|
|
|
use phpDocumentor\Reflection\Middleware\Middleware; |
17
|
|
|
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand; |
18
|
|
|
use phpDocumentor\Reflection\Php\File; |
19
|
|
|
use Stash\Item; |
20
|
|
|
use Stash\Pool; |
21
|
|
|
|
22
|
|
|
final class CacheMiddleware implements Middleware |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Cache namespace used for this repository. |
26
|
|
|
*/ |
27
|
|
|
const CACHE_NAMESPACE = 'Documentation\\Api\\Php'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cache pool used to store files. |
31
|
|
|
* |
32
|
|
|
* @var Pool |
33
|
|
|
*/ |
34
|
|
|
private $dataStore; |
35
|
|
|
|
36
|
|
|
private $enabled = true; |
37
|
|
|
|
38
|
2 |
|
public function __construct(Pool $dataStore) |
39
|
|
|
{ |
40
|
2 |
|
$this->dataStore = $dataStore; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
public function disable() |
44
|
|
|
{ |
45
|
|
|
$this->enabled = false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Executes this middle ware class. |
50
|
|
|
* A middle ware class MUST return a File object or call the $next callable. |
51
|
|
|
* |
52
|
|
|
* @param CreateCommand $command |
53
|
|
|
* @param callable $next |
54
|
|
|
* |
55
|
|
|
* @return File |
56
|
|
|
*/ |
57
|
2 |
|
public function execute($command, callable $next) |
58
|
|
|
{ |
59
|
2 |
|
$itemName = $this->getItemName($command->getFile()->path()); |
60
|
2 |
|
$item = $this->dataStore->getItem($itemName); |
61
|
2 |
|
if ($item->isMiss() || $this->enabled === false) { |
62
|
1 |
|
return $this->updateCache($command, $next, $item); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @var File $cachedFile */ |
66
|
1 |
|
$cachedFile = $item->get(); |
67
|
|
|
|
68
|
1 |
|
if ($cachedFile === null) { |
69
|
|
|
return $this->updateCache($command, $next, $item); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
if ($cachedFile->getHash() !== $command->getFile()->md5()) { |
73
|
1 |
|
return $this->updateCache($command, $next, $item); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $cachedFile; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param CreateCommand $command |
81
|
|
|
* @param callable $next |
82
|
|
|
* @param Item $item |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
2 |
|
private function updateCache(CreateCommand $command, callable $next, $item) |
86
|
|
|
{ |
87
|
2 |
|
$file = $next($command); |
88
|
2 |
|
$item->lock(); |
89
|
2 |
|
$this->dataStore->save($item->set($file)); |
90
|
2 |
|
return $file; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Convert path to ItemName |
95
|
|
|
* |
96
|
|
|
* @param path |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
2 |
|
private function getItemName($path) |
100
|
|
|
{ |
101
|
2 |
|
return static::CACHE_NAMESPACE . '\\' . md5($path); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.