From 45ca7eb083931d33ee830e2375396b88e13c676c Mon Sep 17 00:00:00 2001 From: Fabiano Sant'Ana Date: Sat, 11 Jul 2020 01:04:38 -0300 Subject: [PATCH 1/2] PostHog/posthog-php#3: Update composer.json to support PSR-4 --- composer.json | 6 +++--- lib/PostHog.php | 4 ++-- lib/PostHog/Client.php | 17 ++++++++--------- lib/PostHog/Consumer.php | 5 ++++- lib/PostHog/Consumer/File.php | 6 +++++- lib/PostHog/Consumer/ForkCurl.php | 6 +++++- lib/PostHog/Consumer/LibCurl.php | 6 +++++- lib/PostHog/Consumer/Socket.php | 6 +++++- lib/PostHog/QueueConsumer.php | 4 +++- test/ConsumerFileTest.php | 6 +++--- test/ConsumerForkCurlTest.php | 2 +- test/ConsumerLibCurlTest.php | 2 +- test/ConsumerSocketTest.php | 12 ++++++------ test/PostHogTest.php | 2 +- 14 files changed, 52 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index db06ad9..f1a9fbc 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,9 @@ "squizlabs/php_codesniffer": "^3.5" }, "autoload": { - "files": [ - "lib/PostHog.php" - ] + "psr-4": { + "Posthog": "lib" + } }, "bin": [ "bin/posthog" diff --git a/lib/PostHog.php b/lib/PostHog.php index da22ab4..72dbf13 100644 --- a/lib/PostHog.php +++ b/lib/PostHog.php @@ -1,6 +1,6 @@ "PostHog_Consumer_Socket", + "socket" => "Socket", "file" => "PostHog_Consumer_File", "fork_curl" => "PostHog_Consumer_ForkCurl", "lib_curl" => "PostHog_Consumer_LibCurl" diff --git a/lib/PostHog/Consumer.php b/lib/PostHog/Consumer.php index c660954..3fdaf95 100644 --- a/lib/PostHog/Consumer.php +++ b/lib/PostHog/Consumer.php @@ -1,5 +1,8 @@ filename()); } - $this->client = new PostHog_Client( + $this->client = new Client( "BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", array( "consumer" => "file", @@ -78,7 +78,7 @@ public function testSend() public function testProductionProblems() { // Open to a place where we should not have write access. - $client = new PostHog_Client( + $client = new Client( "BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", array( "consumer" => "file", diff --git a/test/ConsumerForkCurlTest.php b/test/ConsumerForkCurlTest.php index 764c0d6..109295f 100644 --- a/test/ConsumerForkCurlTest.php +++ b/test/ConsumerForkCurlTest.php @@ -9,7 +9,7 @@ class ConsumerForkCurlTest extends PHPUnit\Framework\TestCase public function setUp(): void { date_default_timezone_set("UTC"); - $this->client = new PostHog_Client( + $this->client = new Client( "OnMMoZ6YVozrgSBeZ9FpkC0ixH0ycYZn", array( "consumer" => "fork_curl", diff --git a/test/ConsumerLibCurlTest.php b/test/ConsumerLibCurlTest.php index 8fd1a4b..7d6ec38 100644 --- a/test/ConsumerLibCurlTest.php +++ b/test/ConsumerLibCurlTest.php @@ -9,7 +9,7 @@ class ConsumerLibCurlTest extends PHPUnit\Framework\TestCase public function setUp(): void { date_default_timezone_set("UTC"); - $this->client = new PostHog_Client( + $this->client = new Client( "BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", array( "consumer" => "lib_curl", diff --git a/test/ConsumerSocketTest.php b/test/ConsumerSocketTest.php index 56d9873..623b4db 100644 --- a/test/ConsumerSocketTest.php +++ b/test/ConsumerSocketTest.php @@ -13,7 +13,7 @@ public function setUp(): void public function testCapture() { - $client = new PostHog_Client( + $client = new Client( "BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", array( "consumer" => "socket", @@ -28,7 +28,7 @@ public function testCapture() public function testIdentify() { - $client = new PostHog_Client( + $client = new Client( "BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", array( "consumer" => "socket", @@ -46,7 +46,7 @@ public function testIdentify() public function testShortTimeout() { - $client = new PostHog_Client( + $client = new Client( "BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", array( "timeout" => 0.01, @@ -69,7 +69,7 @@ public function testShortTimeout() public function testProductionProblems() { - $client = new PostHog_Client("x", + $client = new Client("x", array( "consumer" => "socket", "error_handler" => function () { @@ -92,7 +92,7 @@ public function testLargeMessage() "consumer" => "socket", ); - $client = new PostHog_Client("BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", $options); + $client = new Client("BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", $options); $big_property = ""; @@ -112,7 +112,7 @@ public function testLargeMessage() public function testConnectionError() { $this->expectException('RuntimeException'); - $client = new PostHog_Client("x", array( + $client = new Client("x", array( "consumer" => "socket", "host" => "t.posthog.comcomcom", "error_handler" => function ($errno, $errmsg) { diff --git a/test/PostHogTest.php b/test/PostHogTest.php index 5285f08..d687e10 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -1,6 +1,6 @@ Date: Thu, 1 Apr 2021 00:41:29 +0200 Subject: [PATCH 2/2] Finish psr-4 refactoring --- Makefile | 5 ++++- bin/posthog | 4 +++- composer.json | 4 ++-- lib/{PostHog => }/Client.php | 24 +++++++++++------------- lib/{PostHog => }/Consumer.php | 2 +- lib/{PostHog => }/Consumer/File.php | 8 ++++---- lib/{PostHog => }/Consumer/ForkCurl.php | 6 +++--- lib/{PostHog => }/Consumer/LibCurl.php | 6 +++--- lib/{PostHog => }/Consumer/Socket.php | 8 ++++---- lib/PostHog.php | 6 +++++- lib/PostHog/Version.php | 3 --- lib/{PostHog => }/QueueConsumer.php | 2 +- send.php | 6 ++++-- test/ConsumerFileTest.php | 4 ++-- test/ConsumerForkCurlTest.php | 2 +- test/ConsumerLibCurlTest.php | 2 +- test/ConsumerSocketTest.php | 2 +- test/PostHogTest.php | 2 +- 18 files changed, 51 insertions(+), 45 deletions(-) rename lib/{PostHog => }/Client.php (91%) rename lib/{PostHog => }/Consumer.php (99%) rename lib/{PostHog => }/Consumer/File.php (94%) rename lib/{PostHog => }/Consumer/ForkCurl.php (96%) rename lib/{PostHog => }/Consumer/LibCurl.php (96%) rename lib/{PostHog => }/Consumer/Socket.php (98%) delete mode 100644 lib/PostHog/Version.php rename lib/{PostHog => }/QueueConsumer.php (99%) diff --git a/Makefile b/Makefile index 241534d..a8976fb 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,9 @@ vendor: composer.phar composer.phar: @curl -sS https://getcomposer.org/installer | php +docker: + @docker run -it --rm -v ${PWD}:/app -w /app chialab/php:7.4 bash + test: lint @vendor/bin/phpunit --colors test/ @php ./composer.phar validate @@ -23,7 +26,7 @@ lint: dependencies release: @printf "releasing ${VERSION}..." - @printf ' ./lib/PostHog/Version.php + @sed -Ei "s/(public const VERSION =).+/\1 '${VERSION}';/" ./lib/PostHog.php @node -e "var fs = require('fs'), pkg = require('./composer'); pkg.version = '${VERSION}'; fs.writeFileSync('./composer.json', JSON.stringify(pkg, null, '\t'));" @git changelog -t ${VERSION} @git release ${VERSION} diff --git a/bin/posthog b/bin/posthog index d2b6224..775cc79 100755 --- a/bin/posthog +++ b/bin/posthog @@ -1,7 +1,9 @@ #!/usr/bin/env php "Socket", - "file" => "PostHog_Consumer_File", - "fork_curl" => "PostHog_Consumer_ForkCurl", - "lib_curl" => "PostHog_Consumer_LibCurl" + "socket" => Socket::class, + "file" => File::class, + "fork_curl" => ForkCurl::class, + "lib_curl" => LibCurl::class, ); // Use our socket libcurl by default @@ -173,18 +173,16 @@ private function formatTime($ts) { */ private function message($msg){ - global $POSTHOG_VERSION; - if (!isset($msg["properties"])) { $msg["properties"] = array(); } $msg["library"] = 'posthog-php'; - $msg["library_version"] = $POSTHOG_VERSION; + $msg["library_version"] = PostHog::VERSION; $msg["library_consumer"] = $this->consumer->getConsumer(); $msg["properties"]['$lib'] = 'posthog-php'; - $msg["properties"]['$lib_version'] = $POSTHOG_VERSION; + $msg["properties"]['$lib_version'] = PostHog::VERSION; $msg["properties"]['$lib_consumer'] = $this->consumer->getConsumer(); if (isset($msg["distinctId"])) { diff --git a/lib/PostHog/Consumer.php b/lib/Consumer.php similarity index 99% rename from lib/PostHog/Consumer.php rename to lib/Consumer.php index 3fdaf95..32d5bd1 100644 --- a/lib/PostHog/Consumer.php +++ b/lib/Consumer.php @@ -1,6 +1,6 @@ file_handle = fopen($options["filename"], "a"); chmod($options["filename"], 0777); - } catch (Exception $e) { + } catch (\Exception $e) { $this->handleError($e->getCode(), $e->getMessage()); } } diff --git a/lib/PostHog/Consumer/ForkCurl.php b/lib/Consumer/ForkCurl.php similarity index 96% rename from lib/PostHog/Consumer/ForkCurl.php rename to lib/Consumer/ForkCurl.php index b6f858a..2f455e2 100644 --- a/lib/PostHog/Consumer/ForkCurl.php +++ b/lib/Consumer/ForkCurl.php @@ -1,10 +1,10 @@ handleError($e->getCode(), $e->getMessage()); $this->socket_failed = true; @@ -111,7 +111,7 @@ private function makeRequest($socket, $req, $retry = true) { try { // Since we're try catch'ing prevent PHP logs. $written = @fwrite($socket, substr($req, $bytes_written)); - } catch (Exception $e) { + } catch (\Exception $e) { $this->handleError($e->getCode(), $e->getMessage()); $closed = true; } diff --git a/lib/PostHog.php b/lib/PostHog.php index 72dbf13..0eceb6a 100644 --- a/lib/PostHog.php +++ b/lib/PostHog.php @@ -1,8 +1,12 @@ "file", - "filename" => "/dev/xxxxxxx", + "filename" => "/dev/x/xxxxxxx", ) ); diff --git a/test/ConsumerForkCurlTest.php b/test/ConsumerForkCurlTest.php index 109295f..4f5fff7 100644 --- a/test/ConsumerForkCurlTest.php +++ b/test/ConsumerForkCurlTest.php @@ -1,6 +1,6 @@