Skip to content

RC7 - improve composer handler and its validation #51

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 1 commit into from
Jun 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,56 @@

class ScriptHandler
{
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)
{
$extras = $event->getComposer()->getPackage()->getExtra();
$extra = $event->getComposer()->getPackage()->getExtra();

static::validateConfiguration($extra);

if (!isset($extras['css-compiler'])) {
throw new \InvalidArgumentException('The parameter handler needs to be configured through the extra.css-compiler setting.');
$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);
}

$configs = $extras['css-compiler'];
$processor->saveOutput();
}

if (!is_array($configs)) {
throw new \InvalidArgumentException('The extra.css-compiler setting must be an array of a configuration objects.');
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 (array_keys($configs) !== range(0, count($configs) - 1)) {
$configs = [$configs];
if (!is_array($config[static::CONFIG_MAIN_KEY])) {
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of a configuration objects.');
}

$processor = new Processor($event->getIO());
$prefix = getcwd();
foreach ($configs as $config) {
if (!is_array($config)) {
foreach ($config[static::CONFIG_MAIN_KEY] as $el) {
if (!is_array($el)) {
throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
}

foreach ($config['input'] as $item => $value) {
$processor->attachFiles("{$prefix}/{$value}", "{$prefix}/{$config['output']}");
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!');
}

$processor->processFiles(isset($config['format']) ? $config['format'] : 'compact');
}

$processor->saveOutput();
}
}