|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Component\Utils; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
9
|
|
|
use Vich\UploaderBundle\Mapping\PropertyMapping; |
|
10
|
|
|
use Vich\UploaderBundle\Naming\ConfigurableInterface; |
|
11
|
|
|
use Vich\UploaderBundle\Naming\DirectoryNamerInterface; |
|
12
|
|
|
use Vich\UploaderBundle\Util\Transliterator; |
|
13
|
|
|
|
|
14
|
|
|
class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $propertyPath; |
|
20
|
|
|
private $charsPerDir = 2; |
|
21
|
|
|
private $dirs = 1; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
private $transliterate = false; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var PropertyAccessorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $propertyAccessor; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Transliterator |
|
35
|
|
|
*/ |
|
36
|
|
|
private $transliterator; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(?PropertyAccessorInterface $propertyAccessor, Transliterator $transliterator) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); |
|
41
|
|
|
$this->transliterator = $transliterator; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param array $options Options for this namer. The following options are accepted: |
|
46
|
|
|
* - chars_per_dir: how many chars use for each dir. |
|
47
|
|
|
* - dirs: how many dirs create |
|
48
|
|
|
*/ |
|
49
|
|
|
public function configure(array $options): void |
|
50
|
|
|
{ |
|
51
|
|
|
if (empty($options['property'])) { |
|
52
|
|
|
throw new \InvalidArgumentException('Option "property" is missing or empty.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->propertyPath = $options['property']; |
|
56
|
|
|
|
|
57
|
|
|
$options = \array_merge(['chars_per_dir' => $this->charsPerDir, 'dirs' => $this->dirs], $options); |
|
58
|
|
|
|
|
59
|
|
|
$this->charsPerDir = $options['chars_per_dir']; |
|
60
|
|
|
$this->dirs = $options['dirs']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function directoryName($object, PropertyMapping $mapping): string |
|
64
|
|
|
{ |
|
65
|
|
|
$fileName = $mapping->getFileName($object); |
|
66
|
|
|
|
|
67
|
|
|
$category = $this->propertyAccessor->getValue($object, $this->propertyPath); |
|
68
|
|
|
|
|
69
|
|
|
$parts[] = $category; |
|
|
|
|
|
|
70
|
|
|
/*for ($i = 0, $start = 0; $i < $this->dirs; $i++, $start += $this->charsPerDir) { |
|
71
|
|
|
$parts[] = \substr($fileName, $start, $this->charsPerDir); |
|
72
|
|
|
}*/ |
|
73
|
|
|
$parts[] = $fileName; |
|
74
|
|
|
|
|
75
|
|
|
return \implode('/', $parts); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|