|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 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 2015 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\Controller; |
|
16
|
|
|
|
|
17
|
|
|
use Hoa\File\Read; |
|
18
|
|
|
use Hoa\Mime\Mime; |
|
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
|
24
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
25
|
|
|
|
|
26
|
|
|
class StaticThemeAssetsController extends Controller |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Directory with assets inside theme. |
|
30
|
|
|
*/ |
|
31
|
|
|
const ASSETS_DIRECTORY = 'public'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @Route("/{fileName}.{fileExtension}", name="static_theme_assets_root", requirements={ |
|
35
|
|
|
* "fileName": "sw|manifest" |
|
36
|
|
|
* }) |
|
37
|
|
|
* @Method("GET") |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function rootAction($fileName, $fileExtension) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$themes = $this->get('sylius.theme.hierarchy_provider')->getThemeHierarchy( |
|
42
|
1 |
|
$this->get('sylius.context.theme')->getTheme() |
|
43
|
1 |
|
); |
|
44
|
1 |
View Code Duplication |
foreach ($themes as $theme) { |
|
|
|
|
|
|
45
|
|
|
$filePath = $theme->getPath().'/'.self::ASSETS_DIRECTORY.'/'.$fileName.'.'.$fileExtension; |
|
46
|
|
|
if (null !== $response = $this->handleFileLoading($filePath)) { |
|
47
|
|
|
return $response; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new NotFoundHttpException('Page was not found.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
/** |
|
55
|
|
|
* @Route("/themes/{type}/{themeName}/screenshots/{fileName}", name="static_theme_screenshots", requirements={ |
|
56
|
1 |
|
* "type": "organization|tenant" |
|
57
|
1 |
|
* }) |
|
58
|
1 |
|
* @Method("GET") |
|
59
|
1 |
|
*/ |
|
60
|
|
|
public function screenshotsAction(string $type, string $themeName, $fileName) |
|
61
|
|
|
{ |
|
62
|
1 |
|
if ('organization' === $type) { |
|
63
|
|
|
$theme = $this->loadOrganizationTheme(str_replace('__', '/', $themeName)); |
|
64
|
|
|
} elseif ('tenant' === $type) { |
|
65
|
|
|
$theme = $this->loadTenantTheme(str_replace('__', '/', $themeName)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$filePath = $theme->getPath().'/screenshots/'.$fileName; |
|
|
|
|
|
|
69
|
|
|
if (null !== $response = $this->handleFileLoading($filePath)) { |
|
70
|
2 |
|
return $response; |
|
71
|
|
|
} |
|
72
|
2 |
|
|
|
73
|
2 |
|
throw new NotFoundHttpException('Page was not found.'); |
|
74
|
2 |
|
} |
|
75
|
2 |
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @Route("/public/{filePath}", name="static_theme_assets_public", requirements={"filePath"=".+"}) |
|
78
|
2 |
|
* @Method("GET") |
|
79
|
2 |
|
*/ |
|
80
|
2 |
|
public function publicAction($filePath) |
|
81
|
2 |
|
{ |
|
82
|
2 |
|
$themes = $this->get('sylius.theme.hierarchy_provider')->getThemeHierarchy( |
|
83
|
|
|
$this->get('sylius.context.theme')->getTheme() |
|
84
|
2 |
|
); |
|
85
|
|
View Code Duplication |
foreach ($themes as $theme) { |
|
|
|
|
|
|
86
|
1 |
|
$themeFilePath = $theme->getPath().'/'.self::ASSETS_DIRECTORY.'/'.$filePath; |
|
87
|
|
|
if (null !== $response = $this->handleFileLoading($themeFilePath, basename($filePath))) { |
|
|
|
|
|
|
88
|
|
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
throw new NotFoundHttpException('File was not found.', null, 404); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $filePath |
|
97
|
|
|
* |
|
98
|
|
|
* @return Response |
|
99
|
|
|
*/ |
|
100
|
|
|
private function handleFileLoading($filePath) |
|
101
|
|
|
{ |
|
102
|
|
|
if (file_exists($filePath)) { |
|
103
|
|
|
$response = new Response(file_get_contents($filePath)); |
|
|
|
|
|
|
104
|
|
|
$disposition = $response->headers->makeDisposition( |
|
105
|
|
|
ResponseHeaderBag::DISPOSITION_INLINE, |
|
106
|
|
|
basename($filePath) |
|
107
|
|
|
); |
|
108
|
|
|
$response->headers->set('Content-Disposition', $disposition); |
|
109
|
|
|
$type = new Mime(new Read($filePath)); |
|
110
|
|
|
$mime = str_replace('/x-', '/', Mime::getMimeFromExtension($type->getExtension())); |
|
111
|
|
|
$response->headers->set('Content-Type', $mime); |
|
112
|
|
|
$response->setStatusCode(Response::HTTP_OK); |
|
113
|
|
|
$response->setPublic(); |
|
114
|
|
|
$response->setMaxAge(3600); |
|
115
|
|
|
$response->setSharedMaxAge(7200); |
|
116
|
|
|
|
|
117
|
|
|
return $response; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $themeName |
|
123
|
|
|
* |
|
124
|
|
|
* @return mixed |
|
125
|
|
|
*/ |
|
126
|
|
|
private function loadOrganizationTheme(string $themeName) |
|
127
|
|
|
{ |
|
128
|
|
|
$loadedThemes = $this->container->get('swp_core.loader.organization.theme')->load(); |
|
129
|
|
|
|
|
130
|
|
|
return $this->filterThemes($loadedThemes, $themeName); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $themeName |
|
135
|
|
|
* |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
private function loadTenantTheme(string $themeName) |
|
139
|
|
|
{ |
|
140
|
|
|
$loadedThemes = $this->container->get('sylius.repository.theme')->findAll(); |
|
141
|
|
|
|
|
142
|
|
|
return $this->filterThemes($loadedThemes, $themeName); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param array $loadedThemes |
|
147
|
|
|
* @param string $themeName |
|
148
|
|
|
* |
|
149
|
|
|
* @return mixed |
|
150
|
|
|
*/ |
|
151
|
|
|
private function filterThemes($loadedThemes, string $themeName) |
|
152
|
|
|
{ |
|
153
|
|
|
$themes = array_filter( |
|
154
|
|
|
$loadedThemes, |
|
155
|
|
|
function ($element) use (&$themeName) { |
|
156
|
|
|
return $element->getName() === $themeName; |
|
157
|
|
|
} |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
if (count($themes) === 0) { |
|
161
|
|
|
throw new NotFoundHttpException(sprintf('Theme with name "%s" was not found in organization themes.', $themeName)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return reset($themes); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
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.