|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pavlusha311245\UnitPhpSdk\Abstract; |
| 4 | + |
| 5 | +use Pavlusha311245\UnitPhpSdk\Config\Application\ProcessManagement\ApplicationProcess; |
| 6 | +use Pavlusha311245\UnitPhpSdk\Config\Application\ProcessManagement\ProcessIsolation; |
| 7 | +use Pavlusha311245\UnitPhpSdk\Config\Application\ProcessManagement\RequestLimit; |
| 8 | +use Pavlusha311245\UnitPhpSdk\Enums\ApplicationTypeEnum; |
| 9 | +use Pavlusha311245\UnitPhpSdk\Exceptions\UnitException; |
| 10 | +use Pavlusha311245\UnitPhpSdk\Interfaces\ApplicationInterface; |
| 11 | + |
| 12 | +abstract class ApplicationAbstract implements ApplicationInterface |
| 13 | +{ |
| 14 | + private string $_type; |
| 15 | + |
| 16 | + /** |
| 17 | + * Environment variables to be passed to the app |
| 18 | + * |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + private array $_environment; |
| 22 | + |
| 23 | + /** |
| 24 | + * Group name that runs the app process |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected string $_group; |
| 29 | + |
| 30 | + /** |
| 31 | + * Username that runs the app process |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + protected string $_user; |
| 36 | + |
| 37 | + /** |
| 38 | + * The app working directory. |
| 39 | + * |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + private string $_working_directory; |
| 43 | + |
| 44 | + private string $_name; |
| 45 | + |
| 46 | + /** |
| 47 | + * The file path to which Unit redirects the application's error stream output in --no-daemon mode. |
| 48 | + * |
| 49 | + * @var string |
| 50 | + */ |
| 51 | + private string $_stderr = '/dev/null'; |
| 52 | + |
| 53 | + /** |
| 54 | + * The file path to which Unit redirects the output of the application output stream in --no-daemon mode. |
| 55 | + * |
| 56 | + * @var string |
| 57 | + */ |
| 58 | + private string $_stdout = '/dev/null'; |
| 59 | + |
| 60 | + /** |
| 61 | + * Static number of app processes or object options |
| 62 | + * |
| 63 | + * @var ApplicationProcess|string |
| 64 | + */ |
| 65 | + private ApplicationProcess|string $_processes; |
| 66 | + |
| 67 | + private RequestLimit $_limits; |
| 68 | + |
| 69 | + private ProcessIsolation $_isolation; |
| 70 | + |
| 71 | + public function __construct(array $data = null) |
| 72 | + { |
| 73 | + if (!empty($data)) { |
| 74 | + $this->parseFromArray($data); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function getType(): string |
| 79 | + { |
| 80 | + return $this->_type; |
| 81 | + } |
| 82 | + |
| 83 | + public function setType(string $type) |
| 84 | + { |
| 85 | + $this->_type = $type; |
| 86 | + } |
| 87 | + |
| 88 | + public function getGroup(): string |
| 89 | + { |
| 90 | + return $this->_group; |
| 91 | + } |
| 92 | + |
| 93 | + public function setGroup(string $name): void |
| 94 | + { |
| 95 | + $this->_group = $name; |
| 96 | + } |
| 97 | + |
| 98 | + public function getUser(): string |
| 99 | + { |
| 100 | + return $this->_user; |
| 101 | + } |
| 102 | + |
| 103 | + public function setUser(string $name): void |
| 104 | + { |
| 105 | + $this->_user = $name; |
| 106 | + } |
| 107 | + |
| 108 | + public function getEnvironment(): array |
| 109 | + { |
| 110 | + return $this->_environment; |
| 111 | + } |
| 112 | + |
| 113 | + public function setEnvironment(array $environment): void |
| 114 | + { |
| 115 | + foreach ($environment as $key => $value) { |
| 116 | + if (!is_string($value)) { |
| 117 | + throw new UnitException('Parse Exception'); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + $this->_environment = $environment; |
| 122 | + } |
| 123 | + |
| 124 | + public function getIsolation(): ProcessIsolation |
| 125 | + { |
| 126 | + return $this->_isolation; |
| 127 | + } |
| 128 | + |
| 129 | + public function setIsolation(ProcessIsolation $isolation): void |
| 130 | + { |
| 131 | + $this->_isolation = $isolation; |
| 132 | + } |
| 133 | + |
| 134 | + public function getProcesses(): ApplicationProcess|int |
| 135 | + { |
| 136 | + return $this->_processes; |
| 137 | + } |
| 138 | + |
| 139 | + public function setProcesses(ApplicationProcess|int $processes): void |
| 140 | + { |
| 141 | + $this->_processes = $processes; |
| 142 | + } |
| 143 | + |
| 144 | + public function getLimits(): RequestLimit |
| 145 | + { |
| 146 | + return $this->_limits; |
| 147 | + } |
| 148 | + |
| 149 | + public function setLimits(RequestLimit $requestLimit): void |
| 150 | + { |
| 151 | + $this->_limits = $requestLimit; |
| 152 | + } |
| 153 | + |
| 154 | + public function getStdErr(): string |
| 155 | + { |
| 156 | + return $this->_stderr; |
| 157 | + } |
| 158 | + |
| 159 | + public function setStdErr(string $path): void |
| 160 | + { |
| 161 | + $this->_stderr = $path; |
| 162 | + } |
| 163 | + |
| 164 | + public function getStdOut(): string |
| 165 | + { |
| 166 | + return $this->_stdout; |
| 167 | + } |
| 168 | + |
| 169 | + public function setStdOut(string $path): void |
| 170 | + { |
| 171 | + $this->_stdout = $path; |
| 172 | + } |
| 173 | + |
| 174 | + public function getWorkingDirectory(): string |
| 175 | + { |
| 176 | + return $this->_working_directory; |
| 177 | + } |
| 178 | + |
| 179 | + public function setWorkingDirectory(string $path): void |
| 180 | + { |
| 181 | + $this->_working_directory = $path; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @return string |
| 186 | + */ |
| 187 | + public function getName(): string |
| 188 | + { |
| 189 | + return $this->_name; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @param string $name |
| 194 | + */ |
| 195 | + public function setName(string $name): void |
| 196 | + { |
| 197 | + $this->_name = $name; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Parse data from array |
| 202 | + * |
| 203 | + * @throws UnitException |
| 204 | + */ |
| 205 | + public function parseFromArray(array $data): void |
| 206 | + { |
| 207 | + if (!array_key_exists('type', $data)) { |
| 208 | + throw new UnitException('Parse Exception'); |
| 209 | + } |
| 210 | + |
| 211 | + $this->setType($data['type']); |
| 212 | + |
| 213 | + if (array_key_exists('working_directory', $data)) { |
| 214 | + $this->setWorkingDirectory($data['working_directory']); |
| 215 | + } |
| 216 | + |
| 217 | + if (array_key_exists('user', $data)) { |
| 218 | + $this->setUser($data['user']); |
| 219 | + } |
| 220 | + |
| 221 | + if (array_key_exists('group', $data)) { |
| 222 | + $this->setGroup($data['group']); |
| 223 | + } |
| 224 | + |
| 225 | + if (array_key_exists('environment', $data)) { |
| 226 | + $this->setEnvironment($data['environment']); |
| 227 | + } |
| 228 | + |
| 229 | + if (array_key_exists('stderr', $data)) { |
| 230 | + $this->setStdErr($data['stderr']); |
| 231 | + } |
| 232 | + |
| 233 | + if (array_key_exists('stdout', $data)) { |
| 234 | + $this->setStdOut($data['stdout']); |
| 235 | + } |
| 236 | + |
| 237 | +// TODO: implement isolation object |
| 238 | +// if (array_key_exists('isolation', $data)) { |
| 239 | +// $this->setIsolation($data['isolation']); |
| 240 | +// } |
| 241 | + |
| 242 | + if (array_key_exists('processes', $data)) { |
| 243 | + if (is_array($data['processes'])) { |
| 244 | + $this->setProcesses(new ApplicationProcess($data['processes'])); |
| 245 | + } else { |
| 246 | + $this->setProcesses($data['processes']); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + if (array_key_exists('limits', $data)) { |
| 251 | + $this->setLimits(new RequestLimit($data['limits'])); |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments