Skip to content

RC1 - cleanup #3

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 3 commits into from
Jun 14, 2016
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public static function compileCSS(Event $event)

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

exec('rm -rf ' . __DIR__ . '/var/*');

foreach ($configs as $config) {
if (!is_array($config)) {
throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
Expand All @@ -38,7 +36,7 @@ public static function compileCSS(Event $event)
$processor->attachFiles(__DIR__ . "/{$value}", __DIR__ . "/{$config['output']}");
}

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

$processor->saveOutput();
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "eugenematvejev/css_compiler",
"name": "eugene-matvejev/css-compiler",
"description": "compiles css assets from sass or less on composer events",
"type": "lib",
"license": "MIT",
Expand All @@ -21,7 +21,7 @@
}
},
"require": {
"php": ">= 7.0.4",
"php": ">= 5.5.9",
"leafo/lessphp": "^0.5",
"leafo/scssphp": "0.6.3 as dev-master",
"leafo/scssphp-compass": "dev-master"
Expand All @@ -45,15 +45,13 @@
"css-compiler": [
{
"format": "compact",
"force": true,
"input": [
"tests/shared-fixtures/scss"
],
"output": "var/cache/assets/scss.css"
},
{
"format": "compact",
"force": true,
"input": [
"tests/shared-fixtures/sass"
],
Expand Down
80 changes: 67 additions & 13 deletions src/Container/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,81 +31,131 @@ class File
*/
private $type;

public function __construct(string $sourcePath, string $outputPath)
/**
* @param string $sourcePath
* @param string $outputPath
*/
public function __construct($sourcePath, $outputPath)
{
$this->setSourcePath($sourcePath);
$this->outputPath = $outputPath;
}

public function getSourcePath() : string
public function getSourcePath()
{
return $this->sourcePath;
}

public function setSourcePath(string $path) : self
/**
* @param string $path
*
* @return File
*/
public function setSourcePath($path)
{
$this->sourcePath = $path;
$this->detectSourceTypeFromPath($path);

return $this;
}

public function getOutputPath() : string
/**
* @return string
*/
public function getOutputPath()
{
return $this->outputPath;
}

public function setOutputPath(string $path) : self
/**
* @param string $path
*
* @return File
*/
public function setOutputPath($path)
{
$this->outputPath = $path;

return $this;
}

public function getSourceContent() : string
/**
* @return string
*/
public function getSourceContent()
{
return $this->sourceContent;
}

public function setSourceContent(string $content) : self
/**
* @param string $content
*
* @return File
*/
public function setSourceContent($content)
{
$this->sourceContent = $content;

return $this;
}

public function setSourceContentFromSourcePath() : self
/**
* @return File
* @throws FileException
*/
public function setSourceContentFromSourcePath()
{
$this->sourceContent = $this->readSourceContentByPath();

return $this;
}

public function getParsedContent() : string
/**
* @return string
*/
public function getParsedContent()
{
return $this->parsedContent;
}

public function setParsedContent(string $content) : self
/**
* @param string $content
*
* @return File
*/
public function setParsedContent($content)
{
$this->parsedContent = $content;

return $this;
}

public function getType() : string
/**
* @return string
*/
public function getType()
{
return $this->type;
}

public function setType(string $type) : self
/**
* @param string $type
*
* @return File
*/
public function setType($type)
{
$this->type = $type;

return $this;
}

private function detectSourceTypeFromPath(string $path)
/**
* @param string $path
*
* @return void
*/
private function detectSourceTypeFromPath($path)
{
switch (true) {
case 0 !== preg_match('/^.*\.' . static::TYPE_SCSS . '/', $path):
Expand All @@ -125,6 +175,10 @@ private function detectSourceTypeFromPath(string $path)
}
}

/**
* @return string
* @throws FileException
*/
private function readSourceContentByPath()
{
if (!file_exists($this->getSourcePath())) {
Expand Down
24 changes: 21 additions & 3 deletions src/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ protected function initCompilers()
$this->compass = new CompassCompiler($this->sass);
}

public function attachFiles(string $inputPath, string $outputPath)
/**
* @param string $inputPath
* @param string $outputPath
*
* @throws \Exception
*/
public function attachFiles($inputPath, $outputPath)
{
if (is_dir($inputPath)) {
$files = scandir($inputPath);
Expand All @@ -71,7 +77,10 @@ public function attachFiles(string $inputPath, string $outputPath)
}
}

public function concatOutput() : array
/**
* @return string[]
*/
public function concatOutput()
{
$outputMap = [];
foreach ($this->files as $file) {
Expand All @@ -85,6 +94,9 @@ public function concatOutput() : array
return $outputMap;
}

/**
* save output into file
*/
public function saveOutput()
{
foreach ($this->concatOutput() as $path => $content) {
Expand All @@ -100,7 +112,12 @@ public function saveOutput()
}
}

public function processFiles(string $formatter)
/**
* @param string $formatter
*
* @throws CompilerException
*/
public function processFiles($formatter)
{
switch ($formatter) {
case 'compressed':
Expand Down Expand Up @@ -135,6 +152,7 @@ public function processFiles(string $formatter)
case static::TYPE_COMPASS:
case static::TYPE_SCSS:
case static::TYPE_SASS:
$this->sass->setFormatter($formatter);
$content = $this->sass->compile($file->getSourceContent());
break;
case static::TYPE_LESS:
Expand Down