-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 5c8ebdd
Showing
6 changed files
with
233 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,133 @@ | ||
<?php | ||
|
||
namespace Statamic\Addons\Blend; | ||
|
||
use Statamic\API\Str; | ||
use Statamic\API\URL; | ||
use Statamic\API\File; | ||
use Statamic\API\Config; | ||
use Statamic\Extend\HasParameters; | ||
use Illuminate\Support\HtmlString; | ||
|
||
class Blend | ||
{ | ||
/** | ||
* Access Statamic's methods for retrieving parameters | ||
*/ | ||
use HasParameters; | ||
|
||
/** | ||
* Get the path to a versioned Mix file with some Statamic adaptation. | ||
* | ||
* @param string $path | ||
* @param string $manifestDirectory | ||
* @return \Illuminate\Support\HtmlString|string | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function __invoke($path, $manifestDirectory = '') | ||
{ | ||
static $manifests = []; | ||
|
||
if (! Str::startsWith($path, '/')) { | ||
$path = "/{$path}"; | ||
} | ||
|
||
if ($manifestDirectory && ! Str::startsWith($manifestDirectory, '/')) { | ||
$manifestDirectory = "/{$manifestDirectory}"; | ||
} | ||
|
||
if (File::exists($this->getPath('hot', $manifestDirectory))) { | ||
$url = rtrim(File::get($this->getPath('hot', $manifestDirectory))); | ||
|
||
if (Str::startsWith($url, ['http://', 'https://'])) { | ||
return new HtmlString($this->after($url, ':').$path); | ||
} | ||
|
||
return new HtmlString("//localhost:8080{$path}"); | ||
} | ||
|
||
$manifestPath = $this->getPath('mix-manifest.json', $manifestDirectory); | ||
|
||
if (! isset($manifests[$manifestPath])) { | ||
if (! File::exists($manifestPath)) { | ||
throw new \Exception('The Mix manifest does not exist.'); | ||
} | ||
|
||
$manifests[$manifestPath] = collect(json_decode(File::get($manifestPath), true)); | ||
} | ||
|
||
$manifest = $manifests[$manifestPath]; | ||
|
||
$exception = new \Exception("Unable to locate Mix file: {$path}."); | ||
|
||
if (! isset($manifest[$path])) { | ||
$exception = new \Exception("Unable to locate Mix file: {$path}."); | ||
|
||
if (! app('config')->get('app.debug')) { | ||
return $path; | ||
} else { | ||
throw $exception; | ||
} | ||
} | ||
|
||
return $this->themeUrl($manifest[$path]); | ||
} | ||
|
||
/** | ||
* Return the remainder of a string after a given value. | ||
* | ||
* Since Statamic 2 uses an old version of Laravel, the after() method | ||
* is missing from the Str class so we have it here. | ||
* | ||
* @param string $subject | ||
* @param string $search | ||
* @return string | ||
*/ | ||
private function after($subject, $search) | ||
{ | ||
return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0]; | ||
} | ||
|
||
/** | ||
* Get the path. | ||
* | ||
* @param string $path | ||
* @param string $manifestDirectory | ||
* @return string | ||
*/ | ||
private function getPath($path, $manifestDirectory) | ||
{ | ||
if ($manifestDirectory) { | ||
return root_path( | ||
URL::assemble( | ||
$manifestDirectory, | ||
$path | ||
) | ||
); | ||
} | ||
|
||
return root_path( | ||
URL::assemble( | ||
Config::get('system.filesystems.themes.root'), | ||
Config::get('theming.theme'), | ||
$path | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Transforms the asset directory into a relative URL for use in the front-end. | ||
* | ||
* @param string $path | ||
* @return string | ||
*/ | ||
private function themeUrl($path) | ||
{ | ||
return URL::assemble( | ||
Config::get('system.filesystems.themes.url'), | ||
Config::get('theming.theme'), | ||
$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,20 @@ | ||
<?php | ||
|
||
namespace Statamic\Addons\Blend; | ||
|
||
use Statamic\Extend\ServiceProvider; | ||
|
||
class BlendServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->singleton(Blend::class); | ||
|
||
require_once(__DIR__.'/helpers.php'); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
use Statamic\Addons\Blend\Blend; | ||
|
||
if (! function_exists('mix')) { | ||
/** | ||
* Get the path to a versioned Mix file. | ||
* | ||
* @param string $path | ||
* @param string $manifestDirectory | ||
* @return \Illuminate\Support\HtmlString|string | ||
* | ||
* @throws \Exception | ||
*/ | ||
function mix($path, $manifestDirectory = '') | ||
{ | ||
return app(Blend::class)(...func_get_args()); | ||
} | ||
} |
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,6 @@ | ||
name: Blend | ||
version: '1.0' | ||
description: Use Laravel Mix with Statamic in Blade templates like you already do with Laravel. | ||
url: https://github.com/zawilliams/statamic-blend | ||
developer: Zach Williams | ||
developer_url: zachwilliams.me |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Zach Williams | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,34 @@ | ||
# Blend for Statamic ![Statamic 2.11](https://img.shields.io/badge/statamic-2.11-blue.svg?style=flat-square) | ||
|
||
Use Laravel Mix with Statamic in Blade templates like you already do with Laravel. It uses the [Mix](https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Mix.php) class from Laravel as a singleton with some Statamic adaptations for finding theme files. This Addon works with Blade templates only. If you're using Antlers, check out [Statamic Mix](https://statamic.com/marketplace/addons/statamic-mix). | ||
|
||
## Installation | ||
|
||
Simply copy the `Blend` folder into `site/addons/`. That's it! | ||
|
||
## Usage | ||
|
||
Just use it like you would normally use [Mix](https://laravel-mix.com/docs/4.0/workflow) with Laravel Blade templates: | ||
|
||
```html | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Statamic</title> | ||
<link rel="stylesheet" href="{{ mix('css/app.css') }}"> | ||
</head> | ||
<body> | ||
<script src="{{ mix('js/app.js') }}"></script> | ||
</body> | ||
</html> | ||
``` | ||
|
||
You can also change your manifest directory just like you can with Laravel: | ||
|
||
```html | ||
<link rel="stylesheet" href="{{ mix('css/app.css', 'public/build') }}"> | ||
``` | ||
|
||
## Acknowledgements | ||
|
||
- Thanks to [Ben Furfie](https://github.com/benfurfie) for some code in [Statamic Mix](https://github.com/benfurfie/statamic-mix-version) |