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 "PostHog_Consumer_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 @@ -174,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 97% rename from lib/PostHog/Consumer.php rename to lib/Consumer.php index c660954..32d5bd1 100644 --- a/lib/PostHog/Consumer.php +++ b/lib/Consumer.php @@ -1,5 +1,8 @@ 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 eafd427..2f455e2 100644 --- a/lib/PostHog/Consumer/ForkCurl.php +++ b/lib/Consumer/ForkCurl.php @@ -1,6 +1,10 @@ handleError($e->getCode(), $e->getMessage()); $this->socket_failed = true; @@ -107,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 da22ab4..0eceb6a 100644 --- a/lib/PostHog.php +++ b/lib/PostHog.php @@ -1,8 +1,12 @@ filename()); } - $this->client = new PostHog_Client( + $this->client = new Client( "BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", array( "consumer" => "file", @@ -78,11 +78,11 @@ 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", - "filename" => "/dev/xxxxxxx", + "filename" => "/dev/x/xxxxxxx", ) ); diff --git a/test/ConsumerForkCurlTest.php b/test/ConsumerForkCurlTest.php index 764c0d6..4f5fff7 100644 --- a/test/ConsumerForkCurlTest.php +++ b/test/ConsumerForkCurlTest.php @@ -1,6 +1,6 @@ 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..75f8e01 100644 --- a/test/ConsumerLibCurlTest.php +++ b/test/ConsumerLibCurlTest.php @@ -1,6 +1,6 @@ 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..5165431 100644 --- a/test/ConsumerSocketTest.php +++ b/test/ConsumerSocketTest.php @@ -1,6 +1,6 @@ "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..ae6fcc0 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -1,6 +1,6 @@