Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Access named counter values #437

Merged
merged 2 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/usage/framework-agnostic-php-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/usage/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
5 changes: 5 additions & 0 deletions src/Ray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
14 changes: 14 additions & 0 deletions tests/RayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down