-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProcessor.php
155 lines (133 loc) · 3.99 KB
/
Processor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Baldwin\LessJsCompiler\Css\PreProcessor\Adapter\Less;
use Psr\Log\LoggerInterface;
use Magento\Framework\Phrase;
use Magento\Framework\App\State;
use Magento\Framework\ShellInterface;
use Magento\Framework\View\Asset\File;
use Magento\Framework\View\Asset\Source;
use Magento\Framework\Css\PreProcessor\File\Temporary;
use Magento\Framework\View\Asset\ContentProcessorException;
use Magento\Framework\View\Asset\ContentProcessorInterface;
/**
* Class Processor
*/
class Processor implements ContentProcessorInterface
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var State
*/
private $appState;
/**
* @var Source
*/
private $assetSource;
/**
* @var Temporary
*/
private $temporaryFile;
/**
* @var ShellInterface
*/
private $shell;
/**
* Constructor
*
* @param LoggerInterface $logger
* @param State $appState
* @param Source $assetSource
* @param Temporary $temporaryFile
* @param ShellInterface $shell
*/
public function __construct(
LoggerInterface $logger,
State $appState,
Source $assetSource,
Temporary $temporaryFile,
ShellInterface $shell
) {
$this->logger = $logger;
$this->appState = $appState;
$this->assetSource = $assetSource;
$this->temporaryFile = $temporaryFile;
$this->shell = $shell;
}
/**
* {@inheritdoc}
* @throws ContentProcessorException
*/
public function processContent(File $asset)
{
$path = $asset->getPath();
try
{
$content = $this->assetSource->getContent($asset);
if (trim($content) === '') {
return '';
}
$tmpFilePath = $this->temporaryFile->createFile($path, $content);
$content = $this->compileFile($tmpFilePath);
if (trim($content) === '') {
$errorMessage = PHP_EOL . self::ERROR_MESSAGE_PREFIX . PHP_EOL . $path;
$this->logger->critical($errorMessage);
throw new ContentProcessorException(new Phrase($errorMessage));
}
return $content;
}
catch (\Exception $e)
{
$previousExceptionMessage = $e->getPrevious() !== null ? (PHP_EOL . $e->getPrevious()->getMessage()) : '';
$errorMessage = PHP_EOL . self::ERROR_MESSAGE_PREFIX .
PHP_EOL . $path .
PHP_EOL . $e->getMessage() . $previousExceptionMessage;
$this->logger->critical($errorMessage);
throw new ContentProcessorException(new Phrase($errorMessage));
}
}
/**
* Compiles less file and returns output as a string
*
* @param string $filePath
* @return string
*/
protected function compileFile($filePath)
{
$cmdArgs = $this->getCompilerArgsAsString();
$cmd = "%s $cmdArgs %s";
// to log or not to log, that's the question
// also, it would be better to use the logger in the Shell class,
// since that one will contain the exact correct command, and not this sprintf version
// $this->logger->debug('Less compilation command: `'
// . sprintf($cmd, $this->getPathToLessCompiler(), $filePath)
// . '`');
return $this->shell->execute($cmd,
[
$this->getPathToLessCompiler(),
$filePath,
]
);
}
/**
* Get all arguments which will be used in the cli call to the lessc compiler
*
* @return string
*/
protected function getCompilerArgsAsString()
{
$args = []; // for example: --no-ie-compat, --no-js, --compress, ...
return implode(' ', $args);
}
/**
* Get the path to the lessc nodejs compiler
*
* @return string
*/
protected function getPathToLessCompiler()
{
return BP . '/node_modules/.bin/lessc';
}
}