Skip to content

Commit

Permalink
package init
Browse files Browse the repository at this point in the history
  • Loading branch information
cbl committed Jun 24, 2020
1 parent fb7a99e commit 65f1cea
Show file tree
Hide file tree
Showing 16 changed files with 781 additions and 0 deletions.
28 changes: 28 additions & 0 deletions composer.json
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"
]
}
}
}
36 changes: 36 additions & 0 deletions config/script.php
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'))
),

];
124 changes: 124 additions & 0 deletions src/Compiler/ScriptCompiler.php
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]);
}
}
77 changes: 77 additions & 0 deletions src/Components/ScriptComponent.php
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()
{
//
}
}
36 changes: 36 additions & 0 deletions src/Components/ScriptsComponent.php
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();
}
}
14 changes: 14 additions & 0 deletions src/Contracts/ScriptEngine.php
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);
}
13 changes: 13 additions & 0 deletions src/Contracts/ScriptMinifier.php
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);
}
14 changes: 14 additions & 0 deletions src/Contracts/Transpiler.php
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);
}
36 changes: 36 additions & 0 deletions src/Engine/CompilerEngine.php
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));
}
}
Loading

0 comments on commit 65f1cea

Please sign in to comment.