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 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/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/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/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; + } +} 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()); } }