Skip to content

Commit 4ed421c

Browse files
committed
wip
1 parent 74483e8 commit 4ed421c

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

runtime-light/state/init-functions.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "runtime-light/state/init-functions.h"
66

77
#include <cinttypes>
8+
#include <string_view>
89
#include <cstdint>
910

1011
#include "runtime-common/core/utils/kphp-assert-core.h"
@@ -14,13 +15,56 @@
1415
#include "runtime-light/server/http/init-functions.h"
1516
#include "runtime-light/server/init-functions.h"
1617
#include "runtime-light/server/job-worker/job-worker-server-state.h"
18+
#include "runtime-light/state/component-state.h"
1719
#include "runtime-light/state/instance-state.h"
1820
#include "runtime-light/streams/streams.h"
1921
#include "runtime-light/tl/tl-core.h"
2022
#include "runtime-light/tl/tl-functions.h"
2123

2224
namespace {
2325

26+
array<mixed> format_cli_argv() {
27+
constexpr std::string_view INI_ARG_PHP_PREFIX = "-D";
28+
constexpr std::string_view SHORT_ARG_PHP_PREFIX = "-";
29+
constexpr std::string_view LONG_ARG_PHP_PREFIX = "--";
30+
constexpr std::string_view RUNTIME_CONFIG_PHP_PREFIX = "--runtime_config";
31+
32+
array<mixed> argv;
33+
34+
const auto &component_state{ComponentState::get()};
35+
argv.reserve(component_state.argc * 2, false);
36+
37+
for (const auto &ini_opt : component_state.ini_opts) {
38+
argv.push_back(string{INI_ARG_PHP_PREFIX.data()});
39+
argv.push_back(ini_opt.get_key());
40+
41+
const auto &value{ini_opt.get_value()};
42+
if (!value.empty()) {
43+
argv.push_back(string{"="});
44+
argv.push_back(value);
45+
}
46+
}
47+
48+
for (const auto &cli_opt : component_state.cli_opts) {
49+
const bool is_short_option = cli_opt.get_key().as_string().size() == 1;
50+
argv.push_back((is_short_option ? string{SHORT_ARG_PHP_PREFIX.data()} : string{LONG_ARG_PHP_PREFIX.data()}).append(cli_opt.get_key()));
51+
52+
const auto &value{cli_opt.get_value()};
53+
if (!value.empty()) {
54+
argv.push_back(string{"="});
55+
argv.push_back(value);
56+
}
57+
}
58+
59+
if (!component_state.runtime_config.empty()) {
60+
argv.push_back(string{RUNTIME_CONFIG_PHP_PREFIX.data()});
61+
argv.push_back(string{"="});
62+
argv.push_back(component_state.runtime_config);
63+
}
64+
65+
return argv;
66+
}
67+
2468
void process_k2_invoke_http(tl::TLBuffer &tlb) noexcept {
2569
tl::K2InvokeHttp invoke_http{};
2670
if (!invoke_http.fetch(tlb)) {
@@ -44,8 +88,8 @@ task_t<uint64_t> init_kphp_cli_component() noexcept {
4488
{ // TODO superglobals init
4589
auto &superglobals{InstanceState::get().php_script_mutable_globals_singleton.get_superglobals()};
4690
using namespace PhpServerSuperGlobalIndices;
47-
superglobals.v$argc = static_cast<int64_t>(0);
48-
superglobals.v$argv = array<mixed>{};
91+
superglobals.v$argv = format_cli_argv();
92+
superglobals.v$argc = superglobals.v$argv.as_array().size().size;
4993
superglobals.v$_SERVER.set_value(string{ARGC.data(), ARGC.size()}, superglobals.v$argc);
5094
superglobals.v$_SERVER.set_value(string{ARGV.data(), ARGV.size()}, superglobals.v$argv);
5195
superglobals.v$_SERVER.set_value(string{PHP_SELF.data(), PHP_SELF.size()}, string{});

0 commit comments

Comments
 (0)