Skip to content

Commit

Permalink
Fixed for PHP 5.2 and ZTS environment
Browse files Browse the repository at this point in the history
  • Loading branch information
hnw committed Sep 12, 2012
1 parent 7d494f5 commit fb18236
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
10 changes: 10 additions & 0 deletions php_timecop.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ extern zend_module_entry timecop_module_entry;
#include <time.h>
#include "Zend/zend_interfaces.h"

#ifndef Z_ADDREF_P
/* PHP 5.2, old GC */
#define Z_ADDREF_P(pz) (++(pz)->refcount)
#define Z_DELREF_P(pz) (--(pz)->refcount)
#define Z_REFCOUNT_P(pz) ((pz)->refcount)
#define Z_SET_REFCOUNT_P(pz, rc) ((pz)->refcount = rc)
#define Z_REFCOUNT_PP(ppz) Z_REFCOUNT_P(*(ppz))
#define Z_DELREF_PP(ppz) Z_DELREF_P(*(ppz))
#endif

PHP_MINIT_FUNCTION(timecop);
PHP_MSHUTDOWN_FUNCTION(timecop);
PHP_RINIT_FUNCTION(timecop);
Expand Down
7 changes: 5 additions & 2 deletions tests/006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Check for timecop.sync_request_time=1 and $_SERVER['REQUEST_TIME']
--SKIPIF--
<?php
extension_loaded('timecop') or die('skip timecop not available');
$required_func = array("timecop_freeze", "timecop_return");
$required_func = array("timecop_freeze", "timecop_travel", "timecop_return");
foreach ($required_func as $func_name) {
if (!function_exists($func_name)) {
die("skip $func_name() function is not available.");
Expand All @@ -14,10 +14,13 @@ Check for timecop.sync_request_time=1 and $_SERVER['REQUEST_TIME']
--FILE--
<?php
$orig_request_time = $_SERVER['REQUEST_TIME'];
timecop_freeze(12345);
timecop_freeze(1234);
var_dump($_SERVER['REQUEST_TIME']);
timecop_travel(12345);
var_dump($_SERVER['REQUEST_TIME']);
timecop_return();
var_dump($orig_request_time === $_SERVER['REQUEST_TIME']);
--EXPECT--
int(1234)
int(12345)
bool(true)
7 changes: 5 additions & 2 deletions tests/007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Check for timecop.sync_request_time=0 and $_SERVER['REQUEST_TIME']
--SKIPIF--
<?php
extension_loaded('timecop') or die('skip timecop not available');
$required_func = array("timecop_freeze", "timecop_return");
$required_func = array("timecop_freeze", "timecop_travel", "timecop_return");
foreach ($required_func as $func_name) {
if (!function_exists($func_name)) {
die("skip $func_name() function is not available.");
Expand All @@ -15,10 +15,13 @@ timecop.sync_request_time=0
--FILE--
<?php
$orig_request_time = $_SERVER['REQUEST_TIME'];
timecop_freeze(12345);
timecop_freeze(1234);
var_dump($orig_request_time === $_SERVER['REQUEST_TIME']);
timecop_travel(12345);
var_dump($orig_request_time === $_SERVER['REQUEST_TIME']);
timecop_return();
var_dump($orig_request_time === $_SERVER['REQUEST_TIME']);
--EXPECT--
bool(true)
bool(true)
bool(true)
10 changes: 5 additions & 5 deletions timecop.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ static int update_request_time(long unixtime TSRMLS_DC)
if (TIMECOP_G(orig_request_time) == NULL) {
TIMECOP_G(orig_request_time) = *request_time;
} else {
Z_DELREF(**request_time);
Z_DELREF_PP(request_time);
}
MAKE_STD_ZVAL(*request_time);
ZVAL_LONG(*request_time, unixtime);
Expand All @@ -479,7 +479,7 @@ static int restore_request_time(TSRMLS_D)
zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &server_vars) == SUCCESS &&
Z_TYPE_PP(server_vars) == IS_ARRAY &&
zend_hash_find(Z_ARRVAL_PP(server_vars), "REQUEST_TIME", sizeof("REQUEST_TIME"), (void **) &request_time) == SUCCESS) {
Z_DELREF(**request_time);
Z_DELREF_PP(request_time);
*request_time = TIMECOP_G(orig_request_time);
TIMECOP_G(orig_request_time) = NULL;
}
Expand Down Expand Up @@ -590,7 +590,7 @@ PHP_FUNCTION(timecop_freeze)
TIMECOP_G(freezed_timestamp) = timestamp;

if (TIMECOP_G(sync_request_time)){
update_request_time(timestamp);
update_request_time(timestamp TSRMLS_CC);
}

RETURN_TRUE;
Expand All @@ -609,7 +609,7 @@ PHP_FUNCTION(timecop_travel)
TIMECOP_G(travel_offset) = timestamp - time(NULL);

if (TIMECOP_G(sync_request_time)){
update_request_time(timestamp);
update_request_time(timestamp TSRMLS_CC);
}

RETURN_TRUE;
Expand All @@ -623,7 +623,7 @@ PHP_FUNCTION(timecop_return)
TIMECOP_G(timecop_mode) = TIMECOP_MODE_NORMAL;

if (TIMECOP_G(sync_request_time)){
restore_request_time();
restore_request_time(TSRMLS_C);
}

RETURN_TRUE;
Expand Down

0 comments on commit fb18236

Please sign in to comment.