|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2018 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2018 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Theme\Service; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\CoreBundle\Model\TenantInterface; |
|
20
|
|
|
use SWP\Bundle\CoreBundle\Theme\Installer\ThemeInstallerInterface; |
|
21
|
|
|
use SWP\Bundle\CoreBundle\Theme\Model\ThemeInterface; |
|
22
|
|
|
use SWP\Bundle\CoreBundle\Theme\Processor\RequiredDataProcessorInterface; |
|
23
|
|
|
use SWP\Bundle\CoreBundle\Theme\Repository\ReloadableThemeRepositoryInterface; |
|
24
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
|
25
|
|
|
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface; |
|
26
|
|
|
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface; |
|
27
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class InstallThemeService. |
|
31
|
|
|
*/ |
|
32
|
|
|
final class ThemeService implements ThemeServiceInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var ThemeInstallerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $themeInstaller; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var RequiredDataProcessorInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $requiredDataProcessor; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $cacheDir; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var TenantContextInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $tenantContext; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var ThemeRepositoryInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
private $themeRepository; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var ThemeContextInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
private $themeContext; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* ThemeService constructor. |
|
66
|
|
|
* |
|
67
|
|
|
* @param ThemeInstallerInterface $themeInstaller |
|
68
|
|
|
* @param RequiredDataProcessorInterface $requiredDataProcessor |
|
69
|
|
|
* @param string $cacheDir |
|
70
|
|
|
* @param TenantContextInterface $tenantContext |
|
71
|
|
|
* @param ReloadableThemeRepositoryInterface $themeRepository |
|
72
|
|
|
* @param ThemeContextInterface $themeContext |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct( |
|
75
|
|
|
ThemeInstallerInterface $themeInstaller, |
|
76
|
|
|
RequiredDataProcessorInterface $requiredDataProcessor, |
|
77
|
|
|
string $cacheDir, |
|
78
|
|
|
TenantContextInterface $tenantContext, |
|
79
|
|
|
ReloadableThemeRepositoryInterface $themeRepository, |
|
80
|
|
|
ThemeContextInterface $themeContext |
|
81
|
|
|
) { |
|
82
|
|
|
$this->themeInstaller = $themeInstaller; |
|
83
|
|
|
$this->requiredDataProcessor = $requiredDataProcessor; |
|
84
|
|
|
$this->cacheDir = $cacheDir; |
|
85
|
|
|
$this->tenantContext = $tenantContext; |
|
86
|
|
|
$this->themeRepository = $themeRepository; |
|
87
|
|
|
$this->themeContext = $themeContext; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function installAndProcessGeneratedData(string $sourceDir, string $themeDir) |
|
94
|
|
|
{ |
|
95
|
|
|
$messages = []; |
|
96
|
|
|
/** @var TenantInterface $tenant */ |
|
97
|
|
|
$tenant = $this->tenantContext->getTenant(); |
|
98
|
|
|
$backupThemeDir = $this->cacheDir.\DIRECTORY_SEPARATOR.'backup'.\DIRECTORY_SEPARATOR.'themes'.\DIRECTORY_SEPARATOR.$tenant->getCode().\DIRECTORY_SEPARATOR.\basename($themeDir).'_previous'; |
|
99
|
|
|
$fileSystem = new Filesystem(); |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
if ($fileSystem->exists($themeDir)) { |
|
103
|
|
|
$fileSystem->rename($themeDir, $backupThemeDir, true); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->themeInstaller->install($sourceDir, $themeDir); |
|
107
|
|
|
$messages[] = 'Theme has been installed successfully!'; |
|
108
|
|
|
$this->themeRepository->reloadThemes(); |
|
|
|
|
|
|
109
|
|
|
if (file_exists($themeDir.\DIRECTORY_SEPARATOR.'theme.json')) { |
|
110
|
|
|
$themeName = json_decode(file_get_contents($themeDir.\DIRECTORY_SEPARATOR.'theme.json'), true)['name']; |
|
111
|
|
|
$tenant->setThemeName($themeName); |
|
112
|
|
|
/** @var ThemeInterface $theme */ |
|
113
|
|
|
$theme = $this->themeContext->getTheme(); |
|
114
|
|
|
$this->requiredDataProcessor->processTheme($theme); |
|
115
|
|
|
$messages[] = 'Required data were generated and persisted successfully'; |
|
116
|
|
|
} |
|
117
|
|
|
} catch (\Exception $e) { |
|
118
|
|
|
$fileSystem->remove($themeDir); |
|
119
|
|
|
if ($fileSystem->exists($backupThemeDir)) { |
|
120
|
|
|
$fileSystem->rename($backupThemeDir, $themeDir); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $e; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ($fileSystem->exists($backupThemeDir)) { |
|
127
|
|
|
$fileSystem->remove($backupThemeDir); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $messages; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getDirectoriesForTheme(string $themeName): array |
|
137
|
|
|
{ |
|
138
|
|
|
/** @var ThemeInterface $theme */ |
|
139
|
|
|
$theme = $this->themeInstaller->getThemeFromOrganizationThemes($themeName); |
|
140
|
|
|
$sourceDir = $theme->getPath(); |
|
141
|
|
|
$themeDir = $this->themeInstaller->getThemesPath().DIRECTORY_SEPARATOR.\basename($sourceDir); |
|
142
|
|
|
|
|
143
|
|
|
return [$sourceDir, $themeDir]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: