11
11
12
12
namespace Symfony \Bundle \WebServerBundle \Command ;
13
13
14
+ use Symfony \Bundle \WebServerBundle \WebServer ;
14
15
use Symfony \Component \Console \Input \InputArgument ;
15
16
use Symfony \Component \Console \Input \InputOption ;
16
17
use Symfony \Component \Console \Input \InputInterface ;
17
18
use Symfony \Component \Console \Output \OutputInterface ;
18
19
use Symfony \Component \Console \Output \ConsoleOutputInterface ;
19
20
use Symfony \Component \Console \Style \SymfonyStyle ;
20
- use Symfony \Component \Process \PhpExecutableFinder ;
21
- use Symfony \Component \Process \Process ;
22
- use Symfony \Component \Process \ProcessBuilder ;
23
- use Symfony \Component \Process \Exception \RuntimeException ;
24
21
25
22
/**
26
23
* Runs Symfony application using a local web server.
@@ -36,10 +33,9 @@ protected function configure()
36
33
{
37
34
$ this
38
35
->setDefinition (array (
39
- new InputArgument ('address ' , InputArgument::OPTIONAL , 'Address:port ' , '127.0.0.1 ' ),
40
- new InputOption ('port ' , 'p ' , InputOption::VALUE_REQUIRED , 'Address port number ' , '8000 ' ),
36
+ new InputArgument ('addressport ' , InputArgument::OPTIONAL , 'The address to listen to (can be address:port, address, or port) ' , '127.0.0.1:8000 ' ),
41
37
new InputOption ('docroot ' , 'd ' , InputOption::VALUE_REQUIRED , 'Document root ' , null ),
42
- new InputOption ('router ' , 'r ' , InputOption::VALUE_REQUIRED , 'Path to custom router script ' ),
38
+ new InputOption ('router ' , 'r ' , InputOption::VALUE_REQUIRED , 'Path to custom router script ' , dirname ( __DIR__ ). ' /Resources/router.php ' ),
43
39
))
44
40
->setName ('server:run ' )
45
41
->setDescription ('Runs a local web server ' )
@@ -65,7 +61,6 @@ protected function configure()
65
61
"prod", or "test".
66
62
67
63
See also: http://www.php.net/manual/en/features.commandline.webserver.php
68
-
69
64
EOF
70
65
)
71
66
;
@@ -83,85 +78,45 @@ protected function execute(InputInterface $input, OutputInterface $output)
83
78
}
84
79
85
80
if (!is_dir ($ documentRoot )) {
86
- $ io ->error (sprintf ('The document root directory "%s" does not exist ' , $ documentRoot ));
81
+ $ io ->error (sprintf ('The document root directory "%s" does not exist. ' , $ documentRoot ));
87
82
88
83
return 1 ;
89
84
}
90
85
91
86
$ env = $ this ->getContainer ()->getParameter ('kernel.environment ' );
92
- $ address = $ input ->getArgument ('address ' );
93
-
94
- if (false === strpos ($ address , ': ' )) {
95
- $ address = $ address .': ' .$ input ->getOption ('port ' );
96
- }
97
-
98
- if ($ this ->isOtherServerProcessRunning ($ address )) {
99
- $ io ->error (sprintf ('A process is already listening on http://%s. ' , $ address ));
100
-
101
- return 1 ;
102
- }
103
-
104
- if (false === $ router = $ this ->determineRouterScript ($ documentRoot , $ input ->getOption ('router ' ), $ env )) {
105
- $ io ->error ('Unable to guess the front controller file. ' );
106
-
107
- return 1 ;
108
- }
109
-
110
87
if ('prod ' === $ env ) {
111
88
$ io ->error ('Running this server in production environment is NOT recommended! ' );
112
89
}
113
90
114
- $ io ->success (sprintf ('Server listening on http://%s ' , $ address ));
115
- $ io ->comment ('Quit the server with CONTROL-C. ' );
91
+ $ router = $ input ->getOption ('router ' );
116
92
117
- if (null === $ builder = $ this ->createPhpProcessBuilder ($ io , $ address , $ router , $ env )) {
118
- return 1 ;
119
- }
120
-
121
- $ builder ->setWorkingDirectory ($ documentRoot );
122
- $ builder ->setTimeout (null );
123
- $ process = $ builder ->getProcess ();
124
93
$ callback = null ;
125
-
126
- if (OutputInterface:: VERBOSITY_NORMAL > $ output ->getVerbosity ()) {
127
- $ process -> disableOutput () ;
94
+ $ disableOutput = false ;
95
+ if ($ output ->isQuiet ()) {
96
+ $ disableOutput = true ;
128
97
} else {
129
- try {
130
- $ process ->setTty (true );
131
- } catch (RuntimeException $ e ) {
132
- $ callback = function ($ type , $ buffer ) use ($ output ) {
133
- if (Process::ERR === $ type && $ output instanceof ConsoleOutputInterface) {
134
- $ output = $ output ->getErrorOutput ();
135
- }
136
- $ output ->write ($ buffer , false , OutputInterface::OUTPUT_RAW );
137
- };
138
- }
98
+ $ callback = function ($ type , $ buffer ) use ($ output ) {
99
+ if (Process::ERR === $ type && $ output instanceof ConsoleOutputInterface) {
100
+ $ output = $ output ->getErrorOutput ();
101
+ }
102
+ $ output ->write ($ buffer , false , OutputInterface::OUTPUT_RAW );
103
+ };
139
104
}
140
- $ process ->run ($ callback );
141
105
142
- if (!$ process ->isSuccessful ()) {
143
- $ errorMessages = array ('Server terminated unexpectedly. ' );
106
+ try {
107
+ $ server = new WebServer ($ input ->getArgument ('addressport ' ));
108
+ $ server ->setConfig ($ documentRoot , $ env );
144
109
145
- if ($ process ->isOutputDisabled ()) {
146
- $ errorMessages [] = 'Run the command again with -v option for more details. ' ;
147
- }
110
+ $ io ->success (sprintf ('Server listening on http://%s ' , $ server ->getAddress ()));
111
+ $ io ->comment ('Quit the server with CONTROL-C. ' );
148
112
149
- $ io ->error ($ errorMessages );
150
- }
113
+ $ exitCode = $ server ->run ($ router , $ disableOutput , $ callback );
114
+ } catch (\Exception $ e ) {
115
+ $ io ->error ($ e ->getMessage ());
151
116
152
- return $ process ->getExitCode ();
153
- }
154
-
155
- private function createPhpProcessBuilder (SymfonyStyle $ io , $ address , $ router , $ env )
156
- {
157
- $ finder = new PhpExecutableFinder ();
158
-
159
- if (false === $ binary = $ finder ->find ()) {
160
- $ io ->error ('Unable to find PHP binary to run server. ' );
161
-
162
- return ;
117
+ return 1 ;
163
118
}
164
119
165
- return new ProcessBuilder ( array ( $ binary , ' -S ' , $ address , $ router )) ;
120
+ return $ exitCode ;
166
121
}
167
122
}
0 commit comments