Skip to content

Commit

Permalink
feat: update for latest 4
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Jul 19, 2023
1 parent 206a231 commit 43d9465
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 222 deletions.
50 changes: 26 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Hash Path

Hash Path provides a function in SilverStripe templates which, given a path to an asset, returns a modified path with a file hash appended. In combination with a web server rewrite rule, browser caching can be completely mitigated as the file URL sent to the browser changes whenever the file does.
Hash Path provides a function in SilverStripe templates which, given a path to
an asset, returns a modified path with a file hash appended. In combination with
a web server rewrite rule, browser caching can be completely mitigated as the
file URL sent to the browser changes whenever the file does.

```php
// Template:
Expand All @@ -10,14 +13,6 @@ $HashPath(css/style.css)
/themes/my-theme/css/style.vpOQ8F6ybteKQQYND5dzZQ.css
```


The latest version is only compatible with SilverStripe `4`.

For a SilverStripe `3` compatible version see branch `2.1`.

For a SilverStripe `2.4` compatible version see branch `1.1`.


## License

Hash Path is licensed under an [MIT license](http://heyday.mit-license.org/)
Expand All @@ -26,23 +21,29 @@ Hash Path is licensed under an [MIT license](http://heyday.mit-license.org/)

### Composer

Installing from composer is easy,
Installing from composer is easy,

Create or edit a `composer.json` file in the root of your SilverStripe project, and make sure the following is present.
Create or edit a `composer.json` file in the root of your SilverStripe project,
and make sure the following is present.

```json
{
"require": {
"heyday/silverstripe-hashpath": "^3.0.0"
}
"require": {
"heyday/silverstripe-hashpath": "^3.0.0"
}
}
```

After completing this step, navigate in Terminal or similar to the SilverStripe root directory and run `composer install` or `composer update` depending on whether or not you have composer already in use.
After completing this step, navigate in Terminal or similar to the SilverStripe
root directory and run `composer install` or `composer update` depending on
whether or not you have composer already in use.

### Web server config

As Hash Path returns paths that don't exist on disk, a rewrite rule needs to be added to your web server in order to return the file that was originally given to Hash Path. The URL format is `.v[hash]` inserted before the file extension, so you end up with `.v[hash].[extension]`.
As Hash Path returns paths that don't exist on disk, a rewrite rule needs to be
added to your web server in order to return the file that was originally given
to Hash Path. The URL format is `.v[hash]` inserted before the file extension,
so you end up with `.v[hash].[extension]`.

#### Apache

Expand Down Expand Up @@ -71,9 +72,11 @@ location /themes {

## How to use

Provided the correct theme is set, you can simply call `$HashPath` with the asset location relative to the current theme as the first argument.
Provided the correct theme is set, you can simply call `$HashPath` with the
asset location relative to the current theme as the first argument.

For example, for a file located at `themes/my-theme/js/general.js` and with `my-theme` current, using:
For example, for a file located at `themes/my-theme/js/general.js` and with
`my-theme` current, using:

```html
<script src="$HashPath("js/general.js")"></script>
Expand All @@ -85,7 +88,8 @@ will result in:
<script src="/themes/my-theme/js/general.v54473acf909c645bb14f011d86a47733.js"></script>
```

If you are wanting to use an asset that is not relative to the current theme, use:
If you are wanting to use an asset that is not relative to the current theme,
use:

```html
<script src="$HashPath("/my-module/js/general.js", 0)"></script>
Expand All @@ -98,16 +102,14 @@ PHP Unit now comes with SS
### Running the unit tests

From the command line:

vendor/bin/phpunit silverstripe-hashpath/tests


vendor/bin/phpunit silverstripe-hashpath/tests

## Contributing

### Code guidelines

This project follows the standards defined in:

* [PSR-1](http://www.php-fig.org/psr/psr-1/)
* [PSR-2](http://www.php-fig.org/psr/psr-1/)
- [PSR-1](http://www.php-fig.org/psr/psr-1/)
- [PSR-2](http://www.php-fig.org/psr/psr-1/)
104 changes: 0 additions & 104 deletions code/HashPathExtension.php

This file was deleted.

57 changes: 18 additions & 39 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,20 @@
{
"name": "heyday/silverstripe-hashpath",
"type": "silverstripe-vendormodule",
"description": "Hash path provides a function in SilverStripe templates which given a path to an asset returns a path including a hash of the asset",
"license": "MIT",
"authors": [
{
"name": "Cam Spiers",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "Pieter Vanderwerff",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "Ben Dubuisson",
"email": "[email protected]",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"Heyday\\HashPath\\": "code/"
}
},
"support": {
"issues": "https://github.com/heyday/silverstripe-hashpath/issues",
"source": "https://github.com/heyday/silverstripe-hashpath"
},
"require": {
"composer/installers": "~1.0",
"silverstripe/framework": "~4.0"
},
"require-dev": {
"composer/composer": "~1.0.0-alpha9"
},
"prefer-stable": true,
"minimum-stability": "dev"
"name": "heyday/silverstripe-hashpath",
"type": "silverstripe-vendormodule",
"description": "Hash path provides a function in SilverStripe templates which given a path to an asset returns a path including a hash of the asset",
"license": "MIT",
"autoload": {
"psr-4": {
"Heyday\\HashPath\\": "src/"
}
},
"support": {
"issues": "https://github.com/heyday/silverstripe-hashpath/issues",
"source": "https://github.com/heyday/silverstripe-hashpath"
},
"require": {
"silverstripe/framework": "^4 || ^5"
},
"prefer-stable": true,
"minimum-stability": "dev"
}
102 changes: 102 additions & 0 deletions src/HashPathExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Heyday\HashPath;

use SilverStripe\Core\Extension;
use SilverStripe\View\SSViewer;

/**
* Class HashPathExtension
*/
class HashPathExtension extends Extension
{
/**
* The format of the web path
* @var string
*/
protected static $format = '%s/%s.v%s.%s';
/**
* Whether or not to output links as relative
* @var boolean
*/
protected static $relativeLinks = false;

/**
* Set the format for use in producing the web path
* @param string $format The format to set
*/
public static function setFormat($format)
{
self::$format = $format;
}

/**
* Output links as relative
* @param boolean $val Whether or not to output links as relative
*/
public static function setRelativeLinks($val = true)
{
self::$relativeLinks = $val;
}

/**
* Returns an md5 hash of a file
* @param string $path Relative or absolute path to file
* @param boolean $theme Whether or not to take current theme into account
* @return string The md5 of the file
*/
public function HashFile($path, $theme = true)
{
$path = $theme ? $this->getPath($path) : $path;

if (file_exists($path)) {
$hash = md5_file($path);
return str_replace(array('/', '+', '='), '', base64_encode(pack('H*', $hash)));
}

return '';
}

/**
* Template function to return new web path to asset which includes
* md5 hash
* @param string $path Relative or absolute path to file
* @param boolean $theme Whether or not to take current theme into account
* @return string The web path include the md5 hash
*/
public function HashPath($path, $theme = true)
{
$filepath = $this->getPath($path, $theme);
$path_parts = pathinfo($filepath);

return sprintf(
self::$format,
str_replace(BASE_PATH . (self::$relativeLinks ? '/' : ''), '', $path_parts['dirname']),
basename($path, ".{$path_parts['extension']}"),
$this->HashFile($filepath, false),
$path_parts['extension']
);
}

/**
* Returns the absolute path to a file based on whether or not
* the input is relative to the current theme
* @param string $path Relative or absolute path to file
* @param boolean $theme Whether or not to take current theme into account
* @return string The absolute path to the file
*/
protected function getPath($path, $theme = true)
{
if ($theme) {
$themes = SSViewer::get_themes();
if (is_array($themes)) {
$themePath = $themes[0];
} else {
$themePath = 'simple';
}
$path = '/themes/' . $themePath . "/$path";
}

return BASE_PATH . $path;
}
}
Loading

0 comments on commit 43d9465

Please sign in to comment.