Skip to content

Commit

Permalink
Merge pull request #9 from michal-kocarek/tried-older-php-versions
Browse files Browse the repository at this point in the history
Extend compatibility with PHP 5.4.
  • Loading branch information
Michal Kočárek committed Mar 28, 2016
2 parents 678d689 + f0d4d40 commit aba0d78
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 21 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Here is a minimal example of a `composer.json` file that just defines a dependen

{
"require": {
"michal-kocarek/teamcity-messages": "^1.1"
"michal-kocarek/teamcity-messages": "^1.2"
}
}

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "michal-kocarek/teamcity-messages",
"description": "Write TeamCity service messages from PHP.",
"version": "1.1.0",
"version": "1.2.0",
"keywords": [
"teamcity"
],
Expand All @@ -14,7 +14,7 @@
}
],
"require": {
"php": ">=5.5"
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "^4.8.9",
Expand Down
54 changes: 43 additions & 11 deletions src/MessageLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MichalKocarek\TeamcityMessages;

use Exception;
use InvalidArgumentException;
use MichalKocarek\TeamcityMessages\Writers\Writer;

Expand Down Expand Up @@ -179,14 +180,18 @@ public function blockClosed($name)
* @param string $description The block description. (Since TeamCity 9.1.5.)
* @param callable $callback Callback that is called inside block. First argument passed is this instance.
* @return mixed The callback return value.
* @throws Exception Exception raised inside callback may be thrown.
*/
public function block($name, $description = '', callable $callback)
{
$this->blockOpened($name, $description);
try {
return $callback($this);
} finally {
$result = $callback($this);
$this->blockClosed($name);
return $result;
} catch(Exception $ex) {
$this->blockClosed($name);
throw $ex;
}
}

Expand Down Expand Up @@ -229,16 +234,20 @@ public function compilationFinished($compilerName)
* @param string $compilerName Arbitrary name of compiler performing an operation.
* @param callable $callback Callback that is called inside block. First argument passed is this instance.
* @return mixed The callback return value.
* @throws Exception Exception raised inside callback may be thrown.
*
* @see https://confluence.jetbrains.com/display/TCD9/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-reportingCompilationBlocksReportingCompilationMessages
*/
public function compilation($compilerName, callable $callback)
{
$this->compilationStarted($compilerName);
try {
return $callback($this);
} finally {
$result = $callback($this);
$this->compilationFinished($compilerName);
return $result;
} catch(Exception $ex) {
$this->compilationFinished($compilerName);
throw $ex;
}
}

Expand Down Expand Up @@ -483,14 +492,18 @@ public function progressFinish($message)
* @param string $message The message.
* @param callable $callback Callback that is called inside block. First argument passed is this instance.
* @return mixed The callback return value.
* @throws Exception Exception raised inside callback may be thrown.
*/
public function progress($message, callable $callback)
{
$this->progressStart($message);
try {
return $callback($this);
} finally {
$result = $callback($this);
$this->progressFinish($message);
return $result;
} catch(Exception $ex) {
$this->progressFinish($message);
throw $ex;
}
}

Expand Down Expand Up @@ -633,14 +646,18 @@ public function enableServiceMessages()
*
* @param callable $callback Callback that is called inside block. First argument passed is this instance.
* @return mixed The callback return value.
* @throws Exception Exception raised inside callback may be thrown.
*/
public function withoutServiceMessages(callable $callback)
{
$this->disableServiceMessages();
try {
return $callback($this);
} finally {
$result = $callback($this);
$this->enableServiceMessages();
return $result;
} catch(Exception $ex) {
$this->enableServiceMessages();
throw $ex;
}
}

Expand All @@ -663,9 +680,9 @@ public function importData($type, $path, $parseOutOfDate = null, $whenNoDataPubl
$this->write('importData', [
'type' => $type,
'path' => $path,
'parseOutOfDate' => $parseOutOfDate === null ? null : ($parseOutOfDate ? 'true' : 'false'),
'whenNoDataPublished' => $whenNoDataPublished === null ? null : ($whenNoDataPublished ? 'true' : 'false'),
'verbose' => $verbose === null ? null : ($verbose ? 'true' : 'false'),
'parseOutOfDate' => $this->castToOptionalBoolean($parseOutOfDate),
'whenNoDataPublished' => $this->castToOptionalBoolean($whenNoDataPublished),
'verbose' => $this->castToOptionalBoolean($verbose),
]);
}

Expand Down Expand Up @@ -705,4 +722,19 @@ private function write($messageName, array $parameters)
$this->writer->write(Util::format($messageName, $parameters));
}

/**
* @param $param
* @return null|string
*/
private function castToOptionalBoolean($param)
{
if ($param === null) {
return null;
}

return $param
? 'true'
: 'false';
}

}
9 changes: 6 additions & 3 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MichalKocarek\TeamcityMessages;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use InvalidArgumentException;
Expand Down Expand Up @@ -70,7 +71,7 @@ public static function ensureValidJavaId($value)
* @return string
* @see https://confluence.jetbrains.com/display/TCD9/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-MessageCreationTimestamp
*/
public static function formatTimestamp(DateTimeInterface $date = null)
public static function formatTimestamp($date = null)
{
if (!$date) {
$date = self::nowMicro();
Expand Down Expand Up @@ -115,7 +116,7 @@ private static function escape($value)
/**
* Get current time with microseconds included.
*
* @return DateTimeImmutable
* @return DateTimeInterface
*/
public static function nowMicro()
{
Expand All @@ -128,7 +129,9 @@ public static function nowMicro()

// order of format matters; timestamp sets timezone and set microseconds to zero;
// that's why microseconds must be set after parsing the timestamp
return DateTimeImmutable::createFromFormat('U u', "$timestamp $microseconds");
return PHP_VERSION_ID >= 50500
? DateTimeImmutable::createFromFormat('U u', "$timestamp $microseconds")
: DateTime::createFromFormat('U u', "$timestamp $microseconds");
}

}
8 changes: 4 additions & 4 deletions tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace MichalKocarek\TeamcityMessages\Tests;

use MichalKocarek\TeamcityMessages\Util;
use DateTimeImmutable;
use DateTime;
use InvalidArgumentException;
use PHPUnit_Framework_TestCase;

Expand Down Expand Up @@ -82,15 +82,15 @@ public function testNowMicro()

public function testFormatTimestamp()
{
$now = new DateTimeImmutable('2000-01-01 12:34:56.12345 Europe/Prague');
$now = new DateTime('2000-01-01 12:34:56.12345 Europe/Prague');
self::assertSame('2000-01-01T12:34:56.123450+0100', Util::formatTimestamp($now));
}

public function testFormatTimestampNow()
{
$now = new DateTimeImmutable();
$now = new DateTime();
$result = Util::formatTimestamp();

self::assertEquals($now, new DateTimeImmutable($result), '', 1.0);
self::assertEquals($now, new DateTime($result), '', 1.0);
}
}

0 comments on commit aba0d78

Please sign in to comment.