forked from Sylius/Sylius
/
OutputAwareAssetsInstaller.php
119 lines (100 loc) · 3.65 KB
/
OutputAwareAssetsInstaller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ThemeBundle\Asset\Installer;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
/**
* @author Kamil Kokot <kamil@kokot.me>
*/
final class OutputAwareAssetsInstaller implements AssetsInstallerInterface, OutputAwareInterface
{
/**
* @var AssetsInstallerInterface
*/
private $assetsInstaller;
/**
* @var OutputInterface
*/
private $output;
/**
* @param AssetsInstallerInterface $assetsInstaller
*/
public function __construct(AssetsInstallerInterface $assetsInstaller)
{
$this->assetsInstaller = $assetsInstaller;
$this->output = new NullOutput();
}
/**
* {@inheritdoc}
*/
public function setOutput(OutputInterface $output): void
{
$this->output = $output;
}
/**
* {@inheritdoc}
*/
public function installAssets(string $targetDir, int $symlinkMask): int
{
$this->output->writeln($this->provideExpectationComment($symlinkMask));
return $this->assetsInstaller->installAssets($targetDir, $symlinkMask);
}
/**
* {@inheritdoc}
*/
public function installBundleAssets(BundleInterface $bundle, string $targetDir, int $symlinkMask): int
{
$this->output->writeln(sprintf('Installing assets for <comment>%s</comment>', $bundle->getNamespace(), $targetDir));
$effectiveSymlinkMask = $this->assetsInstaller->installBundleAssets($bundle, $targetDir, $symlinkMask);
$this->output->writeln($this->provideResultComment($symlinkMask, $effectiveSymlinkMask));
return $effectiveSymlinkMask;
}
/**
* @param int $symlinkMask
* @param int $effectiveSymlinkMask
*
* @return string
*/
private function provideResultComment(int $symlinkMask, int $effectiveSymlinkMask): string
{
if ($effectiveSymlinkMask === $symlinkMask) {
switch ($symlinkMask) {
case AssetsInstallerInterface::HARD_COPY:
return 'The assets were copied.';
case AssetsInstallerInterface::SYMLINK:
return 'The assets were installed using symbolic links.';
case AssetsInstallerInterface::RELATIVE_SYMLINK:
return 'The assets were installed using relative symbolic links.';
}
}
switch ($symlinkMask + $effectiveSymlinkMask) {
case AssetsInstallerInterface::SYMLINK:
case AssetsInstallerInterface::RELATIVE_SYMLINK:
return 'It looks like your system doesn\'t support symbolic links, so the assets were copied.';
case AssetsInstallerInterface::RELATIVE_SYMLINK + AssetsInstallerInterface::SYMLINK:
return 'It looks like your system doesn\'t support relative symbolic links, so the assets were installed by using absolute symbolic links.';
}
return 'Something gone bad, can\'t provide the result of assets installing!';
}
/**
* @param int $symlinkMask
*
* @return string
*/
private function provideExpectationComment(int $symlinkMask): string
{
if (AssetsInstallerInterface::HARD_COPY === $symlinkMask) {
return 'Installing assets as <comment>hard copies</comment>.';
}
return 'Trying to install assets as <comment>symbolic links</comment>.';
}
}