Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherdarling committed Feb 20, 2017
0 parents commit 3c17c8c
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Chrristopher Darling

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.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Theme Manifest assets

loads a JSON manifest file from {theme}/dist/assets.json, looks up a given path and outputs the hashed filename

## Example

### Folder structure
dist/
- *subfolders*
- assets.json

### assets.json
```
{
'img/logo.png': 'img/logo_HASH.png'
}
```

### .ss template
```
<img src="{$ThemeManifestAsset(img/logo.png)}" alt="Logo">
```
Will output
```
<img src="themes/default/dist/img/logo_HASH.png" alt="Logo">
```
Empty file added _config.php
Empty file.
45 changes: 45 additions & 0 deletions code/ThemeManifestAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace ChristopherDarling\ThemeManifestAssets;

use \Director;
use \SSViewer;

class ThemeManifestAssets implements \TemplateGlobalProvider
{
public static function get_template_global_variables() {
return array(
'ThemeManifestAsset' => 'getPath',
);
}

private static $manifest_files_cache = null;

private static function getManifestFiles()
{
if (null === self::$manifest_files_cache) {
$manifest = SSViewer::get_theme_folder() . '/dist/assets.json';
$absPath = Director::getAbsFile($manifest);

if (file_exists($absPath)) {
$contents = json_decode(file_get_contents($absPath), true);

self::$manifest_files_cache[] = $contents;
}
}

return self::$manifest_files_cache;
}

public static function getPath($path) {
if ($manifests = self::getManifestFiles()):
foreach ($manifests as $manifest => $map):
if (isset($map[$path])) {
return SSViewer::get_theme_folder() . '/dist/' . $map[$path];
}
endforeach;
endif;

return false;
}
}
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "christopherdarling/silverstripe-theme-manifest-assets",
"description": "Loads theme assets from a JSON manifest produced from a build tool like Webpack",
"type": "silverstripe-module",
"keywords": ["silverstripe", "manifest", "asset", "theme", "webpack"],
"license": "MIT",
"authors": [
{
"name": "Christopher Darling",
"homepage": "http://www.christopherdarling.co.uk",
"role": "Developer"
}
],
"require": {
"silverstripe/framework": ">=3.0.1",
"composer/installers": "*"
},
"support": {
"issues": "https://github.com/christopherdarling/silverstripe-theme-manifest-assets"
},
"extra": {
"installer-name": "cd_silverstripe-theme-manifest-assets"
}
}

0 comments on commit 3c17c8c

Please sign in to comment.