From ea9b2b5a03ebbdcaf60eb066ecd7ab402b399310 Mon Sep 17 00:00:00 2001 From: Brian Feaver Date: Fri, 4 Jun 2021 10:10:09 -0700 Subject: [PATCH 1/3] build: add commitizen config file --- .cz.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 .cz.json diff --git a/.cz.json b/.cz.json new file mode 100644 index 0000000..106afa5 --- /dev/null +++ b/.cz.json @@ -0,0 +1 @@ +{"commitizen": {"name": "cz_conventional_commits", "version": "3.0.2", "tag_format": "$version"}} \ No newline at end of file From c3e984da727b20a8502c2dbebe017582b30f95be Mon Sep 17 00:00:00 2001 From: Brian Feaver Date: Fri, 4 Jun 2021 10:11:56 -0700 Subject: [PATCH 2/3] feat: add *Aware interface and trait for service injection New *Aware interface and trait allow easier auto injection of client service. Fixes: #2 --- README.md | 27 +++++++++++++++++++++++++++ src/StatsdAwareInterface.php | 14 ++++++++++++++ src/StatsdAwareTrait.php | 22 ++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/StatsdAwareInterface.php create mode 100644 src/StatsdAwareTrait.php diff --git a/README.md b/README.md index c9dd267..ef5ba13 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,33 @@ To disable sending any metrics to the statsd server, you can use the `Domnikl\St 
class instead of the default socket abstraction. This may be incredibly useful for feature flags. Another options is to use `Domnikl\Statsd\Connection\InMemory` connection class, that will collect your messages but won't actually send them. +## StatsdAwareInterface + +You can use the `StatsdAwareInterface` and `StatsdAwareTrait` in order to have dependency injection containers (such as +Symfony's DI component) automatically detect the StatsdAwareInterface and inject the client into your service. + +### Symfony + +```yaml +# config/services.yaml +services: + _instanceof: + Domnikl\Statsd\StatsdAwareInterface: + calls: + - [setStatsdClient, ['@Domnikl\Statsd\Client']] + + Domnikl\Statsd\Client: + arguments: + $connection: '@app.statsd_connection' + $namespace: '' + + app.statsd_connection: + class: Domnikl\Statsd\Connection\UdpSocket + arguments: + $host: '%env(STATSD_HOST)%' + $port: '%env(STATSD_PORT)%' +``` + ## Authors Original author: Dominik Liebler diff --git a/src/StatsdAwareInterface.php b/src/StatsdAwareInterface.php new file mode 100644 index 0000000..47100eb --- /dev/null +++ b/src/StatsdAwareInterface.php @@ -0,0 +1,14 @@ +statsd = $client; + } +} From 8143f4a49a5a44f4b702df90081816d58e8f6fb6 Mon Sep 17 00:00:00 2001 From: Brian Feaver Date: Fri, 4 Jun 2021 10:44:44 -0700 Subject: [PATCH 3/3] style(php-cs-fixer): replace phpcs with php-cs-fixer --- .github/workflows/php.yml | 2 +- .gitignore | 3 +++ .php-cs-fixer.dist.php | 13 +++++++++ composer.json | 4 +-- phpcs.xml.dist | 11 -------- tests/unit/ClientBatchTest.php | 48 +++++++++++++++++----------------- 6 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 .php-cs-fixer.dist.php delete mode 100644 phpcs.xml.dist diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index ff18ad3..16d161d 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -25,7 +25,7 @@ jobs: run: composer install --prefer-dist --no-progress --no-suggest --no-interaction - name: phpcs - run: vendor/bin/phpcs -s --standard=vendor/flyeralarm/php-code-validator/ruleset.xml src/ tests/ + run: vendor/bin/php-cs-fixer fix --diff --dry-run -v - name: psalm run: vendor/bin/psalm --show-info=false diff --git a/.gitignore b/.gitignore index e560a23..6e26b63 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ stats.log /vendor /composer.lock /composer.phar + +# php-cs-fixer +.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..60b18e0 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,13 @@ +in(__DIR__) +; + +$config = new PhpCsFixer\Config(); +return $config->setRules([ + '@PSR12' => true, + 'array_syntax' => ['syntax' => 'short'], +]) + ->setFinder($finder) +; diff --git a/composer.json b/composer.json index 34c2f3c..6c1aeb1 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,8 @@ }, "require-dev": { "phpunit/phpunit": "^9", - "flyeralarm/php-code-validator": "^3.2", - "vimeo/psalm": "^4.6" + "vimeo/psalm": "^4.6", + "friendsofphp/php-cs-fixer": "^3.0" }, "autoload": { "psr-4": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist deleted file mode 100644 index ee4e245..0000000 --- a/phpcs.xml.dist +++ /dev/null @@ -1,11 +0,0 @@ - - - **/*.php - vendor - docs - - - - - - diff --git a/tests/unit/ClientBatchTest.php b/tests/unit/ClientBatchTest.php index 50dc886..7836516 100644 --- a/tests/unit/ClientBatchTest.php +++ b/tests/unit/ClientBatchTest.php @@ -32,47 +32,47 @@ public function testInit() public function testStartBatch() { - $this->client->startBatch(); - $this->assertTrue($this->client->isBatch()); + $this->client->startBatch(); + $this->assertTrue($this->client->isBatch()); } public function testSendIsRecordingInBatch() { - $this->client->startBatch(); - $this->client->increment("foobar", 1); + $this->client->startBatch(); + $this->client->increment("foobar", 1); - $message = $this->connection->getLastMessage(); - $this->assertNull($message); + $message = $this->connection->getLastMessage(); + $this->assertNull($message); } public function testEndBatch() { - $this->client->startBatch(); - $this->client->count("foobar", 1); - $this->client->count("foobar", 2); - $this->client->endBatch(); + $this->client->startBatch(); + $this->client->count("foobar", 1); + $this->client->count("foobar", 2); + $this->client->endBatch(); - $this->assertFalse($this->client->isBatch()); - $this->assertSame("foobar:1|c\nfoobar:2|c", $this->connection->getLastMessage()); + $this->assertFalse($this->client->isBatch()); + $this->assertSame("foobar:1|c\nfoobar:2|c", $this->connection->getLastMessage()); - // run a new batch => don't send the old values! + // run a new batch => don't send the old values! - $this->client->startBatch(); - $this->client->count("baz", 100); - $this->client->count("baz", 300); - $this->client->endBatch(); + $this->client->startBatch(); + $this->client->count("baz", 100); + $this->client->count("baz", 300); + $this->client->endBatch(); - $this->assertFalse($this->client->isBatch()); - $this->assertSame("baz:100|c\nbaz:300|c", $this->connection->getLastMessage()); + $this->assertFalse($this->client->isBatch()); + $this->assertSame("baz:100|c\nbaz:300|c", $this->connection->getLastMessage()); } public function testCancelBatch() { - $this->client->startBatch(); - $this->client->count("foobar", 4); - $this->client->cancelBatch(); + $this->client->startBatch(); + $this->client->count("foobar", 4); + $this->client->cancelBatch(); - $this->assertFalse($this->client->isBatch()); - $this->assertNull($this->connection->getLastMessage()); + $this->assertFalse($this->client->isBatch()); + $this->assertNull($this->connection->getLastMessage()); } }