|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Laposte\DatanovaBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
|
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
9
|
|
|
|
|
10
|
|
|
class Finder |
|
11
|
|
|
{ |
|
12
|
|
|
const DEFAULT_FORMAT = 'JSON'; |
|
13
|
|
|
const RESSOURCES_FOLDER = '@LaposteDatanovaBundle/Resources/dataset'; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Filesystem $filesystem */ |
|
16
|
|
|
private $filesystem; |
|
17
|
|
|
|
|
18
|
|
|
/** @var FileLocator $locator */ |
|
19
|
|
|
private $locator; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string $directory */ |
|
22
|
|
|
private $directory; |
|
23
|
|
|
|
|
24
|
|
|
/** @var LoggerInterface $logger */ |
|
25
|
|
|
private $logger; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Filesystem $filesystem |
|
29
|
|
|
* @param FileLocator $locator |
|
30
|
|
|
* @param string $rootDir |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Filesystem $filesystem, FileLocator $locator, $rootDir = self::RESSOURCES_FOLDER) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->filesystem = $filesystem; |
|
35
|
|
|
$this->locator = $locator; |
|
36
|
|
|
$rootDir = $this->locator->locate($rootDir); |
|
37
|
|
|
$this->setWorkingDirectory($rootDir); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param LoggerInterface $logger |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setLogger(LoggerInterface $logger) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->logger = $logger; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $directory the working directory for filesystem operations |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setWorkingDirectory($directory) |
|
52
|
|
|
{ |
|
53
|
|
|
$directory = preg_replace('#/+#', '/', $directory); // remove multiple slashes |
|
54
|
|
|
try { |
|
55
|
|
|
$directory = $this->locator->locate($directory); |
|
56
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
$exists = $this->filesystem->exists($directory); |
|
59
|
|
|
if (!$exists) { |
|
60
|
|
|
try { |
|
61
|
|
|
$this->filesystem->mkdir($directory); |
|
62
|
|
|
$this->logger->notice("Working directory created at " . $directory); |
|
63
|
|
|
} catch (IOExceptionInterface $exception) { |
|
64
|
|
|
$this->logger->error( |
|
65
|
|
|
"An error occurred while creating your directory at " . $exception->getPath(), |
|
66
|
|
|
$exception->getTrace() |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
$this->directory = $directory; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check if a dataset local file exists |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $dataset |
|
77
|
|
|
* @param string $format |
|
78
|
|
|
* @param string $filter |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function exists($dataset, $format, $filter = null) |
|
83
|
|
|
{ |
|
84
|
|
|
$uri = $this->getFilePath($dataset, $format, $filter); |
|
85
|
|
|
|
|
86
|
|
|
return $this->filesystem->exists($uri); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $dataset |
|
91
|
|
|
* @param string $format |
|
92
|
|
|
* @param string $filter |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getFilePath($dataset, $format, $filter = null) |
|
97
|
|
|
{ |
|
98
|
|
|
$filter = preg_replace('#:|=#', '_', $filter); |
|
99
|
|
|
$filepath = sprintf( |
|
100
|
|
|
'%s%s%s%s%s_%s.%s', |
|
101
|
|
|
$this->directory, |
|
102
|
|
|
DIRECTORY_SEPARATOR, |
|
103
|
|
|
$dataset, |
|
104
|
|
|
DIRECTORY_SEPARATOR, |
|
105
|
|
|
$dataset, |
|
106
|
|
|
$filter, |
|
107
|
|
|
$format |
|
108
|
|
|
); |
|
109
|
|
|
$filepath = preg_replace('#/+#', '/', $filepath); // remove multiple slashes |
|
110
|
|
|
|
|
111
|
|
|
return $filepath; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Save a records to dataset file |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $dataset |
|
118
|
|
|
* @param string $content |
|
119
|
|
|
* @param string $format |
|
120
|
|
|
* @param string $filter |
|
121
|
|
|
* @param bool $force |
|
122
|
|
|
* |
|
123
|
|
|
* @return false|string saved file path |
|
124
|
|
|
*/ |
|
125
|
|
|
public function save($dataset, $content, $format = self::DEFAULT_FORMAT, $filter = null, $force = false) |
|
126
|
|
|
{ |
|
127
|
|
|
$saved = false; |
|
128
|
|
|
$filename = $dataset; |
|
129
|
|
|
$path = $this->getFilePath($filename, $format, $filter); |
|
130
|
|
|
if ($this->filesystem->exists($path) && !$force) { |
|
131
|
|
|
$this->logger->error("An error occurred while saving existing dataset at " . $path); |
|
132
|
|
|
} else { |
|
133
|
|
|
try { |
|
134
|
|
|
$this->filesystem->dumpFile($path, $content); |
|
135
|
|
|
$this->logger->notice(sprintf('Saving %s dataset at %s', $dataset, $path)); |
|
136
|
|
|
$saved = realpath($path); |
|
137
|
|
|
} catch (IOExceptionInterface $exception) { |
|
138
|
|
|
$this->logger->error( |
|
139
|
|
|
"An error occurred while saving the dataset at " . $exception->getPath(), |
|
140
|
|
|
$exception->getTrace() |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $saved; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $dataset |
|
150
|
|
|
* @param string $format |
|
151
|
|
|
* @param string $filter |
|
152
|
|
|
* |
|
153
|
|
|
* @return false|string dataset file path |
|
154
|
|
|
*/ |
|
155
|
|
|
public function findDataset($dataset, $format = self::DEFAULT_FORMAT, $filter = null) |
|
156
|
|
|
{ |
|
157
|
|
|
$datasetPath = false; |
|
158
|
|
|
$path = $this->getFilePath($dataset, $format, $filter); |
|
159
|
|
|
if ($this->filesystem->exists($path)) { |
|
160
|
|
|
$datasetPath = realpath($path); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $datasetPath; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $filepath |
|
168
|
|
|
* |
|
169
|
|
|
* @return null|string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getContent($filepath) |
|
172
|
|
|
{ |
|
173
|
|
|
$content = null; |
|
174
|
|
|
if (file_exists($filepath)) { |
|
175
|
|
|
$content = file_get_contents($filepath); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $content; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.