aboutsummaryrefslogtreecommitdiffstats
path: root/spawn
blob: 3cb35a5bd9b9ab5a01f85da77ebdeb841611d05c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env php
<?php
/**
 * @author Laurent Bachelier <laurent@bachelier.name>
 */
require dirname(__FILE__).'/lib/bootstrap.php';
require dirname(__FILE__).'/lib/Argument.php';
require dirname(__FILE__).'/lib/Template.php';
require dirname(__FILE__).'/lib/FileTools.php';
require dirname(__FILE__).'/lib/PosixTools.php';
require dirname(__FILE__).'/lib/MultiConfig.php';
require dirname(__FILE__).'/lib/Symfony.php';
require dirname(__FILE__).'/lib/Tail.php';
require dirname(__FILE__).'/lib/Color.php';

$project_path = Symfony::getProjectPath();
$options = MultiConfig::get();

$options['port'] = intval(Argument::get('p', 'port', 4042));
$options['bind'] = Argument::get('A', 'all', false)
                 ? false
                 : Argument::get('b', 'bind', '127.0.0.1');
$options['project_path'] = $project_path;
$options['config_dir'] = $project_path.'/cache/lighttpd';
$options['config_file'] = $options['config_dir'].'/lighttpd.conf';
$options['log_dir'] = $project_path.'/log/lighttpd';
// hack: .sf files are not removed by symfony cc
$options['pidfile'] = $options['config_dir'].'/.sf';
$options['restartfile'] = $options['config_dir'].'/.symfttpd_restart';
$options['tail'] = Argument::get('t', 'tail', false);
$options['fork'] = !Argument::get('s', 'single-process', false);
if ($options['fork'] && !function_exists('pcntl_fork'))
{
  log_message('Warning: No fork() support.'
      . 'symfttpd will run in single-process mode.');
  $options['fork'] = false;
}
$options['color'] = !Argument::get('C', 'no-color', false) && posix_isatty(STDOUT);
if ($options['color'])
{
  Color::enable();
}

if (Argument::get('K', 'kill', false))
{
  if (file_exists($options['restartfile']))
  {
    unlink($options['restartfile']);
  }
  exit(!PosixTools::killPid($options['pidfile']));
}

FileTools::mkdirs($options['config_dir']);
FileTools::mkdirs($options['log_dir']);

PosixTools::setCustomPath($options['custom_path']);
try
{
  if (empty($options['lighttpd_cmd']))
  {
    $options['lighttpd_cmd'] = PosixTools::which('lighttpd');
  }

  if (empty($options['php_cgi_cmd']))
  {
    $options['php_cgi_cmd'] = PosixTools::which('php-cgi');
  }

  if (empty($options['php_cmd']))
  {
    $options['php_cmd'] = PosixTools::which('php');
  }
}
catch (ExecutableNotFoundError $e)
{
  log_message('Required executable not found.', false);
  log_message($e->getMessage()
    . ' not found in the specified paths: '
    . implode(', ', PosixTools::getPaths()), false);
  exit(1);
}

if ($options['genconf_cmd'] === false)
{
  $options['genconf_cmd'] = $options['php_cmd'].' '.escapeshellarg(realpath(dirname(__FILE__)).'/genconf').' -p '.escapeshellarg($options['project_path'].'/web');
}
Template::writeConfig($options);

// Pretty information. Nothing interesting code-wise.
log_message('lighttpd started on '
  . Color::style('bright')
  . (strlen($options['bind']) ? $options['bind'] : 'all interfaces')
  . Color::style('normal')
  . ', port '
  . Color::style('bright') . $options['port'] . Color::style('normal')
  . '.');
log_message("\nAvailable applications:");
$apps = array();
foreach (new DirectoryIterator($project_path.'/web') as $file)
{
  if ($file->isFile() && preg_match('/\.php$/', $file->getFilename()))
  {
    $apps[] = $file->getFilename();
  }
}
$host = in_array($options['bind'], array(false, '0.0.0.0', '::'), true)
      ? 'localhost'
      : $options['bind'];
sort($apps);
foreach ($apps as $app)
{
  log_message(' http://'.$host.':'.$options['port']
      .'/'.Color::style('bright').$app.Color::style('normal'));
}
log_message("\nPress Ctrl+C to stop serving.");
flush();

if (!$options['fork'])
{
  passthru($options['lighttpd_cmd'].' -D -f '.escapeshellarg($options['config_file']));
  log_message('Terminated.');
}
else
{
  if ($options['tail'])
  {
    $multitail = new MultiTail();
    $multitail->add('access', new Tail($options['log_dir'].'/access.log'),
      Color::fgColor('blue'), Color::style('normal'));
    $multitail->add(' error', new Tail($options['log_dir'].'/error.log'),
      Color::style('bright').Color::fgColor('red'), Color::style('normal'));
    // We have to do it before the fork to capture the startup messages
    $multitail->consume();
  }
  $pid = pcntl_fork();
  if ($pid)
  {
    // Parent process
    $prev_genconf = null;
    while (false !== sleep(1))
    {
      $handle = popen($options['genconf_cmd'], 'r');
      $genconf = stream_get_contents($handle);
      pclose($handle);
      if ($prev_genconf !== null && $prev_genconf !== $genconf)
      {
        touch($options['restartfile']);
        !PosixTools::killPid($options['pidfile']);
      }
      $prev_genconf = $genconf;

      if ($options['tail'])
      {
        $multitail->consume();
      }

      // If the children is defunct, we are finished here
      if (pcntl_waitpid($pid, $status, WNOHANG))
      {
        exit(0);
      }
    }
  }
  elseif ($pid == 0)
  {
    // Child process
    do
    {
      if (file_exists($options['restartfile']))
      {
        unlink($options['restartfile']);
      }
      passthru($options['lighttpd_cmd'].' -D -f '.escapeshellarg($options['config_file']));
      if (!file_exists($options['restartfile']))
      {
        log_message('Terminated.');
      }
      else
      {
        log_message(Color::style('bright').'Something in web/ changed. Restarting lighttpd.'.Color::style('normal'));
        Template::writeConfig($options);
      }
    } while (file_exists($options['restartfile']));
  }
  else
  {
    log_message('Unable to fork!', true);
    exit(1);
  }
}