From 8f33996e46c0447a5bc175d71a3296ff23e0478d Mon Sep 17 00:00:00 2001 From: Jie Date: Sun, 11 May 2025 15:55:00 +0800 Subject: [PATCH 1/4] feat: Add support for excluding all environment variables --- config/config.default.php | 1 + src/ProfilingData.php | 7 +++++++ tests/ProfilingDataTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 tests/ProfilingDataTest.php diff --git a/config/config.default.php b/config/config.default.php index d086ca0..9f5925b 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -32,6 +32,7 @@ ), 'profiler.options' => array(), 'profiler.exclude-env' => array(), + 'profiler.is-exclude-all-env' => false, 'profiler.simple_url' => function ($url) { return preg_replace('/=\d+/', '', $url); }, diff --git a/src/ProfilingData.php b/src/ProfilingData.php index 5ab12aa..e6916c1 100644 --- a/src/ProfilingData.php +++ b/src/ProfilingData.php @@ -13,10 +13,13 @@ final class ProfilingData private $simpleUrl; /** @var callable|null */ private $replaceUrl; + /** @var bool */ + private $isExcludeAllEnv; public function __construct(Config $config) { $this->excludeEnv = isset($config['profiler.exclude-env']) ? (array)$config['profiler.exclude-env'] : array(); + $this->isExcludeAllEnv = isset($config['profiler.is-exclude-all-env']) ? $config['profiler.is-exclude-all-env'] : false; $this->simpleUrl = isset($config['profiler.simple_url']) ? $config['profiler.simple_url'] : null; $this->replaceUrl = isset($config['profiler.replace_url']) ? $config['profiler.replace_url'] : null; } @@ -79,6 +82,10 @@ public function getProfilingData(array $profile) */ private function getEnvironment(array $env) { + if($this->isExcludeAllEnv) { + return array(); + } + foreach ($this->excludeEnv as $key) { unset($env[$key]); } diff --git a/tests/ProfilingDataTest.php b/tests/ProfilingDataTest.php new file mode 100644 index 0000000..1b05f28 --- /dev/null +++ b/tests/ProfilingDataTest.php @@ -0,0 +1,25 @@ + true + ]); + $profilingData = new ProfilingData($config); + + $profile = ['example' => 'data']; + $result = $profilingData->getProfilingData($profile); + + + $this->assertEmpty($result['meta']['env']); + } + + +} From 674b1df120268d3f460675a6d373c7c0b7ef7bd0 Mon Sep 17 00:00:00 2001 From: Jie Date: Sun, 11 May 2025 17:18:31 +0800 Subject: [PATCH 2/4] feat: Add test for handling profiler environment exclusion --- tests/ProfilingDataTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/ProfilingDataTest.php b/tests/ProfilingDataTest.php index 1b05f28..b269053 100644 --- a/tests/ProfilingDataTest.php +++ b/tests/ProfilingDataTest.php @@ -9,8 +9,10 @@ class ProfilingDataTest extends TestCase { public function testExcludeAllEnv() { + $_ENV['TEST_EXCLUDE_ENV'] = 'TEST'; + $config = new Config([ - 'profiler.is-exclude-all-env' => true + 'profiler.is-exclude-all-env' => true, ]); $profilingData = new ProfilingData($config); @@ -21,5 +23,20 @@ public function testExcludeAllEnv() $this->assertEmpty($result['meta']['env']); } + public function testNotExcludeAllEnv() + { + $_ENV['TEST_EXCLUDE_ENV'] = 'TEST'; + + $config = new Config([ + 'profiler.is-exclude-all-env' => false, + ]); + $profilingData = new ProfilingData($config); + + $profile = ['example' => 'data']; + $result = $profilingData->getProfilingData($profile); + + $this->assertEquals('TEST', $result['meta']['env']['TEST_EXCLUDE_ENV']); + } + } From c1a5c3b7ecf73d14a7c8e0191d7b8e3953535835 Mon Sep 17 00:00:00 2001 From: Jie Date: Sat, 2 Aug 2025 20:33:09 +0800 Subject: [PATCH 3/4] refactor: Rename profiler configuration key for environment exclusion --- config/config.default.php | 2 +- src/ProfilingData.php | 6 +++--- tests/ProfilingDataTest.php | 7 ++----- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/config/config.default.php b/config/config.default.php index 9f5925b..ba0291a 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -32,7 +32,7 @@ ), 'profiler.options' => array(), 'profiler.exclude-env' => array(), - 'profiler.is-exclude-all-env' => false, + 'profiler.exclude-all-env' => false, 'profiler.simple_url' => function ($url) { return preg_replace('/=\d+/', '', $url); }, diff --git a/src/ProfilingData.php b/src/ProfilingData.php index e6916c1..6b44928 100644 --- a/src/ProfilingData.php +++ b/src/ProfilingData.php @@ -14,12 +14,12 @@ final class ProfilingData /** @var callable|null */ private $replaceUrl; /** @var bool */ - private $isExcludeAllEnv; + private $excludeAllEnv; public function __construct(Config $config) { $this->excludeEnv = isset($config['profiler.exclude-env']) ? (array)$config['profiler.exclude-env'] : array(); - $this->isExcludeAllEnv = isset($config['profiler.is-exclude-all-env']) ? $config['profiler.is-exclude-all-env'] : false; + $this->excludeAllEnv = isset($config['profiler.exclude-all-env']) ? $config['profiler.exclude-all-env'] : false; $this->simpleUrl = isset($config['profiler.simple_url']) ? $config['profiler.simple_url'] : null; $this->replaceUrl = isset($config['profiler.replace_url']) ? $config['profiler.replace_url'] : null; } @@ -82,7 +82,7 @@ public function getProfilingData(array $profile) */ private function getEnvironment(array $env) { - if($this->isExcludeAllEnv) { + if ($this->excludeAllEnv) { return array(); } diff --git a/tests/ProfilingDataTest.php b/tests/ProfilingDataTest.php index b269053..ac0171f 100644 --- a/tests/ProfilingDataTest.php +++ b/tests/ProfilingDataTest.php @@ -12,14 +12,13 @@ public function testExcludeAllEnv() $_ENV['TEST_EXCLUDE_ENV'] = 'TEST'; $config = new Config([ - 'profiler.is-exclude-all-env' => true, + 'profiler.exclude-all-env' => true, ]); $profilingData = new ProfilingData($config); $profile = ['example' => 'data']; $result = $profilingData->getProfilingData($profile); - $this->assertEmpty($result['meta']['env']); } @@ -28,7 +27,7 @@ public function testNotExcludeAllEnv() $_ENV['TEST_EXCLUDE_ENV'] = 'TEST'; $config = new Config([ - 'profiler.is-exclude-all-env' => false, + 'profiler.exclude-all-env' => false, ]); $profilingData = new ProfilingData($config); @@ -37,6 +36,4 @@ public function testNotExcludeAllEnv() $this->assertEquals('TEST', $result['meta']['env']['TEST_EXCLUDE_ENV']); } - - } From 021526711572192fe6c12e1d71af33d05bbdcee7 Mon Sep 17 00:00:00 2001 From: Jie Date: Sat, 2 Aug 2025 21:15:18 +0800 Subject: [PATCH 4/4] feat: Add configuration to exclude all environment variables from profiling --- examples/autoload.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/autoload.php b/examples/autoload.php index 13a451a..e689932 100644 --- a/examples/autoload.php +++ b/examples/autoload.php @@ -118,6 +118,11 @@ 'profiler.replace_url' => function ($url) { return str_replace('token', '', $url); }, + /** + * If true, excludes all environment variables from profiling data. + * @return bool + */ + 'profiler.exclude-all-env' => false, ); /**