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

PSR-4 support #9

Merged
merged 2 commits into from
May 22, 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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +26,7 @@ lint: dependencies

release:
@printf "releasing ${VERSION}..."
@printf '<?php\nglobal $$POSTHOG_VERSION;\n$$POSTHOG_VERSION = "%b";\n' ${VERSION} > ./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}
Expand Down
4 changes: 3 additions & 1 deletion bin/posthog
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env php
<?php

require_once(__DIR__ . '/../lib/PostHog.php');
require_once __DIR__ . '/../vendor/autoload.php';

use PostHog\PostHog;

if (in_array('--help', $argv)) {
print(usage());
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
},
"require-dev": {
"phpunit/phpunit": "~8.0",
"overtrue/phplint": "^1.2",
"overtrue/phplint": "^2.3",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"files": [
"lib/PostHog.php"
]
"psr-4": {
"PostHog\\": "lib/"
}
},
"bin": [
"bin/posthog"
Expand Down
29 changes: 13 additions & 16 deletions lib/PostHog/Client.php → lib/Client.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

require_once(__DIR__ . '/Consumer.php');
require_once(__DIR__ . '/QueueConsumer.php');
require_once(__DIR__ . '/Consumer/File.php');
require_once(__DIR__ . '/Consumer/ForkCurl.php');
require_once(__DIR__ . '/Consumer/LibCurl.php');
require_once(__DIR__ . '/Consumer/Socket.php');
require_once(__DIR__ . '/Version.php');
namespace PostHog;

class PostHog_Client {
use PostHog\Consumer\File;
use PostHog\Consumer\ForkCurl;
use PostHog\Consumer\LibCurl;
use PostHog\Consumer\Socket;

class Client {

/**
* Consumer object handles queueing and bundling requests to Posthog.
Expand All @@ -28,10 +27,10 @@ class PostHog_Client {
*/
public function __construct($apiKey, $options = array()) {
$consumers = array(
"socket" => "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
Expand Down Expand Up @@ -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"])) {
Expand Down
5 changes: 4 additions & 1 deletion lib/PostHog/Consumer.php → lib/Consumer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
abstract class PostHog_Consumer {

namespace PostHog;

abstract class Consumer {
protected $type = "Consumer";

protected $options;
Expand Down
8 changes: 6 additions & 2 deletions lib/PostHog/Consumer/File.php → lib/Consumer/File.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

class PostHog_Consumer_File extends PostHog_Consumer {
namespace PostHog\Consumer;

use PostHog\Consumer;

class File extends Consumer {
protected $type = "File";

private $file_handle;
Expand All @@ -20,7 +24,7 @@ public function __construct($apiKey, $options = array()) {
try {
$this->file_handle = fopen($options["filename"], "a");
chmod($options["filename"], 0777);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->handleError($e->getCode(), $e->getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

class PostHog_Consumer_ForkCurl extends PostHog_QueueConsumer {
namespace PostHog\Consumer;

use PostHog\QueueConsumer;

class ForkCurl extends QueueConsumer {
protected $type = "ForkCurl";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

class PostHog_Consumer_LibCurl extends PostHog_QueueConsumer {
namespace PostHog\Consumer;

use PostHog\QueueConsumer;

class LibCurl extends QueueConsumer {
protected $type = "LibCurl";

/**
Expand Down
10 changes: 7 additions & 3 deletions lib/PostHog/Consumer/Socket.php → lib/Consumer/Socket.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

class PostHog_Consumer_Socket extends PostHog_QueueConsumer {
namespace PostHog\Consumer;

use PostHog\QueueConsumer;

class Socket extends QueueConsumer {
protected $type = "Socket";
private $socket_failed;

Expand Down Expand Up @@ -77,7 +81,7 @@ private function createSocket() {
}

return $socket;
} catch (Exception $e) {
} catch (\Exception $e) {
$this->handleError($e->getCode(), $e->getMessage());
$this->socket_failed = true;

Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions lib/PostHog.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

require_once __DIR__ . '/PostHog/Client.php';
namespace PostHog;

use PostHog\Client;

class PostHog {
public const VERSION = '1.0.0';

private static $client;

/**
Expand All @@ -12,7 +16,7 @@ class PostHog {
*/
public static function init($apiKey, $options = array()) {
self::assert($apiKey, "PostHog::init() requires an apiKey");
self::$client = new PostHog_Client($apiKey, $options);
self::$client = new Client($apiKey, $options);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions lib/PostHog/Version.php

This file was deleted.

4 changes: 3 additions & 1 deletion lib/PostHog/QueueConsumer.php → lib/QueueConsumer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

abstract class PostHog_QueueConsumer extends PostHog_Consumer {
namespace PostHog;

abstract class QueueConsumer extends Consumer {
protected $type = "QueueConsumer";

protected $queue;
Expand Down
6 changes: 4 additions & 2 deletions send.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';

/**
* require client
*/

require_once(__DIR__ . "/lib/PostHog.php");
use PostHog\PostHog;

/**
* Args
Expand Down Expand Up @@ -70,7 +72,7 @@
if (!trim($line)) continue;
$payload = json_decode($line, true);
$type = $payload["type"];
$ret = call_user_func_array(array("PostHog", "raw"), array($payload));
$ret = call_user_func_array(array("PostHog\\PostHog", "raw"), array($payload));
if ($ret) $successful++;
$total++;
if ($total % 100 === 0) PostHog::flush();
Expand Down
8 changes: 4 additions & 4 deletions test/ConsumerFileTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once __DIR__ . "/../lib/PostHog/Client.php";
use PostHog\Client;

class ConsumerFileTest extends PHPUnit\Framework\TestCase
{
Expand All @@ -14,7 +14,7 @@ public function setUp(): void
unlink($this->filename());
}

$this->client = new PostHog_Client(
$this->client = new Client(
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg",
array(
"consumer" => "file",
Expand Down Expand Up @@ -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",
)
);

Expand Down
4 changes: 2 additions & 2 deletions test/ConsumerForkCurlTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once __DIR__ . "/../lib/PostHog/Client.php";
use PostHog\Client;

class ConsumerForkCurlTest extends PHPUnit\Framework\TestCase
{
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions test/ConsumerLibCurlTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once __DIR__ . "/../lib/PostHog/Client.php";
use PostHog\Client;

class ConsumerLibCurlTest extends PHPUnit\Framework\TestCase
{
Expand All @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions test/ConsumerSocketTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once __DIR__ . "/../lib/PostHog/Client.php";
use PostHog\Client;

class ConsumerSocketTest extends PHPUnit\Framework\TestCase
{
Expand All @@ -13,7 +13,7 @@ public function setUp(): void

public function testCapture()
{
$client = new PostHog_Client(
$client = new Client(
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg",
array(
"consumer" => "socket",
Expand All @@ -28,7 +28,7 @@ public function testCapture()

public function testIdentify()
{
$client = new PostHog_Client(
$client = new Client(
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg",
array(
"consumer" => "socket",
Expand All @@ -46,7 +46,7 @@ public function testIdentify()

public function testShortTimeout()
{
$client = new PostHog_Client(
$client = new Client(
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg",
array(
"timeout" => 0.01,
Expand All @@ -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 () {
Expand All @@ -92,7 +92,7 @@ public function testLargeMessage()
"consumer" => "socket",
);

$client = new PostHog_Client("BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", $options);
$client = new Client("BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", $options);

$big_property = "";

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/PostHogTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once __DIR__ . "/../lib/PostHog.php";
use PostHog\PostHog;

class PostHogTest extends PHPUnit\Framework\TestCase
{
Expand Down