Skip to content

Commit 5acd05f

Browse files
committed
Made Twig download extension compatible with the file id
1 parent 10d8a33 commit 5acd05f

4 files changed

Lines changed: 73 additions & 6 deletions

File tree

src/BenGorFile/FileBundle/BenGorFileBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use BenGorFile\FileBundle\DependencyInjection\Compiler\ApplicationQueriesPass;
1818
use BenGorFile\FileBundle\DependencyInjection\Compiler\DomainServicesPass;
1919
use BenGorFile\FileBundle\DependencyInjection\Compiler\RoutesPass;
20+
use BenGorFile\FileBundle\DependencyInjection\Compiler\TwigPass;
2021
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2122
use Symfony\Component\DependencyInjection\ContainerBuilder;
2223
use Symfony\Component\HttpKernel\Bundle\Bundle;
@@ -38,6 +39,7 @@ public function build(ContainerBuilder $container)
3839
$container->addCompilerPass(new ApplicationDataTransformersPass(), PassConfig::TYPE_OPTIMIZE);
3940
$container->addCompilerPass(new ApplicationQueriesPass(), PassConfig::TYPE_OPTIMIZE);
4041
$container->addCompilerPass(new RoutesPass(), PassConfig::TYPE_OPTIMIZE);
42+
$container->addCompilerPass(new TwigPass(), PassConfig::TYPE_OPTIMIZE);
4143

4244
$this->buildLoadableBundles($container);
4345
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BenGorFile package.
5+
*
6+
* (c) Beñat Espiña <benatespina@gmail.com>
7+
* (c) Gorka Laucirica <gorka.lauzirika@gmail.com>
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\DependencyInjection\Compiler;
14+
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
18+
/**
19+
* Register Twig services compiler pass.
20+
*
21+
* @author Beñat Espiña <benatespina@gmail.com>
22+
*/
23+
class TwigPass implements CompilerPassInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function process(ContainerBuilder $container)
29+
{
30+
if (!$container->hasDefinition('bengor_file.file_bundle.twig.download_extension')) {
31+
return;
32+
}
33+
34+
$config = $container->getParameter('bengor_file.config');
35+
36+
$handlers = [];
37+
foreach ($config['file_class'] as $key => $file) {
38+
$handlers[$key] = $container->getDefinition('bengor.file.application.query.' . $key . '_of_id');
39+
}
40+
41+
$container->getDefinition('bengor_file.file_bundle.twig.download_extension')->replaceArgument(
42+
1,
43+
$handlers
44+
);
45+
}
46+
}

src/BenGorFile/FileBundle/Resources/config/services/twig.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ services:
1111
class: BenGorFile\FileBundle\Twig\DownloadExtension
1212
arguments:
1313
- '@router'
14+
- []
1415
public: false
1516
tags:
1617
- { name: twig.extension }

src/BenGorFile/FileBundle/Twig/DownloadExtension.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
namespace BenGorFile\FileBundle\Twig;
1414

15+
use BenGorFile\File\Application\Query\FileOfIdQuery;
16+
use BenGorFile\File\Domain\Model\FileDoesNotExistException;
1517
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1618

1719
/**
18-
* File download Twig function.
20+
* File download by id Twig function.
1921
*
2022
* @author Beñat Espiña <benatespina@gmail.com>
2123
*/
@@ -28,14 +30,23 @@ class DownloadExtension extends \Twig_Extension
2830
*/
2931
private $urlGenerator;
3032

33+
/**
34+
* The file handlers.
35+
*
36+
* @var array
37+
*/
38+
private $handlers;
39+
3140
/**
3241
* Constructor.
3342
*
3443
* @param UrlGeneratorInterface $anUrlGenerator The URL generator
44+
* @param array $handlers The file handlers
3545
*/
36-
public function __construct(UrlGeneratorInterface $anUrlGenerator)
46+
public function __construct(UrlGeneratorInterface $anUrlGenerator, array $handlers)
3747
{
3848
$this->urlGenerator = $anUrlGenerator;
49+
$this->handlers = $handlers;
3950
}
4051

4152
/**
@@ -49,18 +60,25 @@ public function getFunctions()
4960
}
5061

5162
/**
52-
* Generates the url that returns the file of given file type and name.
63+
* Generates the url that returns the file of given file type and file.
5364
*
5465
* @param string $fileClass The file type type
55-
* @param string $name The file name
66+
* @param string $q The file query
5667
*
5768
* @return string
5869
*/
59-
public function download($fileClass, $name)
70+
public function download($fileClass, $q)
6071
{
72+
try {
73+
$file = $this->handlers[$fileClass]->__invoke(new FileOfIdQuery($q));
74+
$filename = $file['file_name'];
75+
} catch (FileDoesNotExistException $exception) {
76+
$filename = $q;
77+
}
78+
6179
return $this->urlGenerator->generate(
6280
'bengor_file_' . $fileClass . '_download',
63-
['filename' => $name],
81+
['filename' => $filename],
6482
UrlGeneratorInterface::ABSOLUTE_URL
6583
);
6684
}

0 commit comments

Comments
 (0)