-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathFileManager.php
More file actions
129 lines (104 loc) · 3.16 KB
/
FileManager.php
File metadata and controls
129 lines (104 loc) · 3.16 KB
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
123
124
125
126
127
128
129
<?php
namespace Lexik\Bundle\TranslationBundle\Manager;
use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
/**
* Manager for translations files.
*
* @author Cédric Girard <c.girard@lexik.fr>
* @author Nikola Petkanski <nikola@petkanski.com>
*/
class FileManager implements FileManagerInterface
{
/**
* @var StorageInterface
*/
private $storage;
/**
* @var string
*/
private $rootDir;
/**
* Construct.
*
* @param StorageInterface $storage
* @param string $rootDir
*/
public function __construct(StorageInterface $storage, $rootDir)
{
$this->storage = $storage;
$this->rootDir = $rootDir;
}
/**
* {@inheritdoc}
*/
public function getFor($name, $path = null)
{
if (null === $path) {
$path = sprintf('%s/Resources/translations', $this->rootDir);
}
$hash = $this->generateHash($name, $this->getFileRelativePath($path));
$file = $this->storage->getFileByHash($hash);
return $file instanceof FileInterface? $file : $this->create($name, $path);
}
/**
* {@inheritdoc}
*/
public function create($name, $path, $flush = false)
{
$path = $this->getFileRelativePath($path);
$class = $this->storage->getModelClass('file');
$file = new $class();
$file->setName($name);
$file->setPath($path);
$file->setHash($this->generateHash($name, $path));
$this->storage->persist($file);
if ($flush) {
$this->storage->flush();
}
return $file;
}
/**
* Returns the has for the given file.
*
* @param string $name
* @param string $relativePath
* @return string
*/
protected function generateHash($name, $relativePath)
{
return md5($relativePath.DIRECTORY_SEPARATOR.$name);
}
/**
* Returns the relative according to the kernel.root_dir value.
*
* @param string $filePath
* @return string
*/
protected function getFileRelativePath($filePath)
{
$commonParts = array();
// replace window \ to work with /
$rootDir = (false !== strpos($this->rootDir, '\\')) ? str_replace('\\', '/', $this->rootDir) : $this->rootDir;
$antiSlash = false;
if (false !== strpos($filePath, '\\')) {
$filePath = str_replace('\\', '/', $filePath);
$antiSlash = true;
}
$rootDirParts = explode('/', $rootDir);
$filePathParts = explode('/', $filePath);
$i = 0;
while ($i < count($rootDirParts)) {
if (isset($rootDirParts[$i], $filePathParts[$i]) && $rootDirParts[$i] == $filePathParts[$i]) {
$commonParts[] = $rootDirParts[$i];
}
$i++;
}
$filePath = str_replace(implode('/', $commonParts).'/', '', $filePath);
$nbCommonParts = count($commonParts);
$nbRootParts = count($rootDirParts);
for ($i = $nbCommonParts; $i < $nbRootParts; $i++) {
$filePath = '../'.$filePath;
}
return $antiSlash ? str_replace('/', '\\', $filePath) : $filePath;
}
}