-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
781 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "cbl/blade-script", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Lennart Carstens-Behrens", | ||
"email": "lennart.carbe@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"matthiasmullie/minify": "^1.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"BladeScript\\": "src/" | ||
}, | ||
"files": [ | ||
"src/Support/helpers.php" | ||
] | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"BladeScript\\ServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Minify Scripts | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This option determines wether the compiled scripts should be minified. | ||
| It is recommended to minify your scripts in production. This simplifies | ||
| debugging in your local environment. | ||
| | ||
| The php minifier from Matthias Mullie is used by default. | ||
| https://github.com/matthiasmullie/minify | ||
| | ||
*/ | ||
|
||
'minify' => !config('app.debug'), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Compiled Script Path | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This option determines where all the compiled styles will be stored for | ||
| your application just like for your views. | ||
| | ||
*/ | ||
|
||
'compiled' => env( | ||
'SCRIPT_COMPILED_PATH', | ||
realpath(storage_path('framework/scripts')) | ||
), | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace BladeScript\Compiler; | ||
|
||
use BladeScript\Contracts\Transpiler; | ||
use Illuminate\Filesystem\Filesystem; | ||
use BladeScript\Engines\MinifierEngine; | ||
use Illuminate\View\Compilers\CompilerInterface; | ||
use Illuminate\View\Compilers\Compiler as ViewCompiler; | ||
|
||
class ScriptCompiler extends ViewCompiler implements CompilerInterface | ||
{ | ||
/** | ||
* Minifier engine. | ||
* | ||
* @var \BladeScript\Engines\MinifierEngine | ||
*/ | ||
protected $engine; | ||
|
||
/** | ||
* Transpiler stack. | ||
* | ||
* @var array | ||
*/ | ||
protected $transpiler = []; | ||
|
||
/** | ||
* Create a new compiler instance. | ||
* | ||
* @param \BladeScript\Engines\MinifierEngine $engine | ||
* @param \Illuminate\Filesystem\Filesystem $files | ||
* @param string $cachePath | ||
* @return void | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct(MinifierEngine $engine, Filesystem $files, $cachePath) | ||
{ | ||
parent::__construct($files, $cachePath); | ||
|
||
$this->engine = $engine; | ||
} | ||
|
||
/** | ||
* Add transpiler. | ||
* | ||
* @return void | ||
*/ | ||
public function addTranspiler(Transpiler $transpiler) | ||
{ | ||
$this->transpiler[] = $transpiler; | ||
} | ||
|
||
/** | ||
* Compile the style at the given path. | ||
* | ||
* @param string $path | ||
* @return void | ||
*/ | ||
public function compile($path) | ||
{ | ||
$script = $this->compileScript( | ||
$this->getRaw($path) | ||
); | ||
|
||
if (config('script.minify')) { | ||
$script = $this->engine->minify($script); | ||
} | ||
|
||
$this->files->put( | ||
$this->getCompiledPath($path), | ||
$script | ||
); | ||
} | ||
|
||
/** | ||
* Compile script. | ||
* | ||
* @param string $script | ||
* @return string | ||
*/ | ||
public function compileScript(string $script) | ||
{ | ||
foreach ($this->transpiler as $transpiler) { | ||
$script = $transpiler->transpile($script); | ||
} | ||
|
||
return $script; | ||
} | ||
|
||
/** | ||
* Get raw style from path. | ||
* | ||
* @param string $path | ||
* @return string|null | ||
*/ | ||
public function getRaw($path) | ||
{ | ||
return $this->getStyleFromString( | ||
$this->files->get($path) | ||
); | ||
} | ||
|
||
/** | ||
* Get style from string. | ||
* | ||
* @param string|null $string | ||
* @return string | ||
*/ | ||
protected function getStyleFromString(string $string) | ||
{ | ||
preg_match('/<x-script [^>]*>(.|\n)*?<\/x-script>/', $string, $matches); | ||
|
||
if (empty($matches)) { | ||
preg_match('/<x-script>(.|\n)*?<\/x-script>/', $string, $matches); | ||
} | ||
|
||
if (empty($matches)) { | ||
return; | ||
} | ||
|
||
return preg_replace('/<[^>]*>/', '', $matches[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace BladeScript\Components; | ||
|
||
use BladeScript\Factory; | ||
use Illuminate\Support\Str; | ||
use Illuminate\View\Component; | ||
use Illuminate\Support\Facades\File; | ||
|
||
class ScriptComponent extends Component | ||
{ | ||
/** | ||
* Style language. | ||
* | ||
* @var string | ||
*/ | ||
public $lang; | ||
|
||
public $style; | ||
|
||
public $factory; | ||
|
||
/** | ||
* Create new StyleComponent instance. | ||
* | ||
* @param string $lang | ||
* @return void | ||
*/ | ||
public function __construct(Factory $factory, $lang = 'css') | ||
{ | ||
$this->lang = $lang; | ||
$this->factory = $factory; | ||
|
||
$this->makeScript(); | ||
} | ||
|
||
public function makeScript() | ||
{ | ||
$path = $this->getPathFromTrace(); | ||
|
||
if (!$path) { | ||
return; | ||
} | ||
|
||
$this->script = $this->factory->make($path); | ||
} | ||
|
||
protected function getPathFromTrace() | ||
{ | ||
foreach (debug_backtrace() as $trace) { | ||
if (!array_key_exists('file', $trace)) { | ||
continue; | ||
} | ||
|
||
if (!Str::startsWith($trace['file'], config('view.compiled'))) { | ||
continue; | ||
} | ||
|
||
return $this->getPathFromCompiled($trace['file']); | ||
} | ||
} | ||
|
||
protected function getPathFromCompiled($compiled) | ||
{ | ||
return trim(Str::between(File::get($compiled), '/**PATH', 'ENDPATH**/')); | ||
} | ||
|
||
/** | ||
* Get the view / contents that represent the component. | ||
* | ||
* @return void | ||
*/ | ||
public function render() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace BladeScript\Components; | ||
|
||
use BladeScript\Factory; | ||
use Illuminate\View\Component; | ||
|
||
class ScriptsComponent extends Component | ||
{ | ||
/** | ||
* Factory instance. | ||
* | ||
* @var \BladeScript\Factory | ||
*/ | ||
public $scripts; | ||
|
||
/** | ||
* Create ScriptsComponent instance. | ||
* | ||
* @param Factory $factory | ||
*/ | ||
public function __construct(Factory $scripts) | ||
{ | ||
$this->scripts = $scripts; | ||
} | ||
|
||
/** | ||
* Get the view / contents that represent the component. | ||
* | ||
* @return \Illuminate\View\View|string | ||
*/ | ||
public function render() | ||
{ | ||
return $this->scripts->render(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace BladeScript\Contracts; | ||
|
||
interface ScriptEngine | ||
{ | ||
/** | ||
* Get compiled script from path. | ||
* | ||
* @param string $path | ||
* @return string | ||
*/ | ||
public function get(string $path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace BladeScript\Contracts; | ||
|
||
interface ScriptMinifier | ||
{ | ||
/** | ||
* Minify script. | ||
* | ||
* @return string | ||
*/ | ||
public function minify(string $script); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace BladeScript\Contracts; | ||
|
||
interface Transpiler | ||
{ | ||
/** | ||
* Transpile script and return result. | ||
* | ||
* @param string $script | ||
* @return string | ||
*/ | ||
public function transpile($script); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace BladeScript\Engines; | ||
|
||
use Illuminate\Support\Facades\File; | ||
use BladeScript\Contracts\ScriptEngine; | ||
use Illuminate\View\Compilers\CompilerInterface; | ||
|
||
class CompilerEngine implements ScriptEngine | ||
{ | ||
/** | ||
* Create a new Blade view engine instance. | ||
* | ||
* @param \BladeStyle\StyleCompiler $compiler | ||
* @return void | ||
*/ | ||
public function __construct(CompilerInterface $compiler) | ||
{ | ||
$this->compiler = $compiler; | ||
} | ||
|
||
/** | ||
* Get compiled style from the given path. | ||
* | ||
* @param string $path | ||
* @return void | ||
*/ | ||
public function get(string $path) | ||
{ | ||
if ($this->compiler->isExpired($path)) { | ||
$this->compiler->compile($path); | ||
} | ||
|
||
return File::get($this->compiler->getCompiledPath($path)); | ||
} | ||
} |
Oops, something went wrong.