Skip to content

Commit

Permalink
Properly test lifecycle injection
Browse files Browse the repository at this point in the history
  • Loading branch information
danhunsaker authored and sergeyklay committed May 12, 2018
1 parent bd59ef3 commit 10331f5
Show file tree
Hide file tree
Showing 7 changed files with 544 additions and 4 deletions.
10 changes: 7 additions & 3 deletions Library/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1715,8 +1715,12 @@ public function processCodeInjection(array $entries, $section = 'request')

if (isset($entries[$section])) {
foreach ($entries[$section] as $entry) {
$codes[] = $entry['code'] . ';';
$includes[] = "#include \"" . $entry['include'] . "\"";
if (isset($entry['code']) && !empty($entry['code'])) {
$codes[] = $entry['code'] . ';';
}
if (isset($entry['include']) && !empty($entry['include'])) {
$includes[] = "#include \"" . $entry['include'] . "\"";
}
}
}

Expand Down Expand Up @@ -1964,7 +1968,7 @@ public function createProjectFiles($project)
array_merge($completeInterfaceInits, $completeClassInits)
),
'%INIT_GLOBALS%' => implode(
PHP_EOL,
PHP_EOL . "\t",
array_merge((array)$globalsDefault[0], array($glbInitializers))
),
'%INIT_MODULE_GLOBALS%' => $globalsDefault[1],
Expand Down
2 changes: 1 addition & 1 deletion unit-tests/Zephir/Test/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setUp()
*/
public function testConstructWithBadConfigFile()
{
chdir(__DIR__ . DIRECTORY_SEPARATOR . '_files');
chdir(__DIR__ . DIRECTORY_SEPARATOR . '_files/badconfig');
$config = new Config();
}

Expand Down
84 changes: 84 additions & 0 deletions unit-tests/Zephir/Test/LifeCycleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
+--------------------------------------------------------------------------+
| Zephir |
| Copyright (c) 2013-present Zephir Team (https://zephir-lang.com/) |
| |
| This source file is subject the MIT license, that is bundled with this |
| package in the file LICENSE, and is available through the world-wide-web |
| at the following url: http://zephir-lang.com/license.html |
+--------------------------------------------------------------------------+
*/

namespace Zephir\Test;

use Zephir\Config;
use Zephir\Compiler;
use Zephir\Logger;
use Zephir\Parser;
use Zephir\Parser\Manager;
use Zephir\Backends\ZendEngine2\Backend as Zend2;
use Zephir\Backends\ZendEngine3\Backend as Zend3;

class LifeCycleTest extends \PHPUnit_Framework_TestCase
{
/**
* Common directory.
*
* @var string
*/
private $pwd;

public function setUp()
{
/* Store the current directory before to be change */
$this->pwd = getcwd();
}

public function testLifeCycleInjectionZend2()
{
chdir(__DIR__ . DIRECTORY_SEPARATOR . '_files/lifecycle');
$config = new Config();
$logger = new Logger($config);
$backend = new Zend2($config);
$parser = new Parser();
$manager = new Manager($parser, $logger);
$compiler = new Compiler($config, $logger, $backend, $manager);
$compiler->createProjectFiles('lifecycle');
$this->assertSame(
implode(PHP_EOL, file('ext/lifecycle.c', FILE_IGNORE_NEW_LINES)),
implode(PHP_EOL, file('expected2.c', FILE_IGNORE_NEW_LINES))
);
}

public function testLifeCycleInjectionZend3()
{
chdir(__DIR__ . DIRECTORY_SEPARATOR . '_files/lifecycle');
$config = new Config();
$logger = new Logger($config);
$backend = new Zend3($config);
$parser = new Parser();
$manager = new Manager($parser, $logger);
$compiler = new Compiler($config, $logger, $backend, $manager);
$compiler->createProjectFiles('lifecycle');
$this->assertSame(
implode(PHP_EOL, file('ext/lifecycle.c', FILE_IGNORE_NEW_LINES)),
implode(PHP_EOL, file('expected3.c', FILE_IGNORE_NEW_LINES))
);
}

/**
* Restore current directory, and clean config.json.
*/
public function tearDown()
{
if (file_exists('ext')) {
exec('rm -rf ext/');
}

if (getcwd() != $this->pwd) {
chdir($this->pwd);
}
}
}
File renamed without changes.
63 changes: 63 additions & 0 deletions unit-tests/Zephir/Test/_files/lifecycle/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"namespace": "lifecycle",
"name": "Life Cycle Test",
"description": "Description: Life Cycle Test 'Extension'",
"author": "Zephir Team",
"version": "1.0.0",
"verbose": false,
"silent": true,
"initializers": {
"globals": [
{
"include": "setup_funcs.h",
"code": "ext_setup_globals()"
}
],
"module": [
{
"include": "setup_funcs.h",
"code": "ext_setup_module()"
},
{
"include": "prep_funcs.h",
"code": "ext_prep_module()"
}
],
"request": [
{
"include": "setup_funcs.h",
"code": "ext_setup_request()"
}
]
},
"destructors": {
"request": [
{
"include": "takedown_funcs.h",
"code": "ext_takedown_request()"
}
],
"post-request": [
{
"include": "takedown_funcs.h",
"code": "ext_takedown_request()"
}
],
"module": [
{
"include": "takedown_funcs.h",
"code": "ext_takedown_module()"
},
{
"include": "",
"code": "ext_kill_module()"
}
],
"globals": [
{
"include": "takedown_funcs.h",
"code": "ext_takedown_globals()"
}
]
}
}
206 changes: 206 additions & 0 deletions unit-tests/Zephir/Test/_files/lifecycle/expected2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@

/* This file was generated automatically by Zephir do not modify it! */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <php.h>

// TODO: Deprecated. Will be removed in future
#if PHP_VERSION_ID < 50500
#include <locale.h>
#endif

#include "php_ext.h"
#include "lifecycle.h"

#include <ext/standard/info.h>

#include <Zend/zend_operators.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>

#include "kernel/globals.h"
#include "kernel/main.h"
#include "kernel/fcall.h"
#include "kernel/memory.h"


#include "takedown_funcs.h"
#include "setup_funcs.h"
#include "prep_funcs.h"



ZEND_DECLARE_MODULE_GLOBALS(lifecycle)

PHP_INI_BEGIN()

PHP_INI_END()

static PHP_MINIT_FUNCTION(lifecycle)
{
// TODO: Deprecated. Will be removed in future
#if PHP_VERSION_ID < 50500
char* old_lc_all = setlocale(LC_ALL, NULL);
if (old_lc_all) {
size_t len = strlen(old_lc_all);
char *tmp = calloc(len+1, 1);
if (UNEXPECTED(!tmp)) {
return FAILURE;
}

memcpy(tmp, old_lc_all, len);
old_lc_all = tmp;
}

setlocale(LC_ALL, "C");
#endif
REGISTER_INI_ENTRIES();

ext_setup_module();
ext_prep_module();

// TODO: Deprecated. Will be removed in future
#if PHP_VERSION_ID < 50500
setlocale(LC_ALL, old_lc_all);
free(old_lc_all);
#endif
return SUCCESS;
}

#ifndef ZEPHIR_RELEASE
static PHP_MSHUTDOWN_FUNCTION(lifecycle)
{
ext_takedown_module();
ext_kill_module();
zephir_deinitialize_memory(TSRMLS_C);
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
#endif

/**
* Initialize globals on each request or each thread started
*/
static void php_zephir_init_globals(zend_lifecycle_globals *lifecycle_globals TSRMLS_DC)
{
lifecycle_globals->initialized = 0;

/* Memory options */
lifecycle_globals->active_memory = NULL;

/* Virtual Symbol Tables */
lifecycle_globals->active_symbol_table = NULL;

/* Cache Enabled */
lifecycle_globals->cache_enabled = 1;

/* Recursive Lock */
lifecycle_globals->recursive_lock = 0;

/* Static cache */
memset(lifecycle_globals->scache, '\0', sizeof(zephir_fcall_cache_entry*) * ZEPHIR_MAX_CACHE_SLOTS);


ext_setup_globals();
}

/**
* Initialize globals only on each thread started
*/
static void php_zephir_init_module_globals(zend_lifecycle_globals *lifecycle_globals TSRMLS_DC)
{

}

static PHP_RINIT_FUNCTION(lifecycle)
{
zend_lifecycle_globals *lifecycle_globals_ptr = ZEPHIR_VGLOBAL;

php_zephir_init_globals(lifecycle_globals_ptr TSRMLS_CC);
//zephir_init_interned_strings(TSRMLS_C);
zephir_initialize_memory(lifecycle_globals_ptr TSRMLS_CC);

ext_setup_request();
return SUCCESS;
}

static PHP_RSHUTDOWN_FUNCTION(lifecycle)
{
ext_takedown_request();
zephir_deinitialize_memory(TSRMLS_C);
return SUCCESS;
}

#define ZEPHIR_POST_REQUEST 1
static PHP_PRSHUTDOWN_FUNCTION(lifecycle)
{
ext_takedown_request();
}

static PHP_MINFO_FUNCTION(lifecycle)
{
php_info_print_box_start(0);
php_printf("%s", PHP_LIFECYCLE_DESCRIPTION);
php_info_print_box_end();

php_info_print_table_start();
php_info_print_table_header(2, PHP_LIFECYCLE_NAME, "enabled");
php_info_print_table_row(2, "Author", PHP_LIFECYCLE_AUTHOR);
php_info_print_table_row(2, "Version", PHP_LIFECYCLE_VERSION);
php_info_print_table_row(2, "Build Date", __DATE__ " " __TIME__ );
php_info_print_table_row(2, "Powered by Zephir", "Version " PHP_LIFECYCLE_ZEPVERSION);
php_info_print_table_end();

DISPLAY_INI_ENTRIES();
}

static PHP_GINIT_FUNCTION(lifecycle)
{
php_zephir_init_globals(lifecycle_globals TSRMLS_CC);
php_zephir_init_module_globals(lifecycle_globals TSRMLS_CC);
}

static PHP_GSHUTDOWN_FUNCTION(lifecycle)
{
ext_takedown_globals();
}


zend_function_entry php_lifecycle_functions[] = {
ZEND_FE_END

};

zend_module_entry lifecycle_module_entry = {
STANDARD_MODULE_HEADER_EX,
NULL,
NULL,
PHP_LIFECYCLE_EXTNAME,
php_lifecycle_functions,
PHP_MINIT(lifecycle),
#ifndef ZEPHIR_RELEASE
PHP_MSHUTDOWN(lifecycle),
#else
NULL,
#endif
PHP_RINIT(lifecycle),
PHP_RSHUTDOWN(lifecycle),
PHP_MINFO(lifecycle),
PHP_LIFECYCLE_VERSION,
ZEND_MODULE_GLOBALS(lifecycle),
PHP_GINIT(lifecycle),
PHP_GSHUTDOWN(lifecycle),
#ifdef ZEPHIR_POST_REQUEST
PHP_PRSHUTDOWN(lifecycle),
#else
NULL,
#endif
STANDARD_MODULE_PROPERTIES_EX
};

#ifdef COMPILE_DL_LIFECYCLE
ZEND_GET_MODULE(lifecycle)
#endif
Loading

0 comments on commit 10331f5

Please sign in to comment.