| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Views; |
| 6 |
|
| 7 |
use Packetery\Module\Framework\WpAdapter; |
| 8 |
|
| 9 |
class UrlBuilder { |
| 10 |
/** |
| 11 |
* @var WpAdapter |
| 12 |
*/ |
| 13 |
private $wpAdapter; |
| 14 |
|
| 15 |
public function __construct( WpAdapter $wpAdapter ) { |
| 16 |
$this->wpAdapter = $wpAdapter; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Builds asset URL. |
| 21 |
* |
| 22 |
* @param string $asset Relative asset path without leading slash. |
| 23 |
* |
| 24 |
* @return string|null |
| 25 |
*/ |
| 26 |
public function buildAssetUrl( string $asset ): ?string { |
| 27 |
$url = $this->wpAdapter->pluginDirUrl( PACKETERY_PLUGIN_DIR . '/packeta.php' ) . $asset; |
| 28 |
$filename = PACKETERY_PLUGIN_DIR . '/' . $asset; |
| 29 |
|
| 30 |
if ( ! file_exists( $filename ) ) { |
| 31 |
return null; |
| 32 |
} |
| 33 |
|
| 34 |
return $this->wpAdapter->addQueryArg( [ 'v' => md5( (string) filemtime( $filename ) ) ], $url ); |
| 35 |
} |
| 36 |
} |
| 37 |
|