Skip to content

Commit

Permalink
Merge pull request #182 from North-Two-Five/Issue-#104-Namespaced
Browse files Browse the repository at this point in the history
Namespace and version updates
  • Loading branch information
pooyaj authored Jul 16, 2021
2 parents 77d4698 + 5d4ca62 commit a1212a1
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 53 deletions.
10 changes: 10 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

2.0.0 / 2021-07-14
==================

* Modify Endpoint to match API docs (#171)
* usleep in flush() causes unexpected delays on page loads (#173)
* Support PHP 8 (#152)
* Remove Support for PHP 7.2
* Namespacing (#182)


1.8.0 / 2021-05-31
==================

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "segmentio/analytics-php",
"version": "1.8.0",
"version": "2.0.0",
"description": "Segment Analytics PHP Library",
"keywords": [
"analytics",
Expand All @@ -17,7 +17,7 @@
}
],
"require": {
"php": ">=7.2"
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "~9.0",
Expand Down
10 changes: 6 additions & 4 deletions lib/Segment.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
namespace Segment;
use Segment\Consumer\Client as Client;

require_once __DIR__ . '/Segment/Client.php';

Expand All @@ -12,7 +14,7 @@ class Segment {
*/
public static function init($secret, $options = array()) {
self::assert($secret, "Segment::init() requires secret");
self::$client = new Segment_Client($secret, $options);
self::$client = new Client($secret, $options);
}

/**
Expand Down Expand Up @@ -132,7 +134,7 @@ private static function checkClient(){
return;
}

throw new Exception("Segment::init() must be called before any other tracking method.");
throw new \Exception("Segment::init() must be called before any other tracking method.");
}

/**
Expand All @@ -144,11 +146,11 @@ private static function checkClient(){
*/
private static function assert($value, $msg) {
if (!$value) {
throw new Exception($msg);
throw new \Exception($msg);
}
}
}

if (!function_exists('json_encode')) {
throw new Exception('Segment needs the JSON PHP extension.');
throw new \Exception('Segment needs the JSON PHP extension.');
}
18 changes: 10 additions & 8 deletions lib/Segment/Client.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Segment\Consumer;

require_once(__DIR__ . '/Consumer.php');
require_once(__DIR__ . '/QueueConsumer.php');
require_once(__DIR__ . '/Consumer/File.php');
Expand All @@ -8,7 +10,7 @@
require_once(__DIR__ . '/Consumer/Socket.php');
require_once(__DIR__ . '/Version.php');

class Segment_Client {
class Client {
protected $consumer;

/**
Expand All @@ -23,18 +25,18 @@ class Segment_Client {
public function __construct($secret, $options = array()) {

$consumers = array(
"socket" => "Segment_Consumer_Socket",
"file" => "Segment_Consumer_File",
"fork_curl" => "Segment_Consumer_ForkCurl",
"lib_curl" => "Segment_Consumer_LibCurl"
"socket" => "Segment\Consumer\Socket",
"file" => "Segment\Consumer\File",
"fork_curl" => "Segment\Consumer\ForkCurl",
"lib_curl" => "Segment\Consumer\LibCurl"
);
// Use our socket libcurl by default
$consumer_type = isset($options["consumer"]) ? $options["consumer"] :
"lib_curl";

if (!array_key_exists($consumer_type, $consumers) && class_exists($consumer_type)) {
if (!is_subclass_of($consumer_type, Segment_Consumer::class)) {
throw new Exception('Consumers must extend the Segment_Consumer abstract class');
if (!is_subclass_of($consumer_type, Consumer::class)) {
throw new \Exception('Consumers must extend the Segment/Consumer/Consumer abstract class');
}
// Try to resolve it by class name
$this->consumer = new $consumer_type($secret, $options);
Expand Down Expand Up @@ -141,7 +143,7 @@ public function flush() {
}

/**
* @return Segment_Consumer
* @return Segment\Consumer\Consumer
*/
public function getConsumer() {
return $this->consumer;
Expand Down
5 changes: 4 additions & 1 deletion lib/Segment/Consumer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
abstract class Segment_Consumer {

namespace Segment\Consumer;

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

protected $options;
Expand Down
5 changes: 3 additions & 2 deletions lib/Segment/Consumer/File.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Segment\Consumer;

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

private $file_handle;
Expand All @@ -25,7 +26,7 @@ public function __construct($secret, $options = array()) {
} else {
chmod($options["filename"], 0777);
}
} catch (Exception $e) {
} catch (\Exception $e) {
$this->handleError($e->getCode(), $e->getMessage());
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Segment/Consumer/ForkCurl.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Segment\Consumer;

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

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Segment/Consumer/LibCurl.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Segment\Consumer;

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

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Segment/Consumer/Socket.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Segment\Consumer;

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

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

abstract class Segment_QueueConsumer extends Segment_Consumer {
namespace Segment\Consumer;

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

protected $queue;
Expand Down
2 changes: 1 addition & 1 deletion lib/Segment/Version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
global $SEGMENT_VERSION;
$SEGMENT_VERSION = "1.8.0";
$SEGMENT_VERSION = "2.0.0";
6 changes: 3 additions & 3 deletions send.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Segment;
/**
* require client
*/
Expand Down Expand Up @@ -70,7 +70,7 @@
if (!trim($line)) continue;
$total++;
$payload = json_decode($line, true);
$dt = new DateTime($payload["timestamp"]);
$dt = new \DateTime($payload["timestamp"]);
$ts = floatval($dt->getTimestamp() . "." . $dt->format("u"));
$payload["timestamp"] = date("c", (int) $ts);
$type = $payload["type"];
Expand All @@ -86,7 +86,7 @@
$currentBatch = array();
}
$payload["timestamp"] = $ts;
call_user_func_array(array("Segment", $type), array($payload));
call_user_func_array(array("Segment\Segment", $type), array($payload));
}

$libCurlResponse = Segment::flush();
Expand Down
3 changes: 2 additions & 1 deletion test/AnalyticsTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
namespace Segment;

require_once __DIR__ . "/../lib/Segment.php";

Expand Down Expand Up @@ -46,7 +47,7 @@ public function testGroupAnonymous()
*/
public function testGroupNoUser(): void
{
$this->expectException(Exception::class);
$this->expectException( \Exception::class);
Segment::group(array(
"groupId" => "group-id",
"traits" => array(
Expand Down
15 changes: 8 additions & 7 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
namespace Segment\Consumer;

require_once __DIR__ . '/../lib/Segment/Client.php';

Expand All @@ -7,25 +8,25 @@ class ClientTest extends \PHPUnit\Framework\TestCase
/** @test */
public function it_uses_the_lib_curl_consumer_as_default()
{
$client = new Segment_Client('foobar', []);
$this->assertInstanceOf(Segment_Consumer_LibCurl::class, $client->getConsumer());
$client = new Client('foobar', []);
$this->assertInstanceOf(LibCurl::class, $client->getConsumer());
}

/** @test */
public function can_provide_the_consumer_configuration_as_string()
{
$client = new Segment_Client('foobar', [
$client = new Client('foobar', [
'consumer' => 'fork_curl',
]);
$this->assertInstanceOf(Segment_Consumer_ForkCurl::class, $client->getConsumer());
$this->assertInstanceOf(ForkCurl::class, $client->getConsumer());
}

/** @test */
public function can_provide_a_class_namespace_as_consumer_configuration()
{
$client = new Segment_Client('foobar', [
'consumer' => Segment_Consumer_ForkCurl::class,
$client = new Client('foobar', [
'consumer' => ForkCurl::class,
]);
$this->assertInstanceOf(Segment_Consumer_ForkCurl::class, $client->getConsumer());
$this->assertInstanceOf(ForkCurl::class, $client->getConsumer());
}
}
9 changes: 5 additions & 4 deletions test/ConsumerFileTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
namespace Segment\Consumer;

require_once __DIR__ . "/../lib/Segment/Client.php";

Expand All @@ -14,7 +15,7 @@ public function setUp(): void
unlink($this->filename());
}

$this->client = new Segment_Client(
$this->client = new Client(
"oq0vdlg7yi",
array(
"consumer" => "file",
Expand Down Expand Up @@ -111,7 +112,7 @@ public function testSend()
public function testProductionProblems()
{
// Open to a place where we should not have write access.
$client = new Segment_Client(
$client = new Client(
"oq0vdlg7yi",
array(
"consumer" => "file",
Expand All @@ -124,7 +125,7 @@ public function testProductionProblems()
}

public function testFileSecurityCustom() {
$client = new Segment_Client(
$client = new Client(
"testsecret",
array(
"consumer" => "file",
Expand All @@ -137,7 +138,7 @@ public function testFileSecurityCustom() {
}

public function testFileSecurityDefaults() {
$client = new Segment_Client(
$client = new Client(
"testsecret",
array(
"consumer" => "file",
Expand Down
5 changes: 3 additions & 2 deletions test/ConsumerForkCurlTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
namespace Segment\Consumer;

require_once __DIR__ . "/../lib/Segment/Client.php";

Expand All @@ -9,7 +10,7 @@ class ConsumerForkCurlTest extends \PHPUnit\Framework\TestCase
public function setUp(): void
{
date_default_timezone_set("UTC");
$this->client = new Segment_Client(
$this->client = new Client(
"OnMMoZ6YVozrgSBeZ9FpkC0ixH0ycYZn",
array(
"consumer" => "fork_curl",
Expand Down Expand Up @@ -87,7 +88,7 @@ public function testRequestCompression() {
);

// Create client and send Track message
$client = new Segment_Client("OnMMoZ6YVozrgSBeZ9FpkC0ixH0ycYZn", $options);
$client = new Client("OnMMoZ6YVozrgSBeZ9FpkC0ixH0ycYZn", $options);
$result = $client->track(array(
"userId" => "some-user",
"event" => "PHP Fork Curl'd\" Event with compression",
Expand Down
7 changes: 4 additions & 3 deletions test/ConsumerLibCurlTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
namespace Segment\Consumer;

require_once __DIR__ . "/../lib/Segment/Client.php";

Expand All @@ -9,7 +10,7 @@ class ConsumerLibCurlTest extends \PHPUnit\Framework\TestCase
public function setUp(): void
{
date_default_timezone_set("UTC");
$this->client = new Segment_Client(
$this->client = new Client(
"oq0vdlg7yi",
array(
"consumer" => "lib_curl",
Expand Down Expand Up @@ -88,7 +89,7 @@ public function testRequestCompression() {
},
);

$client = new Segment_Client("x", $options);
$client = new Client("x", $options);

# Should error out with debug on.
$this->assertTrue($client->track(array("user_id" => "some-user", "event" => "Socket PHP Event")));
Expand All @@ -102,7 +103,7 @@ public function testLargeMessageSizeError()
"consumer" => "lib_curl",
);

$client = new Segment_Client("testlargesize", $options);
$client = new Client("testlargesize", $options);

$big_property = "";

Expand Down
Loading

0 comments on commit a1212a1

Please sign in to comment.