1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\Locator; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\CoreBundle\Detection\DeviceDetectionInterface; |
18
|
|
|
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface; |
19
|
|
|
use Sylius\Bundle\ThemeBundle\Twig\Locator\TemplateNotFoundException; |
20
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
21
|
|
|
|
22
|
|
|
class ApplicationResourceLocator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Filesystem |
26
|
|
|
*/ |
27
|
|
|
private $filesystem; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DeviceDetectionInterface |
31
|
|
|
*/ |
32
|
|
|
private $deviceDetection; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Filesystem $filesystem |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Filesystem $filesystem, DeviceDetectionInterface $deviceDetection) |
38
|
101 |
|
{ |
39
|
|
|
$this->filesystem = $filesystem; |
40
|
101 |
|
$this->deviceDetection = $deviceDetection; |
41
|
101 |
|
} |
42
|
101 |
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function locate(string $resourceName, ThemeInterface $theme): string |
47
|
12 |
|
{ |
48
|
|
View Code Duplication |
if (null !== $this->deviceDetection->getType()) { |
|
|
|
|
49
|
12 |
|
$path = sprintf('%s/%s/views/%s', $theme->getPath(), $this->deviceDetection->getType(), $resourceName); |
50
|
12 |
|
if ($this->filesystem->exists($path)) { |
51
|
12 |
|
return $path; |
52
|
12 |
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$path = sprintf('%s/views/%s', $theme->getPath(), $resourceName); |
56
|
1 |
|
if (!$this->filesystem->exists($path)) { |
57
|
|
|
throw new TemplateNotFoundException($resourceName, [$theme]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $path; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function supports(string $template): bool |
64
|
|
|
{ |
65
|
12 |
|
return strpos($template, '@') !== 0; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.