diff --git a/README.md b/README.md index 79ace1f..798e12c 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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/) @@ -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 @@ -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 @@ -85,7 +88,8 @@ will result in: ``` -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 @@ -98,10 +102,8 @@ 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 @@ -109,5 +111,5 @@ From the command line: 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/) diff --git a/code/HashPathExtension.php b/code/HashPathExtension.php deleted file mode 100644 index 8bce482..0000000 --- a/code/HashPathExtension.php +++ /dev/null @@ -1,104 +0,0 @@ -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; - } - -} diff --git a/composer.json b/composer.json index 1256e1f..0300341 100644 --- a/composer.json +++ b/composer.json @@ -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": "cameron@heyday.co.nz", - "role": "Developer" - }, - { - "name": "Pieter Vanderwerff", - "email": "pieter@heyday.co.nz", - "role": "Developer" - }, - { - "name": "Ben Dubuisson", - "email": "ben.dubuisson@heyday.co.nz", - "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" } diff --git a/src/HashPathExtension.php b/src/HashPathExtension.php new file mode 100644 index 0000000..de406a7 --- /dev/null +++ b/src/HashPathExtension.php @@ -0,0 +1,102 @@ +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; + } +} diff --git a/tests/HashPathExtensionTest.php b/tests/HashPathExtensionTest.php index 8eec67e..288e4f3 100644 --- a/tests/HashPathExtensionTest.php +++ b/tests/HashPathExtensionTest.php @@ -1,4 +1,5 @@ assertEquals( - 'L32cPgz9Rj8qwwSRHsr8A', - $hashPath->HashFile( - __DIR__ . '/test.txt', - false - ) - ); - - $this->assertEquals( - '', - $hashPath->HashFile( - 'fakefile', - false - ) - ); - - } - - /** - * - */ - public function testHashPath() - { - - $hashPath = new HashPathExtension(); - - $this->assertEquals( - '/silverstripe-hashpath/tests/test.vL32cPgz9Rj8qwwSRHsr8A.txt', - $hashPath->HashPath( - '/silverstripe-hashpath/tests/test.txt', - false - ) - ); - - $hashPath->setFormat('%s/%s.v.%s.%s'); - - $this->assertEquals( - '/silverstripe-hashpath/tests/test.v.L32cPgz9Rj8qwwSRHsr8A.txt', - $hashPath->HashPath( - '/silverstripe-hashpath/tests/test.txt', - false - ) - ); - - } - -} \ No newline at end of file + /** + * + */ + public function testHashFile() + { + + $hashPath = new HashPathExtension(); + + $this->assertEquals( + 'L32cPgz9Rj8qwwSRHsr8A', + $hashPath->HashFile( + __DIR__ . '/test.txt', + false + ) + ); + + $this->assertEquals( + '', + $hashPath->HashFile( + 'fakefile', + false + ) + ); + } + + /** + * + */ + public function testHashPath() + { + + $hashPath = new HashPathExtension(); + + $this->assertEquals( + '/silverstripe-hashpath/tests/test.vL32cPgz9Rj8qwwSRHsr8A.txt', + $hashPath->HashPath( + '/silverstripe-hashpath/tests/test.txt', + false + ) + ); + + $hashPath->setFormat('%s/%s.v.%s.%s'); + + $this->assertEquals( + '/silverstripe-hashpath/tests/test.v.L32cPgz9Rj8qwwSRHsr8A.txt', + $hashPath->HashPath( + '/silverstripe-hashpath/tests/test.txt', + false + ) + ); + } +}