Skip to content

Commit

Permalink
Merge pull request #31 from wilr/feature/global-domination
Browse files Browse the repository at this point in the history
feat: support global installation
  • Loading branch information
emteknetnz authored Jun 14, 2022
2 parents 72b36c5 + cf9e41b commit ae12b4b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
45 changes: 31 additions & 14 deletions bin/serve
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,64 @@ use SilverStripe\Core\Environment;

define('BASE_URL', '');

// Parse command-line options and env vars
$options = getopt('', ['host:', 'port:', 'hash:', 'bootstrap-file:', 'open', 'path:']);

// Autoload
$paths = [
__DIR__ . "/../autoload.php", // in ./vendor/bin
__DIR__ . "/../vendor/autoload.php", // in ./bin
__DIR__ . "/../../../autoload.php", // in ./vendor/silverstripe/serve/bin
];


if (!empty($options['path'])) {
$options['path'] = realpath($options['path']);
$dir = rtrim($options['path'], '/');

if (!is_dir($dir)) {
throw new Exception('--path is not a valid directory');
}

$paths[] = $dir . '/../autoload.php';
}

foreach ($paths as $path) {
if (file_exists($path)) {
require_once $path;
break;
}
}

// Parse command-line options and env vars
$options = getopt(null, [ 'host:', 'port:', 'hash:', 'bootstrap-file:', 'open' ]);

if (!empty($options['host'])) {
$host = $options['host'];
$host = $options['host'];
} elseif (Environment::getEnv('SERVE_HOST')) {
$host = Environment::getEnv('SERVE_HOST');
$host = Environment::getEnv('SERVE_HOST');
} else {
$host = '0.0.0.0';
$host = '0.0.0.0';
}

if (!empty($options['port'])) {
$port = $options['port'];
$port = $options['port'];
} elseif (Environment::getEnv('SERVE_PORT')) {
$port = Environment::getEnv('SERVE_PORT');
$port = Environment::getEnv('SERVE_PORT');
} else {
$port = '8080';
$port = '8080';
}

if (!empty($options['bootstrap-file'])) {
$bootstrapFile = $options['bootstrap-file'];
$bootstrapFile = $options['bootstrap-file'];
} elseif (Environment::getEnv('SERVE_BOOTSTRAP_FILE')) {
$bootstrapFile = Environment::getEnv('SERVE_BOOTSTRAP_FILE');
$bootstrapFile = Environment::getEnv('SERVE_BOOTSTRAP_FILE');
} else {
$bootstrapFile = null;
$bootstrapFile = null;
}

$path = defined('PUBLIC_PATH') ? PUBLIC_PATH : BASE_PATH;
if (!empty($options['path'])) {
$path = $options['path'];
} else {
$path = defined('PUBLIC_PATH') ? PUBLIC_PATH : BASE_PATH;
}

$factory = new SilverStripe\Serve\ServerFactory($path);

Expand All @@ -61,7 +78,7 @@ print "Server running at " . $server->getURL() . " for " . $path . "...\n";
// Check if the user wants to open the page in their browser
if (isset($options['open'])) {
// Simple function to detect if a command exists on *nix system
$command_exist = function($cmd) {
$command_exist = function ($cmd) {
$return = shell_exec(sprintf("which %s", escapeshellarg($cmd)));
return !empty($return);
};
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ launchServer allows the following options to be passed to it:
* **preferredPort:** The preferred port. If this port isn't available, the next
highest one will be used
* **bootstrapFile:** The bootstrap file, as described above

## Using as global

```
composer global require silverstripe/serve
```

Then you can run `serve` with the `--path` argument

```
serve --path=.
```

0 comments on commit ae12b4b

Please sign in to comment.