|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* part-db version 0.1 |
|
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
|
6
|
|
|
* http://www.cl-projects.de/ |
|
7
|
|
|
* |
|
8
|
|
|
* part-db version 0.2+ |
|
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
|
10
|
|
|
* http://code.google.com/p/part-db/ |
|
11
|
|
|
* |
|
12
|
|
|
* Part-DB Version 0.4+ |
|
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
|
14
|
|
|
* https://github.com/jbtronics |
|
15
|
|
|
* |
|
16
|
|
|
* This program is free software; you can redistribute it and/or |
|
17
|
|
|
* modify it under the terms of the GNU General Public License |
|
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
19
|
|
|
* of the License, or (at your option) any later version. |
|
20
|
|
|
* |
|
21
|
|
|
* This program is distributed in the hope that it will be useful, |
|
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
24
|
|
|
* GNU General Public License for more details. |
|
25
|
|
|
* |
|
26
|
|
|
* You should have received a copy of the GNU General Public License |
|
27
|
|
|
* along with this program; if not, write to the Free Software |
|
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
namespace App\Services\Attachments; |
|
33
|
|
|
|
|
34
|
|
|
use App\Entity\Attachments\Attachment; |
|
35
|
|
|
use App\Services\Attachments\AttachmentPathResolver; |
|
36
|
|
|
use Symfony\Component\Finder\Finder; |
|
37
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
38
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
39
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* This service is used to find builtin attachment ressources |
|
43
|
|
|
* @package App\Services |
|
44
|
|
|
*/ |
|
45
|
|
|
class BuiltinAttachmentsFinder |
|
46
|
|
|
{ |
|
47
|
|
|
protected $pathResolver; |
|
48
|
|
|
protected $cache; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct(CacheInterface $cache, AttachmentPathResolver $pathResolver) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->pathResolver = $pathResolver; |
|
53
|
|
|
$this->cache = $cache; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function configureOptions(OptionsResolver $resolver) |
|
57
|
|
|
{ |
|
58
|
|
|
$resolver->setDefaults([ |
|
59
|
|
|
'limit' => 15, //Given only 15 entries |
|
60
|
|
|
//'allowed_extensions' => [], //Filter the filenames. For example ['jpg', 'jpeg'] to only get jpegs. |
|
61
|
|
|
//'placeholders' => Attachment::BUILTIN_PLACEHOLDER, //By default use all builtin ressources, |
|
62
|
|
|
'empty_returns_all' => false //Return the whole list of ressources when empty keyword is given |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns a list of all builtin ressources. |
|
68
|
|
|
* The array is a list of the relative filenames using the %PLACEHOLDERS%. |
|
69
|
|
|
* The list contains the files from all configured valid ressoureces. |
|
70
|
|
|
* @return array The list of the ressources, or an empty array if an error happened. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getListOfRessources() : array |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
return $this->cache->get('attachment_builtin_ressources', function () { |
|
76
|
|
|
$results = []; |
|
77
|
|
|
|
|
78
|
|
|
$finder = new Finder(); |
|
79
|
|
|
//We search only files |
|
80
|
|
|
$finder->files(); |
|
81
|
|
|
//Add the folder for each placeholder |
|
82
|
|
|
foreach (Attachment::BUILTIN_PLACEHOLDER as $placeholder) { |
|
83
|
|
|
$tmp = $this->pathResolver->placeholderToRealPath($placeholder); |
|
84
|
|
|
//Ignore invalid/deactivated placeholders: |
|
85
|
|
|
if ($tmp !== null) { |
|
86
|
|
|
$finder->in($tmp); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
foreach ($finder as $file) { |
|
91
|
|
|
$results[] = $this->pathResolver->realPathToPlaceholder($file->getPathname()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
//Sort results ascending |
|
95
|
|
|
sort($results); |
|
96
|
|
|
|
|
97
|
|
|
return $results; |
|
98
|
|
|
}); |
|
99
|
|
|
} catch (\Psr\Cache\InvalidArgumentException $ex) { |
|
100
|
|
|
return []; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Find all ressources which are matching the given keyword and the specified options |
|
106
|
|
|
* @param string $keyword The keyword you want to search for. |
|
107
|
|
|
* @param array $options Here you can specify some options (see configureOptions for list of options) |
|
108
|
|
|
* @param array|null $base_list The list from which should be used as base for filtering. |
|
109
|
|
|
* @return array The list of the results matching the specified keyword and options |
|
110
|
|
|
*/ |
|
111
|
|
|
public function find(string $keyword, array $options = [], ?array $base_list = []) : array |
|
112
|
|
|
{ |
|
113
|
|
|
if (empty($base_list)) { |
|
114
|
|
|
$base_list = $this->getListOfRessources(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$resolver = new OptionsResolver(); |
|
118
|
|
|
$this->configureOptions($resolver); |
|
119
|
|
|
$options = $resolver->resolve($options); |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/* |
|
124
|
|
|
if (empty($options['placeholders'])) { |
|
125
|
|
|
return []; |
|
126
|
|
|
} */ |
|
127
|
|
|
|
|
128
|
|
|
if ($keyword === '') { |
|
129
|
|
|
if ($options['empty_returns_all']) { |
|
130
|
|
|
$keyword = '.*'; |
|
131
|
|
|
} else { |
|
132
|
|
|
return []; |
|
133
|
|
|
} |
|
134
|
|
|
} else { |
|
135
|
|
|
//Quote all values in the keyword (user is not allowed to use regex characters) |
|
136
|
|
|
$keyword = preg_quote($keyword, '/'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/*TODO: Implement placheolder and extension filter */ |
|
140
|
|
|
/* if (!empty($options['allowed_extensions'])) { |
|
141
|
|
|
$keyword .= "\.("; |
|
142
|
|
|
foreach ($options['allowed_extensions'] as $extension) { |
|
143
|
|
|
$keyword .= preg_quote($extension, '/') . '|'; |
|
144
|
|
|
} |
|
145
|
|
|
$keyword .= ')$'; |
|
146
|
|
|
} */ |
|
147
|
|
|
|
|
148
|
|
|
//Ignore case |
|
149
|
|
|
$regex = '/.*' . $keyword . '.*/i'; |
|
150
|
|
|
|
|
151
|
|
|
return preg_grep($regex, $base_list); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
} |