Skip to content

Commit cbd3fd5

Browse files
author
Eugene Matvejev
authored
Merge pull request #52 from eugene-matvejev/refactoring_file_container
RC7 huge refactoring, improved validations, autoloading, tests
2 parents 29fb7d3 + e1c0293 commit cbd3fd5

File tree

7 files changed

+256
-66
lines changed

7 files changed

+256
-66
lines changed

ScriptHandler.php

+6-51
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,15 @@
33
namespace EM\CssCompiler\Handler;
44

55
use Composer\Script\Event;
6-
use EM\CssCompiler\Processor\Processor;
6+
use EM\CssCompiler\ScriptHandler as Handler;
77

8-
class ScriptHandler
8+
/**
9+
* @deprecated
10+
*/
11+
class ScriptHandler extends Handler
912
{
10-
const CONFIG_MAIN_KEY = 'css-compiler';
11-
const CONFIG_INPUT_KEY = 'input';
12-
const CONFIG_OUTPUT_KEY = 'output';
13-
const CONFIG_FORMATTER_KEY = 'format';
14-
const CONFIG_DEFAULT_FORMATTER = 'compact';
15-
1613
public static function compileCSS(Event $event)
1714
{
18-
$extra = $event->getComposer()->getPackage()->getExtra();
19-
20-
static::validateConfiguration($extra);
21-
22-
$processor = new Processor($event->getIO());
23-
$currentDirectory = getcwd();
24-
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
25-
foreach ($config[static::CONFIG_INPUT_KEY] as $item => $value) {
26-
$processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::CONFIG_OUTPUT_KEY]}");
27-
}
28-
29-
$formatter = isset($config[static::CONFIG_FORMATTER_KEY])
30-
? $config[static::CONFIG_FORMATTER_KEY]
31-
: static::CONFIG_DEFAULT_FORMATTER;
32-
33-
$processor->processFiles($formatter);
34-
}
35-
36-
$processor->saveOutput();
37-
}
38-
39-
protected static function validateConfiguration(array $config)
40-
{
41-
if (!isset($config[static::CONFIG_MAIN_KEY])) {
42-
throw new \InvalidArgumentException('the parameter handler needs to be configured through the extra.css-compiler setting.');
43-
}
44-
45-
if (!is_array($config[static::CONFIG_MAIN_KEY])) {
46-
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of a configuration objects.');
47-
}
48-
49-
foreach ($config[static::CONFIG_MAIN_KEY] as $el) {
50-
if (!is_array($el)) {
51-
throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
52-
}
53-
54-
if (!isset($el[static::CONFIG_INPUT_KEY])) {
55-
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_INPUT_KEY . ' is required!');
56-
}
57-
if (!isset($el[static::CONFIG_OUTPUT_KEY])) {
58-
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_OUTPUT_KEY . ' is required!');
59-
}
60-
}
15+
static::generateCSS($event);
6116
}
6217
}

src/Container/File.php renamed to src/Container/FileContainer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @since 0.1
99
*/
10-
class File
10+
class FileContainer
1111
{
1212
const TYPE_UNKNOWN = 'unknown';
1313
const TYPE_COMPASS = 'compass';

src/Processor/Processor.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace EM\CssCompiler\Processor;
44

55
use Composer\IO\IOInterface;
6-
use EM\CssCompiler\Container\File;
6+
use EM\CssCompiler\Container\FileContainer;
77
use EM\CssCompiler\Exception\CompilerException;
88
use Leafo\ScssPhp\Compiler as SASSCompiler;
99
use lessc as LESSCompiler;
@@ -31,7 +31,7 @@ class Processor
3131
*/
3232
private $io;
3333
/**
34-
* @var File[]
34+
* @var FileContainer[]
3535
*/
3636
private $files = [];
3737
/**
@@ -73,14 +73,14 @@ public function attachFiles($inputPath, $outputPath)
7373
$this->attachFiles("$inputPath/$file", $outputPath);
7474
}
7575
} else if (is_file($inputPath)) {
76-
$this->files[] = new File($inputPath, $outputPath);
76+
$this->files[] = new FileContainer($inputPath, $outputPath);
7777
} else {
7878
throw new \Exception("file doesn't exists");
7979
}
8080
}
8181

8282
/**
83-
* @return File[]
83+
* @return FileContainer[]
8484
*/
8585
public function getFiles()
8686
{
@@ -149,14 +149,14 @@ public function processFiles($formatter)
149149
* @return File
150150
* @throws CompilerException
151151
*/
152-
public function processFile(File $file)
152+
public function processFile(FileContainer $file)
153153
{
154154
switch ($file->getType()) {
155-
case File::TYPE_COMPASS:
156-
case File::TYPE_SCSS:
157-
case File::TYPE_SASS:
155+
case FileContainer::TYPE_COMPASS:
156+
case FileContainer::TYPE_SCSS:
157+
case FileContainer::TYPE_SASS:
158158
return $file->setParsedContent($this->sass->compile($file->getSourceContent()));
159-
case File::TYPE_LESS:
159+
case FileContainer::TYPE_LESS:
160160
return $file->setParsedContent($this->less->compile($file->getSourceContent()));
161161
}
162162

src/ScriptHandler.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace EM\CssCompiler;
4+
5+
use Composer\Script\Event;
6+
use EM\CssCompiler\Processor\Processor;
7+
8+
class ScriptHandler
9+
{
10+
const CONFIG_MAIN_KEY = 'css-compiler';
11+
const OPTION_KEY_INPUT = 'input';
12+
const OPTION_KEY_OUTPUT = 'output';
13+
const OPTION_KEY_FORMATTER = 'format';
14+
const DEFAULT_OPTION_FORMATTER = 'compact';
15+
protected static $mandatoryOptions = [
16+
self::OPTION_KEY_INPUT,
17+
self::OPTION_KEY_OUTPUT
18+
];
19+
20+
/**
21+
* @param Event $event
22+
*
23+
* @throws \InvalidArgumentException
24+
*/
25+
public static function generateCSS(Event $event)
26+
{
27+
$extra = $event->getComposer()->getPackage()->getExtra();
28+
static::validateConfiguration($extra);
29+
30+
$processor = new Processor($event->getIO());
31+
$currentDirectory = getcwd();
32+
33+
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
34+
foreach ($config[static::OPTION_KEY_INPUT] as $value) {
35+
$processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::OPTION_KEY_OUTPUT]}");
36+
}
37+
38+
$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
39+
40+
$processor->processFiles($formatter);
41+
}
42+
$processor->saveOutput();
43+
}
44+
45+
/**
46+
* @param array $config
47+
*
48+
* @return bool
49+
* @throws \InvalidArgumentException
50+
*/
51+
protected static function validateConfiguration(array $config)
52+
{
53+
if (empty($config[static::CONFIG_MAIN_KEY])) {
54+
throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting');
55+
}
56+
57+
if (!is_array($config[static::CONFIG_MAIN_KEY])) {
58+
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
59+
}
60+
61+
foreach ($config[static::CONFIG_MAIN_KEY] as $index => $el) {
62+
if (!is_array($el)) {
63+
throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array');
64+
}
65+
66+
static::validateOptions($el);
67+
}
68+
69+
return true;
70+
}
71+
72+
/**
73+
* @param array $config
74+
*
75+
* @return bool
76+
* @throws \InvalidArgumentException
77+
*/
78+
protected static function validateOptions(array $config)
79+
{
80+
foreach (static::$mandatoryOptions as $option) {
81+
if (empty($config[$option])) {
82+
throw new \InvalidArgumentException("The extra.css-compiler[].{$option} required!");
83+
}
84+
}
85+
if (!is_array($config[static::OPTION_KEY_INPUT])) {
86+
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_INPUT . ' should be array!');
87+
}
88+
if (!is_string($config[static::OPTION_KEY_OUTPUT])) {
89+
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!');
90+
}
91+
92+
return true;
93+
}
94+
}

tests/phpunit/Container/File.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EM\CssCompiler\Container;
4+
5+
class FileContainerTest
6+
{
7+
}

tests/phpunit/ProcessorTest.php renamed to tests/phpunit/Processor/ProcessorTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace EM\Tests\PHPUnit;
3+
namespace EM\Tests\PHPUnit\Processor;
44

55
use Composer\IO\IOInterface;
6-
use EM\CssCompiler\Container\File;
6+
use EM\CssCompiler\Container\FileContainer;
77
use EM\CssCompiler\Processor\Processor;
88
use EM\Tests\Environment\IntegrationTestSuite;
99

@@ -64,7 +64,7 @@ public function attachFilesExpectedException()
6464
*/
6565
public function processFileSASS()
6666
{
67-
$file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/layout.scss', ''))
67+
$file = (new FileContainer(static::getSharedFixturesDirectory() . '/compass/sass/layout.scss', ''))
6868
->setSourceContentFromSourcePath();
6969

7070
(new Processor($this->io))->processFile($file);
@@ -80,9 +80,9 @@ public function processFileSASS()
8080
*/
8181
public function processFileExpectedException()
8282
{
83-
$file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/', ''))
83+
$file = (new FileContainer(static::getSharedFixturesDirectory() . '/compass/sass/', ''))
8484
->setSourceContentFromSourcePath()
85-
->setType(File::TYPE_UNKNOWN);
85+
->setType(FileContainer::TYPE_UNKNOWN);
8686

8787
(new Processor($this->io))->processFile($file);
8888
}

0 commit comments

Comments
 (0)