Skip to content

Commit

Permalink
Finish psr-4 refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienbrault committed Mar 31, 2021
1 parent 168c5b9 commit f709966
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 45 deletions.
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
},
"require-dev": {
"phpunit/phpunit": "~8.0",
"overtrue/phplint": "^1.2",
"overtrue/phplint": "^2.3",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
"Posthog": "lib"
"PostHog\\": "lib/"
}
},
"bin": [
Expand Down
24 changes: 11 additions & 13 deletions lib/PostHog/Client.php → lib/Client.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Posthog;
namespace PostHog;

use Posthog\Consumer\File;
use Posthog\Consumer\ForkCurl;
use Posthog\Consumer\LibCurl;
use Posthog\Consumer\Socket;
use PostHog\Consumer\File;
use PostHog\Consumer\ForkCurl;
use PostHog\Consumer\LibCurl;
use PostHog\Consumer\Socket;

class Client {

Expand All @@ -26,10 +26,10 @@ class Client {
*/
public function __construct($apiKey, $options = array()) {
$consumers = array(
"socket" => "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 @@ -172,18 +172,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
2 changes: 1 addition & 1 deletion lib/PostHog/Consumer.php → lib/Consumer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Posthog;
namespace PostHog;

abstract class Consumer {
protected $type = "Consumer";
Expand Down
8 changes: 4 additions & 4 deletions lib/PostHog/Consumer/File.php → lib/Consumer/File.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Posthog\Consumer;
namespace PostHog\Consumer;

use Posthog\Consumer;
use PostHog\Consumer;

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

private $file_handle;
Expand All @@ -24,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,10 +1,10 @@
<?php

namespace Posthog\Consumer;
namespace PostHog\Consumer;

use Posthog\QueueConsumer;
use PostHog\QueueConsumer;

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

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

namespace Posthog\Consumer;
namespace PostHog\Consumer;

use Posthog\QueueConsumer;
use PostHog\QueueConsumer;

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

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

namespace Posthog\Consumer;
namespace PostHog\Consumer;

use Posthog\QueueConsumer;
use PostHog\QueueConsumer;

class Socket extends QueueConsumer {
protected $type = "Socket";
Expand Down Expand Up @@ -81,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 @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/PostHog.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

use Posthog\Client;
namespace PostHog;

use PostHog\Client;

class PostHog {
public const VERSION = '1.0.0';

private static $client;

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

This file was deleted.

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

namespace Posthog;
namespace PostHog;

abstract class QueueConsumer extends Consumer {
protected $type = "QueueConsumer";
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
4 changes: 2 additions & 2 deletions test/ConsumerFileTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Posthog\Client;
use PostHog\Client;

class ConsumerFileTest extends PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -82,7 +82,7 @@ public function testProductionProblems()
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg",
array(
"consumer" => "file",
"filename" => "/dev/xxxxxxx",
"filename" => "/dev/x/xxxxxxx",
)
);

Expand Down
2 changes: 1 addition & 1 deletion 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 Down
2 changes: 1 addition & 1 deletion 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 Down
2 changes: 1 addition & 1 deletion 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 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

use Posthog;
use PostHog\PostHog;

class PostHogTest extends PHPUnit\Framework\TestCase
{
Expand Down

0 comments on commit f709966

Please sign in to comment.