Skip to content

Commit 995483a

Browse files
committed
refactor: cleanup the code a lot
1 parent 36aaf3a commit 995483a

File tree

2 files changed

+123
-61
lines changed

2 files changed

+123
-61
lines changed

agent/php_execute.c

Lines changed: 123 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -932,16 +932,36 @@ static void nr_execute_handle_library(const char* filename,
932932
}
933933
}
934934

935-
static void nr_get_composer_package_information() {
935+
static bool nr_execute_handle_autoload_composer_is_initialized() {
936+
zend_class_entry* zce = NULL;
937+
938+
if (NULL == (zce = nr_php_find_class("composer\\installedversions"))) {
939+
nrl_verbosedebug(NRL_INSTRUMENT, "Composer\\InstalledVersions class not found");
940+
return false;
941+
};
942+
943+
// the class is found - there's hope!
944+
if (NULL == nr_php_find_class_method(zce, "getinstalledpackages") || NULL == nr_php_find_class_method(zce, "getversion")) {
945+
nrl_verbosedebug(NRL_INSTRUMENT, "Composer\\InstalledVersions class found, but methods not found");
946+
return false;
947+
}
948+
949+
return true;
950+
}
951+
952+
static int nr_execute_handle_autoload_composer_init(const char* vendor_path) {
953+
char* code = NULL;
936954
zval retval;
937955
int result = -1;
938956

939-
if (NRPRG(txn)->composer_info.inside_eval_string) {
940-
return;
957+
if (nr_execute_handle_autoload_composer_is_initialized()) {
958+
nrl_verbosedebug(NRL_INSTRUMENT, "%s: already initialized", __func__);
959+
return NR_SUCCESS;
941960
}
942961

943-
char* check
944-
= ""
962+
#if 0
963+
code
964+
= nr_formatf(""
945965
"(function() {"
946966
" try {"
947967
" if (class_exists('Composer\\InstalledVersions')) {"
@@ -958,13 +978,38 @@ static void nr_get_composer_package_information() {
958978
" } catch (Exception $e) {"
959979
" return NULL;"
960980
" }"
961-
"})();";
981+
"})();");
982+
#else
983+
code = nr_formatf("include_once '%s/composer/InstalledVersions.php';", vendor_path);
984+
#endif
985+
986+
result = zend_eval_string(code, &retval, "newrelic\\init_composer_api");
987+
if (result != SUCCESS) {
988+
nrl_verbosedebug(NRL_INSTRUMENT, "%s: zend_eval_string(%s) failed, result=%d", __func__, code, result);
989+
return NR_FAILURE;
990+
}
991+
992+
zval_dtor(&retval);
993+
nr_free(code);
994+
995+
// Make sure runtime API is available after loading Composer\\InstalledVersions class:
996+
if (!nr_execute_handle_autoload_composer_is_initialized()) {
997+
nrl_verbosedebug(NRL_INSTRUMENT, "%s: unable to initialize Composer runtime API", __func__);
998+
return NR_FAILURE;
999+
}
1000+
1001+
return NR_SUCCESS;
1002+
}
1003+
1004+
static void nr_execute_handle_autoload_composer_get_packages_information(const char* vendor_path) {
1005+
zval retval;
1006+
int result = -1;
9621007

9631008
char* getpackagename =
9641009
""
9651010
"(function() {"
9661011
" try {"
967-
" return Composer\\InstalledVersions::getInstalledPackages();"
1012+
" return \\Composer\\InstalledVersions::getInstalledPackages();"
9681013
" } catch (Exception $e) {"
9691014
" return NULL;"
9701015
" }"
@@ -974,36 +1019,22 @@ static void nr_get_composer_package_information() {
9741019
""
9751020
"(function() {"
9761021
" try {"
977-
" return Composer\\InstalledVersions::getVersion(\"%s\");"
1022+
" return \\Composer\\InstalledVersions::getVersion(\"%s\");"
9781023
" } catch (Exception $e) {"
9791024
" return NULL;"
9801025
" }"
9811026
"})();";
9821027

983-
NRPRG(txn)->composer_info.inside_eval_string = 1;
984-
result = zend_eval_string(check, &retval, "check if class and methods exist" TSRMLS_CC);
985-
NRPRG(txn)->composer_info.inside_eval_string = 0;
986-
987-
if (result == SUCCESS) {
988-
if (Z_TYPE(retval) == IS_NULL) {
989-
if (NULL != NRPRG(txn)) {
990-
NRPRG(txn)->composer_info.api_called = 1;
991-
}
992-
nrl_verbosedebug(NRL_TXN, "IS_NULL");
993-
return;
994-
} else if (Z_TYPE(retval) == IS_FALSE) {
995-
nrl_verbosedebug(NRL_TXN, "IS_FALSE");
996-
return;
997-
} else if (Z_TYPE(retval) != IS_TRUE) {
998-
nrl_verbosedebug(NRL_TXN, "NOT_TRUE");
999-
return;
1000-
}
1028+
if (NR_SUCCESS != nr_execute_handle_autoload_composer_init(vendor_path)) {
1029+
nrl_debug(NRL_INSTRUMENT, "%s - unable to initialize Composer runtime API - package info unavailable", __func__);
1030+
return;
10011031
}
1002-
zval_dtor(&retval);
1003-
NRPRG(txn)->composer_info.inside_eval_string = 1;
1032+
1033+
nrl_verbosedebug(NRL_INSTRUMENT, "%s - Composer runtime API available", __func__);
1034+
1035+
#if 1
10041036
result = zend_eval_string(getpackagename, &retval,
10051037
"get installed packages by name" TSRMLS_CC);
1006-
NRPRG(txn)->composer_info.inside_eval_string = 0;
10071038
if (result == SUCCESS) {
10081039
if (Z_TYPE(retval) == IS_ARRAY) {
10091040
zval* value;
@@ -1015,9 +1046,7 @@ static void nr_get_composer_package_information() {
10151046
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(retval), value) {
10161047
if (Z_TYPE_P(value) == IS_STRING) {
10171048
buf = nr_formatf(getversion, Z_STRVAL_P(value));
1018-
NRPRG(txn)->composer_info.inside_eval_string = 1;
10191049
result2 = zend_eval_string(buf, &retval2, "retrieve version for packages");
1020-
NRPRG(txn)->composer_info.inside_eval_string = 0;
10211050
nr_free(buf);
10221051
if (SUCCESS == result2) {
10231052
if (nr_php_is_zval_valid_string(&retval2)) {
@@ -1031,24 +1060,27 @@ static void nr_get_composer_package_information() {
10311060
}
10321061
ZEND_HASH_FOREACH_END();
10331062
} else {
1034-
if (NULL != NRPRG(txn)) {
1035-
NRPRG(txn)->composer_info.api_called = 1;
1036-
}
10371063
zval_dtor(&retval);
10381064
return;
10391065
}
10401066
zval_dtor(&retval);
1041-
NRPRG(txn)->composer_info.api_called = 1;
10421067
}
1068+
#else
1069+
zv = nr_php_call(NULL, "Composer\\InstalledVersions::getInstalledPackages", NULL);
1070+
if (NULL != zv) {
1071+
char strbuf[NR_EXECUTE_DEBUG_STRBUFSZ];
1072+
nr_format_zval_for_debug(zv, strbuf, 0, NR_EXECUTE_DEBUG_STRBUFSZ - 1,
1073+
0);
1074+
nrl_always("Composer\\InstalledVersions::getInstalledPackages()=%s",
1075+
strbuf);
1076+
nr_php_zval_free(&zv);
1077+
}
1078+
#endif
10431079
}
10441080

1045-
static bool nr_execute_autoload_is_composer(const char* filename) {
1046-
#define COMPOSER_MAGIC_FILE "composer/autoload_real.php"
1047-
#define COMPOSER_MAGIC_FILE_LEN (sizeof(COMPOSER_MAGIC_FILE) - 1)
1081+
static char* nr_execute_handle_autoload_composer_get_vendor_path(const char* filename) {
10481082
char* vendor_path = NULL; // result of dirname(filename)
1049-
char* composer_magic_file = NULL; // vendor_path + COMPOSER_MAGIC_FILE
10501083
char* cp = NULL;
1051-
bool composer_detected = false;
10521084

10531085
// vendor_path = dirname(filename):
10541086
// 1. copy filename to vendor_path
@@ -1058,38 +1090,73 @@ static bool nr_execute_autoload_is_composer(const char* filename) {
10581090
// 3. replace '/' with '\0' to get the directory path
10591091
*cp = '\0';
10601092

1061-
// composer_magic_file = vendor_path + "/composer/autoload_real.php"
1062-
cp = composer_magic_file = nr_malloc(nr_strlen(vendor_path) + COMPOSER_MAGIC_FILE_LEN + 2);
1063-
cp = nr_strcpy(cp, vendor_path);
1064-
*cp++ = '/';
1065-
cp = nr_strcpy(cp, COMPOSER_MAGIC_FILE);
1093+
return vendor_path;
1094+
}
1095+
1096+
static bool nr_execute_handle_autoload_composer_file_exists(const char* vendor_path, const char* filename) {
1097+
char* composer_magic_file = NULL; // vendor_path + filename
1098+
bool file_exists = false;
10661099

1100+
composer_magic_file = nr_formatf("%s/%s", vendor_path, filename);
10671101
if (0 == nr_access(composer_magic_file, F_OK | R_OK)) {
1068-
nrl_debug(NRL_INSTRUMENT, "detected composer with %s", composer_magic_file);
1069-
composer_detected = true;
1070-
nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Composer");
1102+
file_exists = true;
1103+
}
1104+
nr_free(composer_magic_file);
1105+
return file_exists;
1106+
}
1107+
1108+
static void nr_execute_handle_autoload_composer(const char* filename) {
1109+
// Composer signature file"
1110+
#define COMPOSER_MAGIC_FILE_1 "composer/autoload_real.php"
1111+
#define COMPOSER_MAGIC_FILE_1_LEN (sizeof(COMPOSER_MAGIC_FILE_1) - 1)
1112+
// Composer runtime API file:
1113+
#define COMPOSER_MAGIC_FILE_2 "composer/InstalledVersions.php"
1114+
#define COMPOSER_MAGIC_FILE_2_LEN (sizeof(COMPOSER_MAGIC_FILE_2) - 1)
1115+
char* vendor_path = NULL; // result of dirname(filename)
1116+
1117+
vendor_path = nr_execute_handle_autoload_composer_get_vendor_path(filename);
1118+
if (NULL == vendor_path) {
1119+
nrl_verbosedebug(NRL_FRAMEWORK, "unable to get vendor path from '%s'", filename);
1120+
return;
1121+
}
1122+
1123+
if (!nr_execute_handle_autoload_composer_file_exists(vendor_path, COMPOSER_MAGIC_FILE_1)) {
1124+
nrl_verbosedebug(NRL_FRAMEWORK, "'%s' not found in '%s'", COMPOSER_MAGIC_FILE_1, vendor_path);
1125+
return;
1126+
}
1127+
1128+
if (!nr_execute_handle_autoload_composer_file_exists(vendor_path, COMPOSER_MAGIC_FILE_2)) {
1129+
nrl_verbosedebug(NRL_FRAMEWORK, "'%s' not found in '%s'", COMPOSER_MAGIC_FILE_2, vendor_path);
1130+
return;
10711131
}
10721132

1133+
nrl_verbosedebug(NRL_FRAMEWORK, "detected composer");
1134+
NRPRG(txn)->composer_info.composer_detected = true;
1135+
nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Composer");
1136+
1137+
nr_execute_handle_autoload_composer_get_packages_information(vendor_path);
10731138
nr_free(vendor_path);
1074-
nr_free(composer_magic_file);
1075-
return composer_detected;
10761139
}
10771140

10781141
static void nr_execute_handle_autoload(const char* filename, const size_t filename_len) {
10791142
#define AUTOLOAD_MAGIC_FILE "vendor/autoload.php"
10801143
#define AUTOLOAD_MAGIC_FILE_LEN (sizeof(AUTOLOAD_MAGIC_FILE) - 1)
10811144

10821145
if (NRPRG(txn)->composer_info.autoload_detected) {
1146+
// autoload already handled
10831147
return;
10841148
}
10851149

1086-
if (nr_striendswith(STR_AND_LEN(filename), AUTOLOAD_MAGIC_FILE, AUTOLOAD_MAGIC_FILE_LEN)) {
1087-
nrl_debug(NRL_INSTRUMENT, "detected autoload with %s, which ends with %s", filename, AUTOLOAD_MAGIC_FILE);
1088-
NRPRG(txn)->composer_info.autoload_detected = true;
1089-
nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Autoloader");
1090-
1091-
NRPRG(txn)->composer_info.composer_detected = nr_execute_autoload_is_composer(filename);
1150+
if (!nr_striendswith(STR_AND_LEN(filename), AUTOLOAD_MAGIC_FILE, AUTOLOAD_MAGIC_FILE_LEN)) {
1151+
// not an autoload file
1152+
return;
10921153
}
1154+
1155+
nrl_debug(NRL_FRAMEWORK, "detected autoload with %s, which ends with %s", filename, AUTOLOAD_MAGIC_FILE);
1156+
NRPRG(txn)->composer_info.autoload_detected = true;
1157+
nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Autoloader");
1158+
1159+
nr_execute_handle_autoload_composer(filename);
10931160
}
10941161

10951162
static void nr_execute_handle_logging_framework(const char* filename,
@@ -1199,9 +1266,6 @@ static void nr_php_execute_file(const zend_op_array* op_array,
11991266

12001267
nr_php_add_user_instrumentation(TSRMLS_C);
12011268

1202-
if (NRPRG(txn)->composer_info.composer_detected && !NRPRG(txn)->composer_info.api_called) {
1203-
nr_get_composer_package_information();
1204-
}
12051269
}
12061270

12071271
/*

axiom/nr_txn.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ typedef enum _nr_cpu_usage_t {
206206
typedef struct _nr_composer_info_t {
207207
bool autoload_detected;
208208
bool composer_detected;
209-
int api_called;
210-
int inside_eval_string;
211209
} nr_composer_info_t;
212210

213211
/*

0 commit comments

Comments
 (0)