From 9089466b289502282b94d9c600ac8e77b7d224c8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 27 Apr 2021 12:56:43 -0400 Subject: [PATCH 1/2] add `counterValue()` method (#416) --- src/Ray.php | 5 +++++ tests/RayTest.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Ray.php b/src/Ray.php index 1923ef2c..c926dc6b 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -414,6 +414,11 @@ public function clearCounters(): self return $this; } + public function counterValue(string $name): int + { + return self::$counters->get($name); + } + public function pause(): self { $lockName = md5(time()); diff --git a/tests/RayTest.php b/tests/RayTest.php index 71a655e6..f2ea1fe7 100644 --- a/tests/RayTest.php +++ b/tests/RayTest.php @@ -760,6 +760,20 @@ public function it_clears_all_counters() $this->assertEquals(0, Ray::$counters->get('first')); } + /** @test */ + public function it_returns_the_value_of_a_named_counter() + { + $this->assertEquals(0, ray()->counterValue('first')); + + ray()->count('first'); + + $this->assertEquals(1, ray()->counterValue('first')); + + ray()->count('first'); + + $this->assertEquals(2, ray()->counterValue('first')); + } + /** @test */ public function it_will_respect_the_raw_values_config_setting() { From 60fef2edf371a87cdca15acd730b85e3ecf863e1 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 27 Apr 2021 12:57:05 -0400 Subject: [PATCH 2/2] add description and examples for `counterValue()` function --- docs/usage/framework-agnostic-php-project.md | 12 ++++++++++++ docs/usage/reference.md | 1 + 2 files changed, 13 insertions(+) diff --git a/docs/usage/framework-agnostic-php-project.md b/docs/usage/framework-agnostic-php-project.md index a9fd9df4..7637cfdc 100644 --- a/docs/usage/framework-agnostic-php-project.md +++ b/docs/usage/framework-agnostic-php-project.md @@ -151,6 +151,18 @@ foreach (range(1, 4) as $i) { } ``` +You may access the value of a named counter using the `counterValue` function. + +```php +foreach (range(1, 4) as $i) { + ray()->count('first'); + + if (ray()->counterValue('first') === 2) { + echo "counter value is two!"; + } +} +``` + This is how that looks like in Ray. ![screenshot](/docs/ray/v1/images/named-count.png) diff --git a/docs/usage/reference.md b/docs/usage/reference.md index dd7026eb..cfd69046 100644 --- a/docs/usage/reference.md +++ b/docs/usage/reference.md @@ -38,6 +38,7 @@ To display something in Ray use the `ray()` function. It accepts everything: str | `ray()->clearScreen()` | Clear current screen | | `ray()->clearAll()` | Clear current and all previous screens | | `ray()->count()` | Count how many times a piece of code is called | +| `ray()->counterValue(name)` | Return the value of a named counter | | `ray(…)->die()` or `rd(…)` | Stop the PHP process | | `ray()->disable()` | Disable sending stuff to Ray | | `ray()->disabled()` | Check if Ray is disabled |