File tree 8 files changed +78
-7
lines changed
Filter options
src/Symfony/Bundle/WebServerBundle
Tests/DependencyInjection 8 files changed +78
-7
lines changed
Original file line number Diff line number Diff line change @@ -31,13 +31,15 @@ class ServerRunCommand extends Command
31
31
{
32
32
private $ documentRoot ;
33
33
private $ environment ;
34
+ private $ pidFileDirectory ;
34
35
35
36
protected static $ defaultName = 'server:run ' ;
36
37
37
- public function __construct (string $ documentRoot = null , string $ environment = null )
38
+ public function __construct (string $ documentRoot = null , string $ environment = null , string $ pidFileDirectory = null )
38
39
{
39
40
$ this ->documentRoot = $ documentRoot ;
40
41
$ this ->environment = $ environment ;
42
+ $ this ->pidFileDirectory = $ pidFileDirectory ;
41
43
42
44
parent ::__construct ();
43
45
}
@@ -129,7 +131,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
129
131
}
130
132
131
133
try {
132
- $ server = new WebServer ();
134
+ $ server = new WebServer ($ this -> pidFileDirectory );
133
135
$ config = new WebServerConfig ($ documentRoot , $ env , $ input ->getArgument ('addressport ' ), $ input ->getOption ('router ' ));
134
136
135
137
$ message = sprintf ('Server listening on http://%s ' , $ config ->getAddress ());
Original file line number Diff line number Diff line change @@ -31,13 +31,15 @@ class ServerStartCommand extends Command
31
31
{
32
32
private $ documentRoot ;
33
33
private $ environment ;
34
+ private $ pidFileDirectory ;
34
35
35
36
protected static $ defaultName = 'server:start ' ;
36
37
37
- public function __construct (string $ documentRoot = null , string $ environment = null )
38
+ public function __construct (string $ documentRoot = null , string $ environment = null , string $ pidFileDirectory = null )
38
39
{
39
40
$ this ->documentRoot = $ documentRoot ;
40
41
$ this ->environment = $ environment ;
42
+ $ this ->pidFileDirectory = $ pidFileDirectory ;
41
43
42
44
parent ::__construct ();
43
45
}
@@ -133,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
133
135
$ this ->getApplication ()->setDispatcher (new EventDispatcher ());
134
136
135
137
try {
136
- $ server = new WebServer ();
138
+ $ server = new WebServer ($ this -> pidFileDirectory );
137
139
if ($ server ->isRunning ($ input ->getOption ('pidfile ' ))) {
138
140
$ io ->error (sprintf ('The web server has already been started. It is currently listening on http://%s. Please stop the web server before you try to start it again. ' , $ server ->getAddress ($ input ->getOption ('pidfile ' ))));
139
141
Original file line number Diff line number Diff line change @@ -30,6 +30,15 @@ class ServerStatusCommand extends Command
30
30
{
31
31
protected static $ defaultName = 'server:status ' ;
32
32
33
+ private $ pidFileDirectory ;
34
+
35
+ public function __construct (string $ pidFileDirectory = null )
36
+ {
37
+ $ this ->pidFileDirectory = $ pidFileDirectory ;
38
+
39
+ parent ::__construct ();
40
+ }
41
+
33
42
/**
34
43
* {@inheritdoc}
35
44
*/
@@ -64,7 +73,7 @@ protected function configure()
64
73
protected function execute (InputInterface $ input , OutputInterface $ output )
65
74
{
66
75
$ io = new SymfonyStyle ($ input , $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output );
67
- $ server = new WebServer ();
76
+ $ server = new WebServer ($ this -> pidFileDirectory );
68
77
if ($ filter = $ input ->getOption ('filter ' )) {
69
78
if ($ server ->isRunning ($ input ->getOption ('pidfile ' ))) {
70
79
list ($ host , $ port ) = explode (': ' , $ address = $ server ->getAddress ($ input ->getOption ('pidfile ' )));
Original file line number Diff line number Diff line change @@ -28,6 +28,15 @@ class ServerStopCommand extends Command
28
28
{
29
29
protected static $ defaultName = 'server:stop ' ;
30
30
31
+ private $ pidFileDirectory ;
32
+
33
+ public function __construct (string $ pidFileDirectory = null )
34
+ {
35
+ $ this ->pidFileDirectory = $ pidFileDirectory ;
36
+
37
+ parent ::__construct ();
38
+ }
39
+
31
40
/**
32
41
* {@inheritdoc}
33
42
*/
@@ -55,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
55
64
$ io = new SymfonyStyle ($ input , $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output );
56
65
57
66
try {
58
- $ server = new WebServer ();
67
+ $ server = new WebServer ($ this -> pidFileDirectory );
59
68
$ server ->stop ($ input ->getOption ('pidfile ' ));
60
69
$ io ->success ('Stopped the web server. ' );
61
70
} catch (\Exception $ e ) {
Original file line number Diff line number Diff line change @@ -31,6 +31,12 @@ public function load(array $configs, ContainerBuilder $container)
31
31
$ container ->getDefinition ('web_server.command.server_run ' )->replaceArgument (0 , $ publicDirectory );
32
32
$ container ->getDefinition ('web_server.command.server_start ' )->replaceArgument (0 , $ publicDirectory );
33
33
34
+ $ pidFileDirectory = $ this ->getPidFileDirectory ($ container );
35
+ $ container ->getDefinition ('web_server.command.server_run ' )->replaceArgument (2 , $ pidFileDirectory );
36
+ $ container ->getDefinition ('web_server.command.server_start ' )->replaceArgument (2 , $ pidFileDirectory );
37
+ $ container ->getDefinition ('web_server.command.server_stop ' )->replaceArgument (0 , $ pidFileDirectory );
38
+ $ container ->getDefinition ('web_server.command.server_status ' )->replaceArgument (0 , $ pidFileDirectory );
39
+
34
40
if (!class_exists (ConsoleFormatter::class)) {
35
41
$ container ->removeDefinition ('web_server.command.server_log ' );
36
42
}
@@ -54,4 +60,16 @@ private function getPublicDirectory(ContainerBuilder $container)
54
60
55
61
return $ kernelProjectDir .'/ ' .$ publicDir ;
56
62
}
63
+
64
+ private function getPidFileDirectory (ContainerBuilder $ container ): string
65
+ {
66
+ $ kernelCacheDir = $ container ->getParameter ('kernel.cache_dir ' );
67
+ $ environment = $ container ->getParameter ('kernel.environment ' );
68
+
69
+ if (basename ($ kernelCacheDir ) !== $ environment ) {
70
+ return $ container ->getParameter ('kernel.project_dir ' );
71
+ }
72
+
73
+ return \dirname ($ container ->getParameter ('kernel.cache_dir ' ));
74
+ }
57
75
}
Original file line number Diff line number Diff line change 10
10
<service id =" web_server.command.server_run" class =" Symfony\Bundle\WebServerBundle\Command\ServerRunCommand" >
11
11
<argument >%kernel.project_dir%/public</argument >
12
12
<argument >%kernel.environment%</argument >
13
+ <argument >%kernel.project_dir%/var/cache</argument >
13
14
<tag name =" console.command" command =" server:run" />
14
15
</service >
15
16
16
17
<service id =" web_server.command.server_start" class =" Symfony\Bundle\WebServerBundle\Command\ServerStartCommand" >
17
18
<argument >%kernel.project_dir%/public</argument >
18
19
<argument >%kernel.environment%</argument >
20
+ <argument >%kernel.project_dir%/var/cache</argument >
19
21
<tag name =" console.command" command =" server:start" />
20
22
</service >
21
23
22
24
<service id =" web_server.command.server_stop" class =" Symfony\Bundle\WebServerBundle\Command\ServerStopCommand" >
25
+ <argument >%kernel.project_dir%/var/cache</argument >
23
26
<tag name =" console.command" command =" server:stop" />
24
27
</service >
25
28
26
29
<service id =" web_server.command.server_status" class =" Symfony\Bundle\WebServerBundle\Command\ServerStatusCommand" >
30
+ <argument >%kernel.project_dir%/var/cache</argument >
27
31
<tag name =" console.command" command =" server:status" />
28
32
</service >
29
33
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ public function testLoad()
22
22
{
23
23
$ container = new ContainerBuilder ();
24
24
$ container ->setParameter ('kernel.project_dir ' , __DIR__ );
25
+ $ container ->setParameter ('kernel.cache_dir ' , __DIR__ .'/var/cache/test ' );
26
+ $ container ->setParameter ('kernel.environment ' , 'test ' );
25
27
(new WebServerExtension ())->load ([], $ container );
26
28
27
29
$ this ->assertSame (
@@ -32,6 +34,24 @@ public function testLoad()
32
34
__DIR__ .'/test ' ,
33
35
$ container ->getDefinition ('web_server.command.server_start ' )->getArgument (0 )
34
36
);
37
+
38
+ $ this ->assertSame (
39
+ __DIR__ .'/var/cache ' ,
40
+ $ container ->getDefinition ('web_server.command.server_run ' )->getArgument (2 )
41
+ );
42
+ $ this ->assertSame (
43
+ __DIR__ .'/var/cache ' ,
44
+ $ container ->getDefinition ('web_server.command.server_start ' )->getArgument (2 )
45
+ );
46
+ $ this ->assertSame (
47
+ __DIR__ .'/var/cache ' ,
48
+ $ container ->getDefinition ('web_server.command.server_stop ' )->getArgument (0 )
49
+ );
50
+ $ this ->assertSame (
51
+ __DIR__ .'/var/cache ' ,
52
+ $ container ->getDefinition ('web_server.command.server_status ' )->getArgument (0 )
53
+ );
54
+
35
55
$ this ->assertTrue ($ container ->hasDefinition ('web_server.command.server_run ' ));
36
56
$ this ->assertTrue ($ container ->hasDefinition ('web_server.command.server_start ' ));
37
57
$ this ->assertTrue ($ container ->hasDefinition ('web_server.command.server_stop ' ));
Original file line number Diff line number Diff line change @@ -25,6 +25,13 @@ class WebServer
25
25
const STARTED = 0 ;
26
26
const STOPPED = 1 ;
27
27
28
+ private $ pidFileDirectory ;
29
+
30
+ public function __construct (string $ pidFileDirectory = null )
31
+ {
32
+ $ this ->pidFileDirectory = $ pidFileDirectory ;
33
+ }
34
+
28
35
public function run (WebServerConfig $ config , $ disableOutput = true , callable $ callback = null )
29
36
{
30
37
if ($ this ->isRunning ()) {
@@ -166,6 +173,6 @@ private function createServerProcess(WebServerConfig $config)
166
173
167
174
private function getDefaultPidFile ()
168
175
{
169
- return getcwd ().'/.web-server-pid ' ;
176
+ return ( $ this -> pidFileDirectory ?? getcwd () ).'/.web-server-pid ' ;
170
177
}
171
178
}
You can’t perform that action at this time.
0 commit comments