Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from userfrosting/develop
Browse files Browse the repository at this point in the history
Release 5.0.2
  • Loading branch information
Silic0nS0ldier authored Apr 7, 2019
2 parents a031f45 + d3c6cff commit 3efe527
Show file tree
Hide file tree
Showing 30 changed files with 614 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Thanks to Cees-Jan Kiewiet: https://blog.wyrihaximus.net/2016/11/running-php-unit-tests-on-windows-using-appveyor-and-chocolatey/

## Versioning pattern
version: 5.0.0.{build}
version: 5.0.2.{build}

## Disable unneeded features
build: off
Expand Down
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
4 changes: 1 addition & 3 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,11 @@ $rules = [

'header_comment' => [
'header' => $header,
'separate' => 'bottom',
'comment_type' => 'PHPDoc',
]
];

$finder = PhpCsFixer\Finder::create()
->in([__DIR__ . '/src']);
->in([__DIR__ . '/src', __DIR__ . '/tests']);

return PhpCsFixer\Config::create()
->setRules($rules)
Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unknown]

## [5.0.2] - 2019-04-07

### Added
- Test coverage for remaining untested source.

### Changed
- Stabilised absolute path returned by `Assets::urlPathToAbsolutePath` and hardened against silent failure.
- Slightly simplified MIME resolution logic.
- Marked usage of `Assets::readSchema` without a respository as the return value.
- Removed various bit of dead code.

### Fixed
- Fixed silent failure in `gulp-bundle-assets` loading support.

## [5.0.1] - 2019-01-13

### Changed
- Updated Dependencies

## [5.0.0] - 2019-01-12
Expand Down
111 changes: 45 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
# UserFrosting/Assets

[![Latest Version](https://img.shields.io/github/release/userfrosting/assets.svg)](https://github.com/userfrosting/assets/releases)
[![Latest Version][version-badge]][releases]
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md)
[![Build Status](https://travis-ci.org/userfrosting/assets.svg?branch=master)](https://travis-ci.org/userfrosting/assets)
[![Windows Build](https://ci.appveyor.com/api/projects/status/02gtbpena70q46q2?svg=true)](https://ci.appveyor.com/project/userfrosting/assets)
[![codecov](https://codecov.io/gh/userfrosting/assets/branch/master/graph/badge.svg)](https://codecov.io/gh/userfrosting/assets)
[![Join the chat at https://chat.userfrosting.com/channel/support](https://demo.rocket.chat/images/join-chat.svg)](https://chat.userfrosting.com/channel/support)
[![Donate](https://img.shields.io/badge/Open%20Collective-Donate-blue.svg)](https://opencollective.com/userfrosting#backer)

| Branch | Build | Coverage | Style |
| ------ | ----- | -------- | ----- |
| [master][master] | [![Build Status][master-build-travis-badge]][travis] [![Windows Build][master-build-appveyor-badge]][appveyor] | [![codecov][master-codecov-badge]][codecov] | [![][master-styleci-badge]][styleci] |
| [develop][develop] | [![Build Status][develop-build-travis-badge]][travis] [![Windows Build][develop-build-appveyor-badge]][appveyor] | [![codecov][develop-codecov-badge]][codecov] | [![][develop-styleci-badge]][styleci] |

<!-- Links -->
[codecov]: https://codecov.io/gh/userfrosting/assets
[releases]: https://github.com/userfrosting/assets/releases
[styleci]: https://github.styleci.io/repos/55460230
[travis]: https://travis-ci.org/userfrosting/assets
[appveyor]: https://ci.appveyor.com/project/userfrosting/assets
[version-badge]: https://img.shields.io/github/release/userfrosting/assets.svg
[master]: https://github.com/userfrosting/assets/tree/master
[master-styleci-badge]: https://github.styleci.io/repos/55460230/shield?branch=master&style=flat
[master-codecov-badge]: https://codecov.io/gh/userfrosting/assets/branch/master/graph/badge.svg
[master-build-travis-badge]: https://travis-ci.org/userfrosting/assets.svg?branch=master
[master-build-appveyor-badge]: https://ci.appveyor.com/api/projects/status/github/userfrosting/assets?branch=master&svg=true
[develop]: https://github.com/userfrosting/assets/tree/develop
[develop-styleci-badge]: https://github.styleci.io/repos/55460230/shield?branch=develop&style=flat
[develop-codecov-badge]: https://codecov.io/gh/userfrosting/assets/branch/develop/graph/badge.svg
[develop-build-travis-badge]: https://travis-ci.org/userfrosting/assets.svg?branch=develop
[develop-build-appveyor-badge]: https://ci.appveyor.com/api/projects/status/github/userfrosting/assets?branch=develop&svg=true

**Assets** is a library originally created for UserFrosting 4 to make it much easier to reference frontend assets in both production and development contexts.

Out of the box it can:

- Provide an easy way to generate an absolute url to an asset via a locator.
- Provide a basic level of integration with *gulp-bundle-assets*, making it easy to reference asset bundles.
- Provide a basic level of integration with [`gulp-bundle-assets`](https://github.com/dowjones/gulp-bundle-assets) and complete integration with [`@userfrosting/gulp-bundle-assets`](https://www.npmjs.com/package/@userfrosting/gulp-bundle-assets), making it easy to reference asset bundles.
- Integrate via a common interface with virtually any bundling system.
- Integrate with Slim to serve assets that are inaccessible from the public folder, in the development context.
- Perform url-to-path transformations. Useful for making debuggable URLs that can be reverted back to the path to be used by the Slim asset server.
Expand Down Expand Up @@ -51,13 +71,13 @@ $locator->registerStream('assets', '', [
$assets = new Assets($locator, 'assets', $baseUrl);
```

> Optionally 1 more argument can be passed into the `Assets` constructor.
> - An instance of `PrefixTransformer`.
> Optionally 1 more argument can be passed into the `Assets` constructor, an instance of `PrefixTransformer`.
>
> Have a look at UserFrosting in dev mode to see this in action!
### Asset Bundles

To access asset bundles from an `Assets` instance, it must first be passed an instance of `AssetBundlesInterface` via `addAssetBundles`. An example of this follows:
To access asset bundles from an `Assets` instance, it must first be passed an instance of `AssetBundlesInterface` via `addAssetBundles`. The following example demonstates how to integrate with `@userfrosting/gulp-bundle-assets` (and by extension `gulp-bundle-assets`). Note that raw bundles are only supported when their configuration is defined as JSON.

*Directory Tree*

Expand All @@ -76,44 +96,20 @@ To access asset bundles from an `Assets` instance, it must first be passed an in
{
"bundle": {
"js/main": {
"scripts": [
"js/bootstrap-3.3.1.js",
"js/crud.js"
],
"options": {
"result": {
"type": {
"scripts": "plain"
}
}
}
"scripts": [
"js/bootstrap-3.3.1.js",
"js/crud.js"
]
},
"css/main": {
"styles": [
"vendor/font-awesome-4.5.0/css/font-awesome.css",
"css/bootstrap-3.3.1.css",
"css/bootstrap-custom.css",
"css/paper.css"
],
"options": {
"result": {
"type": {
"styles": "plain"
}
}
}
}
},
"copy": [
{
"src": "images/**/*",
"base": "."
},
{
"src": "vendor/font-awesome-4.5.0/fonts/**/*",
"base": "vendor/font-awesome-4.5.0/"
"styles": [
"vendor/font-awesome-4.5.0/css/font-awesome.css",
"css/bootstrap-3.3.1.css",
"css/bootstrap-custom.css",
"css/paper.css"
]
}
]
}
}
```

Expand All @@ -123,36 +119,19 @@ To access asset bundles from an `Assets` instance, it must first be passed an in
use UserFrosting\Assets\GulpBundleAssetsRawBundles;

// Load asset bundles.
$assetBundles = new GulpBundleAssetsRawBundles("../build/asset-bundles.json");
$assetBundles = new GulpBundleAssetsRawBundles('../build/asset-bundles.json');

// Send loaded asset bundles to Assets instance.
$assets->addAssetBundles($assetBundles);

// Grab an asset bundle.
$assets->getJsBundleAssets("js/main");
$assets->getJsBundleAssets('js/main');
// Outputs ["js/bootstrap-3.3.1.js", "js/crud.js"]
```

See [gulp-bundle-assets](https://github.com/dowjones/gulp-bundle-assets) for how to use the bundler this example demonstrates integration with.

Just keep in mind that this integration can only work when the gulp-bundle-assets configuration is stored in a separate JSON file, not a JavaScript file.

Also note the setting of `options -> result -> type -> styles/scripts` for each bundle to `plain`. This is important to do, so that `Assets` can parse the output of `gulp-bundle-assets` and correctly render tags for compiled assets. The output of your Gulp task should look something like:

*bundle.result.json*

```json
{
"js/main": {
"scripts": "js/main-8881456f8e.js"
},
"css/main": {
"styles": "css/main-c72ce38fba.css"
}
}
```
Compiled bundles can be used in much the same way, except using `GulpBundleAssetsCompiledBundles` and the bundlers result file.

Using this results file would be done with the `GulpBundleAssetsCompiledBundles` class.
If using the original `gulp-bundle-assets` you'll need to include an additional setting `options->result->type->styles/scripts="plain"` in each bundle.

### The Template Plugin

Expand All @@ -168,14 +147,14 @@ $assetsPlugin = new AssetsTemplatePlugin($assets);

```twig
{# Gets replaced at runtime with the following. Additional argument is optional. #}
{{assets.js("js/main", { defer: true })}}
{{ assets.js("js/main", { defer: true }) }}
```

```html
<script src="https://assets.userfrosting.com/assets/bootstrap/js/bootstrap.js" defer="true"></script>
<script src="https://assets.userfrosting.com/assets/bootstrap/js/npm.js" defer="true"></script>
```

## [Style Guide](STYLE-GUIDE.md)
## [Style Guide](STYLE_GUIDE.md)

## [Testing](RUNNING_TESTS.md)
File renamed without changes.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
{
"name": "Jordan Mele",
"email": "SiliconSolder@outlook.com.au"
"email": "SiliconSolder@outlook.com.au",
"homepage": "https://blog.djmm.me"
}
],
"require": {
Expand Down
11 changes: 8 additions & 3 deletions src/AssetBundles/AssetBundlesInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/**

/*
* UserFrosting Assets (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/assets
Expand All @@ -20,17 +21,21 @@ interface AssetBundlesInterface
/**
* Gets assets in specified CSS bundle.
*
* @param string $bundleName Name of bundle.
* @param string $bundleName Name of bundle.
*
* @throws \OutOfRangeException if requested bundle does not exist.
*
* @return string[]
*/
public function getCssBundleAssets($bundleName = '');

/**
* Gets assets in specified JS bundle.
*
* @param string $bundleName Name of bundle.
* @param string $bundleName Name of bundle.
*
* @throws \OutOfRangeException if requested bundle does not exist.
*
* @return string[]
*/
public function getJsBundleAssets($bundleName = '');
Expand Down
25 changes: 17 additions & 8 deletions src/AssetBundles/GulpBundleAssetsBundles.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/**

/*
* UserFrosting Assets (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/assets
Expand Down Expand Up @@ -29,14 +30,14 @@ abstract class GulpBundleAssetsBundles implements AssetBundlesInterface
protected $jsBundles;

/**
* Constructor
* Constructor.
*
* @param string $filePath Path to gulp-bundle-assets file.
*/
public function __construct($filePath)
{
if (!is_string($filePath)) {
throw new \InvalidArgumentException('$filePath must of type string but was ' . gettype($filePath));
throw new \InvalidArgumentException('$filePath must of type string but was '.gettype($filePath)); // @codeCoverageIgnore
}

// Initalise bundles.
Expand Down Expand Up @@ -71,11 +72,13 @@ public function getJsBundleAssets($bundleName = '')
/**
* Attempts to read the schema file from provided path.
*
* @param string $path Path to schema file.
* @param bool $useRepository
* @param string $path Path to schema file.
* @param bool $useRepository Deprecated, will be removed in a future release. Setting this to true is recommended for an easier migration.
*
* @throws FileNotFoundException if file cannot be found.
* @throws JsonException if file cannot be parsed as JSON.
* @return mixed|Repository Returns file contents parsed by json_decode or a Repository if $useRepository is true.
*
* @return mixed|Repository Returns file contents parsed by json_decode or a Repository if $useRepository is true.
*/
protected function readSchema($path, $useRepository = false)
{
Expand All @@ -84,13 +87,18 @@ protected function readSchema($path, $useRepository = false)
try {
$loader = new YamlFileLoader($path);

return new Repository($loader->load());
return new Repository($loader->load(false));
} catch (FileNotFoundException $e) {
throw new FileNotFoundException('The schema file could not be found.', 0, $e);
} catch (JsonException $e) {
throw new JsonException('The schema file could not be found.', 0, $e);
}
} else {
// @codeCoverageIgnoreStart
// This code path exists to facilitate an easier migration to v5 of this package
// It cannot be properly tested here without increasing technical debt substantially and such such it will be removed in the future
trigger_error('Usage of \'readSchema\' with repository result disabled is deprecated and will be removed in future releases.', E_USER_DEPRECATED);

// Read schema without abstractions
if (!file_exists($path)) {
throw new FileNotFoundException("The schema '$path' could not be found.");
Expand All @@ -103,10 +111,11 @@ protected function readSchema($path, $useRepository = false)

$schema = json_decode($doc);
if ($schema === null) {
throw new JsonException("The schema '$path' does not contain a valid JSON document. JSON error: " . json_last_error());
throw new JsonException("The schema '$path' does not contain a valid JSON document. JSON error: ".json_last_error());
}

return $schema;
// @codeCoverageIgnoreEnd
}
}
}
10 changes: 6 additions & 4 deletions src/AssetBundles/GulpBundleAssetsCompiledBundles.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/**

/*
* UserFrosting Assets (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/assets
Expand All @@ -9,9 +10,9 @@

namespace UserFrosting\Assets\AssetBundles;

use UserFrosting\Assets\Exception\InvalidBundlesFileException;
use UserFrosting\Support\Exception\FileNotFoundException;
use UserFrosting\Support\Exception\JsonException;
use UserFrosting\Assets\Exception\InvalidBundlesFileException;

/**
* Represents a collection of asset bundles, loaded from a gulp-bundle-assets results file.
Expand All @@ -23,6 +24,7 @@ class GulpBundleAssetsCompiledBundles extends GulpBundleAssetsBundles
{
/**
* {@inheritdoc}
*
* @throws FileNotFoundException if file cannot be found.
* @throws JsonException if file cannot be parsed as JSON.
* @throws InvalidBundlesFileException if unexpected value encountered.
Expand All @@ -40,14 +42,14 @@ public function __construct($path)
if (is_string($styles)) {
$this->cssBundles[$bundleName][] = $styles;
} elseif ($styles !== null) {
throw new InvalidBundlesFileException("Expected styles property for '$bundleName' to be of type string but was '" . gettype($styles) . "' for '$path'");
throw new InvalidBundlesFileException("Expected styles property for '$bundleName' to be of type string but was '".gettype($styles)."' for '$path'");
}

$scripts = $schema["$bundleName.scripts"];
if (is_string($scripts)) {
$this->jsBundles[$bundleName][] = $scripts;
} elseif ($scripts !== null) {
throw new InvalidBundlesFileException("Expected scripts property for '$bundleName' to be of type string but was '" . gettype($scripts) . "' for '$path'");
throw new InvalidBundlesFileException("Expected scripts property for '$bundleName' to be of type string but was '".gettype($scripts)."' for '$path'");
}
}
}
Expand Down
Loading

0 comments on commit 3efe527

Please sign in to comment.