mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-08-01 12:30:57 +02:00
Merge pull request #12 from restoreddev/develop
Added ability to version assets for browser cache busting
This commit is contained in:
@@ -8,9 +8,9 @@
|
||||
<meta name="description" content="<?= $subtitle ?? 'A site for learning how to use PHP' ?>">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<link rel="stylesheet" href="/css/site.css">
|
||||
<link rel="stylesheet" href="<?php echo asset('css/site.css') ?>">
|
||||
<link rel="icon" href="/favicon-32.png">
|
||||
<script src="/js/site.js"></script>
|
||||
<script src="<?php echo asset('js/site.js') ?>"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
35
config.php
35
config.php
@@ -3,11 +3,46 @@
|
||||
use Apprentice\Page;
|
||||
|
||||
return [
|
||||
/*
|
||||
*
|
||||
* Directory for SVG icons that can be used in templates
|
||||
*
|
||||
*/
|
||||
'icon_dir' => __DIR__ . '/assets/icons',
|
||||
|
||||
/*
|
||||
*
|
||||
* Directory holding code files used in examples
|
||||
*
|
||||
*/
|
||||
'code_dir' => __DIR__ . '/code',
|
||||
|
||||
/*
|
||||
*
|
||||
* Directory to PHP templates used by pages
|
||||
*
|
||||
*/
|
||||
'templates_dir' => __DIR__ . '/assets/templates',
|
||||
|
||||
/*
|
||||
*
|
||||
* Output directory for html files and assets
|
||||
*
|
||||
*/
|
||||
'output_dir' => __DIR__ . '/.build',
|
||||
|
||||
/*
|
||||
*
|
||||
* Static files that should be loaded into output directory
|
||||
*
|
||||
*/
|
||||
'files_dir' => __DIR__ . '/assets/files',
|
||||
|
||||
/*
|
||||
*
|
||||
* Configuration for all pages on the site
|
||||
*
|
||||
*/
|
||||
'pages' => [
|
||||
Page::create('index', 'index.phtml'),
|
||||
Page::create('installing-php', 'installing-php.phtml'),
|
||||
|
@@ -147,3 +147,24 @@ function config(string $key) {
|
||||
|
||||
return $config[$key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns path to asset based on manifes.json file
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
function asset(string $name): string {
|
||||
$outputDir = config('output_dir');
|
||||
|
||||
if (file_exists($outputDir . '/manifest.json')) {
|
||||
$text = file_get_contents($outputDir . '/manifest.json');
|
||||
$paths = json_decode($text, true);
|
||||
|
||||
if (isset($paths[$name])) {
|
||||
return $paths[$name];
|
||||
}
|
||||
}
|
||||
|
||||
return '/' . $name;
|
||||
}
|
||||
|
@@ -4,9 +4,20 @@ namespace Test;
|
||||
|
||||
class FunctionsTest extends BaseTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
mkdir('/tmp/apprentice_output');
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$GLOBALS['PARTIAL_TEST'] = null;
|
||||
|
||||
$files = glob('/tmp/apprentice_output/*');
|
||||
foreach ($files as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
rmdir('/tmp/apprentice_output');
|
||||
}
|
||||
|
||||
public function test_load_config()
|
||||
@@ -56,4 +67,23 @@ class FunctionsTest extends BaseTestCase
|
||||
|
||||
$this->assertEquals('test var', $GLOBALS['PARTIAL_TEST']);
|
||||
}
|
||||
|
||||
public function test_asset_path_with_manifest()
|
||||
{
|
||||
file_put_contents(
|
||||
'/tmp/apprentice_output/manifest.json',
|
||||
json_encode(['js/app.js' => '/js/app-1234.js'])
|
||||
);
|
||||
|
||||
$path = asset('js/app.js');
|
||||
|
||||
$this->assertEquals('/js/app-1234.js', $path);
|
||||
}
|
||||
|
||||
public function test_asset_path_without_manifest()
|
||||
{
|
||||
$path = asset('js/app.js');
|
||||
|
||||
$this->assertEquals('/js/app.js', $path);
|
||||
}
|
||||
}
|
||||
|
@@ -11,7 +11,8 @@ Encore
|
||||
"languages": ["php"],
|
||||
"css": false,
|
||||
}]);
|
||||
});
|
||||
})
|
||||
.enableVersioning();
|
||||
;
|
||||
|
||||
module.exports = Encore.getWebpackConfig();
|
||||
|
Reference in New Issue
Block a user