From 3c23601ce0e3d81244cb9336baf0064585c7f00c Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Tue, 26 Nov 2024 15:24:11 -0700 Subject: [PATCH] Added the ability to inject arbitrary analytics script code --- .env.example | 5 ----- .gitignore | 1 + app/config/app.php | 12 ++++++++++++ app/config/container.php | 3 ++- app/src/ViewFunctions/Analytics.php | 26 ++++++++++++++++++++++++++ app/views/layouts/app.twig | 4 ++++ tests/HiddenFilesTest.php | 4 ++-- tests/ViewFunctions/AnalyticsTest.php | 25 +++++++++++++++++++++++++ tests/_files/.analytics | 1 + 9 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 app/src/ViewFunctions/Analytics.php create mode 100644 tests/ViewFunctions/AnalyticsTest.php create mode 100644 tests/_files/.analytics diff --git a/.env.example b/.env.example index bed4d8f..bf3156e 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/.gitignore b/.gitignore index fcc6788..98db5be 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /app/vendor/ /node_modules/ /.coverage-cache/ +/.analytics /.env /.hidden /.php-cs-fixer.cache diff --git a/app/config/app.php b/app/config/app.php index aa8ca79..987cd60 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -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. diff --git a/app/config/container.php b/app/config/container.php index 19b6900..6644ab8 100644 --- a/app/config/container.php +++ b/app/config/container.php @@ -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, diff --git a/app/src/ViewFunctions/Analytics.php b/app/src/ViewFunctions/Analytics.php new file mode 100644 index 0000000..187b7ba --- /dev/null +++ b/app/src/ViewFunctions/Analytics.php @@ -0,0 +1,26 @@ +config->get('base_path') . '/' . $this->config->get('analytics_file'); + + if (! is_file($analyticsFile)) { + return ''; + } + + return trim((string) file_get_contents($analyticsFile)); + } +} diff --git a/app/views/layouts/app.twig b/app/views/layouts/app.twig index 9c0b581..b5de62a 100644 --- a/app/views/layouts/app.twig +++ b/app/views/layouts/app.twig @@ -14,10 +14,14 @@ +{{ 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 %} diff --git a/tests/HiddenFilesTest.php b/tests/HiddenFilesTest.php index 44d9c0c..dfe90fe 100644 --- a/tests/HiddenFilesTest.php +++ b/tests/HiddenFilesTest.php @@ -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', ], ], ]; diff --git a/tests/ViewFunctions/AnalyticsTest.php b/tests/ViewFunctions/AnalyticsTest.php new file mode 100644 index 0000000..39ea0d3 --- /dev/null +++ b/tests/ViewFunctions/AnalyticsTest.php @@ -0,0 +1,25 @@ +config); + + $this->assertEquals('', $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()); + } +} diff --git a/tests/_files/.analytics b/tests/_files/.analytics new file mode 100644 index 0000000..682d435 --- /dev/null +++ b/tests/_files/.analytics @@ -0,0 +1 @@ +