Skip to content

Commit

Permalink
Addendum #284 - warning time is not always present
Browse files Browse the repository at this point in the history
For stable composer versions, the `COMPOSER_DEV_WARNING_TIME` is not
defined, we therefore fall back on the RELEASE_DATE constant, which is
always there, this should be "good enough".
  • Loading branch information
discordier committed Apr 27, 2016
1 parent 9771642 commit a2754e3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/system/modules/!composer/src/Runtime.php
Expand Up @@ -297,13 +297,30 @@ public static function increaseMemoryLimit()
public static function readComposerDevWarningTime()
{
$configPathname = new \File(COMPOSER_DIR_RELATIVE . '/composer.phar');
return static::readComposerDevWarningTimeFromStream($configPathname->handle);
}

/**
* Read the stub from the composer.phar and return the warning timestamp.
*
* @param resource $stream The stream to read from.
*
* @return bool|int
*/
public static function readComposerDevWarningTimeFromStream($stream)
{
$buffer = '';
do {
$buffer .= fread($configPathname->handle, 1024);
$buffer .= fread($stream, 1024);
} while (!preg_match('#define\(\'COMPOSER_DEV_WARNING_TIME\',\s*(\d+)\);#', $buffer, $matches)
&& !feof($configPathname->handle)
&& !preg_match('#\s*RELEASE_DATE = \'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\';#', $buffer, $matches)
&& !feof($stream)
);
if ($matches[1]) {
if (isset($matches[1])) {
if (!is_numeric($matches[1])) {
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $matches[1]);
return $date->getTimestamp();
}
return (int) $matches[1];
}
return false;
Expand Down
35 changes: 35 additions & 0 deletions tests/RuntimeTest.php
@@ -0,0 +1,35 @@
<?php

namespace ContaoCommunityAlliance\Contao\Composer\Test;

use ContaoCommunityAlliance\Contao\Composer\Runtime;

class RuntimeTest extends \PHPUnit_Framework_TestCase
{
public function providerReadComposerDevWarningTime()
{
return array(
array(false, ''),
array(1234567890, 'define(\'COMPOSER_DEV_WARNING_TIME\', 1234567890);'),
array(1234567890, 'const RELEASE_DATE = \'' . date('Y-m-d H:i:s', 1234567890) . '\';'),
);
}

/**
* Test the readComposerDevWarningTime
*
* @return boolean
*
* @dataProvider providerReadComposerDevWarningTime
*/
public function testReadComposerDevWarningTime($expected, $testArg)
{
$tempFile = tmpfile();
fwrite($tempFile, str_repeat(PHP_EOL, 10) . $testArg . str_repeat(PHP_EOL, 10));
fseek($tempFile, 0);
$actual = Runtime::readComposerDevWarningTimeFromStream($tempFile);
fclose($tempFile);

$this->assertEquals($expected, $actual);
}
}

0 comments on commit a2754e3

Please sign in to comment.