Added the ability to inject arbitrary analytics script code

This commit is contained in:
Chris Kankiewicz
2024-11-26 15:24:11 -07:00
parent c07d3cec0d
commit 3c23601ce0
9 changed files with 73 additions and 8 deletions

View File

@@ -9,10 +9,5 @@ DISPLAY_READMES=true
READMES_FIRST=false
ZIP_DOWNLOADS=true
GOOGLE_ANALYTICS_ID=false
MATOMO_ANALYTICS_URL=false
MATOMO_ANALYTICS_ID=false
SORT_ORDER=type
REVERSE_SORT=false

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
/app/vendor/
/node_modules/
/.coverage-cache/
/.analytics
/.env
/.hidden
/.php-cs-fixer.cache

View File

@@ -76,6 +76,8 @@ return [
'zip_compress' => env('ZIP_COMPRESS', false),
/**
* DEPRECATED: Will be removed in a future release.
*
* Your Google analytics tracking ID.
*
* Expected format: 'UA-123456789-0'
@@ -84,6 +86,8 @@ return [
'google_analytics_id' => env('GOOGLE_ANALYTICS_ID', false),
/**
* DEPRECATED: Will be removed in a future release.
*
* Your Matomo analytics URL.
*
* Default value: false
@@ -112,6 +116,14 @@ return [
*/
'reverse_sort' => env('REVERSE_SORT', false),
/**
* File containing analytics scripts that will be included in the HTML
* output of your directory listing.
*
* Default value: '.analytics'
*/
'analytics_file' => env('ANALYTICS_FILE', '.analytics'),
/**
* File containing hidden file definitions. Will be merged with definitions
* from the 'hidden_files' configuration option.

View File

@@ -20,7 +20,7 @@ return [
'views_path' => string('{app_path}/views'),
/** Array of application files (to be hidden) */
'app_files' => ['app', 'index.php', '.env', '.env.example', '.hidden'],
'app_files' => ['app', 'index.php', '.analytics', '.env', '.env.example', '.hidden'],
/** Array of application middlewares */
'middlewares' => fn (): array => [
@@ -42,6 +42,7 @@ return [
/** Array of view functions */
'view_functions' => [
ViewFunctions\Analytics::class,
ViewFunctions\Asset::class,
ViewFunctions\Breadcrumbs::class,
ViewFunctions\Config::class,

View File

@@ -0,0 +1,26 @@
<?php
namespace App\ViewFunctions;
use App\Config;
class Analytics extends ViewFunction
{
protected string $name = 'analytics';
public function __construct(
private Config $config
) {}
/** Get the contents of the .analytics file. */
public function __invoke(): string
{
$analyticsFile = $this->config->get('base_path') . '/' . $this->config->get('analytics_file');
if (! is_file($analyticsFile)) {
return '';
}
return trim((string) file_get_contents($analyticsFile));
}
}

View File

@@ -14,10 +14,14 @@
<script src="{{ asset('app.js') }}" defer></script>
{{ analytics() | raw }}
{# NOTE: To be removed in a future release #}
{% if config('google_analytics_id', false) %}
{% include 'components/analytics/google.twig' %}
{% endif %}
{# NOTE: To be removed in a future release #}
{% if config('matomo_analytics_url', false) and config('matomo_analytics_site_id', false) %}
{% include 'components/analytics/matomo.twig' %}
{% endif %}

View File

@@ -51,12 +51,12 @@ class HiddenFilesTest extends TestCase
],
'App files' => [
[], 'NOT_A_REAL_FILE', true, [
'app', 'index.php', '.env', '.env.example', '.hidden',
'app', 'index.php', '.analytics', '.env', '.env.example', '.hidden',
],
],
'All' => [
['foo', 'alpha'], $this->filePath('.hidden'), true, [
'foo', 'alpha', 'bravo', 'app', 'index.php', '.env', '.env.example', '.hidden',
'foo', 'alpha', 'bravo', 'app', 'index.php', '.analytics', '.env', '.env.example', '.hidden',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
namespace Tests\ViewFunctions;
use App\ViewFunctions\Analytics;
use Tests\TestCase;
/** @covers \App\ViewFunctions\Analytics */
class AnalyticsTest extends TestCase
{
public function test_it_can_return_the_analytics_file_contents(): void
{
$analytics = new Analytics($this->config);
$this->assertEquals('<!-- Test file; please ignore -->', $analytics());
}
public function test_it_does_not_return_anything_when_the_analytics_file_does_not_exist(): void
{
$this->container->set('analytics_file', 'NONEXISTENT_FILE');
$analytics = new Analytics($this->config);
$this->assertEquals('', $analytics());
}
}

1
tests/_files/.analytics Normal file
View File

@@ -0,0 +1 @@
<!-- Test file; please ignore -->