/** * Starts the web server and runs the encapsulated tasks. */ public function main() { $this->prepare(); $cmd = Commandline::toString($this->commandline->getCommandline(), true); $streams = [["file", "/dev/null", "r"], ["file", "/dev/null", "w"], ["file", "/dev/null", "w"]]; $handle = proc_open($cmd, $streams, $pipes); if (!is_resource($handle)) { throw new BuildException(get_class($this) . ' could not start web server.'); } else { $this->log(sprintf("Started web server, listening on http://%s:%d", $this->address, $this->port), Project::MSG_INFO); $msg = isset($this->router) ? sprintf("with %s as docroot and %s as router", $this->docroot, $this->router) : sprintf("with %s as docroot", $this->docroot); $this->log($msg, Project::MSG_VERBOSE); } try { foreach ($this->tasks as $task) { $task->perform(); } } finally { // Terminate server with SIGINT proc_terminate($handle, 2); $this->log("Stopped web server", Project::MSG_INFO); } }
/** * Builds the full command to execute and stores it in $realCommand. * * @param none * * @return void */ private function buildCommand() { // Log $this->log('Command building started ', $this->loglevel); // Building the executable $this->realCommand = Commandline::toString($this->commandline->getCommandline(), $this->escape); // Adding the source filename at the end of command, validating the existing // sourcefile position explicit mentioning if ($this->addsourcefile === true && strpos($this->realCommand, self::SOURCEFILE_ID) === false) { $this->realCommand .= ' ' . self::SOURCEFILE_ID; } // Setting command output redirection with content appending if ($this->output !== null) { $this->realCommand .= ' 1>'; $this->realCommand .= $this->appendoutput ? '>' : ''; // Append output $this->realCommand .= ' ' . escapeshellarg($this->output->getPath()); } elseif ($this->spawn) { // Validating the 'spawn' configuration, and redirecting the output to 'null' // Validating the O.S. variant if ('WIN' == $this->osvariant) { $this->realCommand .= ' > NUL'; // MS Windows output nullification } else { $this->realCommand .= ' 1>/dev/null'; // GNU/Linux output nullification } $this->log("For process spawning, setting Output nullification ", $this->loglevel); } // Setting command error redirection with content appending if ($this->error !== null) { $this->realCommand .= ' 2>'; $this->realCommand .= $this->appendoutput ? '>' : ''; // Append error $this->realCommand .= ' ' . escapeshellarg($this->error->getPath()); } // Setting the execution as a background process if ($this->spawn) { // Validating the O.S. variant if ('WIN' == $this->osvariant) { $this->realCommand = 'start /b ' . $this->realcommand; // MS Windows background process forking } else { $this->realCommand .= ' &'; // GNU/Linux background process forking } } // Log $this->log('Command built : ' . $this->realCommand, $this->loglevel); // Log $this->log('Command building completed ', $this->loglevel); return; }
/** * Builds the full command to execute and stores it in $command. * * @return void * @uses $command */ protected function buildCommand() { if ($this->command === null && $this->commandline->getExecutable() === null) { throw new BuildException('ExecTask: Please provide "command" OR "executable"'); } else { if ($this->command === null) { $this->realCommand = Commandline::toString($this->commandline->getCommandline(), $this->escape); } else { if ($this->commandline->getExecutable() === null) { $this->realCommand = $this->command; //we need to escape the command only if it's specified directly // commandline takes care of "executable" already if ($this->escape == true) { $this->realCommand = escapeshellcmd($this->realCommand); } } else { throw new BuildException('ExecTask: Either use "command" OR "executable"'); } } } if ($this->error !== null) { $this->realCommand .= ' 2> ' . escapeshellarg($this->error->getPath()); $this->log("Writing error output to: " . $this->error->getPath(), $this->logLevel); } if ($this->output !== null) { $this->realCommand .= ' 1> ' . escapeshellarg($this->output->getPath()); $this->log("Writing standard output to: " . $this->output->getPath(), $this->logLevel); } elseif ($this->spawn) { $this->realCommand .= ' 1>/dev/null'; $this->log("Sending output to /dev/null", $this->logLevel); } // If neither output nor error are being written to file // then we'll redirect error to stdout so that we can dump // it to screen below. if ($this->output === null && $this->error === null && $this->passthru === false) { $this->realCommand .= ' 2>&1'; } // we ignore the spawn boolean for windows if ($this->spawn) { $this->realCommand .= ' &'; } }