-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathStyle.php
executable file
·110 lines (90 loc) · 2.61 KB
/
Style.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
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\ViewModel;
use Magento\Framework\View\Asset\Source;
use Magento\Framework\View\Asset\Repository as AssetRepository;
use Magefan\Blog\Model\Config;
/**
* Class AbstractCss
*/
class Style implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
/**
* @var \Magento\Framework\View\Asset\Repository
*/
private $assetRepository;
/**
* @var Source
*/
private $source;
/**
* @var array
*/
private $done = [];
/**
* @var Config
*/
private $config;
/**
* @param Source $source
* @param AssetRepository $assetRepository
* @param Config $config
*/
public function __construct(
Source $source,
AssetRepository $assetRepository,
Config $config
) {
$this->source = $source;
$this->assetRepository = $assetRepository;
$this->config = $config;
}
/**
* @return null|string
*/
public function getStyle($file)
{
if (strpos($file, 'bootstrap-4.4.1-custom-min.css') !== false && !$this->config->getIncludeBootstrapCustomMini()) {
return '';
}
if (isset($this->done[$file])) {
return '';
}
$this->done[$file] = true;
if (false === strpos($file, '::')) {
$file = 'Magefan_Blog::css/' . $file;
}
if (false === strpos($file, '.css')) {
$file = $file . '.css';
}
$shortFileName = $file;
$asset = $this->assetRepository->createAsset($file);
$fileContent = '';
$file = $this->source->getFile($asset);
if (!$file || !file_exists($file)) {
$file = $this->source->findRelativeSourceFilePath($asset);
if ($file && !file_exists($file)) {
$file = '../' . $file;
}
}
if ($file && file_exists($file)) {
$fileContent = file_get_contents($file);
}
$fileContent = str_replace(
'url(../',
' url(' . dirname($asset->getUrl('')) . '/../',
$fileContent
);
if (!trim($fileContent)) {
$fileContent = '/* ' . $shortFileName . '.css is empty */';
}
return PHP_EOL . '
<!-- Start CSS ' . $shortFileName . ' ' . ((int)(strlen($fileContent) / 1024)) . 'Kb -->
<style>' . $fileContent . '</style>';
}
}