Skip to content

Commit 2a773d1

Browse files
author
Eugene Matvejev
authored
Merge pull request #16 from learn-symfony/master
sync with main repository
2 parents ba8ccf9 + 0bd897c commit 2a773d1

File tree

12 files changed

+202
-334
lines changed

12 files changed

+202
-334
lines changed

src/Container/FileContainer.php

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,50 @@
22

33
namespace EM\CssCompiler\Container;
44

5-
use EM\CssCompiler\Exception\FileException;
6-
75
/**
6+
* @see FileContainerTest
7+
*
88
* @since 0.1
99
*/
1010
class FileContainer
1111
{
12-
const TYPE_UNKNOWN = 'unknown';
13-
const TYPE_COMPASS = 'compass';
14-
const TYPE_SASS = 'sass';
15-
const TYPE_SCSS = 'scss';
16-
const TYPE_LESS = 'less';
12+
const TYPE_UNKNOWN = 'unknown';
13+
const TYPE_SCSS = 'scss';
14+
const TYPE_LESS = 'less';
1715
static $supportedTypes = [
18-
self::TYPE_COMPASS,
19-
self::TYPE_SASS,
2016
self::TYPE_SCSS,
2117
self::TYPE_LESS
2218
];
2319
/**
2420
* @var string
2521
*/
26-
private $sourcePath;
22+
private $inputPath;
2723
/**
2824
* @var string
2925
*/
3026
private $outputPath;
3127
/**
3228
* @var string
3329
*/
34-
private $sourceContent;
30+
private $inputContent;
3531
/**
3632
* @var string
3733
*/
38-
private $parsedContent;
34+
private $outputContent;
3935
/**
4036
* @var string
4137
*/
4238
private $type;
4339

4440
/**
45-
* @param string $sourcePath
41+
* @param string $inputPath
4642
* @param string $outputPath
4743
*/
48-
public function __construct($sourcePath, $outputPath)
44+
public function __construct($inputPath, $outputPath)
4945
{
50-
$this->setSourcePath($sourcePath);
51-
$this->outputPath = $outputPath;
46+
$this
47+
->setInputPath($inputPath)
48+
->setOutputPath($outputPath);
5249
}
5350

5451
/**
@@ -62,7 +59,7 @@ public function getOutputPath()
6259
/**
6360
* @param string $path
6461
*
65-
* @return File
62+
* @return $this
6663
*/
6764
public function setOutputPath($path)
6865
{
@@ -74,81 +71,57 @@ public function setOutputPath($path)
7471
/**
7572
* @return string
7673
*/
77-
public function getSourceContent()
74+
public function getInputContent()
7875
{
79-
return $this->sourceContent;
76+
return $this->inputContent;
8077
}
8178

8279
/**
8380
* @param string $content
8481
*
85-
* @return File
82+
* @return $this
8683
*/
87-
public function setSourceContent($content)
84+
public function setInputContent($content)
8885
{
89-
$this->sourceContent = $content;
86+
$this->inputContent = $content;
9087

9188
return $this;
9289
}
9390

94-
/**
95-
* @return File
96-
* @throws FileException
97-
*/
98-
public function setSourceContentFromSourcePath()
91+
public function getInputPath()
9992
{
100-
$this->sourceContent = $this->readSourceContentByPath();
101-
102-
return $this;
103-
}
104-
105-
/**
106-
* @return string
107-
* @throws FileException
108-
*/
109-
protected function readSourceContentByPath()
110-
{
111-
if (!file_exists($this->getSourcePath())) {
112-
throw new FileException("file: {$this->sourcePath} doesn't exists");
113-
}
114-
115-
return file_get_contents($this->getSourcePath());
116-
}
117-
118-
public function getSourcePath()
119-
{
120-
return $this->sourcePath;
93+
return $this->inputPath;
12194
}
12295

12396
/**
12497
* @param string $path
12598
*
126-
* @return File
99+
* @return $this
127100
*/
128-
public function setSourcePath($path)
101+
public function setInputPath($path)
129102
{
130-
$this->sourcePath = $path;
131-
$this->type = $this->detectSourceTypeFromPath($path);
103+
$this->inputPath = $path;
104+
$this->detectInputTypeByInputPath();
132105

133106
return $this;
134107
}
135108

136109
/**
137110
* @return string
138111
*/
139-
public function getParsedContent()
112+
public function getOutputContent()
140113
{
141-
return $this->parsedContent;
114+
return $this->outputContent;
142115
}
143116

144117
/**
145118
* @param string $content
146119
*
147-
* @return File
120+
* @return $this
148121
*/
149-
public function setParsedContent($content)
122+
public function setOutputContent($content)
150123
{
151-
$this->parsedContent = $content;
124+
$this->outputContent = $content;
152125

153126
return $this;
154127
}
@@ -164,7 +137,7 @@ public function getType()
164137
/**
165138
* @param string $type
166139
*
167-
* @return File
140+
* @return $this
168141
*/
169142
public function setType($type)
170143
{
@@ -173,17 +146,16 @@ public function setType($type)
173146
return $this;
174147
}
175148

176-
/**
177-
* @param string $path
178-
*
179-
* @return string
180-
*/
181-
protected function detectSourceTypeFromPath($path)
149+
protected function detectInputTypeByInputPath()
182150
{
183-
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
151+
$extension = strtolower(pathinfo($this->getInputPath(), PATHINFO_EXTENSION));
184152

185-
return in_array($extension, static::$supportedTypes)
153+
$type = in_array($extension, static::$supportedTypes)
186154
? $extension
187155
: static::TYPE_UNKNOWN;
156+
157+
$this->setType($type);
158+
159+
return $this;
188160
}
189161
}

src/Processor/Processor.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Composer\IO\IOInterface;
66
use EM\CssCompiler\Container\FileContainer;
77
use EM\CssCompiler\Exception\CompilerException;
8+
use EM\CssCompiler\Exception\FileException;
89
use Leafo\ScssPhp\Compiler as SASSCompiler;
910
use lessc as LESSCompiler;
1011
use scss_compass as CompassCompiler;
@@ -98,7 +99,7 @@ protected function concatOutput()
9899
$outputMap[$file->getOutputPath()] = '';
99100
}
100101

101-
$outputMap[$file->getOutputPath()] .= $file->getParsedContent();
102+
$outputMap[$file->getOutputPath()] .= $file->getOutputContent();
102103
}
103104

104105
return $outputMap;
@@ -132,32 +133,33 @@ public function processFiles($formatter)
132133
$this->io->write("<info>use '{$formatter}' formatting</info>");
133134

134135
foreach ($this->files as $file) {
135-
$this->io->write("<info>processing</info>: {$file->getSourcePath()}");
136-
$file->setSourceContentFromSourcePath();
136+
$this->io->write("<info>processing</info>: {$file->getInputPath()}");
137+
$this->fetchInputContextIntoFile($file);
137138

138139
try {
139140
$this->processFile($file);
140141
} catch (CompilerException $e) {
141-
$this->io->writeError("<error>failed to process: {$file->getSourcePath()}</error>");
142+
$this->io->writeError("<error>failed to process: {$file->getOutputPath()}</error>");
142143
}
143144
}
144145
}
145146

146147
/**
147-
* @param File $file
148+
* @param FileContainer $file
148149
*
149-
* @return File
150+
* @return FileContainer
150151
* @throws CompilerException
151152
*/
152153
public function processFile(FileContainer $file)
153154
{
154155
switch ($file->getType()) {
155-
case FileContainer::TYPE_COMPASS:
156156
case FileContainer::TYPE_SCSS:
157-
case FileContainer::TYPE_SASS:
158-
return $file->setParsedContent($this->sass->compile($file->getSourceContent()));
157+
$this->sass->addImportPath(dirname($file->getInputPath()));
158+
$content = $this->sass->compile($file->getInputContent());
159+
160+
return $file->setOutputContent($content);
159161
case FileContainer::TYPE_LESS:
160-
return $file->setParsedContent($this->less->compile($file->getSourceContent()));
162+
return $file->setOutputContent($this->less->compileFile($file->getInputPath()));
161163
}
162164

163165
throw new CompilerException('unknown compiler');
@@ -176,4 +178,18 @@ protected function getFormatterClass($formatter)
176178

177179
return 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter);
178180
}
181+
182+
/**
183+
* @param FileContainer $file
184+
*
185+
* @throws FileException
186+
*/
187+
protected function fetchInputContextIntoFile(FileContainer $file)
188+
{
189+
if (!file_exists($file->getInputPath())) {
190+
throw new FileException("file: {$file->getInputPath()} doesn't exists");
191+
}
192+
193+
$file->setInputContent(file_get_contents($file->getInputPath()));
194+
}
179195
}

tests/phpunit/Container/File.php

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace EM\CssCompiler\Tests\Container;
4+
5+
use EM\CssCompiler\Container\FileContainer;
6+
use EM\CssCompiler\Tests\Environment\IntegrationTestSuite;
7+
8+
/**
9+
* @see FileContainer
10+
*/
11+
class FileContainerTest extends IntegrationTestSuite
12+
{
13+
/**
14+
* @see FileContainer::__constuct
15+
* @see FileContainer::TYPE_UNKNOWN
16+
*
17+
* @test
18+
*/
19+
public function constructOnUnknownType()
20+
{
21+
$this->invokeConstructor('input', 'output', FileContainer::TYPE_UNKNOWN);
22+
}
23+
24+
/**
25+
* @see FileContainer::__constuct
26+
* @see FileContainer::TYPE_SCSS
27+
*
28+
* @test
29+
*/
30+
public function constructOnSCSSType()
31+
{
32+
$this->invokeConstructor('input.scss', 'output', FileContainer::TYPE_SCSS);
33+
}
34+
35+
/**
36+
* @see FileContainer::__constuct
37+
* @see FileContainer::TYPE_LESS
38+
*
39+
* @test
40+
*/
41+
public function constructOnLESSType()
42+
{
43+
$this->invokeConstructor('input.less', 'output', FileContainer::TYPE_LESS);
44+
}
45+
46+
/**
47+
* as FileContainer can't exists without (in|out)put need to check that:
48+
* (in|out)put paths assigned successfully
49+
* (in|out)content is null
50+
* type should not be null and be detected using @see FileContainer::detectInputTypeByInputPath
51+
*
52+
* @param string $inputPath
53+
* @param string $outputPath
54+
* @param string $expectedType
55+
*/
56+
private function invokeConstructor($inputPath, $outputPath, $expectedType)
57+
{
58+
$file = new FileContainer($inputPath, $outputPath);
59+
60+
$this->assertEquals($inputPath, $file->getInputPath());
61+
$this->assertEquals($outputPath, $file->getOutputPath());
62+
63+
$this->assertNull($file->getOutputContent());
64+
$this->assertNull($file->getInputContent());
65+
66+
$this->assertNotNull($file->getType());
67+
$this->assertEquals($expectedType, $file->getType());
68+
}
69+
70+
/**
71+
* @see FileContainer::detectInputTypeByInputPath
72+
* @test
73+
*/
74+
public function detectInputTypeByInputPath()
75+
{
76+
$inputPaths = [
77+
'input.css' => FileContainer::TYPE_UNKNOWN,
78+
'input' => FileContainer::TYPE_UNKNOWN,
79+
'input.sass' => FileContainer::TYPE_UNKNOWN,
80+
'input.compass' => FileContainer::TYPE_UNKNOWN,
81+
'input.scss' => FileContainer::TYPE_SCSS,
82+
'input.less' => FileContainer::TYPE_LESS
83+
];
84+
85+
foreach ($inputPaths as $inputPath => $expectedType) {
86+
$file = new FileContainer($inputPath, '');
87+
$this->assertEquals($expectedType, $file->getType());
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)