Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit b4102ba

Browse files
Add new applications
1 parent 015693e commit b4102ba

File tree

15 files changed

+909
-123
lines changed

15 files changed

+909
-123
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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+
}

src/Config.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Pavlusha311245\UnitPhpSdk;
44

5+
use Pavlusha311245\UnitPhpSdk\Abstract\ApplicationAbstract;
56
use Pavlusha311245\UnitPhpSdk\Config\Application;
67
use Pavlusha311245\UnitPhpSdk\Config\Listener;
78
use Pavlusha311245\UnitPhpSdk\Config\Route;
9+
use Pavlusha311245\UnitPhpSdk\Enums\ApplicationTypeEnum;
810
use Pavlusha311245\UnitPhpSdk\Interfaces\ConfigInterface;
11+
use PHPUnit\TextUI\Configuration\Php;
912

1013
/**
1114
* This class contains Nginx Unit config data
@@ -17,6 +20,7 @@ class Config implements ConfigInterface
1720
private array $_routes = [];
1821

1922
private array $_applications = [];
23+
private array $_new_applications = [];
2024

2125
private array $_upstreams;
2226

@@ -31,7 +35,13 @@ public function __construct(array $data)
3135
$this->_routes[$routeName] = new Route($routeName, $routeData);
3236
}
3337
foreach ($data['applications'] as $appName => $appData) {
34-
$this->_applications[$appName] = new Application($appName, $appData);
38+
// $this->_applications[$appName] = new Application($appName, $appData);
39+
40+
// TODO: implement go and nodejs detect
41+
$this->_applications[$appName] = match ($appData['type']) {
42+
'php' => new Application\PhpApplication($appData),
43+
'external' => new Application\NodeJsApplication($appData),
44+
};
3545
}
3646

3747
foreach ($data['listeners'] as $listener => $listenerData) {
@@ -41,7 +51,7 @@ public function __construct(array $data)
4151
$typePath = $listener->getPass()[0];
4252
$typePathName = $listener->getPass()[1];
4353

44-
($this->{"_{$typePath}"}[$typePathName])->setListener($listener);
54+
// ($this->{"_{$typePath}"}[$typePathName])->setListener($listener);
4555

4656
$this->_listeners[] = $listener;
4757
}
@@ -76,6 +86,28 @@ public function getListeners(): array
7686
return $this->_listeners ?? [];
7787
}
7888

89+
/**
90+
* Create listener
91+
*
92+
* @param $data
93+
* @return void
94+
*/
95+
public function createListener($data)
96+
{
97+
// TODO: Implement createListener() method.
98+
}
99+
100+
/**
101+
* Update listener
102+
*
103+
* @param $data
104+
* @return void
105+
*/
106+
public function updateListener($data)
107+
{
108+
// TODO: Implement updateListener() method.
109+
}
110+
79111
/**
80112
* Get applications from config
81113
*
@@ -97,6 +129,11 @@ public function getApplication($applicationName)
97129
return $this->_applications[$applicationName];
98130
}
99131

132+
public function createApplication($data)
133+
{
134+
// TODO: Implement createApplication() method.
135+
}
136+
100137
/**
101138
* Get routes from config
102139
*
@@ -118,6 +155,11 @@ public function getRoute($routeName)
118155
return $this->_routes[$routeName];
119156
}
120157

158+
public function createRoute($data)
159+
{
160+
// TODO: Implement createRoute() method.
161+
}
162+
121163
/**
122164
* Get upstreams
123165
*

0 commit comments

Comments
 (0)