From df50ed1be08dd5f807bf380e3e53b7b7df604ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Ko=C4=8D=C3=A1rek?= Date: Mon, 28 Mar 2016 14:21:39 +0200 Subject: [PATCH 1/3] Replace finally statement to allow execution in older PHP versions as well. --- src/MessageLogger.php | 54 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/MessageLogger.php b/src/MessageLogger.php index ce6678d..664bc31 100644 --- a/src/MessageLogger.php +++ b/src/MessageLogger.php @@ -2,6 +2,7 @@ namespace MichalKocarek\TeamcityMessages; +use Exception; use InvalidArgumentException; use MichalKocarek\TeamcityMessages\Writers\Writer; @@ -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; } } @@ -229,6 +234,7 @@ 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 */ @@ -236,9 +242,12 @@ 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; } } @@ -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; } } @@ -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; } } @@ -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), ]); } @@ -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'; + } + } From 368efc3351ffbfb750383976bec35a5c1315f1ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Ko=C4=8D=C3=A1rek?= Date: Mon, 28 Mar 2016 14:22:17 +0200 Subject: [PATCH 2/3] Test library under PHP 5.4 as well. --- .travis.yml | 1 + composer.json | 2 +- src/Util.php | 9 ++++++--- tests/UtilTest.php | 8 ++++---- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 57c488e..45f69e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: php php: + - 5.4 - 5.5 - 5.6 - 7.0 diff --git a/composer.json b/composer.json index 18eaa37..106b57c 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=5.5" + "php": ">=5.4" }, "require-dev": { "phpunit/phpunit": "^4.8.9", diff --git a/src/Util.php b/src/Util.php index d4c9857..fcf22b6 100644 --- a/src/Util.php +++ b/src/Util.php @@ -2,6 +2,7 @@ namespace MichalKocarek\TeamcityMessages; +use DateTime; use DateTimeImmutable; use DateTimeInterface; use InvalidArgumentException; @@ -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(); @@ -115,7 +116,7 @@ private static function escape($value) /** * Get current time with microseconds included. * - * @return DateTimeImmutable + * @return DateTimeInterface */ public static function nowMicro() { @@ -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"); } } diff --git a/tests/UtilTest.php b/tests/UtilTest.php index 34a7157..764df09 100644 --- a/tests/UtilTest.php +++ b/tests/UtilTest.php @@ -3,7 +3,7 @@ namespace MichalKocarek\TeamcityMessages\Tests; use MichalKocarek\TeamcityMessages\Util; -use DateTimeImmutable; +use DateTime; use InvalidArgumentException; use PHPUnit_Framework_TestCase; @@ -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); } } From f0d4d401f7bb14bbf768d2bcd003b4c9fa8f12b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Ko=C4=8D=C3=A1rek?= Date: Mon, 28 Mar 2016 14:34:24 +0200 Subject: [PATCH 3/3] Bump version to 1.2.0. --- README.md | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e116d9..6afa8af 100644 --- a/README.md +++ b/README.md @@ -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" } } diff --git a/composer.json b/composer.json index 106b57c..438b298 100644 --- a/composer.json +++ b/composer.json @@ -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" ],