1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the BenGorFile package. |
5
|
|
|
* |
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace BenGorFile\FileBundle\Twig; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* File download Twig function. |
19
|
|
|
* |
20
|
|
|
* @author Beñat Espiña <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class DownloadExtension extends \Twig_Extension |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The URL generator. |
26
|
|
|
* |
27
|
|
|
* @var UrlGeneratorInterface |
28
|
|
|
*/ |
29
|
|
|
private $urlGenerator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The file type. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $fileClass; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor. |
40
|
|
|
* |
41
|
|
|
* @param UrlGeneratorInterface $anUrlGenerator The URL generator |
42
|
|
|
* @param string $aFileClass The file type |
43
|
|
|
*/ |
44
|
|
|
public function __construct(UrlGeneratorInterface $anUrlGenerator, $aFileClass) |
45
|
|
|
{ |
46
|
|
|
$this->urlGenerator = $anUrlGenerator; |
47
|
|
|
$this->fileClass = $aFileClass; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getFunctions() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
new \Twig_SimpleFunction($this->getName(), [$this, 'download'], ['is_safe' => ['html']]), |
|
|
|
|
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Generates the url that returns the file of given name. |
62
|
|
|
* |
63
|
|
|
* @param string $name The file name |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function download($name) |
68
|
|
|
{ |
69
|
|
|
return $this->urlGenerator->generate( |
70
|
|
|
'bengor_file_' . $this->fileClass . '_download', |
71
|
|
|
['filename' => $name], |
72
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function getName() |
80
|
|
|
{ |
81
|
|
|
return 'bengor_file_' . $this->fileClass . '_download'; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.