@@ -19,9 +19,14 @@ class NginxHandler extends AbstractHandler
1919 private Engine $ templates ;
2020
2121 /**
22- * @var array< string, string>
22+ * @var string
2323 */
24- private array $ options ;
24+ private string $ pidFile ;
25+
26+ /**
27+ * @var string
28+ */
29+ private string $ configFile ;
2530
2631 /**
2732 * @var bool
@@ -40,8 +45,11 @@ class NginxHandler extends AbstractHandler
4045 */
4146 public function __construct (array $ options = [])
4247 {
43- $ this ->templates = new Engine (__DIR__ . DIRECTORY_SEPARATOR . 'templates ' );
44- $ this ->options = $ options ;
48+ $ this ->templates = new Engine (__DIR__ . DIRECTORY_SEPARATOR . 'templates ' );
49+ $ this ->pidFile = $ this ->expandPathLikeShell ($ options ['pid ' ]);
50+ $ this ->configFile = $ this ->expandPathLikeShell ($ options ['config ' ]);
51+
52+ parent ::__construct ();
4553 }
4654
4755 /**
@@ -60,7 +68,9 @@ public function checkDependenciesBeforeStart(): void
6068 */
6169 public function generateConfig (HeaderInterface $ header ): void
6270 {
63- $ this ->logger ->info ("Nginx PID location: {$ this ->options ['pid ' ]}" );
71+ $ this ->logger ->info ("Nginx PID location: {$ this ->pidFile }" );
72+ $ this ->makePathForFiles ([$ this ->pidFile , $ this ->configFile ]);
73+ $ this ->touchFile ($ this ->pidFile );
6474
6575 $ data = $ this ->templates ->render ('nginx_default.conf ' , [
6676 'serverRoot ' => realpath ($ this ->getServerRoot ()),
@@ -73,25 +83,25 @@ public function generateConfig(HeaderInterface $header): void
7383 'prerenderHost ' => $ this ->getHostWithoutTrailingSlash ('server.prerender.host ' ),
7484 'headers ' => $ header ->convert ($ this ->configuration ),
7585 'connProcMethod ' => $ this ->getConnectionProcessingMethod (),
76- 'pidLocation ' => $ this ->options [ ' pid ' ] ,
86+ 'pidLocation ' => $ this ->pidFile ,
7787 'moduleBrotliInstalled ' => $ this ->moduleBrotliInstalled ,
7888 'platformSupportsAsyncIo ' => $ this ->platformSupportsAsyncIo ,
7989 ]);
8090
81- file_put_contents ($ this ->options [ ' config ' ] , $ data );
91+ file_put_contents ($ this ->configFile , $ data );
8292 }
8393
8494 public function checkConfig (): void
8595 {
86- $ this ->runProcess (['nginx ' , '-c ' , $ this ->options [ ' config ' ] , '-t ' ]);
96+ $ this ->runProcess (['nginx ' , '-c ' , $ this ->configFile , '-t ' ]);
8797 }
8898
8999 /**
90100 * Start the web-server.
91101 */
92102 public function start (): void
93103 {
94- $ this ->runProcess (['nginx ' , '-c ' , $ this ->options [ ' config ' ] ], function () {
104+ $ this ->runProcess (['nginx ' , '-c ' , $ this ->configFile ], function () {
95105 $ this ->logger ->info (sprintf (
96106 'Server started at: %s:%d ' ,
97107 $ this ->configuration ->get ('server.host ' ),
@@ -102,20 +112,24 @@ public function start(): void
102112
103113 public function reload (): void
104114 {
105- if (!file_exists ($ this ->options [ ' pid ' ] )) {
115+ if (!file_exists ($ this ->pidFile )) {
106116 throw new LogicException ('Can \'t reload server. Pid file not found. ' );
107117 }
108118
109- if (empty (file_get_contents ($ this ->options [ ' pid ' ] ))) {
119+ if (empty (file_get_contents ($ this ->pidFile ))) {
110120 throw new LogicException ('Can \'t reload server. Pid file is empty. ' );
111121 }
112122
113- $ this ->runProcess (['nginx ' , '-c ' , $ this ->options [ ' config ' ] , '-s ' , 'reload ' ]);
123+ $ this ->runProcess (['nginx ' , '-c ' , $ this ->configFile , '-s ' , 'reload ' ]);
114124 }
115125
116126 public function stop (): void
117127 {
118- $ this ->runProcess (['nginx ' , '-c ' , $ this ->options ['config ' ], '-s ' , 'stop ' ]);
128+ $ this ->runProcess (['nginx ' , '-c ' , $ this ->configFile , '-s ' , 'stop ' ]);
129+
130+ if ($ this ->filesystem ->exists ($ this ->configFile )) {
131+ $ this ->filesystem ->remove ($ this ->configFile );
132+ }
119133 }
120134
121135 /**
0 commit comments