|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Ingenerator\PHPUtils\Assets; |
| 5 | + |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use RuntimeException; |
| 9 | + |
| 10 | +/** |
| 11 | + * Simple / basic provider for CSS, JS, image etc asset URLs supporting cache-busting in dev and remote hosting in prod |
| 12 | + * |
| 13 | + * This class provides a very simple mechanism for generating URLs to a site's inbuilt CSS/JS/etc. It operates in two |
| 14 | + * modes. |
| 15 | + * |
| 16 | + * In `local` (developer workstation) mode, it assumes asset files are reachable on the same web host as the main site, |
| 17 | + * with the files located below the webserver's document root. Assets are served with a `?v=...` querystring based on |
| 18 | + * the file modification time, providing automatic cachebusting during development. Assets that don't exist will throw |
| 19 | + * an exception to help identify typos / incorrect relative urls during development. |
| 20 | + * |
| 21 | + * In `remote` (CI / qa / production) mode, it assumes asset files have been uploaded to a remote file hosting service |
| 22 | + * (google cloud / s3 / etc) during the build process. The application prefixes all asset URLs with a host/path prefix |
| 23 | + * pointing to that service, or to a CDN or similar. Usually this path prefix - set at build time - will contain an SHA |
| 24 | + * or similar identifier so that the assets are in sync with the application. |
| 25 | + * |
| 26 | + * A build script should therefore: |
| 27 | + * |
| 28 | + * * Compile the assets as required |
| 29 | + * * Choose a suitable remote hosting path for this specific version of the assets |
| 30 | + * * Upload them to the remote hosting in the versioned path |
| 31 | + * * Create a config file for the application containing the URL prefix - e.g. |
| 32 | + * `<?php return 'http://my.cool.cdn/project/version-a923';` |
| 33 | + * this file should then be deployed alongside the application. |
| 34 | + * |
| 35 | + * All this class does is read that file to get the URL prefix, and concat it onto the front of every asset path. |
| 36 | + * |
| 37 | + */ |
| 38 | +class StaticAssetUrlProvider |
| 39 | +{ |
| 40 | + const MODE_LOCAL = 'local'; |
| 41 | + const MODE_REMOTE = 'remote'; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + protected $local_asset_path; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var string |
| 50 | + */ |
| 51 | + protected $mode; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var string |
| 55 | + */ |
| 56 | + protected $remote_asset_url; |
| 57 | + |
| 58 | + /** |
| 59 | + * @param string $mode local or remote mode (see above) - commonly toggle based on runtime environment |
| 60 | + * @param string $local_asset_path base path on disk that all assets are relative to (in development) |
| 61 | + * @param string $asset_base_url_file path to a php file generated at build time that returns the URL host/path prefix for remote mode |
| 62 | + */ |
| 63 | + public function __construct( |
| 64 | + string $mode, |
| 65 | + string $local_asset_path, |
| 66 | + string $asset_base_url_file |
| 67 | + ) { |
| 68 | + if ( ! in_array($mode, [static::MODE_LOCAL, static::MODE_REMOTE])) { |
| 69 | + throw new InvalidArgumentException('Invalid asset mode `'.$mode.'` for '.__CLASS__); |
| 70 | + } |
| 71 | + $this->mode = $mode; |
| 72 | + $this->local_asset_path = rtrim($local_asset_path, '/'); |
| 73 | + |
| 74 | + if ($this->mode === static::MODE_REMOTE) { |
| 75 | + $this->remote_asset_url = $this->loadAssetBaseUrl($asset_base_url_file); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + protected function loadAssetBaseUrl(string $asset_base_url_file): string |
| 80 | + { |
| 81 | + if ( ! file_exists($asset_base_url_file)) { |
| 82 | + throw new InvalidArgumentException('No asset base url file at '.$asset_base_url_file); |
| 83 | + } |
| 84 | + |
| 85 | + $url = require $asset_base_url_file; |
| 86 | + |
| 87 | + if (empty($url) OR ! is_string($url)) { |
| 88 | + throw new RuntimeException('Invalid content in asset base url file '.$asset_base_url_file); |
| 89 | + } |
| 90 | + |
| 91 | + return $url; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Generates a URL for a static asset, given its relative path. |
| 96 | + * |
| 97 | + * @param string $rel_path the path of the asset within the docroot / uploaded asset files |
| 98 | + * |
| 99 | + * @return string the full URL to render to the client |
| 100 | + */ |
| 101 | + public function getUrl(string $rel_path): string |
| 102 | + { |
| 103 | + if ($rel_path[0] !== '/') { |
| 104 | + $rel_path = '/'.$rel_path; |
| 105 | + } |
| 106 | + |
| 107 | + if ($this->mode === static::MODE_LOCAL) { |
| 108 | + return $this->getLocalTimestampedUrl($rel_path); |
| 109 | + } else { |
| 110 | + return $this->remote_asset_url.$rel_path; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param string $rel_path |
| 116 | + * |
| 117 | + * @return string |
| 118 | + */ |
| 119 | + protected function getLocalTimestampedUrl(string $rel_path): string |
| 120 | + { |
| 121 | + $local_path = $this->local_asset_path.$rel_path; |
| 122 | + if ( ! file_exists($local_path)) { |
| 123 | + throw new RuntimeException('Undefined asset file '.$local_path); |
| 124 | + } |
| 125 | + |
| 126 | + return $rel_path.'?v='.filemtime($local_path); |
| 127 | + } |
| 128 | +} |
0 commit comments