├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── config └── asset-helper.php └── src ├── AssetHelper.php ├── AssetHelperFacade.php └── AssetHelperServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | before_script: 11 | - composer self-update 12 | - composer install --prefer-source --no-interaction --dev 13 | 14 | script: phpunit 15 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `asset-helper` will be documented in this file 4 | 5 | ## 2.0.0 - 2016-04-22 6 | - Laravel 5.1 compatibility 7 | 8 | ## 1.1.1 - 2015-09-17 9 | 10 | ### Fixed 11 | - Killed a bug where directory would not be returned 12 | 13 | ## 1.1.0 - 2015-09-17 14 | 15 | ### Added 16 | - Support for complex paths 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Freek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [](https://supportukrainenow.org) 3 | 4 | Get the url to a revisioned asset 5 | ======= 6 | [![Latest Stable Version](https://poser.pugx.org/spatie/asset-helper/version.png)](https://packagist.org/packages/spatie/asset-helper) 7 | [![License](https://poser.pugx.org/spatie/asset-helper/license.png)](https://packagist.org/packages/spatie/asset-helper) 8 | 9 | This Laravel 4 package enables you to get an url to a revisioned asset. 10 | 11 | That sounds a bit vague, right? Let me clear it up by showing an example. 12 | Calling this provided method `Asset::getUrl('admin.css')` returns `/assets/admin.0ce5cb43.css` 13 | 14 | This package assumes that your asset pipeline: 15 | - saves all of your assets in one public folder. 16 | - puts a random string in the filename of every asset as a cache-busting mechanism 17 | 18 | Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). 19 | 20 | ## Support us 21 | 22 | Learn how to create a package like this one, by watching our premium video course: 23 | 24 | [![Laravel Package training](https://spatie.be/github/package-training.jpg)](https://laravelpackage.training) 25 | 26 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 27 | 28 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 29 | 30 | ## Postcardware 31 | 32 | You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using. 33 | 34 | Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. 35 | 36 | The best postcards will get published on the open source page on our website. 37 | 38 | ##Installation 39 | The package can be installed through Composer: 40 | 41 | ``` 42 | composer require spatie/asset-helper 43 | ``` 44 | 45 | This service provider must be installed: 46 | 47 | ```php 48 | 49 | //for laravel <=4.2: app/config/app.php 50 | 51 | 'providers' => [ 52 | ... 53 | 'Spatie\AssetHelper\AssetHelperServiceProvider' 54 | ... 55 | ]; 56 | ``` 57 | 58 | This package also comes with a facade, which provides an easy way to call the the functionality. 59 | 60 | 61 | ```php 62 | 63 | //for laravel <=4.2: app/config/app.php 64 | 65 | 'aliases' => array( 66 | ... 67 | 'Asset' => 'Spatie\AssetHelper\AssetHelperFacade', 68 | ... 69 | ) 70 | ``` 71 | 72 | ##Configuration 73 | You can publish the configuration file using this command: 74 | ```console 75 | php artisan config:publish spatie/asset-helper 76 | ``` 77 | 78 | A configuration-file with some sensible defaults will be placed in your config/packages directory: 79 | 80 | ```php 81 | return 82 | [ 83 | /** 84 | * The url that points to the directory were your assets are stored 85 | * 86 | */ 87 | 'assetDirectoryUrl' => '/assets', 88 | ]; 89 | ``` 90 | 91 | ##Usage 92 | ```Asset::getUrl($nonRevisionedAssetName)``` takes a non-revisioned asset name and it returns the url to the revisioned asset. 93 | 94 | ##Example 95 | Let's assume the public path of your website is `/home/forge/yourwebsite.be/public` and that your asset pipeline stores the minified, uglified, ... and whatnot assets in this directory: `/home/forge/yourwebsite.be/public/assets` 96 | 97 | Suppose this is the contents of that directory: 98 | - admin.0ce5cb43.css 99 | - admin.defer.adc60631.js 100 | - admin.head.1954b61c.js 101 | - front.0b4c09c7.css 102 | - front.defer.1ba6b072.js 103 | - front.head.1954b61c.js 104 | 105 | As a cache-busting mechanism your asset pipeline probably puts a random string in the filename of the asset. In the example above this is '0ce5cb43', 'adc60631', '1954b61c.js', ... 106 | 107 | Calling `Asset::getUrl('admin.css')` returns `/assets/admin.0ce5cb43.css` 108 | 109 | So your layout file could look something like this: 110 | 111 | ```html+php 112 | 113 | 114 | 115 | ... 116 | 117 | 118 | ... 119 | 120 | 121 | ... 122 | 123 | 124 | 125 | ``` 126 | 127 | ## About Spatie 128 | Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). 129 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/asset-helper", 3 | "license": "MIT", 4 | "description": "Get the url to a revisioned asset", 5 | "homepage": "https://github.com/spatie/asset-helper", 6 | "keywords": [ 7 | "asset", 8 | "helper", 9 | "url", 10 | "laravel" 11 | ], 12 | "authors": [ 13 | { 14 | "name": "Freek Van der Herten", 15 | "email": "freek@spatie.be" 16 | } 17 | ], 18 | "require": { 19 | "php": "^5.6.0|^7.0", 20 | "illuminate/support": "~5.1.0|~5.2.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Spatie\\AssetHelper\\": "src/" 25 | } 26 | }, 27 | "minimum-stability": "stable" 28 | } 29 | -------------------------------------------------------------------------------- /config/asset-helper.php: -------------------------------------------------------------------------------- 1 | '/assets', 10 | ]; -------------------------------------------------------------------------------- /src/AssetHelper.php: -------------------------------------------------------------------------------- 1 | getRevisionedFileName($assetName); 18 | } 19 | 20 | /** 21 | * Get the revisioned name of the asset. 22 | * 23 | * @param $asset 24 | * 25 | * @return string 26 | */ 27 | public function getRevisionedFileName($asset) 28 | { 29 | $globSearchString = pathinfo($asset, PATHINFO_DIRNAME).'/'. 30 | pathinfo($asset, PATHINFO_FILENAME).'.*.'. 31 | pathinfo($asset, PATHINFO_EXTENSION); 32 | 33 | $globResults = glob(public_path(). 34 | config('asset-helper.assetDirectoryUrl').'/'.$globSearchString 35 | ); 36 | 37 | if (!count($globResults)) { 38 | return ''; 39 | } 40 | 41 | return pathinfo($asset, PATHINFO_DIRNAME).'/'.pathinfo($globResults[0], PATHINFO_BASENAME); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/AssetHelperFacade.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__.'/../config/asset-helper.php', 'asset-helper'); 12 | 13 | $this->publishes([ 14 | __DIR__.'/../config/asset-helper.php' => config_path('asset-helper.php'), 15 | ], 'config'); 16 | } 17 | 18 | public function register() 19 | { 20 | $this->app->bind(AssetHelper::class); 21 | $this->app->alias(AssetHelper::class, 'asset-helper'); 22 | } 23 | } 24 | --------------------------------------------------------------------------------