Skip to content

Commit

Permalink
more readable style exception
Browse files Browse the repository at this point in the history
  • Loading branch information
cbl committed Jun 26, 2020
1 parent b51ce4d commit 08a914a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"require": {
"matthiasmullie/minify": "^1.3",
"cbl/blade-style-sass": "~0.0.2"
"cbl/blade-style-sass": "~0.1"
},
"autoload": {
"psr-4": {
Expand Down
53 changes: 52 additions & 1 deletion src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace BladeStyle\Compiler;

use BladeStyle\Engines\MinifierEngine;
use Throwable;
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use BladeStyle\Engines\MinifierEngine;
use BladeStyle\Exceptions\StyleException;
use Illuminate\View\Compilers\CompilerInterface;
use Illuminate\View\Compilers\Compiler as ViewCompiler;

Expand All @@ -16,6 +19,13 @@ abstract class Compiler extends ViewCompiler implements CompilerInterface
*/
protected $engine;

/**
* Path that is currently being compiled.
*
* @var string
*/
protected $currentPath;

/**
* Compile style string.
*
Expand Down Expand Up @@ -64,6 +74,8 @@ public function compile($path)
return;
}

$this->currentPath = $path;

$css = $this->compileString($raw);

if (config('style.minify')) {
Expand All @@ -74,6 +86,8 @@ public function compile($path)
$this->getCompiledPath($path),
$css
);

$this->currentPath = null;
}

/**
Expand Down Expand Up @@ -109,4 +123,41 @@ protected function getStyleFromString(string $string)

return preg_replace('/<[^>]*>/', '', $matches[0]);
}

/**
* Get line where style starts.
*
* @return int
*/
protected function getLineWhereStyleStarts(string $path)
{
foreach (file($path) as $line => $code) {
if (Str::startsWith($code, '<x-style')) {
return $line + 1;
}
}

return 0;
}

/**
* Throw more readable style exception.
*
* @param Throwable $e
* @return void
*
* @throws \BladeStyle\Exceptions\StyleException
*/
protected function throwStyleException(Throwable $e, int $line = 0)
{
$line = $this->getLineWhereStyleStarts($this->currentPath) + $line - ($line > 0 ? 1 : 0);

throw new StyleException(
$e->getMessage(),
$this->currentPath,
$line,
$e->getCode(),
$e
);
}
}
10 changes: 6 additions & 4 deletions src/Exceptions/StyleException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@

use Exception;
use InvalidArgumentException;
use Illuminate\Support\Facades\File;

class StyleException extends InvalidArgumentException
{
/**
* Create new FieldException instance.
*
* @param string $message
* @param array $options
* @param string $file
* @param int $line
* @param integer $code
* @param Exception $previous
*/
public function __construct($message = null, array $options = [], $code = 0, Exception $previous = null)
public function __construct($message = null, string $file = null, int $line = 0, $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);

$this->file = $options['file'] ?? $this->file;
$this->line = $options['line'] ?? $this->line;
$this->file = $file === null ? $this->file : $file;
$this->line = $line;
}
}

0 comments on commit 08a914a

Please sign in to comment.