Skip to content

RC7 - huge refactoring, improved validations, autoloading, tests #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 22, 2016
57 changes: 6 additions & 51 deletions ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,15 @@
namespace EM\CssCompiler\Handler;

use Composer\Script\Event;
use EM\CssCompiler\Processor\Processor;
use EM\CssCompiler\ScriptHandler as Handler;

class ScriptHandler
/**
* @deprecated
*/
class ScriptHandler extends Handler
{
const CONFIG_MAIN_KEY = 'css-compiler';
const CONFIG_INPUT_KEY = 'input';
const CONFIG_OUTPUT_KEY = 'output';
const CONFIG_FORMATTER_KEY = 'format';
const CONFIG_DEFAULT_FORMATTER = 'compact';

public static function compileCSS(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();

static::validateConfiguration($extra);

$processor = new Processor($event->getIO());
$currentDirectory = getcwd();
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
foreach ($config[static::CONFIG_INPUT_KEY] as $item => $value) {
$processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::CONFIG_OUTPUT_KEY]}");
}

$formatter = isset($config[static::CONFIG_FORMATTER_KEY])
? $config[static::CONFIG_FORMATTER_KEY]
: static::CONFIG_DEFAULT_FORMATTER;

$processor->processFiles($formatter);
}

$processor->saveOutput();
}

protected static function validateConfiguration(array $config)
{
if (!isset($config[static::CONFIG_MAIN_KEY])) {
throw new \InvalidArgumentException('the parameter handler needs to be configured through the extra.css-compiler setting.');
}

if (!is_array($config[static::CONFIG_MAIN_KEY])) {
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of a configuration objects.');
}

foreach ($config[static::CONFIG_MAIN_KEY] as $el) {
if (!is_array($el)) {
throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
}

if (!isset($el[static::CONFIG_INPUT_KEY])) {
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_INPUT_KEY . ' is required!');
}
if (!isset($el[static::CONFIG_OUTPUT_KEY])) {
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_OUTPUT_KEY . ' is required!');
}
}
static::generateCSS($event);
}
}
2 changes: 1 addition & 1 deletion src/Container/File.php → src/Container/FileContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @since 0.1
*/
class File
class FileContainer
{
const TYPE_UNKNOWN = 'unknown';
const TYPE_COMPASS = 'compass';
Expand Down
18 changes: 9 additions & 9 deletions src/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace EM\CssCompiler\Processor;

use Composer\IO\IOInterface;
use EM\CssCompiler\Container\File;
use EM\CssCompiler\Container\FileContainer;
use EM\CssCompiler\Exception\CompilerException;
use Leafo\ScssPhp\Compiler as SASSCompiler;
use lessc as LESSCompiler;
Expand Down Expand Up @@ -31,7 +31,7 @@ class Processor
*/
private $io;
/**
* @var File[]
* @var FileContainer[]
*/
private $files = [];
/**
Expand Down Expand Up @@ -73,14 +73,14 @@ public function attachFiles($inputPath, $outputPath)
$this->attachFiles("$inputPath/$file", $outputPath);
}
} else if (is_file($inputPath)) {
$this->files[] = new File($inputPath, $outputPath);
$this->files[] = new FileContainer($inputPath, $outputPath);
} else {
throw new \Exception("file doesn't exists");
}
}

/**
* @return File[]
* @return FileContainer[]
*/
public function getFiles()
{
Expand Down Expand Up @@ -149,14 +149,14 @@ public function processFiles($formatter)
* @return File
* @throws CompilerException
*/
public function processFile(File $file)
public function processFile(FileContainer $file)
{
switch ($file->getType()) {
case File::TYPE_COMPASS:
case File::TYPE_SCSS:
case File::TYPE_SASS:
case FileContainer::TYPE_COMPASS:
case FileContainer::TYPE_SCSS:
case FileContainer::TYPE_SASS:
return $file->setParsedContent($this->sass->compile($file->getSourceContent()));
case File::TYPE_LESS:
case FileContainer::TYPE_LESS:
return $file->setParsedContent($this->less->compile($file->getSourceContent()));
}

Expand Down
94 changes: 94 additions & 0 deletions src/ScriptHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace EM\CssCompiler;

use Composer\Script\Event;
use EM\CssCompiler\Processor\Processor;

class ScriptHandler
{
const CONFIG_MAIN_KEY = 'css-compiler';
const OPTION_KEY_INPUT = 'input';
const OPTION_KEY_OUTPUT = 'output';
const OPTION_KEY_FORMATTER = 'format';
const DEFAULT_OPTION_FORMATTER = 'compact';
protected static $mandatoryOptions = [
self::OPTION_KEY_INPUT,
self::OPTION_KEY_OUTPUT
];

/**
* @param Event $event
*
* @throws \InvalidArgumentException
*/
public static function generateCSS(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();
static::validateConfiguration($extra);

$processor = new Processor($event->getIO());
$currentDirectory = getcwd();

foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
foreach ($config[static::OPTION_KEY_INPUT] as $value) {
$processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::OPTION_KEY_OUTPUT]}");
}

$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;

$processor->processFiles($formatter);
}
$processor->saveOutput();
}

/**
* @param array $config
*
* @return bool
* @throws \InvalidArgumentException
*/
protected static function validateConfiguration(array $config)
{
if (empty($config[static::CONFIG_MAIN_KEY])) {
throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting');
}

if (!is_array($config[static::CONFIG_MAIN_KEY])) {
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
}

foreach ($config[static::CONFIG_MAIN_KEY] as $index => $el) {
if (!is_array($el)) {
throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array');
}

static::validateOptions($el);
}

return true;
}

/**
* @param array $config
*
* @return bool
* @throws \InvalidArgumentException
*/
protected static function validateOptions(array $config)
{
foreach (static::$mandatoryOptions as $option) {
if (empty($config[$option])) {
throw new \InvalidArgumentException("The extra.css-compiler[].{$option} required!");
}
}
if (!is_array($config[static::OPTION_KEY_INPUT])) {
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_INPUT . ' should be array!');
}
if (!is_string($config[static::OPTION_KEY_OUTPUT])) {
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!');
}

return true;
}
}
7 changes: 7 additions & 0 deletions tests/phpunit/Container/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EM\CssCompiler\Container;

class FileContainerTest
{
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace EM\Tests\PHPUnit;
namespace EM\Tests\PHPUnit\Processor;

use Composer\IO\IOInterface;
use EM\CssCompiler\Container\File;
use EM\CssCompiler\Container\FileContainer;
use EM\CssCompiler\Processor\Processor;
use EM\Tests\Environment\IntegrationTestSuite;

Expand Down Expand Up @@ -64,7 +64,7 @@ public function attachFilesExpectedException()
*/
public function processFileSASS()
{
$file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/layout.scss', ''))
$file = (new FileContainer(static::getSharedFixturesDirectory() . '/compass/sass/layout.scss', ''))
->setSourceContentFromSourcePath();

(new Processor($this->io))->processFile($file);
Expand All @@ -80,9 +80,9 @@ public function processFileSASS()
*/
public function processFileExpectedException()
{
$file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/', ''))
$file = (new FileContainer(static::getSharedFixturesDirectory() . '/compass/sass/', ''))
->setSourceContentFromSourcePath()
->setType(File::TYPE_UNKNOWN);
->setType(FileContainer::TYPE_UNKNOWN);

(new Processor($this->io))->processFile($file);
}
Expand Down
Loading