<?php
namespace SHServ\Models;
use \SHServ\Middleware\ControlScripts;
use \SHServ\Entities\Script;
class Scripts extends \SHServ\Middleware\Model {
public function get_scopes_list(): Array {
$instances = app() -> control_scripts_instances;
$scopes = [];
foreach($instances as $name => $scope) {
$ref = new \ReflectionClass($scope);
$path_info = pathinfo($ref -> getFileName());
$scopes[] = [
"name" => $name,
"filename" => $path_info["basename"],
"path" => $path_info["dirname"],
"state" => $this -> script_state("scope", $name) ? "enabled" : "disabled"
];
}
return $scopes;
}
public function remove_scope(String $name): Bool {
$scopes = app() -> control_scripts_instances;
if(!isset($scopes[$name])) {
return false;
}
$ref = new \ReflectionClass($scopes[$name]);
$filepath = $ref -> getFileName();
if(!file_exists($filepath)) {
return false;
}
if(!@unlink($filepath)) {
return false;
}
$script = new Script($name);
$script -> remove();
return true;
}
public function script_state(String $type, String $uniq_name): Bool {
$result = $this -> thin_builder() -> select(
Script::$table_name,
[ "state" ],
[ ["type", "=", $type], "AND", [ "uniq_name", '=', $uniq_name ] ]
);
return !$result ? false : ($result[0]["state"] == "enabled");
}
public function set_script_state(String $type, String $uniq_name, Bool $state): String | Bool {
// Тут нужно проверить существует ли скрипт с таким именем
if($type == "scope") {
$scopes_names = array_map(function($scope) {
return $scope["name"];
}, $this -> get_scopes_list());
if(!in_array($uniq_name, $scopes_names)) {
return "scope_not_found"; // Err. Scope not exists
}
} elseif($type == "regular") {
$regular_scripts_names = array_map(function($regular_script) {
return $regular_script["alias"];
}, $this -> regular_scripts_list());
if(!in_array($uniq_name, $regular_scripts_names)) {
return "regular_script_not_found"; // Err. Regular script not exists
}
} elseif($type == "action") {
$action_scripts_names = array_map(function($action_script) {
return $action_script["alias"];
}, $this -> actions_scripts_list());
if(!in_array($uniq_name, $action_scripts_names)) {
return "action_script_not_found"; // Err. Action script not exists
}
} else {
return "invalid_type"; // Err of type
}
$result = $this -> thin_builder() -> select(
Script::$table_name,
Script::get_fields(),
[ ["type", "=", $type], "AND", [ "uniq_name", '=', $uniq_name ] ]
);
if(!$result) {
return $this -> thin_builder() -> insert(
Script::$table_name,
[
"uniq_name" => $uniq_name,
"type" => $type,
"state" => $state ? "enabled" : "disabled",
"create_at" => date("Y-m-d H:i:s")
]
) ? true : "undefined_error";
}
// update
$script = new Script($result[0]["id"], $result[0]);
$script -> state = $state ? "enabled" : "disabled";
return $script -> update() ? true : false;
}
public function enable_script(String $type, String $uniq_name): String | Bool {
return $this -> set_script_state($type, $uniq_name, true);
}
public function disable_script(String $type, String $uniq_name): String | Bool {
return $this -> set_script_state($type, $uniq_name, false);
}
public function regular_scripts_list(): Array {
return $this -> get_scripts_list( "regular", ControlScripts::get_regular_scripts() );
}
public function actions_scripts_list(): Array {
return $this -> get_scripts_list( "action", ControlScripts::get_actions_scripts() );
}
public function get_scripts_list(String $type, Array $scripts): Array {
$data = [];
foreach($scripts as $alias => $script_data) {
$data[] = [
"alias" => $alias,
"type" => $type,
"icon" => $script_data["attributes"]["icon"] ?? "",
"code" => $script_data["code"] ?? "",
"name" => $script_data["attributes"]["name"],
"state" => $this -> script_state($type, $alias) ? "enabled" : "disabled",
"description" => $script_data["attributes"]["description"],
"filename" => $script_data["attributes"]["filename"],
"path" => $script_data["attributes"]["path"],
"created_by" => $script_data["attributes"]["author"],
];
}
return $data;
}
public function scope_is_exists(String $name): Bool {
$instances = app() -> control_scripts_instances;
return isset($instances[$name]);
}
}