/
FileLocator.php
125 lines (108 loc) · 3.17 KB
/
FileLocator.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
<?php
namespace Concrete\Core\Filesystem;
use Concrete\Core\Application\Application;
use Concrete\Core\Filesystem\FileLocator\ApplicationLocation;
use Concrete\Core\Filesystem\FileLocator\CoreLocation;
use Concrete\Core\Filesystem\FileLocator\LocationInterface;
use Concrete\Core\Filesystem\FileLocator\PackageLocation;
use Concrete\Core\Filesystem\FileLocator\Record;
use Illuminate\Filesystem\Filesystem;
class FileLocator
{
protected $filesystem;
protected $app;
protected $locations = [];
/**
* @return Filesystem
*/
public function getFilesystem()
{
return $this->filesystem;
}
/**
* @param Filesystem $filesystem
*/
public function setFilesystem($filesystem)
{
$this->filesystem = $filesystem;
}
public function __construct(Filesystem $filesystem, Application $app)
{
$this->filesystem = $filesystem;
$this->app = $app;
$this->cache = $app->make('cache/overrides');
}
public function addDefaultLocations()
{
array_unshift($this->locations, new ApplicationLocation($this->filesystem));
$this->locations[] = new CoreLocation($this->filesystem);
}
public function addLocation(LocationInterface $location)
{
$this->locations[] = $location;
}
public function addPackageLocation($pkgHandle)
{
$this->locations[] = new PackageLocation($pkgHandle);
}
protected function getCacheKey($file)
{
$keys = [];
$file = trim(str_replace('/', DIRECTORY_SEPARATOR, $file), DIRECTORY_SEPARATOR);
foreach($this->locations as $location) {
$cacheKey = $location->getCacheKey();
if (is_array($cacheKey)) {
$keys = array_merge($keys, $cacheKey);
} else {
$keys[] = $cacheKey;
}
}
$keys = array_merge($keys, explode(DIRECTORY_SEPARATOR, $file));
return 'overrides.' . md5(implode('.', $keys));
}
public function getAllRecords($file)
{
$this->addDefaultLocations();
$records = array();
foreach($this->locations as $location) {
$location->setFilesystem($this->filesystem);
if ($record = $location->contains($file)) {
$records[] = $record;
}
}
return $records;
}
/**
* @param $file
* @return Record
*/
public function getRecord($file)
{
$this->addDefaultLocations();
$key = $this->getCacheKey($file);
$item = $this->cache->getItem($key);
$record = null;
if ($item->isMiss()) {
$item->lock();
foreach($this->locations as $location) {
$location->setFilesystem($this->filesystem);
if ($record = $location->contains($file)) {
break;
}
}
if (isset($record)) {
$this->cache->save($item->set($record));
}
} else {
$record = $item->get();
}
return $record;
}
/**
* @return array
*/
public function getLocations()
{
return $this->locations;
}
}