summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@zend.com>2013-09-25 12:14:06 +0400
committerDmitry Stogov <dmitry@zend.com>2013-09-25 12:14:06 +0400
commit69454d9f0f765416f245acbc626b3984dcbf5e1d (patch)
treea1df87900f1e31a61df9f157b36f59b0f5668691
parentdc8705c256cd1d50cfa276e8de31558e089eab92 (diff)
downloadphp-git-69454d9f0f765416f245acbc626b3984dcbf5e1d.tar.gz
Added function opcache_compile_file() to load PHP scripts into cache without execution.
-rw-r--r--NEWS2
-rw-r--r--ext/opcache/ZendAccelerator.c2
-rw-r--r--ext/opcache/ZendAccelerator.h1
-rw-r--r--ext/opcache/zend_accelerator_module.c47
4 files changed, 51 insertions, 1 deletions
diff --git a/NEWS b/NEWS
index cd4d377136..bdc0cd5fb6 100644
--- a/NEWS
+++ b/NEWS
@@ -26,6 +26,8 @@ PHP NEWS
imap). (ryotakatsuki at gmail dot com)
- OPcache:
+ . Added function opcache_compile_file() to load PHP scripts into cache
+ without execution. (Julien)
. Fixed bug #65665 (Exception not properly caught when opcache enabled).
(Laruence)
. Fixed bug #65510 (5.5.2 crashes in _get_zval_ptr_ptr_var). (Dmitry)
diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c
index 7fdae6fc67..72b5a1b9fe 100644
--- a/ext/opcache/ZendAccelerator.c
+++ b/ext/opcache/ZendAccelerator.c
@@ -1446,7 +1446,7 @@ static zend_persistent_script *compile_and_cache_file(zend_file_handle *file_han
}
/* zend_compile() replacement */
-static zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
+zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
{
zend_persistent_script *persistent_script = NULL;
char *key = NULL;
diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h
index 361b60b08f..f223f126e4 100644
--- a/ext/opcache/ZendAccelerator.h
+++ b/ext/opcache/ZendAccelerator.h
@@ -325,6 +325,7 @@ int accelerator_shm_read_lock(TSRMLS_D);
void accelerator_shm_read_unlock(TSRMLS_D);
char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_length, int *key_len TSRMLS_DC);
+zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC);
#if !defined(ZEND_DECLARE_INHERITED_CLASS_DELAYED)
# define ZEND_DECLARE_INHERITED_CLASS_DELAYED 145
diff --git a/ext/opcache/zend_accelerator_module.c b/ext/opcache/zend_accelerator_module.c
index f9ddaa98b8..dedb7215c1 100644
--- a/ext/opcache/zend_accelerator_module.c
+++ b/ext/opcache/zend_accelerator_module.c
@@ -48,6 +48,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_get_status, 0, 0, 0)
ZEND_ARG_INFO(0, fetch_scripts)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_compile_file, 0, 0, 1)
+ ZEND_ARG_INFO(0, file)
+ZEND_END_ARG_INFO()
+
ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_invalidate, 0, 0, 1)
ZEND_ARG_INFO(0, script)
ZEND_ARG_INFO(0, force)
@@ -59,12 +63,14 @@ static ZEND_FUNCTION(opcache_invalidate);
/* Private functions */
static ZEND_FUNCTION(opcache_get_status);
+static ZEND_FUNCTION(opcache_compile_file);
static ZEND_FUNCTION(opcache_get_configuration);
static zend_function_entry accel_functions[] = {
/* User functions */
ZEND_FE(opcache_reset, arginfo_opcache_none)
ZEND_FE(opcache_invalidate, arginfo_opcache_invalidate)
+ ZEND_FE(opcache_compile_file, arginfo_opcache_compile_file)
/* Private functions */
ZEND_FE(opcache_get_configuration, arginfo_opcache_none)
ZEND_FE(opcache_get_status, arginfo_opcache_get_status)
@@ -709,3 +715,44 @@ static ZEND_FUNCTION(opcache_invalidate)
RETURN_FALSE;
}
}
+
+static ZEND_FUNCTION(opcache_compile_file)
+{
+ char *script_name;
+ int script_name_len;
+ zend_file_handle handle;
+ zend_op_array *op_array = NULL;
+ zend_execute_data *orig_execute_data = NULL;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &script_name, &script_name_len) == FAILURE) {
+ return;
+ }
+
+ if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled)) {
+ zend_error(E_NOTICE, ACCELERATOR_PRODUCT_NAME " seems to be disabled, can't compile file");
+ RETURN_FALSE;
+ }
+
+ handle.filename = script_name;
+ handle.free_filename = 0;
+ handle.opened_path = NULL;
+ handle.type = ZEND_HANDLE_FILENAME;
+
+ orig_execute_data = EG(current_execute_data);
+
+ zend_try {
+ op_array = persistent_compile_file(&handle, ZEND_INCLUDE TSRMLS_CC);
+ } zend_catch {
+ EG(current_execute_data) = orig_execute_data;
+ zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME " could not compile file %s" TSRMLS_CC, handle.filename);
+ } zend_end_try();
+
+ if(op_array != NULL) {
+ destroy_op_array(op_array TSRMLS_CC);
+ efree(op_array);
+ RETVAL_TRUE;
+ } else {
+ RETVAL_FALSE;
+ }
+ zend_destroy_file_handle(&handle TSRMLS_CC);
+}