public function import($template, $import_flag = 0)
{
if (is_string($template)) {
$template = json_decode($template);
}
if (!is_array($template)) {
$this->validationError("Sandbox could not import malformed JSON template!", Error::IMPORT_ERROR, null, $template);
}
if (isset($template['options']) && is_array($template['options']) && (!$import_flag || $import_flag & static::IMPORT_OPTIONS)) {
$this->setOptions($template['options']);
}
if (isset($template['definitions']) && is_array($template['definitions']) && (!$import_flag || $import_flag & static::IMPORT_DEFINITIONS)) {
foreach ($template['definitions'] as $type => $data) {
$method = 'define' . str_replace('_', '', ucwords($type, '_'));
if (method_exists($this, $method)) {
switch ($type) {
case 'func':
foreach ($data as $key => $value) {
$function = function () {
};
@eval('$function = ' . $value["fullcode"] . ';');
if (!is_callable($function)) {
$this->validationError("Could not import function {$key}! Please check your code for errors!", Error::IMPORT_ERROR, null, $function);
}
$this->defineFunc($key, $function, $value["pass"]);
}
break;
case 'superglobal':
foreach ($data as $key => $value) {
$this->defineSuperglobal($key, $value["key"], $value["value"]);
}
break;
case 'namespace':
foreach ($data as $key => $value) {
$this->defineNamespace($key);
}
break;
case 'alias':
foreach ($data as $key => $value) {
$this->defineAlias($key, $value ? $value : null);
}
break;
case 'class':
foreach ($data as $key => $value) {
$this->defineClass($key, $value);
}
break;
case 'interface':
foreach ($data as $key => $value) {
$this->defineInterface($key, $value);
}
break;
case 'trait':
foreach ($data as $key => $value) {
$this->defineTrait($key, $value);
}
break;
default:
foreach ($data as $key => $value) {
call_user_func_array([$this, $method], [$key, $value["value"]]);
}
break;
}
}
}
}
if (isset($template['whitelist']) && is_array($template['whitelist']) && (!$import_flag || $import_flag & static::IMPORT_WHITELIST)) {
foreach ($template['whitelist'] as $type => $data) {
$method = 'whitelist' . str_replace('_', '', ucwords($type, '_'));
if (method_exists($this, $method)) {
call_user_func_array([$this, $method], [$data]);
}
}
}
if (isset($template['blacklist']) && is_array($template['blacklist']) && (!$import_flag || $import_flag & static::IMPORT_BLACKLIST)) {
foreach ($template['blacklist'] as $type => $data) {
$method = 'blacklist' . str_replace('_', '', ucwords($type, '_'));
if (method_exists($this, $method)) {
call_user_func_array([$this, $method], [$data]);
}
}
}
if (!$import_flag || $import_flag & static::IMPORT_TRUSTED_CODE) {
$this->clearTrustedCode();
if (isset($template['prepend_code']) && $template['prepend_code']) {
$this->prepend($template['prepend_code']);
}
if (isset($template['append_code']) && $template['append_code']) {
$this->append($template['append_code']);
}
}
if (!$import_flag || $import_flag & static::IMPORT_CODE) {
$this->clearCode();
if (isset($template['code']) && $template['code']) {
$this->prepare($template['code']);
}
}
return $this;
}