Skip to content

Commit 69454d9

Browse files
committed
Added function opcache_compile_file() to load PHP scripts into cache without execution.
1 parent dc8705c commit 69454d9

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ PHP NEWS
2626
imap). (ryotakatsuki at gmail dot com)
2727

2828
- OPcache:
29+
. Added function opcache_compile_file() to load PHP scripts into cache
30+
without execution. (Julien)
2931
. Fixed bug #65665 (Exception not properly caught when opcache enabled).
3032
(Laruence)
3133
. Fixed bug #65510 (5.5.2 crashes in _get_zval_ptr_ptr_var). (Dmitry)

ext/opcache/ZendAccelerator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ static zend_persistent_script *compile_and_cache_file(zend_file_handle *file_han
14461446
}
14471447

14481448
/* zend_compile() replacement */
1449-
static zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
1449+
zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
14501450
{
14511451
zend_persistent_script *persistent_script = NULL;
14521452
char *key = NULL;

ext/opcache/ZendAccelerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ int accelerator_shm_read_lock(TSRMLS_D);
325325
void accelerator_shm_read_unlock(TSRMLS_D);
326326

327327
char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_length, int *key_len TSRMLS_DC);
328+
zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC);
328329

329330
#if !defined(ZEND_DECLARE_INHERITED_CLASS_DELAYED)
330331
# define ZEND_DECLARE_INHERITED_CLASS_DELAYED 145

ext/opcache/zend_accelerator_module.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_get_status, 0, 0, 0)
4848
ZEND_ARG_INFO(0, fetch_scripts)
4949
ZEND_END_ARG_INFO()
5050

51+
ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_compile_file, 0, 0, 1)
52+
ZEND_ARG_INFO(0, file)
53+
ZEND_END_ARG_INFO()
54+
5155
ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_invalidate, 0, 0, 1)
5256
ZEND_ARG_INFO(0, script)
5357
ZEND_ARG_INFO(0, force)
@@ -59,12 +63,14 @@ static ZEND_FUNCTION(opcache_invalidate);
5963

6064
/* Private functions */
6165
static ZEND_FUNCTION(opcache_get_status);
66+
static ZEND_FUNCTION(opcache_compile_file);
6267
static ZEND_FUNCTION(opcache_get_configuration);
6368

6469
static zend_function_entry accel_functions[] = {
6570
/* User functions */
6671
ZEND_FE(opcache_reset, arginfo_opcache_none)
6772
ZEND_FE(opcache_invalidate, arginfo_opcache_invalidate)
73+
ZEND_FE(opcache_compile_file, arginfo_opcache_compile_file)
6874
/* Private functions */
6975
ZEND_FE(opcache_get_configuration, arginfo_opcache_none)
7076
ZEND_FE(opcache_get_status, arginfo_opcache_get_status)
@@ -709,3 +715,44 @@ static ZEND_FUNCTION(opcache_invalidate)
709715
RETURN_FALSE;
710716
}
711717
}
718+
719+
static ZEND_FUNCTION(opcache_compile_file)
720+
{
721+
char *script_name;
722+
int script_name_len;
723+
zend_file_handle handle;
724+
zend_op_array *op_array = NULL;
725+
zend_execute_data *orig_execute_data = NULL;
726+
727+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &script_name, &script_name_len) == FAILURE) {
728+
return;
729+
}
730+
731+
if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled)) {
732+
zend_error(E_NOTICE, ACCELERATOR_PRODUCT_NAME " seems to be disabled, can't compile file");
733+
RETURN_FALSE;
734+
}
735+
736+
handle.filename = script_name;
737+
handle.free_filename = 0;
738+
handle.opened_path = NULL;
739+
handle.type = ZEND_HANDLE_FILENAME;
740+
741+
orig_execute_data = EG(current_execute_data);
742+
743+
zend_try {
744+
op_array = persistent_compile_file(&handle, ZEND_INCLUDE TSRMLS_CC);
745+
} zend_catch {
746+
EG(current_execute_data) = orig_execute_data;
747+
zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME " could not compile file %s" TSRMLS_CC, handle.filename);
748+
} zend_end_try();
749+
750+
if(op_array != NULL) {
751+
destroy_op_array(op_array TSRMLS_CC);
752+
efree(op_array);
753+
RETVAL_TRUE;
754+
} else {
755+
RETVAL_FALSE;
756+
}
757+
zend_destroy_file_handle(&handle TSRMLS_CC);
758+
}

0 commit comments

Comments
 (0)