@@ -95,16 +95,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
95
95
}
96
96
97
97
$ env = $ this ->getContainer ()->getParameter ('kernel.environment ' );
98
+ $ address = $ input ->getArgument ('address ' );
99
+
100
+ if (false === strpos ($ address , ': ' )) {
101
+ $ output ->writeln ('The address has to be of the form <comment>bind-address:port</comment>. ' );
102
+
103
+ return 1 ;
104
+ }
105
+
106
+ if ($ this ->isOtherServerProcessRunning ($ address )) {
107
+ $ output ->writeln (sprintf ('<error>A process is already listening on http://%s.</error> ' , $ address ));
108
+
109
+ return 1 ;
110
+ }
98
111
99
112
if ('prod ' === $ env ) {
100
113
$ output ->writeln ('<error>Running PHP built-in server in production environment is NOT recommended!</error> ' );
101
114
}
102
115
103
- if (null === $ builder = $ this ->createPhpProcessBuilder ($ input , $ output , $ env )) {
116
+ if (null === $ builder = $ this ->createPhpProcessBuilder ($ output , $ address , $ input -> getOption ( ' router ' ) , $ env )) {
104
117
return 1 ;
105
118
}
106
119
107
- $ output ->writeln (sprintf ("Server running on <info>http://%s</info> \n" , $ input -> getArgument ( ' address ' ) ));
120
+ $ output ->writeln (sprintf ("Server running on <info>http://%s</info> \n" , $ address ));
108
121
$ output ->writeln ('Quit the server with CONTROL-C. ' );
109
122
110
123
$ builder ->setWorkingDirectory ($ documentRoot );
@@ -127,9 +140,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
127
140
return $ process ->getExitCode ();
128
141
}
129
142
130
- private function createPhpProcessBuilder (InputInterface $ input , OutputInterface $ output , $ env )
143
+ private function isOtherServerProcessRunning ($ address )
144
+ {
145
+ list ($ hostname , $ port ) = explode (': ' , $ address );
146
+
147
+ $ fp = @fsockopen ($ hostname , $ port , $ errno , $ errstr , 5 );
148
+
149
+ if (false !== $ fp ) {
150
+ fclose ($ fp );
151
+
152
+ return true ;
153
+ }
154
+
155
+ return false ;
156
+ }
157
+
158
+ private function createPhpProcessBuilder (OutputInterface $ output , $ address , $ router , $ env )
131
159
{
132
- $ router = $ input -> getOption ( ' router ' ) ?: $ this
160
+ $ router = $ router ?: $ this
133
161
->getContainer ()
134
162
->get ('kernel ' )
135
163
->locateResource (sprintf ('@FrameworkBundle/Resources/config/router_%s.php ' , $ env ))
@@ -150,6 +178,6 @@ private function createPhpProcessBuilder(InputInterface $input, OutputInterface
150
178
return ;
151
179
}
152
180
153
- return new ProcessBuilder (array ($ binary , '-S ' , $ input -> getArgument ( ' address ' ) , $ router ));
181
+ return new ProcessBuilder (array ($ binary , '-S ' , $ address , $ router ));
154
182
}
155
183
}
0 commit comments